java创建二叉树及遍历
来源:互联网 发布:java jar 日志输出 编辑:程序博客网 时间:2024/06/05 16:33
已知外部提供创建树所需要的数据。
方法1:按照从上到下从左到右依次把数据放在对应结点来创建树。
即数组中index处的数据在树中仍然是放在index处,而它的左右子结点分别是 2*index+1 , 2*index+2
-- BinaryTree.javapublic class BinaryTree { Node root; int size; // 结点总个数 Object[] data; // 内部类 private class Node{ private Node left; private Node right; private Object data; public Node(Object data){ this.left = null; this.right = null; this.data = data; } } public BinaryTree(Object[] data){ this.data = data; size = data.length; root = createTree(0); } // 创建下标为index处的结点及其子树,递归 public Node createTree(int index){ if (index >= size) return null; Node node = new Node(data[index]); node.left = createTree(2*index+1); node.right = createTree(2*index+2); return node; } // 先序遍历 public void preShow(Node node) { if(node!=null){ System.out.print(node.data + " "); preShow(node.left); preShow(node.right); } } // 先序非递归 public void preShow(TreeNode root){ Stack<TreeNode> s = new Stack<>(); TreeNode p = root; while(p!=null || !s.isEmpty()){ while(p!=null){ System.out.println(p.val); s.push(p); p=p.left; } p=s.pop(); p=p.right; } } // 中序遍历 public void middleShow(Node node) { if (node != null) { middleShow(node.left); System.out.print(node.data + " "); middleShow(node.right); } } // 中序非递归 public void midShow(TreeNode root){ Stack<TreeNode> s = new Stack<>(); TreeNode p = root; while(p!=null || !s.isEmpty()){ while(P!=null){ s.push(p); p=p.left; } p=s.pop(); System.out.println(p.val); p=p.right; } } // 后序遍历 public void backShow(Node node) { if (node != null) { middleShow(node.left); middleShow(node.right); System.out.print(node.data + " "); } } // 层次遍历 public void levelShow(Node node){ Queue<Node> queue = new LinkedList<>(); if(node == null) return; queue.add(node); while(!queue.isEmpty()){ Node n = queue.poll(); System.out.print(n.data+" "); if(n.left!=null) queue.offer(n.left); if(n.right!=null) queue.offer(n.right); } } // 求树的深度 public int getDepth(Node node){ if(node==null) return 0; return getDepth(node.left)>getDepth(node.right)?getDepth(node.left)+1:getDepth(node.right)+1; } // 判断是否平衡二叉树 // 网友方法,只要有个结点不平衡,result就会变为falseprivate boolean result = true;public boolean isBalanced(TreeNode root) { maxDepth(root); return result;}public int maxDepth(TreeNode root) { if (root == null) return 0; int l = maxDepth(root.left); int r = maxDepth(root.right); if (Math.abs(l - r) > 1) result = false; return 1 + Math.max(l, r);} // 树中序遍历时是否是升序的? public boolean isValidBST(TreeNode root) { return isValidBST(root, Long.MIN_VALUE, Long.MAX_VALUE); } public boolean isValidBST(TreeNode root, long minVal, long maxVal) { if (root == null) return true; if (root.val >= maxVal || root.val <= minVal) return false; return isValidBST(root.left, minVal, root.val) && isValidBST(root.right, root.val, maxVal); } public static void main(String[] args) { Object[] my = new Object[]{'1', '2', '3', '4', '0', '5', '6', '7', '8', '0', '0', '9', 'A'}; BinaryTree tree = new BinaryTree(my); tree.preShow(tree.root); System.out.println(); tree.middleShow(tree.root); }}
法2:采用先序遍历创建二叉树
-- BinaryTree2.javapublic class BinaryTree2 { Node root; int size; // 结点总个数 Object[] data; int index; //表示取到数组中下标为index的元素了 // 内部类 private class Node{ private Node left; private Node right; private Object data; public Node(Object data){ this.left = null; this.right = null; this.data = data; } } public BinaryTree2(Object[] data){ size = data.length; this.data = data; index = 0; root = createTree(); } // 按照先序遍历创造树 public Node createTree(){ if(index>=size) return null; Object o = data[index++]; Node node = new Node(o); node.left = createTree(); node.right = createTree(); return node; } // 先序遍历 public void preShow(Node node) { if(node!=null){ System.out.print(node.data + " "); preShow(node.left); preShow(node.right); } } // 中序遍历 public void middleShow(Node node) { if (node != null) { middleShow(node.left); System.out.print(node.data + " "); middleShow(node.right); } } // 后序遍历 public void backShow(Node node) { if (node != null) { middleShow(node.left); middleShow(node.right); System.out.print(node.data + " "); } } public static void main(String[] args) { Object[] my = new Object[]{'1', '2', '3', '4', '0', '5', '6', '7', '8', '0', '0', '9', 'A'}; BinaryTree2 tree = new BinaryTree2(my); tree.preShow(tree.root); System.out.println(); tree.middleShow(tree.root); }}
0 0
- java创建二叉树及遍历
- 二叉树创建及遍历
- 二叉树创建及遍历
- 二叉树创建及遍历
- Java实现二叉树创建及便遍历
- java实现二叉树的创建及遍历
- java实现二叉树的创建及5种遍历
- java实现二叉树的创建及5种遍历
- 二叉树的创建及遍历--java实现
- java实现二叉树的创建及5种遍历
- 二叉树的遍历及创建(java)
- Java创建/遍历二叉树
- Java 二叉树创建遍历
- 二叉树的创建及遍历方式
- 二叉树的创建及遍历
- 二叉树创建及遍历算法
- 链式二叉树的创建及遍历
- 链式二叉树的创建及遍历
- hive基础进阶
- [数据挖掘]
- Android UI 开发入门—线性布局练习
- 2种方法解决mysql主从不同步
- [转]关于软件测试的几个经典问题(2)
- java创建二叉树及遍历
- 《Linux多线程服务端编程》笔记——线程同步精要
- 字符char 输入输出流 和 NIO
- Mysql 优化之 Explain 执行计划
- Java学习-装箱和拆箱
- leetCode:493. Reverse Pairs
- iMX283 学习笔记----1. GPIO2_4(P2.4) 操作
- 设计模式C++实现(1)——工厂模式
- 各种算法的优缺点