二叉树的创建,递归和非递归遍历

来源:互联网 发布:淘宝质量鉴定什么意思 编辑:程序博客网 时间:2024/06/05 19:22

递归的实现很简单,我就不介绍了;下面我就说说非递归遍历


1. 先序非递归遍历

        根据前序遍历访问的顺序,优先访问根结点,然后再分别访问左孩子和右孩子。即对于任一结点,其可看做是根结点,因此可以直接访问,访问完之后,若其左孩子不为空,按相同规则访问它的左子树;当访问其左子树时,再访问它的右子树。因此其处理过程如下:

     对于任一结点P:

     1)访问结点P,并将结点P入栈;

     2)判断结点P的左孩子是否为空,若为空,则取栈顶结点并进行出栈操作,并将栈顶结点的右孩子置为当前的结点P,循环至1);若不为空,则将P的左孩子置为当前的结点P;

     3)直到P为NULL并且栈为空,则遍历结束。

代码片段:

private static void preUnRecursive(node p) {// TODO Auto-generated method stubStack<node> stack=new Stack<node>();while (p!=null||!stack.isEmpty()) {while (p!=null) {System.out.print(p.val+" ");   //<span style="color:#FF0000;">访问节点</span>stack.push(p);p=p.left;}if (!stack.isEmpty()) {    p=stack.pop();p=p.right;}}}

2.中序非递归遍历

根据中序遍历的顺序,对于任一结点,优先访问其左孩子,而左孩子结点又可以看做一根结点,然后继续访问其左孩子结点,直到遇到左孩子结点为空的结点才进行访问,然后按相同的规则访问其右子树。因此其处理过程如下:

   对于任一结点P,

  1)若其左孩子不为空,则将P入栈并将P的左孩子置为当前的P,然后对当前结点P再进行相同的处理;

  2)若其左孩子为空,则取栈顶元素并进行出栈操作,访问该栈顶结点,然后将当前的P置为栈顶结点的右孩子;

  3)直到P为NULL并且栈为空则遍历结束


代码片段:

private static void inOrderUnRecursive(node p) {// TODO Auto-generated method stubStack<node> stack=new Stack<node>();while (p!=null||!stack.isEmpty()) {while (p!=null) {stack.push(p);p=p.left;}if (!stack.isEmpty()) {p=stack.pop();System.out.print(p.val+" ");      //<span style="color:#FF0000;">访问节点</span>p=p.right;}}}

细心的同学可能发现了,先序和后序的代码差别很小,仅仅在于访问节点的那一句. 记住了先序那么中序也就记住了。


下面是最难的后序遍历了,其实也没那么难,不要被吓到了。。。。。好好的看下面一段话你就明白了

思路:要保证根结点在左孩子和右孩子访问之后才能访问,因此对于任一结点P,先将其入栈。如果P不存在左孩子和右孩子,则可以直接访问它;或者P存在左孩子或者右孩子,但是其左孩子和右孩子都已被访问过了,则同样可以直接访问该结点。若非上述两种情况,则将P的右孩子和左孩子依次入栈,这样就保证了每次取栈顶元素的时候,左孩子在右孩子前面被访问,左孩子和右孩子都在根结点前面被访问。

代码片段:

private static void postUnRecursive(node p) {// TODO Auto-generated method stubStack<node> stack=new Stack<node>();node pre=null;stack.push(p);while (!stack.isEmpty()) {p=stack.peek();if ((p.left==null&&p.right==null)||(pre==p.right)) {// p为叶子节点或者p的右节点刚刚访问过System.out.print(stack.pop().val+" ");    //出栈并且打印   pre=p;                                    //更新pre }else {                                       //将非空右节点和左节点加入栈if (p.right!=null) {                 stack.push(p.right);}if (p.left!=null) {stack.push(p.left);}}}}


完整代码:

package 二叉树;import java.util.Scanner;import java.util.Stack;class node{    node left;    node right;    int val;    public node(int val) {        this.val=val;    }}public class BinaryTree {     /**       *       1     *     /   \       *    2     3       *   / \      / \     *  4   5     6  7     *       */        public static void main(String[] args) {        // TODO Auto-generated method stub        Scanner in=new Scanner(System.in);        System.out.println("请输入要创建的树先序遍历(空节点用0代替)-----------");        node root=    createBinaryTree (in);        System.out.println("递归先序遍历-----------");        preRecursive (root);        System.out.println();        System.out.println("非递归先序遍历-----------");                preUnRecursive (root);        System.out.println();        System.out.println("递归中序遍历-----------");        inOrderRecursive(root);        System.out.println();        System.out.println("非递归中序遍历-----------");        inOrderUnRecursive(root);        System.out.println();        System.out.println("递归后序遍历-----------");        postRecursive(root);        System.out.println();        System.out.println("非递归后序遍历-----------");        postUnRecursive(root);    }    private static void postUnRecursive(node p) {        Stack<node> stack=new Stack<node>();        node pre=null;        stack.push(p);                while (!stack.isEmpty()) {            p=stack.peek();            boolean leaf=p.left==null&&p.right==null;            if (leaf||pre==p.right) {                System.out.print(stack.pop().val+" ");                pre=p;            }else {                if (p.right!=null ) {                    stack.push(p.right);                }                if (p.left!=null ) {                    stack.push(p.left);                }            }        }                                            }    private static void postRecursive(node root) {        // TODO Auto-generated method stub        if (root!=null) {            postRecursive(root.left);            postRecursive(root.right);            System.out.print(root.val+" ");        }    }    private static void inOrderUnRecursive(node p) {        Stack<node> stack=new Stack<node>();        while (p!=null||!stack.isEmpty()) {            while (p!=null ) {                                stack.push(p);                p=p.left;            }                        if (!stack.isEmpty()) {                p=stack.pop();                System.out.print(p.val+" ");                p=p.right;            }        }    }    private static void inOrderRecursive(node root) {        // TODO Auto-generated method stub        if (root!=null) {            inOrderRecursive(root.left);            System.out.print(root.val+" ");            inOrderRecursive(root.right);        }    }    private static void preUnRecursive(node p) {        Stack<node> stack=new Stack<node>();        while (p!=null||!stack.isEmpty()) {            while (p!=null ) {                System.out.print(p.val+" ");                stack.push(p);                p=p.left;            }                        if (!stack.isEmpty()) {                p=stack.pop();                p=p.right;            }        }    }    private static void preRecursive(node root) {        // TODO Auto-generated method stub        if (root!=null) {            System.out.print(root.val+" ");            preRecursive(root.left);            preRecursive(root.right);        }    }    private static node createBinaryTree(Scanner in) {        // TODO Auto-generated method stub        int val=in.nextInt();        node newNode=null;        if (val==0) {            return null;        }else {            newNode=new node(val);            newNode.left=createBinaryTree(in);            newNode.right=createBinaryTree(in);        }        return newNode;    }}

测试结果:



1 0
原创粉丝点击