1. #ifndef __TREE_H
  2. #define __TREE_H
  3. #include <iostream>
  4.  
  5. template<typename T> class TreeNode {
  6. private:
  7. T _data;
  8. TreeNode<T>* _left;
  9. TreeNode<T>* _right;
  10. TreeNode<T>* _parent;
  11. public:
  12. TreeNode(T data,
  13. TreeNode<T>* parent=,
  14. TreeNode<T>* left=,
  15. TreeNode<T>* right=)
  16. :_data(data),_parent(parent),_left(left),_right(right) {}
  17. void insertAtLeft(T data);
  18. void insertAtRight(T data);
  19. T& getData();
  20. TreeNode<T>*& getLeft();
  21. TreeNode<T>*& getRight();
  22. TreeNode<T>*& getParent();
  23. };
  24.  
  25. template<typename T>
  26. T& TreeNode<T>::getData()
  27. {
  28. return _data;
  29. }
  30.  
  31. template<typename T>
  32. TreeNode<T>*& TreeNode<T>::getLeft()
  33. {
  34. return _left;
  35. }
  36.  
  37. template<typename T>
  38. TreeNode<T>*& TreeNode<T>::getRight()
  39. {
  40. return _right;
  41. }
  42.  
  43. template<typename T>
  44. TreeNode<T>*& TreeNode<T>::getParent()
  45. {
  46. return _parent;
  47. }
  48.  
  49. template<typename T>
  50. void TreeNode<T>::insertAtLeft(T data)
  51. {
  52. _left = new TreeNode(data,this);
  53. }
  54.  
  55. template<typename T>
  56. void TreeNode<T>::insertAtRight(T data)
  57. {
  58. _right = new TreeNode(data,this);
  59. }
  60.  
  61. template <typename T> class Tree{
  62. private:
  63. TreeNode<T>* _root;
  64. int _size;
  65. protected:
  66. TreeNode<T>* findIn(TreeNode<T>* position,T element) const;
  67. /*
  68. * The last tmp argument is necessary,
  69. * it is used here to changed the parent's _left/_right field
  70. * to potint the node which is created
  71. */
  72. TreeNode<T>* insertIn(TreeNode<T>* position,
  73. T element,TreeNode<T>* tmp);
  74. void travIn(TreeNode<T>* position);
  75. void travPrev(TreeNode<T>* position);
  76. void travPost(TreeNode<T>* position);
  77. public:
  78. Tree(T data)
  79. :_root(new TreeNode<T> (data)),_size() { }
  80. TreeNode<T>* find(T element) const;
  81. TreeNode<T>* findMax() const;
  82. TreeNode<T>* findMin() const;
  83. TreeNode<T>* insert(T data);
  84. void traversalIn();
  85. void traversalPrev();
  86. void traversalPost();
  87. };
  88.  
  89. template<typename T>
  90. void Tree<T>::travIn(TreeNode<T>* position)
  91. {
  92. if(!position)
  93. return ;
  94. travIn(position->getLeft());
  95. std::cout << position->getData() << " ";
  96. travIn(position->getRight());
  97. }
  98.  
  99. template<typename T>
  100. void Tree<T>::travPrev(TreeNode<T>* position)
  101. {
  102. if(!position)
  103. return ;
  104. std::cout << position->getData() << " ";
  105. travPrev(position->getLeft());
  106. travPrev(position->getRight());
  107. }
  108.  
  109. template<typename T>
  110. void Tree<T>::travPost(TreeNode<T>* position)
  111. {
  112. if(!position)
  113. return ;
  114. travPost(position->getLeft());
  115. travPost(position->getRight());
  116. std::cout << position->getData() << " ";
  117. }
  118.  
  119. template<typename T>
  120. void Tree<T>::traversalPost()
  121. {
  122. travPost(_root);
  123. std::cout << std::endl;
  124. }
  125.  
  126. template<typename T>
  127. void Tree<T>::traversalPrev()
  128. {
  129. travPrev(_root);
  130. std::cout << std::endl;
  131. }
  132.  
  133. template<typename T>
  134. void Tree<T>::traversalIn()
  135. {
  136. travIn(_root);
  137. std::cout << std::endl;
  138. }
  139.  
  140. template<typename T>
  141. TreeNode<T>* Tree<T>::insertIn(TreeNode<T>* position,T data,
  142. TreeNode<T>* tmp)
  143. {
  144. if(!position){
  145. if(!tmp)
  146. return new TreeNode<T>(data);
  147. else
  148. return (tmp->getData() >data ? tmp->getLeft():tmp->getRight())
  149. = new TreeNode<T>(data,tmp);
  150. }
  151. if(data < position->getData())
  152. insertIn(position->getLeft(),data,position);
  153. else if(position->getData() < data)
  154. insertIn(position->getRight(),data,position);
  155. else
  156. return position;
  157. }
  158. template<typename T>
  159. TreeNode<T>* Tree<T>::insert(T data)
  160. {
  161. return insertIn(_root,data,);
  162. }
  163.  
  164. template<typename T>
  165. TreeNode<T>* Tree<T>::findIn(TreeNode<T>* position,T data)const
  166. {
  167. while(position && position->getData() != data)
  168. {
  169. if(position->getData() > data)
  170. position = position->getLeft();
  171. else if(position->getData() < data)
  172. position = position->getRight();
  173. }
  174. return position;
  175. }
  176.  
  177. template<typename T>
  178. TreeNode<T>* Tree<T>::find(T data) const
  179. {
  180. return findIn(_root,data);
  181. }
  182.  
  183. template<typename T>
  184. TreeNode<T>* Tree<T>::findMax() const
  185. {
  186. TreeNode<T>* tmp = _root;
  187. while(tmp->getRight())
  188. tmp = tmp->getRight();
  189. return tmp;
  190. }
  191.  
  192. template<typename T>
  193. TreeNode<T>* Tree<T>::findMin() const
  194. {
  195. TreeNode<T>* tmp = _root;
  196. while(tmp->getLeft())
  197. tmp = tmp->getLeft();
  198. return tmp;
  199. }
  200.  
  201. #endif

二叉搜索树

Binary search tree的更多相关文章

  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 ...

  10. [LeetCode] Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

随机推荐

  1. (原) 1.2 Zookeeper伪集群安装

    本文为原创文章,转载请注明出处,谢谢 Zookeeper伪集群安装 zookeeper单机安装配置可以查看 1.1 zookeeper单机安装 1.复制三份zookeeper,分别为zookeeper ...

  2. 经典案例:那些让人赞不绝口的创新 HTML5 网站

    在过去的10年里,网页设计师使用 Flash.JavaScript 或其他复杂的软件和技术来创建网站.但现在你可以前所未有的快速.轻松地设计或创造互动的.有趣好看的网站.如何创建?答案是 HTML5 ...

  3. 微信网页授权(OAuth2.0) PHP 源码简单实现

    提要: 1. 建议对OAuth2.0协议做一个学习. 2. 微信官方文档和微信官网工具要得到充分利用. 比较简单,直接帖源代码了.其中“xxxxxxxxxx”部分,是需要依据自己环境做替换的 /** ...

  4. git和svn

    git 分布式管理工具 svn 集中式管理工具 1. Git是分布式的,SVN是集中式的,好处是跟其他同事不会有太多的冲突,自己写的代码放在自己电脑上,一段时间后再提交.合并,也可以不用联网在本地提交 ...

  5. Android自带的theme

    android:theme="@android:style/Theme.Dialog" 将一个Activity显示为能话框模式 android:theme="@andro ...

  6. Android Property Animation动画

    3.0以前,android支持两种动画模式,tween animation,frame animation,在android3.0中又引入了一个新的动画系统:property animation,这三 ...

  7. SQL for SQLite

    语法 verb + subject + predicate commannds(命令) SQL由命令组成,以分号为结束.命令有token组成,token由white space分隔,包括空格.tab. ...

  8. 转 #ifndef/#define/#endif使用详解

    想必很多人都看过“头文件中的 #ifndef/#define/#endif 防止该头文件被重复引用”.但是是否能理解“被重复引用”是什么意思?是不能在不同的两个文件中使用include来包含这个头文件 ...

  9. Android实现与PHP服务器的交互

    今天算是有点小激动呢!拿到Android与PHP这个课题已经两个星期了,直到今天才算是有了一点点小收获. 虽然还是没能成功上传到服务器,不过已经看到了曙光,已经实现了一半了,那就是已经连接到了服务器. ...

  10. 阿里云服务器怎么去掉tomcat的8080端口

    前言:最近在阿里云申请了一个云服务器,然后已买了一个域名.比如www.haha.com ; 在云服务器上安装了tomcat ,访问服务器就成了 http://www.haha.com:8080/ . ...