数据结构--二叉搜索树2

之前我们实现了简单的二叉搜索树,现在介绍一下,STL中的容器,应对需要使用二叉搜索树的情况

其实,大多数时候,用STL中的set就够了,不需要自己实现



1
#include <iostream> 2 #include <cstdio> 3 #include <set> 4 5 using namespace std; 6 7 // set的内部结构其实不只是搜索二叉树那么简单 8 // set是一种自平衡二叉查找树,名叫红黑树 9 // 如果要对复杂的数据进行操作,需要重写仿函数,来进行大小的确定 10 11 int main() 12 { 13 set<int> s; 14 15 s.insert(1); 16 s.insert(3); 17 s.insert(6); 18 s.insert(5); 19 20 set<int>::iterator it; 21 22 it=s.find(3); 23 24 if(it==s.end()) 25 { 26 puts("find error "); 27 } 28 else 29 { 30 puts("find it "); 31 } 32 33 s.erase(3); 34 35 it=s.find(3); 36 37 if(it==s.end()) 38 { 39 puts("find error "); 40 } 41 else 42 { 43 puts("find it "); 44 } 45 46 // set也有count函数,但是不常用 47 // 如果要求二叉搜索树中,有重复的,要用multiset 48 49 50 // 此外,用set查找元素,还可以用以下三个函数 51 // lower_bound,查找小于等于的元素 52 // upper_bound,查找大于的元素 53 // equal_bound,返回一个pair类型 54 // 第一个是大于等于的元素的位置,第二个是大于的元素的位置 55 56 return 0; 57 }
原文地址:https://www.cnblogs.com/jishuren/p/12268965.html