当前位置:   article > 正文

数据结构 - 二叉树(C++)_c++二叉树定义

c++二叉树定义

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击人工智能教程

二叉树的定义

      二叉树(BinaryTree)是n(n>=0)个结点的有限集,它或者是空集(n=0),或者由一个根结点及两棵互不相交的、分别称为这个根的左子树右子树的二叉树组成。

二叉树的性质

性质1 二叉树第i层上的结点数目最多为2^(i-1)(i≥1)。
证明:

  用数学归纳法证明。
  归纳基础:i=1时,有2^(i-1)=2^(0)=1。因为第1层上只有一个根结点,所以命题成立。
  归纳假设:假设对所有的j(1≤j<i)命题成立,即第j层上至多有2^(j-1)个结点,证明j=i时命题亦成立。
  归纳步骤:根据归纳假设,第i-1层上至多有2^(i-1-1)=2^(i-2)个结点。由于二叉树的每个结点至多有两个孩子,故第i层上的结点数至多是第i-1层上的最大结点数的2倍。即j=i时,该层上至多有2×2^(i-2)=2^(i-1)个结点,故命题成立。

性质2 深度为k的二叉树至多有2^k-1个结点(k≥1)。
证明:

  在具有相同深度的二叉树中,仅当每一层都含有最大结点数时,其树中结点数最多。因此利用性质1可得,深度为k的二叉树的结点数至多为:
    2^0+2^1+…+2^(k-1)=2^k-1
  故命题正确。

性质3 在任意-棵二叉树中,若终端结点的个数为n0,度为2的结点数为n2,则n0=n2+1。
证明:

  因为二叉树中所有结点的度数均不大于2,所以结点总数(记为n)应等于0度结点数n0、1度结点数n1和2度结点数n2之和,即

    n=n0+n1+n2 (式子1)

  另一方面,1度结点有一个孩子,2度结点有两个孩子,故二叉树中孩子结点总数是n1+2n2,而树中只有根结点不是任何结点的孩子,故二叉树中的结点总数又可表示为

    n=n1+2n2+1 (式子2)

  由式子1和式子2得到:

    n0=n2+1

二叉树的实现(C++)

  1. /*
  2. * Created by Chimomo
  3. */
  4. #include <iostream>
  5. #define NULL 0
  6. using namespace std;
  7. template<class T>
  8. struct BTNode {
  9. T data;
  10. BTNode<T> *lChild, *rChild;
  11. BTNode();
  12. BTNode(const T &val, BTNode<T> *lChild = NULL, BTNode<T> *rChild = NULL) {
  13. data = val;
  14. this->lChild = lChild;
  15. this->rChild = rChild;
  16. }
  17. BTNode<T> *CopyTree() {
  18. BTNode<T> *l, *r, *n;
  19. if (&data == NULL) {
  20. return NULL;
  21. }
  22. l = lChild->CopyTree();
  23. r = rChild->CopyTree();
  24. n = new BTNode<T>(data, l, r);
  25. return n;
  26. }
  27. };
  28. template<class T>
  29. BTNode<T>::BTNode() {
  30. lChild = rChild = NULL;
  31. }
  32. template<class T>
  33. class BinaryTree {
  34. public:
  35. BTNode<T> *root;
  36. BinaryTree();
  37. ~BinaryTree();
  38. void Pre_Order();
  39. void In_Order();
  40. void Post_Order();
  41. int TreeHeight() const;
  42. int TreeNodeCount() const;
  43. void DestroyTree();
  44. BTNode<T> *MakeTree(const T &element, BTNode<T> *l, BTNode<T> *r) {
  45. root = new BTNode<T>(element, l, r);
  46. if (root == NULL) {
  47. cout << "Failure for applying storage address, system will close the process." << endl;
  48. exit(1);
  49. }
  50. return root;
  51. }
  52. private:
  53. void PreOrder(BTNode<T> *r);
  54. void InOrder(BTNode<T> *r);
  55. void PostOrder(BTNode<T> *r);
  56. int Height(const BTNode<T> *r) const;
  57. int NodeCount(const BTNode<T> *r) const;
  58. void Destroy(BTNode<T> *&r);
  59. };
  60. template<class T>
  61. BinaryTree<T>::BinaryTree() {
  62. root = NULL;
  63. }
  64. template<class T>
  65. BinaryTree<T>::~BinaryTree() {
  66. DestroyTree();
  67. }
  68. template<class T>
  69. void BinaryTree<T>::Pre_Order() {
  70. PreOrder(root);
  71. }
  72. template<class T>
  73. void BinaryTree<T>::In_Order() {
  74. InOrder(root);
  75. }
  76. template<class T>
  77. void BinaryTree<T>::Post_Order() {
  78. PostOrder(root);
  79. }
  80. template<class T>
  81. int BinaryTree<T>::TreeHeight() const {
  82. return Height(root);
  83. }
  84. template<class T>
  85. int BinaryTree<T>::TreeNodeCount() const {
  86. return NodeCount(root);
  87. }
  88. template<class T>
  89. void BinaryTree<T>::DestroyTree() {
  90. Destroy(root);
  91. }
  92. template<class T>
  93. void BinaryTree<T>::PreOrder(BTNode<T> *r) {
  94. if (r != NULL) {
  95. cout << r->data << ' ';
  96. PreOrder(r->lChild);
  97. PreOrder(r->rChild);
  98. }
  99. }
  100. template<class T>
  101. void BinaryTree<T>::InOrder(BTNode<T> *r) {
  102. if (r != NULL) {
  103. InOrder(r->lChild);
  104. cout << r->data << ' ';
  105. InOrder(r->rChild);
  106. }
  107. }
  108. template<class T>
  109. void BinaryTree<T>::PostOrder(BTNode<T> *r) {
  110. if (r != NULL) {
  111. PostOrder(r->lChild);
  112. PostOrder(r->rChild);
  113. cout << r->data << ' ';
  114. }
  115. }
  116. template<class T>
  117. int BinaryTree<T>::NodeCount(const BTNode<T> *r) const {
  118. if (r == NULL) {
  119. return 0;
  120. } else {
  121. return 1 + NodeCount(r->lChild) + NodeCount(r->rChild);
  122. }
  123. }
  124. template<class T>
  125. int BinaryTree<T>::Height(const BTNode<T> *r) const {
  126. if (r == NULL) {
  127. return 0;
  128. } else {
  129. int lh, rh;
  130. lh = Height(r->lChild);
  131. rh = Height(r->rChild);
  132. return 1 + (lh > rh ? lh : rh);
  133. }
  134. }
  135. /**
  136. * Swap left, right subtree of all nodes.
  137. * @tparam T The type parameter.
  138. * @param r The root pointer.
  139. */
  140. template<class T>
  141. void Swap(BTNode<T> *r) {
  142. BTNode<T> *p;
  143. if (r) {
  144. p = r->lChild;
  145. r->lChild = r->rChild;
  146. r->rChild = p; // Swap left, right child.
  147. Swap(r->lChild); // Swap left, right subtree of all the nodes in left subtree.
  148. Swap(r->rChild); // Swap left, right subtree of all the nodes in right subtree.
  149. }
  150. }
  151. template<class T>
  152. void BinaryTree<T>::Destroy(BTNode<T> *&r) {
  153. if (r != NULL) {
  154. Destroy(r->lChild);
  155. Destroy(r->rChild);
  156. delete r;
  157. r = NULL;
  158. }
  159. }
  160. int main() {
  161. BTNode<char> *b, *c, *d, *e, *f, *g;
  162. BinaryTree<char> a;
  163. b = a.MakeTree('F', NULL, NULL);
  164. c = a.MakeTree('E', NULL, NULL);
  165. d = a.MakeTree('D', NULL, NULL);
  166. e = a.MakeTree('C', b, NULL);
  167. f = a.MakeTree('B', d, c);
  168. g = a.MakeTree('A', f, e);
  169. cout << "Pre Order: ";
  170. a.Pre_Order();
  171. cout << endl;
  172. cout << "In Order: ";
  173. a.In_Order();
  174. cout << endl;
  175. cout << "Post Order: ";
  176. a.Post_Order();
  177. cout << endl;
  178. cout << "Tree Height: ";
  179. cout << a.TreeHeight();
  180. cout << endl;
  181. cout << "The Count of Tree Nodes: ";
  182. cout << a.TreeNodeCount();
  183. cout << endl;
  184. cout << endl << "------ Swap left, right subtree of all nodes ------" << endl << endl;
  185. Swap(a.root);
  186. cout << "Pre Order: ";
  187. a.Pre_Order();
  188. cout << endl;
  189. cout << "In Order: ";
  190. a.In_Order();
  191. cout << endl;
  192. cout << "Post Order: ";
  193. a.Post_Order();
  194. cout << endl;
  195. cout << "Tree Height: ";
  196. cout << a.TreeHeight();
  197. cout << endl;
  198. cout << "The Count of Tree Nodes: ";
  199. cout << a.TreeNodeCount();
  200. cout << endl;
  201. return 0;
  202. }
  203. // Output:
  204. /*
  205. Pre Order: A B D E C F
  206. In Order: D B E A F C
  207. Post Order: D E B F C A
  208. Tree Height: 3
  209. The Count of Tree Nodes: 6
  210. ------ Swap left, right subtree of all nodes ------
  211. Pre Order: A C F B E D
  212. In Order: C F A E B D
  213. Post Order: F C E D B A
  214. Tree Height: 3
  215. The Count of Tree Nodes: 6
  216. */

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/716213
推荐阅读
相关标签
  

闽ICP备14008679号