前一段时间学习了一下二叉查找树的结构,自己也练习一下,把源代码传上来!做了一些简单的测试,基本没有问题!
1 #ifndef BINARYTREE_H 2 #define BINARYTREE_H 3 4 #include <iostream> 5 using namespace std; 6 7 template<class T> 8 struct TreeNode 9 { 10 T m_data; 11 TreeNode *m_lChild, *m_rChild; 12 TreeNode(T &data, TreeNode *left = NULL, TreeNode *right = NULL) 13 : m_data(data), m_lChild(left), m_rChild(right){} 14 }; 15 16 template<class T> 17 class BiTree 18 { 19 public: 20 explicit BiTree(); 21 ~BiTree(); 22 void BuildTree(T *dataArray, int num); 23 void insertNode(T *data); 24 bool findNode(T *data)const; 25 void prevOrder(); 26 void midOrder(); 27 void pasOrder(); 28 void clear(); 29 bool deleteNode(T *data); 30 private: 31 bool deleteAndComb(TreeNode<T> **node); 32 void deleteTree(TreeNode<T> *node); 33 void prevOrder(TreeNode<T> *node); 34 void midOrder(TreeNode<T> *node); 35 void pasOrder(TreeNode<T> * node); 36 bool deleteNode(TreeNode<T> **node, T *key); 37 private: 38 TreeNode<T> *m_root; 39 }; 40 41 template<class T> 42 bool BiTree<T>::deleteNode(T *data) 43 { 44 return deleteNode(&m_root, data); 45 } 46 47 /**************************** 48 递归寻找要删除的节点并调用删除函数 49 *****************************/ 50 template<class T> 51 bool BiTree<T>::deleteNode(TreeNode<T> **node, T *key) 52 { 53 if (!(*node)) 54 { 55 return false; 56 } 57 else if ((*node)->m_data == *key) 58 { 59 return deleteAndComb(node); 60 } 61 else if((*node)->m_data > *key) 62 { 63 return deleteNode(&((*node)->m_lChild), key); 64 } 65 else 66 { 67 return deleteNode(&((*node)->m_rChild), key); 68 } 69 } 70 71 /**************************** 72 删除某个节点 73 如果节点为叶子则直接删除 74 如果只有左子树则 75 *****************************/ 76 template<class T> 77 bool BiTree<T>::deleteAndComb(TreeNode<T> **node) 78 { 79 TreeNode<T> *temp, *temp1; 80 //删除叶子节点 81 if(!(*node)->m_lChild && !(*node)->m_rChild)// 82 { 83 delete *node; 84 *node = NULL; 85 } 86 //只有右子树情况 87 else if (!(*node)->m_lChild) 88 { 89 temp = (*node)->m_rChild; 90 (*node)->m_data = temp->m_data; 91 (*node)->m_lChild = temp->m_lChild; 92 (*node)->m_rChild = temp->m_rChild; 93 delete temp; 94 return true; 95 } 96 //只有左子树情况 97 else if(!(*node)->m_rChild) 98 { 99 temp = (*node)->m_lChild; 100 (*node)->m_data = temp->m_data; 101 (*node)->m_lChild = temp->m_lChild; 102 (*node)->m_rChild = temp->m_rChild; 103 delete temp; 104 return true; 105 } 106 //既有左子树,又有右子树 107 else 108 { 109 temp = (*node); 110 temp1 = (*node)->m_lChild; 111 while(temp1->m_rChild) 112 { 113 temp = temp1; 114 temp1 = temp1->m_rChild; 115 } 116 (*node)->m_data = temp1->m_data; 117 if(temp != (*node)) 118 { 119 temp->m_rChild = temp1->m_lChild; 120 } 121 else 122 { 123 temp->m_lChild = temp1->m_lChild; 124 } 125 delete temp1; 126 return true; 127 } 128 } 129 130 template<class T> 131 void BiTree<T>::clear() 132 { 133 deleteTree(m_root); 134 m_root = NULL; 135 } 136 137 template<class T> 138 void BiTree<T>::pasOrder() 139 { 140 pasOrder(m_root); 141 } 142 143 template<class T> 144 void BiTree<T>::prevOrder() 145 { 146 prevOrder(m_root); 147 } 148 149 template<class T> 150 void BiTree<T>::midOrder() 151 { 152 midOrder(m_root); 153 } 154 155 /**************************** 156 前序遍历 157 *****************************/ 158 template<class T> 159 void BiTree<T>::prevOrder(TreeNode<T> *node) 160 { 161 if (!node) 162 { 163 return; 164 } 165 cout << node->m_data << " "; 166 if (node->m_lChild) 167 { 168 prevOrder(node->m_lChild); 169 } 170 if (node->m_rChild) 171 { 172 prevOrder(node->m_rChild); 173 } 174 } 175 176 /**************************** 177 中序遍历 178 *****************************/ 179 template<class T> 180 void BiTree<T>::midOrder(TreeNode<T> *node) 181 { 182 if (!node) 183 { 184 return; 185 } 186 if (node->m_lChild) 187 { 188 midOrder(node->m_lChild); 189 } 190 cout << node->m_data << " "; 191 if (node->m_rChild) 192 { 193 midOrder(node->m_rChild); 194 } 195 } 196 197 /**************************** 198 后序遍历 199 *****************************/ 200 template<class T> 201 void BiTree<T>::pasOrder(TreeNode<T> *node) 202 { 203 if (!node) 204 { 205 return; 206 } 207 if (node->m_lChild) 208 { 209 pasOrder(node->m_lChild); 210 } 211 if (node->m_rChild) 212 { 213 pasOrder(node->m_rChild); 214 } 215 cout << node->m_data << " "; 216 } 217 218 /**************************** 219 删除整个树 220 *****************************/ 221 template<class T> 222 void BiTree<T>::deleteTree(TreeNode<T> *node) 223 { 224 if(node->m_lChild) 225 { 226 deleteTree(node->m_lChild); 227 } 228 if(node->m_rChild) 229 { 230 deleteTree(node->m_rChild); 231 } 232 delete node; 233 } 234 235 template<class T> 236 BiTree<T>::BiTree() 237 { 238 m_root = NULL; 239 } 240 241 template<class T> 242 BiTree<T>::~BiTree() 243 { 244 deleteTree(m_root); 245 m_root = NULL; 246 } 247 248 /**************************** 249 新建树 250 *****************************/ 251 template<class T> 252 void BiTree<T>::BuildTree(T *dataArray, int num) 253 { 254 for (int i = 0; i < num; i++) 255 { 256 insertNode(&dataArray[i]); 257 } 258 } 259 260 /**************************** 261 插入节点 262 *****************************/ 263 template<class T> 264 void BiTree<T>::insertNode(T *data) 265 { 266 if (!m_root) 267 { 268 m_root = new TreeNode<T>(*data); 269 return; 270 } 271 TreeNode<T> *temp, *prev; 272 temp = prev = m_root; 273 while(temp) 274 { 275 prev = temp; 276 if (*data > temp->m_data) 277 { 278 temp = temp->m_rChild; 279 } 280 else 281 { 282 temp = temp->m_lChild; 283 } 284 } 285 if(*data > prev->m_data) 286 { 287 prev->m_rChild = new TreeNode<T>(*data); 288 } 289 else 290 { 291 prev->m_lChild = new TreeNode<T>(*data); 292 } 293 } 294 295 /**************************** 296 寻找节点 297 *****************************/ 298 template<class T> 299 bool BiTree<T>::findNode(T *data)const 300 { 301 TreeNode<T> *temp = m_root; 302 while(temp) 303 { 304 if (*data == temp->m_data) 305 { 306 return true; 307 } 308 else if(*data > temp->m_data) 309 { 310 temp = temp->m_rChild; 311 } 312 else 313 { 314 temp = temp->m_lChild; 315 } 316 } 317 return false; 318 } 319 320 #endif