当前位置:   article > 正文

福报厂面试之树的遍历、节点统计、树高计算_统计树高

统计树高

q:计算下二叉树的节点:

a:可以用递归,

q:递归堆栈利用高,时间复杂度高,不要用递归

a:emmmmmm… 请看下文

  1. package com.gjw.datastruts_Alg.binarytree;
  2. import lombok.AllArgsConstructor;
  3. import lombok.Data;
  4. import lombok.NoArgsConstructor;
  5. @Data
  6. @NoArgsConstructor
  7. @AllArgsConstructor
  8. public class BiTree {
  9. private BiTree lChild, rChild;
  10. private int data;
  11. public BiTree(int data) {
  12. this.data = data;
  13. }
  14. //先序遍历
  15. public static void PreOrder(BiTree tree) {
  16. BiTree[] stack = new BiTree[MAX];
  17. int top = -1;
  18. stack[++top] = tree;
  19. while (top != -1) {
  20. BiTree p = stack[top--];
  21. while (p != null) {
  22. print(p);
  23. if (p.rChild != null) {
  24. stack[++top] = p.rChild;
  25. }
  26. p = p.lChild;
  27. }
  28. }
  29. }
  30. //中序遍历
  31. public static void InOrder(BiTree tree) {
  32. BiTree[] stack = new BiTree[MAX];
  33. int top = -1;
  34. BiTree p = tree;
  35. while (p != null || top != -1) {
  36. if (p != null) {
  37. stack[++top] = p;
  38. p = p.lChild;
  39. } else {
  40. p = stack[top--];
  41. print(p);
  42. p = p.rChild;
  43. }
  44. }
  45. }
  46. static class SNode {
  47. int flag = 0; //用来记录该节点是否输出 0表示未输出 1表示已经输出
  48. BiTree p;
  49. public SNode(BiTree p) {
  50. this.p = p;
  51. }
  52. }
  53. //后序遍历
  54. public static void PostOrder(BiTree tree) {
  55. SNode[] stack = new SNode[MAX];
  56. int top = -1;
  57. BiTree p = tree;
  58. while (p != null || top != -1) {
  59. while (p != null) {
  60. SNode node = new SNode(p);
  61. stack[++top] = node;
  62. p = p.lChild;
  63. }
  64. SNode node = stack[top--];
  65. if (node.flag == 0) {
  66. node.flag = 1;
  67. stack[++top] = node;
  68. p = node.p.rChild;
  69. } else {
  70. print(node.p);
  71. }
  72. }
  73. }
  74. //层级遍历
  75. public static void LevelOrder(BiTree tree) {
  76. BiTree[] queue = new BiTree[MAX];
  77. int front = 0, rear = 0;
  78. queue[rear++] = tree;
  79. while (front < rear) {
  80. BiTree p = queue[front++];
  81. print(p);
  82. if (p.lChild != null)
  83. queue[rear++] = p.lChild;
  84. if (p.rChild != null)
  85. queue[rear++] = p.rChild;
  86. }
  87. }
  88. //统计叶子节点(递归)
  89. public static int leafCount(BiTree tree) {
  90. int count = 0;
  91. if (tree == null) {
  92. return count;
  93. } else if (tree.lChild == null && tree.rChild == null) {
  94. count = 1;
  95. } else {
  96. count = leafCount(tree.lChild) + leafCount(tree.rChild);
  97. }
  98. return count;
  99. }
  100. //统计非叶子节点(递归)
  101. public static int noLeafCount(BiTree tree) {
  102. int count = 0;
  103. if (tree == null) {
  104. return count;
  105. } else if (tree.lChild != null || tree.rChild != null) {
  106. count = 1;
  107. if (tree.lChild != null)
  108. count += noLeafCount(tree.lChild);
  109. if (tree.rChild != null)
  110. count += noLeafCount(tree.rChild);
  111. }
  112. return count;
  113. }
  114. //统计各种节点(非递归)
  115. public static int LeafCount(BiTree tree) {
  116. if (tree == null)
  117. return 0;
  118. int count = 0;
  119. int top = -1;
  120. BiTree[] stack = new BiTree[MAX];
  121. stack[++top] = tree;
  122. while (top != -1) {
  123. BiTree p = stack[top--];
  124. //if(p.lChild!=null || p.rChild!=null)//统计非叶子节点
  125. //if(p.lChild!=null && p.rChild!=null)//统计有两个孩子的节点
  126. //if ((p.lChild != null && p.rChild == null) || (p.lChild == null && p.rChild != null)) //统计只有一个孩子的节点
  127. if (p.lChild == null && p.rChild == null)//统计叶子节点
  128. count++;
  129. if (p.lChild != null)
  130. stack[++top] = p.lChild;
  131. if (p.rChild != null)
  132. stack[++top] = p.rChild;
  133. }
  134. return count;
  135. }
  136. //统计二叉树的高度
  137. public static int TreeHigh(BiTree tree) {
  138. int leftHigh, rightHigh, maxHigh;
  139. if (tree == null) {
  140. return 0;
  141. } else {
  142. leftHigh = TreeHigh(tree.lChild);
  143. rightHigh = TreeHigh(tree.rChild);
  144. maxHigh = leftHigh > rightHigh ? leftHigh : rightHigh;
  145. return maxHigh + 1;
  146. }
  147. }
  148. public static int treeHigh(BiTree tree) {
  149. BiTree[] queue = new BiTree[MAX];
  150. int front = 0, rear = 0;
  151. queue[rear++] = tree;
  152. int level = 1, high = 0;
  153. while (front < rear) {
  154. BiTree p = queue[front++];
  155. if (p.lChild != null)
  156. queue[rear++] = p.lChild;
  157. if (p.rChild != null)
  158. queue[rear++] = p.rChild;
  159. if (front == level) {
  160. high++;
  161. level = rear;
  162. }
  163. }
  164. return high;
  165. }
  166. private static int MAX = 10;
  167. public static void main(String[] args) {
  168. BiTree biTree = init();
  169. PreOrder(biTree);
  170. System.out.println();
  171. InOrder(biTree);
  172. System.out.println();
  173. PostOrder(biTree);
  174. System.out.println();
  175. LevelOrder(biTree);
  176. System.out.println();
  177. System.out.println("叶子节点个数: " + leafCount(biTree));
  178. System.out.println("非叶子节点个数: " + noLeafCount(biTree));
  179. System.out.println("叶子节点个数: " + LeafCount(biTree));
  180. System.out.println("递归树的高度: " + TreeHigh(biTree));
  181. System.out.println("非递归树的高度: " + treeHigh(biTree));
  182. }
  183. private static void print(BiTree p) {
  184. System.out.print(p.data + " ");
  185. }
  186. private static BiTree init() {
  187. BiTree biTree = new BiTree();
  188. biTree.setData(1);
  189. BiTree two = new BiTree(2);
  190. BiTree three = new BiTree(3);
  191. BiTree four = new BiTree(4);
  192. BiTree five = new BiTree(5);
  193. BiTree six = new BiTree(6);
  194. BiTree seven = new BiTree(7);
  195. biTree.setLChild(two);
  196. biTree.setRChild(three);
  197. two.setLChild(four);
  198. two.setRChild(five);
  199. three.setLChild(six);
  200. three.setRChild(seven);
  201. return biTree;
  202. }
  203. }

 

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

闽ICP备14008679号