二叉树的遍历
来源:互联网 发布:深圳雅思培训机构 知乎 编辑:程序博客网 时间:2024/05/21 05:40
package 算法和数据结构;/*** Filename : TreeBianli.java* Author : zhihao_tian@126.com* Creation time : 下午6:28:39 - 2017年3月11日* Description :*/import java.util.Stack;import java.util.HashMap;class Node { char val; Node left; Node right; Node(char val) { this.val = val; } Node(char val, Node left, Node right) { this.val = val; this.left = left; this.right = right; } public char getKey() { return val; } public Node getLeft() { return left; } public Node getRight() { return right; }}public class BinaryTree { protected Node root; public BinaryTree(Node root) { this.root = root; } public Node getRoot() { return root; } /** 构造树 */ public static Node init() { Node a = new Node('A'); Node b = new Node('B', null, a); Node c = new Node('C'); Node d = new Node('D', b, c); Node e = new Node('E'); Node f = new Node('F', e, null); Node g = new Node('G', null, f); Node h = new Node('H', d, g); return h;// root } /** 访问节点 */ public static void visit(Node p) { System.out.print(p.getKey() + " "); } /** 递归实现前序遍历 */ protected static void preorder(Node p) { if (p != null) { visit(p); preorder(p.getLeft()); preorder(p.getRight()); } } /** 递归实现中序遍历 */ protected static void inorder(Node p) { if (p != null) { inorder(p.getLeft()); visit(p); inorder(p.getRight()); } } /** 递归实现后序遍历 */ protected static void postorder(Node p) { if (p != null) { postorder(p.getLeft()); postorder(p.getRight()); visit(p); } } /** 非递归实现前序遍历 */ protected static void iterativePreorder(Node p) { Stack<Node> stack = new Stack<Node>(); if (p != null) { stack.push(p); while (!stack.empty()) {//先右后左 p = stack.pop(); visit(p); if (p.getRight() != null) stack.push(p.getRight()); if (p.getLeft() != null) stack.push(p.getLeft()); } } } //下面高能/** 非递归实现前序遍历魔板 ********************/ protected static void iterativePreorder2(Node p) { Stack<Node> stack = new Stack<Node>(); Node node = p; while (node != null || stack.size() > 0) { while (node != null) {//压入所有的左节点,压入前访问它 visit(node); stack.push(node); node = node.getLeft(); } if (stack.size() > 0) {//直接只弹出栈 node = stack.pop(); node = node.getRight(); } } } /** 非递归实现中序遍历魔板 **********************/ protected static void iterativeInorder2(Node p) { Stack<Node> stack = new Stack<Node>(); Node node = p; while (node != null || stack.size() > 0) { while (node != null) { stack.push(node); node = node.getLeft(); } if (stack.size() > 0) { node = stack.pop(); visit(node); node = node.getRight(); } } } /** 非递归实现后序遍历魔板 ***********************/ protected static void iterativePostorderN(Node p) { Stack<Node> stack = new Stack<Node>(); Node pre = null, node = p; while(node!=null || stack.size()>0) { while(node!=null) { stack.push(node); node = node.left; } node = stack.peek(); if(node.right==null || node.right == pre){ visit(node); pre = node; stack.pop(); node = null; } else { node = node.right; } } } /** 非递归实现后序遍历 */ protected static void iterativePostorder(Node p) { Node q = p; Stack<Node> stack = new Stack<Node>(); while (p != null) { // 左子树入栈 for (; p.getLeft() != null; p = p.getLeft()) stack.push(p); // 当前节点无右子或右子已经输出 while (p != null && (p.getRight() == null || p.getRight() == q)) { visit(p); q = p;// 记录上一个已输出节点 if (stack.empty()) return; p = stack.pop(); } // 处理右子 stack.push(p); p = p.getRight(); } } /** 非递归实现后序遍历 双栈法 */ protected static void iterativePostorder2(Node p) { Stack<Node> lstack = new Stack<Node>(); Stack<Node> rstack = new Stack<Node>(); Node node = p, right; do { while (node != null) { right = node.getRight(); lstack.push(node); rstack.push(right); node = node.getLeft(); } node = lstack.pop(); right = rstack.pop(); if (right == null) { visit(node); } else { lstack.push(node); rstack.push(null); } node = right; } while (lstack.size() > 0 || rstack.size() > 0); } /** 非递归实现后序遍历 单栈法*/ protected static void iterativePostorder3(Node p) { Stack<Node> stack = new Stack<Node>(); Node node = p, prev = p; while (node != null || stack.size() > 0) { while (node != null) { stack.push(node); node = node.getLeft(); } if (stack.size() > 0) { Node temp = stack.peek().getRight(); if (temp == null || temp == prev) { node = stack.pop(); visit(node); prev = node; node = null; } else { node = temp; } } } } /** 非递归实现后序遍历4 双栈法*/ protected static void iterativePostorder4(Node p) { Stack<Node> stack = new Stack<Node>(); Stack<Node> temp = new Stack<Node>(); Node node = p; while (node != null || stack.size() > 0) { while (node != null) { temp.push(node); stack.push(node); node = node.getRight(); } if (stack.size() > 0) { node = stack.pop(); node = node.getLeft(); } } while (temp.size() > 0) { node = temp.pop(); visit(node); } } /** 非递归实现中序遍历 */ protected static void iterativeInorder(Node p) { Stack<Node> stack = new Stack<Node>(); while (p != null) { while (p != null) { if (p.getRight() != null) stack.push(p.getRight());// 当前节点右子入栈 stack.push(p);// 当前节点入栈 p = p.getLeft(); } p = stack.pop(); while (!stack.empty() && p.getRight() == null) { visit(p); p = stack.pop(); } visit(p); if (!stack.empty()) p = stack.pop(); else p = null; } } /** * @param args */ public static void main(String[] args) { BinaryTree tree = new BinaryTree(init()); System.out.print(" Pre-Order:"); preorder(tree.getRoot()); System.out.println(); System.out.print(" In-Order:"); inorder(tree.getRoot()); System.out.println(); System.out.print("Post-Order:"); postorder(tree.getRoot()); System.out.println(); System.out.print(" Pre-Order:"); iterativePreorder(tree.getRoot()); System.out.println(); System.out.print("Pre-Order2:"); iterativePreorder2(tree.getRoot()); System.out.println(); System.out.print(" In-Order:"); iterativeInorder(tree.getRoot()); System.out.println(); System.out.print(" In-Order2:"); iterativeInorder2(tree.getRoot()); System.out.println(); System.out.print(" Post-Order:"); iterativePostorder(tree.getRoot()); System.out.println(); System.out.print("Post-Order2:"); iterativePostorder2(tree.getRoot()); System.out.println(); System.out.print("Post-Order3:"); iterativePostorder3(tree.getRoot()); System.out.println(); System.out.print("Post-Order4:"); iterativePostorder4(tree.getRoot()); System.out.println(); }}
0 0
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 注解处理工具apt
- centos6.5终端下用sublime打开文件
- location.href
- java获取客户端的信息
- pycharm安装第三方库
- 二叉树的遍历
- Java 常用设计模式-适配器模式adapter
- 分享移动端车牌识别OCR识别流程
- Vue.js前后端同构方案之准备篇——代码优化
- Leetcode 68. Text Justification
- Iframe页面传值
- 1、嵌入式开发工具链
- 使用maven创建spring-boot项目
- android百度地图开发——android studio配置sdk