二叉树遍历

来源:互联网 发布:ubuntu设置用户根目录 编辑:程序博客网 时间:2024/06/08 17:34

        二叉树的递归与非递归算法。

        完整代码:

package cn.ccn.print;import java.util.Stack;// 节点的数据结构class Node{public int value;public Node left;public Node right;public Node(int value) {super();this.value = value;}}public class PrintTree {// 递归前序遍历树public void preOrder(Node head){if(head == null){return;}System.out.print(head.value + "  ");preOrder(head.left);preOrder(head.right);}// 递归中序遍历public void inOrder(Node head){if(head == null){return;}inOrder(head.left);System.out.print(head.value + "  ");inOrder(head.right);}// 递归后续遍历public void posOrder(Node head){if(head == null){return;}posOrder(head.left);posOrder(head.right);System.out.print(head.value + "  ");}// 非递归方法// 非递归前序遍历public void preOrderUuRecur(Node head){if(head != null){Stack<Node> stack = new Stack<Node>();stack.push(head);while(!stack.isEmpty()){Node temp = stack.pop();System.out.print(temp.value + "  ");if(temp.right != null){stack.add(temp.right);}if(temp.left != null){stack.add(temp.left);}}}}// 非递归中序遍历public void inOrderUuRecur(Node head){if(head != null){Stack<Node> stack = new Stack<Node>();while(!stack.isEmpty() || head!=null){if(head != null){stack.add(head);head = head.left;}else{head = stack.pop();System.out.print(head.value + "  ");head = head.right;}}}}// 用两个栈实现非递归后序遍历public void posOrderUnRecur(Node head){if(head != null){Stack<Node> stack1 = new Stack<Node>();Stack<Node> stack2 = new Stack<Node>();stack1.add(head);while(!stack1.isEmpty()){head = stack1.pop();stack2.add(head);if(head.left != null){stack1.add(head.left);}if(head.right != null){stack1.add(head.right);}}while(!stack2.isEmpty()){System.out.print(stack2.pop().value + "  ");}}}// 用一个栈实现非递归后序遍历public void posOrderUnRecurOneStack(Node head){if(head != null){Stack<Node> stack = new Stack<Node>();stack.add(head);Node cur = null; // 用来表示当前栈顶元素while(!stack.isEmpty()){cur = stack.peek();if(cur.left!=null && head!=cur.left && head!=cur.right){ stack.add(cur.left);}else if(cur.right!=null && cur.right!=head){stack.add(cur.right);}else{System.out.print(stack.pop().value + "  ");head = cur;}}}}public static void main(String[] args) {PrintTree printTree = new PrintTree();Node head = new Node(1);Node n1 = new Node(2);Node n2 = new Node(3);Node n3 = new Node(4);Node n4 = new Node(5);Node n5 = new Node(6);Node n6 = new Node(7);head.left = n1;head.right = n2;n1.left = n3;n1.right = n4;n2.left = n5;n2.right = n6;System.out.println("前序遍历:");printTree.preOrder(head);System.out.println();System.out.println("中序遍历:");printTree.inOrder(head);System.out.println();System.out.println("后序遍历:");printTree.posOrder(head);System.out.println();System.out.println("=====非递归=========");System.out.println("前序遍历:");printTree.preOrderUuRecur(head);System.out.println();System.out.println("中序遍历:");printTree.inOrderUuRecur(head);System.out.println();System.out.println("后序遍历:");printTree.posOrderUnRecur(head);System.out.println();System.out.println("后序遍历:");printTree.posOrderUnRecurOneStack(head);}}

原创粉丝点击