今天又写了delete的部分,时间就这么被一天天地浪费了,我感到十分惋惜呀
  1. #pragma once
  2. #include "stdio.h"
  3.  
  4. struct Node
  5. {
  6. Node(int aValue)
  7. :m_Value(aValue)
  8. ,m_pLeft(NULL)
  9. ,m_pRight(NULL)
  10. ,m_pParent(NULL)
  11. {
  12.  
  13. }
  14. int m_Value;
  15. Node* m_pLeft;
  16. Node* m_pRight;
  17. Node* m_pParent;
  18. };
  19.  
  20. class BinarySearchTree
  21. {
  22. public:
  23. BinarySearchTree(void);
  24. ~BinarySearchTree(void);
  25. void Init();
  26. void Insert(int aValue);
  27. void Delete(int aValue);
  28. Node* MaxNode(Node* apNode);
  29. Node* MinNode(Node* apNode);
  30. Node* Search(int aValue);
  31.  
  32. void PreOrderPrint();
  33. void AfterOrderPrint();
  34. void MidOrderPrint();
  35.  
  36. void PrintNode( Node* lpNode )
  37. {
  38. printf("%d ", lpNode->m_Value);
  39. }
  40.  
  41. Node* Successor(Node* aNode);
  42.  
  43. private:
  44. Node* m_pRootNode;
  45. };
  1. #include "BinarySearchTree.h"
  2. #include <stack>
  3. #include <queue>
  4.  
  5. BinarySearchTree::BinarySearchTree(void)
  6. :m_pRootNode(NULL)
  7. {
  8. }
  9.  
  10. BinarySearchTree::~BinarySearchTree(void)
  11. {
  12. }
  13.  
  14. void BinarySearchTree::Insert( int aValue )
  15. {
  16. Node* lpNewNode = new Node(aValue);
  17. if (NULL == m_pRootNode)
  18. {
  19. m_pRootNode = lpNewNode;
  20. }
  21. else
  22. {
  23. Node* lpNode = m_pRootNode;
  24. Node* lpPNode = NULL;
  25.  
  26. while(lpNode)
  27. {
  28. lpPNode = lpNode;
  29. if (lpNode->m_Value > aValue)
  30. {
  31. lpNode = lpNode->m_pLeft;
  32. }
  33. else
  34. {
  35. lpNode = lpNode->m_pRight;
  36. }
  37. }
  38.  
  39. if (lpPNode->m_Value > aValue)
  40. {
  41. lpPNode->m_pLeft = lpNewNode;
  42. }
  43. else
  44. {
  45. lpPNode->m_pRight = lpNewNode;
  46. }
  47. lpNewNode->m_pParent = lpPNode;
  48. }
  49. }
  50.  
  51. void BinarySearchTree::Init()
  52. {
  53. }
  54.  
  55. void BinarySearchTree::Delete( int aValue )
  56. {
  57. Node* lpNode = Search(aValue);
  58. if (NULL == lpNode)
  59. {
  60. return;
  61. }
  62.  
  63. Node* lpDeleteNode = NULL;
  64.  
  65. if (!lpNode->m_pLeft && !lpNode->m_pRight)
  66. {
  67. if (lpNode->m_pParent->m_pLeft = lpNode)
  68. {
  69. lpNode->m_pParent->m_pLeft = NULL;
  70. }
  71. else
  72. {
  73. lpNode->m_pParent->m_pRight = NULL;
  74. }
  75. delete lpNode;
  76. }
  77. else
  78. {
  79. if (!lpNode->m_pLeft && lpNode->m_pRight)
  80. {
  81. if (lpNode->m_pParent->m_pLeft == lpNode)
  82. {
  83. lpNode->m_pParent->m_pLeft = lpNode->m_pRight;
  84. lpNode->m_pRight->m_pParent = lpNode->m_pParent;
  85. }
  86. else
  87. {
  88. lpNode->m_pParent->m_pRight = lpNode->m_pRight;
  89. lpNode->m_pRight->m_pParent = lpNode->m_pParent;
  90. }
  91. delete lpNode;
  92. lpNode =NULL;
  93. }
  94. else if (lpNode->m_pLeft && !lpNode->m_pRight)
  95. {
  96. if (lpNode->m_pParent->m_pLeft == lpNode)
  97. {
  98. lpNode->m_pParent->m_pLeft = lpNode->m_pLeft;
  99. lpNode->m_pLeft->m_pParent = lpNode->m_pParent;
  100. }
  101. else
  102. {
  103. lpNode->m_pParent->m_pRight = lpNode->m_pLeft;
  104. lpNode->m_pLeft->m_pParent = lpNode->m_pParent;
  105. }
  106. delete lpNode;
  107. lpNode = NULL;
  108. }
  109. else
  110. {
  111. Node* lpSuccessorNode = Successor(lpNode);
  112. lpNode->m_Value = lpSuccessorNode->m_Value;
  113. if (lpSuccessorNode->m_pRight)
  114. {
  115. lpSuccessorNode->m_pParent->m_pLeft = lpSuccessorNode->m_pRight;
  116. lpSuccessorNode->m_pRight->m_pParent = lpSuccessorNode->m_pParent;
  117. }
  118. delete lpSuccessorNode;
  119. lpSuccessorNode = NULL;
  120. }
  121. }
  122. }
  123.  
  124. void BinarySearchTree::PreOrderPrint()
  125. {
  126. std::queue<Node*> lStack;
  127. Node* lpCurNode = m_pRootNode;
  128.  
  129. if (NULL != lpCurNode)
  130. {
  131. lStack.push(lpCurNode);
  132. while(!lStack.empty())
  133. {
  134. Node* lpNode = lStack.front();
  135. lStack.pop();
  136. PrintNode(lpNode);
  137. if (lpNode->m_pLeft)
  138. {
  139. lStack.push(lpNode->m_pLeft);
  140. }
  141. if (lpNode->m_pRight)
  142. {
  143. lStack.push(lpNode->m_pRight);
  144. }
  145. }
  146. }
  147. }
  148.  
  149. void BinarySearchTree::AfterOrderPrint()
  150. {
  151. std::stack<Node*> lStack;
  152. Node* lpCurNode = m_pRootNode;
  153. bool lDone = true;
  154. while(!lStack.empty() || lpCurNode)
  155. {
  156. if (lpCurNode)
  157. {
  158. lStack.push(lpCurNode);
  159. lpCurNode = lpCurNode->m_pRight;
  160. }
  161. else
  162. {
  163. lpCurNode = lStack.top();
  164. lStack.pop();
  165. PrintNode(lpCurNode);
  166. lpCurNode = lpCurNode->m_pLeft;
  167. }
  168. }
  169. }
  170.  
  171. void BinarySearchTree::MidOrderPrint()
  172. {
  173. Node* lpNode = m_pRootNode;
  174. std::stack<Node*> lQueue;
  175.  
  176. while(NULL != lpNode || !lQueue.empty())
  177. {
  178. if (NULL != lpNode)
  179. {
  180. lQueue.push(lpNode);
  181. lpNode = lpNode->m_pLeft;
  182. }
  183. else
  184. {
  185. lpNode = lQueue.top();
  186. lQueue.pop();
  187. PrintNode(lpNode);
  188. lpNode = lpNode->m_pRight;
  189. }
  190. }
  191. }
  192.  
  193. Node* BinarySearchTree::MinNode(Node* apNode)
  194. {
  195. Node* lpPreNode = NULL;
  196. Node* lpNode = apNode;
  197.  
  198. while(lpNode)
  199. {
  200. lpPreNode = lpPreNode;
  201. lpNode = lpNode->m_pLeft;
  202. }
  203.  
  204. return lpPreNode;
  205. }
  206.  
  207. Node* BinarySearchTree::MaxNode(Node* apNode)
  208. {
  209. Node* lpPreNode = NULL;
  210. Node* lpNode = apNode;
  211.  
  212. while(lpNode)
  213. {
  214. lpPreNode = lpPreNode;
  215. lpNode = lpNode->m_pRight;
  216. }
  217.  
  218. return lpPreNode;
  219. }
  220.  
  221. Node* BinarySearchTree::Successor(Node* aNode)
  222. {
  223. if (NULL == m_pRootNode)
  224. {
  225. return NULL;
  226. }
  227.  
  228. Node* lpNode = aNode;
  229. if (lpNode->m_pRight)
  230. {
  231. return MinNode(lpNode->m_pRight);
  232. }
  233. else
  234. {
  235. while(lpNode && lpNode->m_pParent->m_pRight == lpNode)
  236. {
  237. lpNode = lpNode->m_pParent;
  238. }
  239.  
  240. return lpNode;
  241. }
  242. }
  243.  
  244. Node* BinarySearchTree::Search( int aValue )
  245. {
  246. Node* lpNode = m_pRootNode;
  247. while(lpNode)
  248. {
  249. if (lpNode->m_Value > aValue)
  250. {
  251. lpNode = lpNode->m_pLeft;
  252. }
  253. else if (lpNode->m_Value < aValue)
  254. {
  255. lpNode = lpNode->m_pRight;
  256. }
  257. else
  258. {
  259. return lpNode;
  260. }
  261. }
  262. return NULL;
  263. }

binary search tree study的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  3. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  4. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

  5. [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

  6. [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值

    Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...

  7. [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  8. [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  9. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

随机推荐

  1. iOS中的时钟动画

    iOS 动画效果非常多,我们在开发中可能会遇到很多动画特效,我们就会用到核心动画框架CoreAnimation,核心动画里面的动画效果有很多,都是在QuartzCore.framework框架里面,今 ...

  2. Linux下简单线程池的实现

    大多数的网络服务器,包括Web服务器都具有一个特点,就是单位时间内必须处理数目巨大的连接请求,但是处理时间却是比较短的.在传统的多线程服务器模型中是这样实现的:一旦有个服务请求到达,就创建一个新的服务 ...

  3. SSE,MSE,RMSE,R-square指标讲解

    SSE(和方差.误差平方和):The sum of squares due to errorMSE(均方差.方差):Mean squared errorRMSE(均方根.标准差):Root mean ...

  4. 转:无监督特征学习——Unsupervised feature learning and deep learning

    http://blog.csdn.net/abcjennifer/article/details/7804962 无监督学习近年来很热,先后应用于computer vision, audio clas ...

  5. Spark Strcutured Streaming中使用Dataset的groupBy agg 与 join 示例(java api)

    Dataset的groupBy agg示例 Dataset<Row> resultDs = dsParsed .groupBy("enodeb_id", "e ...

  6. Java归去来第2集:利用Eclipse创建Maven Web项目

    一.前言 如果还不了解剧情,请返回第一集的剧情          Java归去来第1集:手动给Eclipse配置Maven环境 二.利用Eclipse创建Maven Web项目 选择File-New- ...

  7. Sqlserver 2008 R2安装的盘符空间不够用的解决办法

    例如我把一个sqlserver数据库安装在了D盘,结果发现D盘只剩下20G的可用空间,可是数据却每天的在增长,如何办?于是百度到了以下解决办法 方法很多: 1.可以给primary文件组添加文件.选择 ...

  8. (转)Unity3D研究院之Assetbundle的原理(六十一)

    Assetbundle 是Unity Pro提供提供的功能,它可以把多个游戏对象或者资源二进制文件封装到Assetbundle中,提供了封装与解包的方法使用起来很便利. 1.预设          A ...

  9. [Canvas]奔跑的马

    下载地址:https://files.cnblogs.com/files/xiandedanteng/52-WalkingHorse.rar,请用Chrome浏览器打开观看动态效果. 图例: 源码: ...

  10. python 读取单所有json数据写入mongodb(单个)

    <--------------主函数-------------------> from pymongo import MongoClientfrom bson.objectid impor ...