二叉查找树

  1 #ifndef 二叉查找树_H
  2 #define 二叉查找树_H
  3 
  4 #include <iostream>
  5 
  6 //using namespace std;  头文件里不要这样写
  7 
  8 enum Boolean {FALSE, TRUE};//自己做一个boolean
  9 
 10 template<class Type>
 11 class Element
 12 {
 13     public:
 14     Type key;
 15     //添加更多的数据较容易
 16 };
 17 
 18 template <class Type> class BST;//前置声明BST,友元类的使用才不会出错
 19 
 20 template <class Type>
 21 class BstNode//树节点
 22 {
 23 
 24     friend class BST<Type>;  //BST可以使用private
 25 public:
 26     Element<Type> data;
 27     BstNode* LeftChild;
 28     BstNode* RightChild;
 29     void display(int i);
 30 
 31 };
 32 
 33 
 34 template <class Type>
 35 class BST
 36 {
 37 
 38 public:
 39     BST(BstNode<Type> *init = 0)//构造函数是可以设置默认参数值
 40     {
 41     
 42         root = init;
 43     }
 44 
 45     Boolean Insert(const Element<Type>& x );
 46     BstNode<Type>* Search(const Element<Type>& x);//递归查找需要用两个函数
 47     BstNode<Type>* Search(BstNode<Type>*, const Element<Type>&);
 48     BstNode<Type>* IterSearch(const Element<Type>&);//迭代查找
 49     void display()
 50     {
 51         cout << "\n";
 52         if(root)
 53             root->display(1);
 54         else
 55             cout << "这是空树\n";
 56 
 57     }
 58      
 59 private:
 60     BstNode<Type> *root;
 61 
 62 };
 63 
 64 template <class Type>
 65 void BstNode<Type>::display(int i)//用来显示当前节点的数据以及他左子树和右子树的数据
 66 {
 67     std::cout << "Position: " << i << ", data. key = " << data.key << "\n";
 68     if(LeftChild) LeftChild->display(2*i);//当前节点位置为i,左子树位置为2*i
 69     if(RightChild)RightChild->display(2*i+1);//右子树位置为2*i+1
 70     
 71 }
 72 
 73 template<class Type>
 74 Boolean BST<Type>::Insert(const Element<Type> &x)
 75 {
 76     BstNode<Type> *p = root;
 77     BstNode<Type> *q = 0;//q指向p的父节点,q也是指向当前正在操作节点的父节点
 78     //Insert之前要先查找
 79     while(p)//此循环用来找一个位置插入p
 80     {
 81     
 82         q = p;//每次在p指向自己的子树之前,先用q把p保存下来,因此q就是p的父节点
 83         
 84         if(x.key == p->data.key)return FALSE; //发现重复,失败
 85         
 86         else
 87         {
 88         if(x.key < p->data.key)  
 89             p = p->LeftChild; //如果要插入的数据比根p的数据小,指向p的左子树
 90         else 
 91             p = p->RightChild; //如果要插入的数据比根p的数据大,指向p的右子树
 92         }
 93     }
 94 
 95     //当循环没有返回任何值时,就找到了一个位置q,插入的节点就成为了p的左子树或右子树
 96     
 97     //创捷一个新的节点p
 98     p = new BstNode<Type>;
 99     p->LeftChild = p->RightChild = 0;
100     p->data = x;
101     if(!root) root = p;
102     else if(x.key < q->data.key) q->LeftChild = p;
103     else q->RightChild = p;
104     return TRUE;
105 
106 }
107 
108 template<class Type>
109 BstNode<Type>* BST<Type>::Search(const Element<Type> &x)
110 {
111 
112     return Search(root, x);
113 
114 }
115 
116 template<class Type>
117 BstNode<Type>* BST<Type>::Search(BstNode<Type> * b, const Element<Type> &x)
118 {
119     if(!b) return 0;
120     else if(x.key == b->data.key) return b;
121     else if(x.key < b->data.key) return Search(b->LeftChild, x);
122     else if(x.key > b->data.key) return Search(b->RightChild, x);
123 }
124 
125 template <class Type>
126 BstNode<Type>* BST<Type>::IterSearch(const Element<Type> &x)
127 {
128 
129     for(BstNode<Type>* t = root; t; )
130     {
131     
132         if(x.key == t->data.key) return t;
133         if(x.key < t->data.key) 
134             t =t->LeftChild;
135         else 
136             t = t->RightChild;
137         
138     }
139     return 0;
140 
141 }
142 
143 #endif 
 1 //用来测试
 2 #include <iostream>
 3 #include "二叉查找树.h"
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     BST<int> m;
 9     Element<int> a, b, c, d, e, f, g, h, i, j, k, l;
10     a.key = 5;
11     b.key = 3;
12     c.key = 11;
13     d.key = 3; 
14     e.key = 15;
15     f.key = 2;
16     g.key = 8;
17     h.key = 22;
18     i.key = 20;
19     j.key = 9;
20 
21     cout << endl;
22     cout << m.Insert(a) << endl; //a = 5,就是root
23     cout << m.Insert(b) << endl;
24     cout << m.Insert(c) << endl;
25     cout << m.Insert(d) << endl;
26     cout << m.Insert(e) << endl;
27     cout << m.Insert(f) << endl;
28     cout << m.Insert(g) << endl;
29     cout << m.Insert(h) << endl;
30     
31     m.display();
32     cout << endl;
33 
34     BstNode<int> *p = m.Search(f);
35     cout << "找到的是:" <<p->data.key << endl;
36     BstNode<int> *p2 = m.IterSearch(f);
37     cout << "找到的是:" <<p2->data.key << endl;
38     cout << "Hello,测试!" << endl;
39     return 0;
40 }

 

posted @ 2012-12-17 15:00  uniquews  阅读(180)  评论(0编辑  收藏  举报