二叉树<一> — — 3种方式的遍历

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/kyle0349/article/details/51953085

一、二叉树

百度百科:在计算机科学中,二叉树是每个节点最多有两个子树的树结构。通常子树被称作“左子树”(left subtree)和“右子树”(right subtree)。二叉树常被用于实现二叉查找树和二叉堆。二叉树的每个结点至多只有二棵子树(不存在度大于2的结点),二叉树的子树有左右之分,次序不能颠倒。

二、构建一个二叉树

  • 在这里我们先约定一个存储的方式:
    • 从根节点开始存储数据,后面的存储的每个数据与根节点比较,比根节点小的数据放左子树节点,判断左子树节点是否为空,空则存储数据,不空,则把该节点作为根节点,重复上述比较;比根节点大或者与根节点相同的数据走右子树节点,操作与左节点相同;
    • 最后我们得到的这个二叉树结构是每个左节点比根节点小,每个右节点比根节点大或者与根节点相等。
package cn.test;

public class BinaryTree {

    int node;
    BinaryTree left;
    BinaryTree right;

    public BinaryTree(int node){
        this.node = node;
        left = null;
        right = null;
    }
   //把数据源数组按照约定存储进二叉树结构
    public void insert(BinaryTree root, int node){
        if(node >= root.node){
            if(root.right == null){
                root.right = new BinaryTree(node);
            }else{
                this.insert(root.right, node);
            }
        }else{
            if(root.left == null){
                root.left = new BinaryTree(node);
            }else{
                this.insert(root.left, node);
            }
        }
    }
}

三、遍历(3中方式)递归思想!!!

1、先根遍历:首先获取根节点值,再获取左(右)子树节点值,最后获取右(左)子树节点值,(本例中是先左子树节点,再右子树节点)

//先根遍历  
    public static void rootFirst(BinaryTree root){ 
         if(root != null){
             System.out.print(root.node + " - ");
             if(root.left != null){
                 rootFirst(root.left);
             }
             if(root.right != null){
                 rootFirst(root.right);
             }
         }
    }

2、中根遍历:首先获取左(右)子树节点值,再获取根节点值,最后获取右(左)子树节点值;(本例中是先左子树节点,再右子树节点)

//中根遍历
    public static void rootSeconde(BinaryTree root){
        if(root !=null ){
            if(root.left != null){
                rootSeconde(root.left);
            }
            System.out.print(root.node + " - ");
            if(root.right != null){
                rootSeconde(root.right);
            }
        }
    }

3、后根遍历:首先获取左(右)子树节点值,再获取右(左)子树节点值,最后获取根节点值;(本例中是先左子树节点,再右子树节点)

//后根遍历
    public static void rootLast(BinaryTree root){
        if(root !=null ){
            if(root.left != null){
                rootLast(root.left);
            }
            if(root.right != null){
                rootLast(root.right);
            }
            System.out.print(root.node + " - ");
        }
    }

四、存储完成后得到的二叉树结构图:

这里写图片描述

五、遍历结果

这里写图片描述
PS. 我们发现通过中根遍历获取出来的数据已经按升序排序了。

六、测试类代码

package cn.test;

public class Test {
    public static void main(String[] args) {

        int[] array = new int[20];
       //(随机生成的一个长度为20的数组)
        for(int i = 0 ; i < array.length; i ++ ){
            array[i] = (int) (Math.random()*100);
        }
        //输出数据源数组       System.out.println("数据源数组");
        for(Integer arr:array){
            System.out.print(arr+" - ");
        }
        System.out.println();
        BinaryTree root = new BinaryTree(array[0]);
        //将数据源数组按照约定存储进二叉树结构中
        for(int i = 1 ; i < array.length;i++){
            root.insert(root, array[i]);
        }
        //调用先根遍历
        System.out.println("先根遍历");
        rootFirst(root);
        System.out.println();
        //调用中根遍历
        System.out.println("中根遍历");
        rootSeconde(root);
        System.out.println();
        //调用后根遍历
        System.out.println("后根遍历");
        rootLast(root);
    }
    //先根遍历  
    public static void rootFirst(BinaryTree root){ 
         if(root != null){
             System.out.print(root.node + " - ");
             if(root.left != null){
                 rootFirst(root.left);
             }
             if(root.right != null){
                 rootFirst(root.right);
             }
         }
    }                                
    //中根遍历
    public static void rootSeconde(BinaryTree root){
        if(root !=null ){
            if(root.left != null){
                rootSeconde(root.left);
            }
            System.out.print(root.node + " - ");
            if(root.right != null){
                rootSeconde(root.right);
            }
        }
    }
    //后根遍历
    public static void rootLast(BinaryTree root){
        if(root !=null ){
            if(root.left != null){
                rootLast(root.left);
            }
            if(root.right != null){
                rootLast(root.right);
            }
            System.out.print(root.node + " - ");
        }
    }
}

猜你喜欢

转载自blog.csdn.net/kyle0349/article/details/51953085