二叉树递归,非递归遍历二叉树

来源:互联网 发布:数据质量维度有效性 编辑:程序博客网 时间:2024/06/02 04:24

左程云,左神的课讲解的:

二叉树需要记住的一些性质:不存在度大于二的结点,二叉树的第i层至多有2^{i-1}个结点,深度为k的二叉树至多有2^k-1个结点,如果其终端结点数为n,度为2 的结点数为m,n=m+1

掌握前序,中序,后序遍历,一般写法。

二叉树存储结构---链式存储结构

public class tree{

    public static class  node{

            int data;

            node  left;

            node  right;

        public node(int data){

                this.data=data;

        }

    }

   递归的//前序遍历(当打印位置不一样,都是每个结点访问)

    public static  void pretravel(node head){

        if(head==null){

            return;

        }

        System.out.print(head.data);

        pretravel(head.left);

        pretravel(head.right);

    }

//中序遍历

    public static void zhongtravel(node head){

        if(head==null){

            return;

        }

        zhongtravel(head.left);

        System.out.print(head.data);

        zhongtravel(head.right);

}

//后序遍历

    public static void houtravel(node head){

        if(head==null){

            return;

        }

        houtravel(head.left);

 

        houtravel(head.right);System.out.print(head.data);

}

 

 

 

 

//用栈的方式存储,前序遍历 首先将头结点入栈,出栈并打印,移动到该出栈节点的右子树,压栈,将左子树压栈,出栈一个元素并打印,在压右子树,压左子树,出栈并打印。循环即可

    public  static  void  stapre(node head){

        Stack<node>  stack=new Stack<node>();

        if(head!=null){

            stack.push(head);

            while(!stack.isEmpty())

            {

head=stack.pop();

            System.out.print(head.data);

            if(head.right!=null){

            stack.push(head.right);}

            if(head.left!=null){

            stack.push(head.left);}

            }

        }

}

//中序遍历,压栈,先将所有左边的结点压栈,出栈的同时打印,并移到右结点,如果没有右结点,继续出栈,基本思路就是所有的二叉树,都可以看成左边的树。

    public static void  stazhong(node head){

        Stack<node>  stack=new Stack<node>();

        if(head!=null){

           while(!stack.isEmpty()||head!=null){

                if(head!=null){

                    stack.push(head);

                    head=head.left;

                }else{

                    head=stack.pop();

                   System.out.print(head.data);

                    head=head.right;

                }

 

 

            }

        }

 

 

    }

//前序遍历的顺序就是中左右  后序遍历的逆序是中右左,就是在栈中先压头结点,然后压他的右树,在压左树,出栈。出栈的时候不打印,而是将数据压入另一个栈中,然后出栈就结束了。

    public  static  void  stahou(node head){

        Stack<node>  stack=new Stack<node>();

        Stack<node>  stack1=new Stack<node>();

        if(head!=null){

            stack.push(head);

            while(!stack.isEmpty())

            {   head=stack.pop();

                stack1.push(head);

                if(head.left!=null){

                    stack.push(head.left);}

                if(head.right!=null){

                    stack.push(head.right);}

            }

        }

        while(!stack1.isEmpty()){

           System.out.print(stack1.pop().data);

        }

    }

public static void main(String[] args) {

//创建一颗树

        node head = new node(5);

        head.left = new node(3);

        head.right = new node(8);

        head.left.left = new node(2);

        head.left.right = new node(4);

        head.left.left.left = new node(1);

        head.right.left = new node(7);

        head.right.left.left = new node(6);

        head.right.right = new node(10);

        head.right.right.left = new node(9);

        head.right.right.right = new node(11);

 

    //遍历遍历

        System.out.println("==============recursive==============");

        System.out.print("pre-order:");

        pretravel(head);

        System.out.println();

        System.out.print("in-order:");

        zhongtravel(head);

        System.out.println();

        System.out.print("pos-order:");

        houtravel(head);

        System.out.println();

        stapre(head);

        System.out.println();

        stazhong(head);

        System.out.println();

        stahou(head);

 

    }

 

    }

接下来需要学习层次遍历,以及morris遍历, 等等,路漫漫

 

原创粉丝点击