二叉查找树(数据结构与算法分析代码)
该书的代码是通过public的成员函数使用调用private递归函数的常规技术。
不过我发现书本的insert函数是不可以实现结点相连。所以我参考了《算法导论》的insert函数(非递归)。
PS:有一些函数没有写。。。。注释的地方为原书private成员insert函数。。。。。
PS: 之前搞错了书本的代码。。。。其实书本的代码是可以实现的。。。。。书本的是递归。。。。。。。
注释地方为书本代码。。。。。。
1 #include<iostream> 2 using namespace std; 3 4 template <typename Object> 5 class BinarySearchTree 6 { 7 private: 8 struct BinaryNode 9 { 10 Object data; 11 BinaryNode * left; 12 BinaryNode * right; 13 14 BinaryNode( const Object & d, BinaryNode *l, BinaryNode *r ) 15 : data( d ), left( l ), right( r ) 16 { } 17 }; 18 19 20 public: 21 BinarySearchTree( ) 22 { 23 root = NULL; 24 } 25 26 BinarySearchTree( const BinarySearchTree & rhs ) 27 { 28 root = rhs.root; 29 } 30 ~BinarySearchTree( ) 31 { 32 makeEmpty( ); 33 } 34 35 bool isEmpty( ) const 36 { 37 if( root == NULL ) 38 return true; 39 else 40 return false; 41 } 42 43 const Object & findMin( ) const 44 { 45 return findMin( root ); 46 } 47 48 const Object & findMax( ) const 49 { 50 return findMax( root ); 51 } 52 53 bool contains( const Object & x ) const 54 { 55 return contains( x, root ); 56 } 57 58 void insert( const Object & k ) 59 { 60 BinaryNode * x = root; 61 BinaryNode * y = NULL; 62 BinaryNode * z = new BinaryNode( k, NULL, NULL ); 63 64 while( x != NULL ) 65 { 66 y = x; 67 if( k < x->data ) 68 x = x->left; 69 else 70 x = x->right; 71 } 72 73 if( y == NULL ) 74 root = z; 75 else if( k < y->data ) 76 y->left = z; 77 else 78 y->right = z; 79 } 80 81 void remove( const Object & x ) 82 { 83 remove( x, root ); 84 } 85 86 void makeEmpty( ) 87 { 88 makeEmpty( root ); 89 } 90 91 void preRecursive( ) 92 { 93 preRecursive( root ); 94 } 95 96 private: 97 BinaryNode * root; 98 99 BinaryNode * findMin( BinaryNode * t ) const 100 { 101 if( t == NULL ) 102 return NULL; 103 if( t->left == NULL ) 104 return t; 105 return findMin( t->left ); 106 } 107 108 BinaryNode * findMax( BinaryNode * t ) const 109 { 110 if( t == NULL ) 111 return NULL; 112 if( t->right == NULL ) 113 return t; 114 return findMax( t->right ); 115 } 116 117 bool contains ( const Object & x, BinaryNode * t ) const 118 { 119 if( t == NULL ) 120 return false; 121 else if( x < t->data ) 122 return contains( x, t->left ); 123 else if( x > t->data ) 124 return contains( x, t->right ); 125 else 126 return true; 127 } 128 129 /*void insert( const Object & x, BinaryNode * & t ) const 130 { 131 if( t == NULL ) 132 { 133 t = new BinaryNode( ); 134 t->data = x; 135 t->left = t->right = NULL; 136 137 t = new BinaryNode( x, NULL, NULL ); 138 } 139 else if ( x < t->data ) 140 insert( x, t->left ); 141 else if ( x > t->data ) 142 insert( x, t->right ); 143 else 144 ; 145 }*/ 146 147 void remove( const Object & x, BinaryNode * & t ) const 148 { 149 if( t == NULL ) 150 return ; 151 if( x < t->data ) 152 remove( x, t->left ); 153 else if( x > t->data ) 154 remove( x, t->right ); 155 else if( t->left != NULL && t->right != NULL ) 156 { 157 t->data = findMin( t->right )->data; 158 remove( t->data, t->right ); 159 } 160 else 161 { 162 BinaryNode * oldNode = t; 163 t = ( t->left != NULL ) ? t->left : t->right; 164 delete oldNode; 165 } 166 } 167 168 void makeEmpty( BinaryNode * & t ) const 169 { 170 if( t != NULL ) 171 { 172 makeEmpty( t->left ); 173 makeEmpty( t->right ); 174 delete t; 175 } 176 t = NULL; 177 } 178 179 void preRecursive( BinaryNode * &T ) const 180 { 181 if( T != NULL ) 182 { 183 cout << T->data << " "; 184 preRecursive( T->left ); 185 preRecursive( T->right ); 186 } 187 } 188 189 }; 190 191 192 int main( ) 193 { 194 BinarySearchTree<int> T; 195 T.insert( 10 ); 196 T.insert( 5 ); 197 T.insert( 15 ); 198 T.insert( 3 ); 199 T.insert( 8 ); 200 T.preRecursive( ); 201 cout << endl; 202 T.remove( 5 ); 203 T.preRecursive( ); 204 return 0; 205 }