二叉树简单实现及遍历方式

/**
 * 简单实现二叉树
 * 插入,遍历
 *
 */
public class BinaryTree {
    public int root;
    public BinaryTree leftChild;
    public  BinaryTree rightChild;

    public BinaryTree(int root) {
        this.root = root;
    }

    @Override
    public String toString() {
        return "BinaryTree{" +
                "root=" + root +
                ", leftChild=" + leftChild +
                ", rightChild=" + rightChild +
                '}';
    }

    /**
     * 插入
     * @param data
     */
    public void insert(BinaryTree root,int data){
        if(data>=root.root){
            //右节点没有跟节点,直接保存
            if(null==root.rightChild){
                root.rightChild=new BinaryTree(data);
            }else{
                root.rightChild.insert(root.rightChild,data);
            }
        }else{
            //左节点没有跟节点,直接保存
            if(null==root.leftChild){
                root.leftChild=new BinaryTree(data);
            }else{
                root.leftChild.insert(root.leftChild,data);
            }
        }
    }

    /**
     * 先序遍历
     * 根-》左-》右
     * @param binaryTree
     */
    public  void preQuery(BinaryTree binaryTree){
        if(null!=binaryTree){
            System.out.print(binaryTree.root+"-");
            binaryTree.preQuery(binaryTree.leftChild);
            binaryTree.preQuery(binaryTree.rightChild);
        }
    }


    /**
     * 中序遍历
     * 左-》根-》右
     * @param binaryTree
     */
    public  void inQuery(BinaryTree binaryTree){
        if(null!=binaryTree){
            binaryTree.inQuery(binaryTree.leftChild);
            System.out.print(binaryTree.root+"-");
            binaryTree.inQuery(binaryTree.rightChild);
        }
    }

    /**
     * 后序遍历
     * 左-》右-》根
     * @param binaryTree
     */
    public  void lastQuery(BinaryTree binaryTree){
        if(null!=binaryTree){
            binaryTree.lastQuery(binaryTree.leftChild);
            binaryTree.lastQuery(binaryTree.rightChild);
            System.out.print(binaryTree.root+"-");
        }
    }


    public static void main(String[] args) {
        //创建二叉树
        BinaryTree binaryTree=new BinaryTree(5);
        binaryTree.insert(binaryTree,6);
        binaryTree.insert(binaryTree,4);
        binaryTree.insert(binaryTree,3);
        binaryTree.insert(binaryTree,2);
        binaryTree.insert(binaryTree,7);
        binaryTree.insert(binaryTree,8);
        binaryTree.insert(binaryTree,6);

        System.out.println("前序:");
        binaryTree.preQuery(binaryTree);
        System.out.println("\n");
        System.out.println("中序:");
        binaryTree.inQuery(binaryTree);
        System.out.println("\n");
        System.out.println("后序:");

        binaryTree.lastQuery(binaryTree);
    }

}

结果

前序:
5-4-3-2-6-7-6-8-

中序:
2-3-4-5-6-6-7-8-

后序:
2-3-4-6-8-7-6-5-

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值