二叉树实现(构造,遍历)-jav

妖狐艹你老母 2021-06-12 20:36 546阅读 0赞

构造函数-节点

  1. public class TreeNode {
  2. public int val=0;
  3. public TreeNode left = null;
  4. public TreeNode right = null;
  5. public int getVal() {
  6. return val;
  7. }
  8. public TreeNode(int val) {
  9. this.val = val;
  10. }
  11. }
  12. //主函数
  13. import java.util.ArrayList;
  14. import java.util.LinkedList;
  15. import java.util.List;
  16. import java.util.Queue;
  17. import java.util.Stack;
  18. /**
  19. *
  20. */
  21. /**
  22. * @author Home
  23. *
  24. */
  25. public class BinTreeMain {
  26. /**
  27. * @param args
  28. */
  29. private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  30. private static List<TreeNode> nodeList = null;
  31. public static void main(String[] args) {
  32. new BinTreeMain().createBinTree();
  33. TreeNode root = nodeList.get(0);
  34. System.out.println("递归先序:");
  35. preOrder(root);
  36. System.out.println("非递归先序:");
  37. PreOrder(root);
  38. System.out.println("递归中序:");
  39. inOrder(root);
  40. System.out.println("非递归中序:");
  41. InOrder(root);
  42. System.out.println();
  43. postOrder(root);
  44. System.out.println();
  45. levelOrder(root);
  46. System.out.println();
  47. System.out.println(Height(root));
  48. new BinTreeMain().Mirror(root);
  49. levelOrder(root);
  50. System.out.println(isSymmetrical(root));
  51. }
  52. // 建树
  53. public void createBinTree() {
  54. nodeList = new LinkedList<TreeNode>();
  55. for (int i = 0; i < array.length; i++)
  56. nodeList.add(new TreeNode(array[i]));
  57. for (int parentIndex = 0; parentIndex < array.length / 2 - 1; parentIndex++) {
  58. // 左孩子
  59. nodeList.get(parentIndex).left = nodeList.get(parentIndex * 2 + 1);
  60. nodeList.get(parentIndex).right = nodeList.get(parentIndex * 2 + 2);
  61. }
  62. int lastparentIndex = array.length / 2 - 1;
  63. nodeList.get(lastparentIndex).left = nodeList
  64. .get(lastparentIndex * 2 + 1);
  65. if (array.length % 2 == 1)
  66. nodeList.get(lastparentIndex).right = nodeList
  67. .get(lastparentIndex * 2 + 2);
  68. }
  69. // 先序遍历输出-递归
  70. public static void preOrder(TreeNode node) {
  71. if (node != null) {
  72. System.out.print(node.val + "\t");
  73. preOrder(node.left);
  74. preOrder(node.right);
  75. }
  76. }
  77. // 先序遍历输出-非递归
  78. public static void PreOrder(TreeNode node) {
  79. Stack<TreeNode> stack = new Stack<TreeNode>();
  80. if (node != null) {
  81. TreeNode p = node;
  82. while (p != null || !stack.isEmpty()) {
  83. if (p != null) {
  84. System.out.print(p.val + "\t");
  85. stack.push(p);
  86. p = p.left;
  87. } else {//在刚才那个p的左子树为空,或者p为叶子节点时执行。
  88. p = stack.pop();
  89. p = p.right;
  90. }
  91. }
  92. }
  93. }
  94. // 中序遍历输出
  95. public static void inOrder(TreeNode node) {
  96. if (node != null) {
  97. inOrder(node.left);
  98. System.out.print(node.val + "\t");
  99. inOrder(node.right);
  100. }
  101. }
  102. //中序遍历-非递归
  103. public static void InOrder(TreeNode node){
  104. Stack<TreeNode> stack = new Stack<TreeNode>();
  105. if(node!=null){
  106. TreeNode p = node;
  107. while(p!=null||!stack.isEmpty()){
  108. if(p!=null){
  109. stack.push(p);
  110. p = p.left;
  111. }else{
  112. p = stack.pop();
  113. System.out.print(p.val+"\t");
  114. p = p.right;
  115. }
  116. }
  117. }
  118. }
  119. // 后序递归遍历输出
  120. public static void postOrder(TreeNode node) {
  121. if (node != null) {
  122. postOrder(node.left);
  123. postOrder(node.right);
  124. System.out.print(node.val + "\t");
  125. }
  126. }
  127. //根据先序序列和中序序列唯一建造一棵二叉树,返回二叉树的根
  128. public TreeNode preAndinCreateTree(char[] pre,char[] in,int i,int j,int m,int n){
  129. //数组pre存储先序序列,i,j分别表示pre的上标和下标
  130. //in:中序序列,m,n分别表示in的上标和下标
  131. //函数返回先序序列和中序序列构成的树的根
  132. int k;
  133. TreeNode p=null;
  134. if(n<0)
  135. return null;
  136. p = new TreeNode(pre[i]);
  137. k = m;
  138. //在中序中找根
  139. while((k<=n)&&in[k]!=pre[i])
  140. k++;
  141. p.left = preAndinCreateTree(pre,in,i+1,i+k-m,m,k-1);
  142. p.right = preAndinCreateTree(pre,in,i+k-m+1,j,k+1,n);
  143. return p;
  144. }
  145. // 层次遍历
  146. public static void levelOrder(TreeNode node) {
  147. Queue<TreeNode> queue = new LinkedList<TreeNode>();
  148. if (node != null) {
  149. queue.add(node);
  150. while (!queue.isEmpty()) {
  151. TreeNode nnode = queue.poll();
  152. System.out.print(nnode.val + "\t");
  153. if (nnode.left != null)
  154. queue.add(nnode.left);
  155. if (nnode.right != null)
  156. queue.add(nnode.right);
  157. }
  158. }
  159. }
  160. // 求二叉树的高度
  161. public static int Height(TreeNode node) {
  162. int lh, rh;
  163. if (node == null)
  164. return 0;
  165. else {
  166. lh = Height(node.left);
  167. rh = Height(node.right);
  168. return 1 + (lh > rh ? lh : rh);
  169. }
  170. }
  171. // 操作给定的二叉树,将其变换为源二叉树的镜像。
  172. public void Mirror(TreeNode root) {
  173. if (root != null) {
  174. TreeNode temp = root.left;
  175. root.left = root.right;
  176. root.right = temp;
  177. Mirror(root.left);
  178. Mirror(root.right);
  179. }
  180. }
  181. /*
  182. * 二叉树的下一个结点 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。
  183. * 注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。
  184. */
  185. // 对称的二叉树
  186. static boolean isSymmetrical(TreeNode pRoot) {
  187. if (pRoot == null)
  188. return true;
  189. return lrSym(pRoot.left, pRoot.right);
  190. }
  191. static boolean lrSym(TreeNode left, TreeNode right) {
  192. if (left == null && right == null)
  193. return true;
  194. if (left != null && right != null)
  195. return left.val == right.val && lrSym(left.left, right.right)
  196. && lrSym(left.right, right.left);
  197. return false;
  198. }
  199. }

发表评论

表情:
评论列表 (有 0 条评论,546人围观)

还没有评论,来说两句吧...

相关阅读

    相关 c++实现

    //自己还真是个菜鸡,大一学了一年c++,现在还在基础的语法上转圈,还没有意识到c++真正的 //的强大之处在于它的多变,封装,等算法告一段落了在考虑是往Jav

    相关 c++实现

    //自己还真是个菜鸡,大一学了一年c++,现在还在基础的语法上转圈,还没有意识到c++真正的 //的强大之处在于它的多变,封装,等算法告一段落了在考虑是往Jav