二叉查找树

B树是一种很有用的查找结构,能够在O(h)的时间复杂度内完成查找操作(h为树的高度)。其它操作,比如求最大,最小,前驱,后继等,复杂度均为O(h)。当树越平衡时,时间复杂度越低(O(lg(n)),因此,可以通过随机化建树,达到这一目的。另外还有一些结构,比如红黑树,其自身的性质可以保证树的平衡性,因此性能相对较好。这里我用c++实现了B树,个人认为,除了删除一个节点的操作有点复杂外,B树绝大部分操作都是很直观的,实现起来还是很容易的。代码如下:

btree.h头文件,

 1 #ifndef _BTREE_H_
 2 #define _BTREE_H_
 3 
 4 class BTree
 5 {
 6 public:
 7     struct Node
 8     {
 9         int key = 0;
10         Node* p = nullptr;
11         Node* left = nullptr;
12         Node* right = nullptr;
13     };
14 
15     ~BTree();
16 
17     void inorder_walk();
18     void preorder_walk();
19     void postorder_walk();
20 
21     Node* search(int key);
22     Node* minimum();
23     Node* maximum();
24     Node* successor(Node* x);
25     Node* predecessor(Node* x);
26 
27     Node* insert(int key);
28     void remove(Node* x);
29 
30 private:
31     void inorder(Node* x);
32     void preorder(Node* x);
33     void postorder(Node* x);
34     void transplant(Node* x, Node* y);
35 
36 private:
37     Node* root = nullptr;
38 };
39 
40 #endif//_BTREE_H_

 

btree.cpp实现:

  1 #include <iostream>
  2 #include <stack>
  3 #include "btree.h"
  4 
  5 using namespace std;
  6 
  7 BTree::~BTree()
  8 {
  9     stack<Node*> s;
 10     s.push(root);
 11     while (!s.empty())
 12     {
 13         Node* x = s.top(); s.pop();
 14         if (x != nullptr)
 15         {
 16             s.push(x->left);
 17             s.push(x->right);
 18             delete x;
 19         }
 20     }
 21 }
 22 
 23 void BTree::inorder(Node* x)
 24 {
 25     if (x != nullptr)
 26     {
 27         inorder(x->left);
 28         cout << x->key << ",";
 29         inorder(x->right);
 30     }
 31 }
 32 
 33 void BTree::inorder_walk()
 34 {
 35     inorder(root);
 36     cout << endl;
 37 }
 38 
 39 void BTree::preorder(Node* x)
 40 {
 41     if (x != nullptr)
 42     {
 43         cout << x->key << ",";
 44         preorder(x->left);
 45         preorder(x->right);
 46     }
 47 }
 48 
 49 void BTree::preorder_walk()
 50 {
 51     preorder(root);
 52     cout << endl;
 53 }
 54 
 55 void BTree::postorder(Node* x)
 56 {
 57     if (x != nullptr)
 58     {
 59         postorder(x->left);
 60         postorder(x->right);
 61         cout << x->key << ",";
 62     }
 63 }
 64 
 65 void BTree::postorder_walk()
 66 {
 67     postorder(root);
 68     cout << endl;
 69 }
 70 
 71 
 72 
 73 BTree::Node* BTree::search(int key)
 74 {
 75     Node* x = root;
 76     while (x != nullptr && key != x->key)
 77     {
 78         if (key < x->key)
 79             x = x->left;
 80         else
 81             x = x->right;
 82     }
 83     return x;
 84 }
 85 
 86 BTree::Node* BTree::minimum()
 87 {
 88     Node* x = root;
 89     while (x->left != nullptr)
 90     {
 91         x = x->left;
 92     }
 93     return x;
 94 }
 95 
 96 BTree::Node* BTree::maximum()
 97 {
 98     Node* x = root;
 99     while (x->right != nullptr)
100     {
101         x = x->right;
102     }
103     return x;
104 }
105 
106 BTree::Node* BTree::insert(int key)
107 {
108     Node* x = root;
109     Node* y = nullptr;
110     while (x != nullptr)
111     {
112         y = x;
113         if (key > x->key)
114             x = x->right;
115         else
116             x = x->left;
117     }
118     Node* z = new Node;
119     z->p = y;
120     z->key = key;
121     if (y == nullptr)
122         root = z;
123     else if (key < y->key)
124         y->left = z;
125     else
126         y->right = z;
127     return z;
128 }
129 
130 void BTree::transplant(Node* x, Node* y)
131 {
132     if (x->p == nullptr)
133         root = y;
134     else if (x->p->left == x)
135         x->p->left = y;
136     else
137         x->p->right = y;
138     if (y != nullptr)
139         y->p = x->p;
140 }
141 
142 void BTree::remove(Node* x)
143 {
144     if (x->left == nullptr)
145         transplant(x, x->right);
146     else if (x->right == nullptr)
147         transplant(x, x->left);
148     else
149     {
150         Node* y = successor(x);
151         if (y->p != x)
152         {
153             transplant(y, y->right);
154             y->right = x->right;
155             y->right->p = y;
156         }
157         transplant(x, y);
158         y->left = x->left;
159         y->left->p = y;
160     }
161 }
162 
163 BTree::Node* BTree::successor(Node* x)
164 {
165     if (x->right != nullptr)
166     {
167         Node* y = x->right;
168         while (y->left != nullptr)
169         {
170             y = y->left;
171         }
172         return y;
173     }
174     else
175     {
176         Node* y = x->p;
177         while (y != nullptr && x == y->right)
178         {
179             x = y;
180             y = y->p;
181         }
182         return y;
183     }
184 }
185 
186 BTree::Node* BTree::predecessor(Node* x)
187 {
188     if (x->left != nullptr)
189     {
190         Node* y = x->left;
191         while (y->right != nullptr)
192         {
193             y = y->right;
194         }
195         return y;
196     }
197     else
198     {
199         Node* y = x->p;
200         while (y != nullptr && x == y->left)
201         {
202             x = y;
203             y = y->p;
204         }
205         return y;
206     }
207 }
btree.cpp

 

posted @ 2014-01-06 21:29  Tiancai Ye  阅读(171)  评论(0)    收藏  举报