当前位置:   article > 正文

Java二叉树的建立_二叉树建树java

二叉树建树java

如果你已经知道了如何把一颗二叉树用两个数组来储存,即一个数组储存索引,一个数组储存值。那么今天在这里我们要学的是:如果给你这两个数组,你要去把它还原成一棵二叉树,并且能够实现二叉树的基本方法。

1.算法思想:

1.1我们二叉树的建立,是不是就是初始一棵二叉树?没错,就是再写一个二叉树的构造函数,不同的是初始化直接生成一棵树而非一个结点。

1.2首先他会给你两个数组,那么构造函数就应该以这两个数组为形参:

public BinaryCharTree(char[] paraDataArray, int[] paraIndicesArray)

1.3还原这可二叉树,你可以把给你的这些结点值看做是毫无关系的孤立点,我们先生成一个二叉树数组,它给你多少个结点值,那么二叉树的长度就应该有多大。由于这个二叉树数组中,每个单元的数并没有数值,其左右孩子也没有链接。我们要做的便是给这二叉树数组赋值并且链接起来。

1.4结点值直接按一个for循坏赋值就好了,关键是链接。这里需要两个for循环,采用孩子找妈妈的方法。第一个for是孩子的范围,第二个for是父亲的范围。再利用索引数组判断他们是不是父子,是则相连,不是则跳过。

  1. for (int i = 1; i < tempNumNodes; i++) {
  2. for (int j = 0; j < i; j++) {
  3. System.out.println("indices " + paraIndicesArray[j] + " vs. " + paraIndicesArray[i]);
  4. if (paraIndicesArray[i] == paraIndicesArray[j] * 2 + 1) {
  5. tempAllNodes[j].leftChild = tempAllNodes[i];
  6. System.out.println("Linking " + j + " with " + i);
  7. break;
  8. } else if (paraIndicesArray[i] == paraIndicesArray[j] * 2 + 2) {
  9. tempAllNodes[j].rightChild = tempAllNodes[i];
  10. System.out.println("Linking " + j + " with " + i);
  11. }
  12. } // Of for j
  13. } // Of for i

1.5最后一步了,我们是从第二个结点开始,也就是孩子结点开始。根节点没有链接,最后把根节点链接上。

  1. value = tempAllNodes[0].value;
  2. leftChild = tempAllNodes[0].leftChild;
  3. rightChild = tempAllNodes[0].rightChild;

完整的函数:

  1. // 二叉树的建立
  2. public BinaryCharTree(char[] paraDataArray, int[] paraIndicesArray) {
  3. // 第一步用一顺序表储存数据
  4. int tempNumNodes = paraDataArray.length;
  5. BinaryCharTree[] tempAllNodes = new BinaryCharTree[tempNumNodes];
  6. for (int i = 0; i < tempNumNodes; i++) {
  7. tempAllNodes[i] = new BinaryCharTree(paraDataArray[i]);
  8. } // Of for i
  9. // 第二步,链接结点
  10. for (int i = 1; i < tempNumNodes; i++) {
  11. for (int j = 0; j < i; j++) {
  12. System.out.println("indices " + paraIndicesArray[j] + " vs. " + paraIndicesArray[i]);
  13. if (paraIndicesArray[i] == paraIndicesArray[j] * 2 + 1) {
  14. tempAllNodes[j].leftChild = tempAllNodes[i];
  15. System.out.println("Linking " + j + " with " + i);
  16. break;
  17. } else if (paraIndicesArray[i] == paraIndicesArray[j] * 2 + 2) {
  18. tempAllNodes[j].rightChild = tempAllNodes[i];
  19. System.out.println("Linking " + j + " with " + i);
  20. }
  21. } // Of for j
  22. } // Of for i
  23. // 第三步,第一个结点是根节点
  24. value = tempAllNodes[0].value;
  25. leftChild = tempAllNodes[0].leftChild;
  26. rightChild = tempAllNodes[0].rightChild;
  27. }// Of the second constructor

这里只是思想,你自己要运行需要补充很多地方,了解思想就好了。下面是我集成部分。

  1. package datastructure.tree;
  2. import java.util.Arrays;
  3. import datastructure.queue.*;
  4. /**
  5. ****************************
  6. * TODO
  7. *
  8. * @author Chen Fan
  9. * @version 1.0 time 2021年12月30日
  10. ****************************
  11. */
  12. public class BinaryCharTree {
  13. char value;
  14. BinaryCharTree leftChild;
  15. BinaryCharTree rightChild;
  16. // 第一个构造函数
  17. public BinaryCharTree(char paraName) {
  18. value = paraName;
  19. leftChild = null;
  20. rightChild = null;
  21. }// Of the constructor
  22. //
  23. public static BinaryCharTree manualConstructTree() {
  24. // 第一步构造一个根节点
  25. BinaryCharTree resultTree = new BinaryCharTree('a');
  26. // 第二步构造所有结点。
  27. BinaryCharTree tempTreeB = new BinaryCharTree('b');
  28. BinaryCharTree tempTreeC = new BinaryCharTree('c');
  29. BinaryCharTree tempTreeD = new BinaryCharTree('d');
  30. BinaryCharTree tempTreeE = new BinaryCharTree('e');
  31. BinaryCharTree tempTreeF = new BinaryCharTree('f');
  32. BinaryCharTree tempTreeG = new BinaryCharTree('g');
  33. // 第三步,连接所有节点
  34. resultTree.leftChild = tempTreeB;
  35. resultTree.rightChild = tempTreeC;
  36. tempTreeB.rightChild = tempTreeD;
  37. tempTreeC.leftChild = tempTreeE;
  38. tempTreeD.leftChild = tempTreeF;
  39. tempTreeD.rightChild = tempTreeG;
  40. return resultTree;
  41. }// Of manualConstructTree
  42. // 先序遍历
  43. public void preOrderVisit() {
  44. System.out.print("" + value + " ");
  45. if (leftChild != null) {
  46. leftChild.preOrderVisit();
  47. } // Of if
  48. if (rightChild != null) {
  49. rightChild.preOrderVisit();
  50. } // Of if
  51. }// Of preOrderVisit
  52. // 中序遍历
  53. public void inOrderVisit() {
  54. if (leftChild != null) {
  55. leftChild.inOrderVisit();
  56. } // Of if
  57. System.out.print("" + value + " ");
  58. if (rightChild != null) {
  59. rightChild.inOrderVisit();
  60. } // Of if
  61. }// Of inOrderVisit
  62. // 后序遍历
  63. public void postOrderVisit() {
  64. if (leftChild != null) {
  65. leftChild.postOrderVisit();
  66. } // Of if
  67. if (rightChild != null) {
  68. rightChild.postOrderVisit();
  69. } // Of if
  70. System.out.print("" + value + " ");
  71. }// Of postOrderVisit
  72. // 得到树的高度
  73. public int getDepth() {
  74. // It is a leaf
  75. if ((leftChild == null) && (rightChild == null)) {
  76. return 1;
  77. } // Of if
  78. // The depth of the left child.
  79. int tempLeftDepth = 0;
  80. if (leftChild != null) {
  81. tempLeftDepth = leftChild.getDepth();
  82. } // Of if
  83. // The depth of the right child.
  84. int tempRightDepth = 0;
  85. if (rightChild != null) {
  86. tempRightDepth = rightChild.getDepth();
  87. } // Of if
  88. // 树的高度选取最高的那边+1
  89. if (tempLeftDepth >= tempRightDepth) {
  90. return tempLeftDepth + 1;
  91. } else {
  92. return tempRightDepth + 1;
  93. } // Of if
  94. }// Of getDepth
  95. // 获得结点数目
  96. public int getNumNodes() {
  97. // If it is a leaf.
  98. if (leftChild == null && rightChild == null) {
  99. return 1;
  100. } // Of if
  101. // 左孩子的数目
  102. int templeftNodes = 0;
  103. if (leftChild != null) {
  104. templeftNodes = leftChild.getNumNodes();
  105. } // Of if
  106. // 友孩子的数目
  107. int tempRightNodes = 0;
  108. if (rightChild != null) {
  109. tempRightNodes = rightChild.getNumNodes();
  110. } // Of if
  111. // 总的节点数 = 左孩子数目 + 右孩子数目
  112. return templeftNodes + tempRightNodes + 1;
  113. }// Of getNumNodes
  114. // 根据宽度优先遍历的结点值(我称之广度数组)
  115. char[] valuesArray;
  116. // 完全二叉树的索引
  117. int[] indicesArray;
  118. public void toDataArrays() {
  119. // 初始化数组
  120. int tempLength = getNumNodes();
  121. valuesArray = new char[tempLength];
  122. indicesArray = new int[tempLength];
  123. int i = 0;
  124. // 遍历和转化同时进行
  125. CircleObjectQueue tempQueue = new CircleObjectQueue();
  126. tempQueue.enqueue(this);
  127. CircleIntQueue tempIntQueue = new CircleIntQueue();
  128. tempIntQueue.enqueue(0);
  129. BinaryCharTree tempTree = (BinaryCharTree) tempQueue.dequeue();
  130. int tempIntdex = tempIntQueue.dequeue();
  131. while (tempTree != null) {
  132. valuesArray[i] = tempTree.value;
  133. indicesArray[i] = tempIntdex;
  134. i++;
  135. if (tempTree.leftChild != null) {
  136. tempQueue.enqueue(tempTree.leftChild);
  137. tempIntQueue.enqueue(tempIntdex * 2 + 1);
  138. } // Of if
  139. if (tempTree.rightChild != null) {
  140. tempQueue.enqueue(tempTree.rightChild);
  141. tempIntQueue.enqueue(tempIntdex * 2 + 2);
  142. } // Of if
  143. tempTree = (BinaryCharTree) tempQueue.dequeue();
  144. tempIntdex = tempIntQueue.dequeue();
  145. } // Of while
  146. }// Of toDataArrays
  147. /**
  148. ********************
  149. * Convert the tree to data arrays, including a char array and an int array. The
  150. * results are stored in two member variables.
  151. *
  152. * @see #valuesArray
  153. * @see #indicesArray
  154. *********************
  155. */
  156. @SuppressWarnings("removal")
  157. public void toDataArraysObjectQueue() {
  158. // Initialize arrays.
  159. int tempLength = getNumNodes();
  160. valuesArray = new char[tempLength];
  161. indicesArray = new int[tempLength];
  162. int i = 0;
  163. // Traverse and convert at the same time.
  164. CircleObjectQueue tempQueue = new CircleObjectQueue();
  165. tempQueue.enqueue(this);
  166. CircleObjectQueue tempIntQueue = new CircleObjectQueue();
  167. Integer tempIndexInteger = new Integer(0);
  168. tempIntQueue.enqueue(tempIndexInteger);
  169. BinaryCharTree tempTree = (BinaryCharTree) tempQueue.dequeue();
  170. int tempIndex = ((Integer) tempIntQueue.dequeue()).intValue();
  171. System.out.println("tempIndex = " + tempIndex);
  172. while (tempTree != null) {
  173. valuesArray[i] = tempTree.value;
  174. indicesArray[i] = tempIndex;
  175. i++;
  176. if (tempTree.leftChild != null) {
  177. tempQueue.enqueue(tempTree.leftChild);
  178. tempIntQueue.enqueue(new Integer(tempIndex * 2 + 1));
  179. } // Of if
  180. if (tempTree.rightChild != null) {
  181. tempQueue.enqueue(tempTree.rightChild);
  182. tempIntQueue.enqueue(new Integer(tempIndex * 2 + 2));
  183. } // Of if
  184. tempTree = (BinaryCharTree) tempQueue.dequeue();
  185. if (tempTree == null) {
  186. break;
  187. } // Of if
  188. tempIndex = ((Integer) tempIntQueue.dequeue()).intValue();
  189. } // Of while
  190. }// Of toDataArraysObjectQueue
  191. // 二叉树的建立
  192. public BinaryCharTree(char[] paraDataArray, int[] paraIndicesArray) {
  193. // 第一步用一顺序表储存数据
  194. int tempNumNodes = paraDataArray.length;
  195. BinaryCharTree[] tempAllNodes = new BinaryCharTree[tempNumNodes];
  196. for (int i = 0; i < tempNumNodes; i++) {
  197. tempAllNodes[i] = new BinaryCharTree(paraDataArray[i]);
  198. } // Of for i
  199. // 第二步,链接结点
  200. for (int i = 1; i < tempNumNodes; i++) {
  201. for (int j = 0; j < i; j++) {
  202. System.out.println("indices " + paraIndicesArray[j] + " vs. " + paraIndicesArray[i]);
  203. if (paraIndicesArray[i] == paraIndicesArray[j] * 2 + 1) {
  204. tempAllNodes[j].leftChild = tempAllNodes[i];
  205. System.out.println("Linking " + j + " with " + i);
  206. break;
  207. } else if (paraIndicesArray[i] == paraIndicesArray[j] * 2 + 2) {
  208. tempAllNodes[j].rightChild = tempAllNodes[i];
  209. System.out.println("Linking " + j + " with " + i);
  210. }
  211. } // Of for j
  212. } // Of for i
  213. // 第三步,第一个结点是根节点
  214. value = tempAllNodes[0].value;
  215. leftChild = tempAllNodes[0].leftChild;
  216. rightChild = tempAllNodes[0].rightChild;
  217. }// Of the second constructor
  218. /**
  219. *********************
  220. * The entrance of the program.
  221. *
  222. * @param args Not used now.
  223. *********************
  224. */
  225. public static void main(String args[]) {
  226. char[] tempCharArray = { 'A', 'B', 'C', 'D', 'E', 'F' };
  227. int[] tempIndicesArray = { 0, 1, 3, 7, 15, 31 };
  228. System.out.println("开始建立二叉树:");
  229. BinaryCharTree tempTree2 = new BinaryCharTree(tempCharArray, tempIndicesArray);
  230. System.out.println("\r\nPreorder visit:");
  231. tempTree2.preOrderVisit();
  232. System.out.println("\r\nIn-order visit:");
  233. tempTree2.inOrderVisit();
  234. System.out.println("\r\nPost-order visit:");
  235. tempTree2.postOrderVisit();
  236. }// Of main
  237. }

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

闽ICP备14008679号