【数据结构】二叉搜索树的递归与非递归实现

前端之家收集整理的这篇文章主要介绍了【数据结构】二叉搜索树的递归与非递归实现前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一.什么是二叉搜索

1. 每个节点都有一个作为搜索依据的关键码(key),所有节点的关键码互不相同。

2. 左子树上所有节点的关键码(key)都小于根节点的关键码(key)。

3. 右子树上所有节点的关键码(key)都大于根节点的关键码(key)。

4. 左右子树都是二叉搜索树。


二.二叉搜索树的代码实现(这里我们主要关心增删查改)

  1. #pragma once
  2. using namespace std;
  3. template<class K>
  4. struct SelectBinaryTreeNode
  5. {
  6. typedef SelectBinaryTreeNode<K> Node;
  7. K _key;
  8. Node* _left;
  9. Node* _right;
  10. SelectBinaryTreeNode(const K& key)
  11. :_key(key),_left(NULL),_right(NULL)
  12. {}
  13. };
  14.  
  15. template<class K>
  16. class SelectBinaryTree
  17. {
  18. public:
  19. typedef SelectBinaryTreeNode<K> Node;
  20. SelectBinaryTree()
  21. {}
  22. SelectBinaryTree(const SelectBinaryTree& sbt) //拷贝构造,递归实现
  23. {
  24. _root=_Copy(sbt._root);
  25. }
  26. ~SelectBinaryTree() //析构,递归实现
  27. {
  28. _Delete(_root);
  29. }
  30.  
  31. bool Find(const K& key) //非递归查找
  32. {
  33. if (NULL == _root)
  34. return false;
  35. Node* cur = _root;
  36. while (cur)
  37. {
  38. if (cur->_key < key)
  39. cur = cur->_right;
  40. else if (cur->_key>key)
  41. cur = cur->_left;
  42. else
  43. return true;
  44. }
  45. return false;
  46. }
  47.  
  48. bool FindR(const K& key) //递归查找
  49. {
  50. return _FindR(_root,key);
  51. }
  52.  
  53. void InOrder() //中序遍历
  54. {
  55. _InOrder(_root);
  56. cout << endl;
  57. }
  58.  
  59. bool Insert(const K& key) //非递归插入
  60. {
  61. if (NULL == _root)
  62. {
  63. _root = new Node(key);
  64. return true;
  65. }
  66. Node* cur = _root;
  67. Node* parent = NULL;
  68. while (cur)
  69. {
  70. if (cur->_key < key)
  71. {
  72. parent = cur;
  73. cur = cur->_right;
  74. }
  75. else if (cur->_key>key)
  76. {
  77. parent = cur;
  78. cur = cur->_left;
  79. }
  80. else
  81. return false;
  82. }
  83. if (parent->_key < key)
  84. {
  85. parent->_right = new Node(key);
  86. }
  87. else
  88. parent->_left = new Node(key);
  89. return true;
  90. }
  91.  
  92. bool InsertR(const K& key) //递归插入
  93. {
  94. return _InsertR(_root,key);
  95. }
  96.  
  97. bool RemoveR(const K& key) //递归删除
  98. {
  99. return _RemoveR(_root,key);
  100. }
  101.  
  102. bool Remove(const K& key) //非递归删除
  103. {
  104. if (NULL == _root)
  105. return false;
  106. Node* cur = _root;
  107. Node* parent = NULL;
  108. while (cur)
  109. {
  110. if (cur->_key < key)
  111. {
  112. parent = cur;
  113. cur = cur->_right;
  114. }
  115. else if (cur->_key>key)
  116. {
  117. parent = cur;
  118. cur = cur->_left;
  119. }
  120. else //找到了进行删除
  121. {
  122. Node* del = cur;
  123.  
  124. if (cur->_left == NULL ) //左为空
  125. {
  126. if (parent == NULL) //要删除的是根节点
  127. _root = cur->_right;
  128. else
  129. {
  130. if (parent->_left == cur)
  131. parent->_left = cur->_right;
  132. else
  133. parent->_right = cur->_right;
  134. }
  135. }
  136. else if (cur->_right == NULL) //右为空
  137. {
  138. if (NULL == parent)
  139. _root = cur->_left;
  140. else
  141. {
  142. if (parent->_left == cur)
  143. parent->_left = cur->_left;
  144. else
  145. parent->_right = cur->_left;
  146. }
  147. }
  148.  
  149. else //左右都不为空
  150. {
  151. Node* pcur = cur->_right;
  152. Node* pparent = cur;
  153. while (pcur->_left) //找到右树的最左结点
  154. {
  155.  
  156. pparent = pcur;
  157. pcur = pcur->_left;
  158. }
  159. //K tmp = cur->_key;
  160. //cur->_key = pcur->_key;
  161. //pcur->_key = tmp;
  162. //找到后本应该像上面一样交换,但是因为pcur马上要删除,所以直接赋值就好
  163. cur->_key = pcur->_key;
  164. del = pcur;
  165. if (pparent->_left == pcur)
  166. pparent->_left = pcur->_right;
  167. else
  168. pparent->_right = pcur->_right;
  169. }
  170. delete del;
  171. return true;
  172. }
  173. }
  174. return false;
  175. }
  176. protected:
  177.  
  178. bool _RemoveR(Node*& root,const K& key)
  179. {
  180. if (root->_key < key)
  181. return _RemoveR(root->_right,key);
  182. else if (root->_key>key)
  183. return _RemoveR(root->_left,key);
  184. else
  185. {
  186. Node* del = root;
  187. if (root->_left == NULL) //要删除的结点左为空
  188. {
  189. root = root->_right;
  190. }
  191. else if (root->_right == NULL )//要删除的结点右为空
  192. {
  193. root = root->_left;
  194. }
  195.  
  196. else //左右都不为空
  197. {
  198. Node* pcur = root->_right;
  199. while (pcur->_left)
  200. {
  201. pcur = pcur->_left;
  202. }
  203. root->_key = pcur->_key;
  204. del = pcur;
  205. }
  206. delete del;
  207. return true;
  208. }
  209. return false;
  210. }
  211.  
  212. bool _InsertR(Node*& root,const K& key)
  213. {
  214. if (NULL == root)
  215. {
  216. root = new Node(key);
  217. return true;
  218. }
  219. if (root->_key < key)
  220. return _InsertR(root->_right,key);
  221. else if (root->_key>key)
  222. return _InsertR(root->_left,key);
  223. else
  224. return false;
  225. }
  226.  
  227. bool _FindR(Node* root,const K& key)
  228. {
  229. if (NULL == root)
  230. return false;
  231. if (root->_key == key)
  232. return true;
  233. else if (root->_key < key)
  234. _FindR(root->_right,key);
  235. else if (root->_key>key)
  236. _FindR(root->_left,key);
  237.  
  238. }
  239. void _Delete(Node* root) //递归删除
  240. {
  241. if (root)
  242. {
  243. _Delete(root->_left);
  244. _Delete(root->_right);
  245. delete root;
  246. }
  247. }
  248. Node* _Copy(Node* sroot) //递归拷贝
  249. {
  250. Node* cur = NULL;
  251. if (sroot)
  252. {
  253. cur = new Node(sroot->_key);
  254. cur->_left = _Copy(sroot->_left);
  255. cur->_right = _Copy(sroot->_right);
  256. }
  257. return cur;
  258. }
  259. void _InOrder(Node* root)
  260. {
  261. if (NULL == root)
  262. return;
  263. _InOrder(root->_left);
  264. cout << root->_key<<" ";
  265. _InOrder(root->_right);
  266. }
  267. private:
  268. Node* _root;
  269. };
  270.  
  271. void TestSelectBinaryTree() //测试非递归版
  272. {
  273. int a[] = { 5,3,4,1,7,8,2,6,9 };
  274. SelectBinaryTree<int> sbt;
  275. for (size_t i = 0; i < sizeof(a) / sizeof(a[0]); i++)
  276. {
  277. sbt.Insert(a[i]);
  278. }
  279. SelectBinaryTree<int> sbt1(sbt); //测试拷贝构造
  280. sbt1.InOrder();
  281. sbt.InOrder();
  282. sbt.Remove(0);
  283. sbt.Remove(1);
  284. sbt.Remove(2);
  285. sbt.Remove(3);
  286. sbt.Remove(4);
  287. sbt.Remove(5);
  288. sbt.Remove(6);
  289. sbt.Remove(7);
  290. sbt.Remove(8);
  291. sbt.Remove(9);
  292. sbt.InOrder();
  293. //cout<<sbt.Find(11)<<endl;
  294.  
  295. }
  296.  
  297. void TestSelectBinaryTreeR() //测试递归版
  298. {
  299. int a[] = { 5,9 };
  300. SelectBinaryTree<int> sbt;
  301. for (size_t i = 0; i < sizeof(a) / sizeof(a[0]); i++)
  302. {
  303. sbt.InsertR(a[i]);
  304. }
  305. SelectBinaryTree<int> sbt1(sbt); //测试拷贝构造
  306. sbt1.InOrder();
  307. sbt.InOrder();
  308. sbt.RemoveR(0);
  309. sbt.InOrder();
  310. sbt.RemoveR(1);
  311. sbt.InOrder();
  312. sbt.RemoveR(2);
  313. sbt.InOrder();
  314. sbt.RemoveR(3);
  315. sbt.InOrder();
  316. sbt.RemoveR(4);
  317. sbt.InOrder();
  318. sbt.RemoveR(5);
  319. sbt.InOrder();
  320. sbt.RemoveR(6);
  321. sbt.InOrder();
  322. sbt.RemoveR(7);
  323. sbt.InOrder();
  324. sbt.RemoveR(8);
  325. sbt.InOrder();
  326. sbt.RemoveR(9);
  327. sbt.InOrder();
  328.  
  329. //cout<<sbt.FindR(11)<<endl;
  330.  
  331. }

猜你在找的数据结构相关文章