二叉树的基本操作

本文详细介绍了二叉树的定义及其基本性质,并通过Java代码实现了一个二叉树节点类和二叉树操作类,包括创建二叉树、求深度、节点数、叶子节点数及三种遍历方式。

二叉树的定义是递归的

定义:
二叉树(Binary tree)是n(n≥0)个结点的有限集合。若n=0时称为空树,否则:
⑴有且只有一个特殊的称为树的根(Root)结点;
⑵若n>1时,其余的结点被分成为二个互不相交的子集T1,T2,分别称之为左、右子树,并且左、右子树又都是二叉树。

二叉树的性质:
1.在非空二叉树中,第i层上至多有2i-1个结点(i≧1)
2.深度为k的二叉树至多有2k-1个结点(k≧1)
3.对任何一棵二叉树,若其叶子结点数为n,度为2的结点数为m,则n=m+1

封装一个节点类

public class BTNode {
    char value;//节点存的值
    BTNode Lchild;//左子树
    BTNode Rchild;//右子树
    public char getValue() {
        return value;
    }
    public void setValue(char value) {
        this.value = value;
    }
    public BTNode getLchild() {
        return Lchild;
    }
    public void setLchild(BTNode lchild) {
        Lchild = lchild;
    }
    public BTNode getRchild() {
        return Rchild;
    }
    public void setRchild(BTNode rchild) {
        Rchild = rchild;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((Lchild == null) ? 0 : Lchild.hashCode());
        result = prime * result + ((Rchild == null) ? 0 : Rchild.hashCode());
        result = prime * result + value;
        return result;
    }
    public BTNode(char value, BTNode lchild, BTNode rchild) {
        super();
        this.value = value;
        Lchild = lchild;
        Rchild = rchild;
    }
    public BTNode() {
        super();
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        BTNode other = (BTNode) obj;
        if (Lchild == null) {
            if (other.Lchild != null)
                return false;
        } else if (!Lchild.equals(other.Lchild))
            return false;
        if (Rchild == null) {
            if (other.Rchild != null)
                return false;
        } else if (!Rchild.equals(other.Rchild))
            return false;
        if (value != other.value)
            return false;
        return true;
    }
    @Override
    public String toString() {
        return "BTNode [value=" + value + ", Lchild=" + Lchild + ", Rchild=" + Rchild + "]";
    }
}

二叉树的操作类

public class BinaryTree {
    // 创建一颗二叉树
    public BTNode createBinaryTree(BTNode T) {

        char op = 0;
        Scanner sc = new Scanner(System.in);

        System.out.print("please enter the data:");
        char value;
        value = sc.next().charAt(0);
        T.setValue(value);

        if (T.value == ' ') {
            T.Lchild = null;
            T.Rchild = null;
        } else {
            // 左子树
            System.out.print("Does " + T.value + " have Lchild?(Y|N)");
            op = sc.next().charAt(0);
            // 防止输入y、Y、n、N以外的其他字符
            while (op != 'y' && op != 'Y' && op != 'n' && op != 'N') {
                System.out.println("error\nplease enter again");
                op = sc.next().charAt(0);
            }

            if (op == 'y' || op == 'Y') {
                T.Lchild = new BTNode();
                createBinaryTree(T.Lchild);//递归调用
            } else {
                T.Lchild = null;
            }
            // 右子树
            System.out.print("Does " + T.value + " have Rchild?(Y|N)");
            op = sc.next().charAt(0);
            // 防止输入y、Y、n、N以外的其他字符
            while (op != 'y' && op != 'Y' && op != 'n' && op != 'N') {
                System.out.println("error\nplease enter again");
                op = sc.next().charAt(0);
            }

            if (op == 'y' || op == 'Y') {
                T.Rchild = new BTNode();
                createBinaryTree(T.Rchild);
            } else {
                T.Rchild = null;
            }
        }

        return T;
    }

    // 求二叉树的深度
    public int depth(BTNode T) {
        int Rdepth = 0;//右子树的深度
        int Ldepth = 0;//左子树的深度

        if(T.Lchild != null){
            Ldepth = depth(T.Lchild);
        }
        if(T.Rchild != null){
            Rdepth = depth(T.Rchild);
        }
        return Rdepth > Ldepth ? Rdepth + 1 : Ldepth + 1;
    }

    // 求二叉树中节点的个数
    public int nodeNumber(BTNode T) {
        int Lchildnum = 0;//左子树节点的个数
        int Rchildnum = 0;//右子树节点的个数

        if(T.Lchild != null){
            Lchildnum = nodeNumber(T.Lchild);
        }
        if(T.Rchild != null){
            Rchildnum = nodeNumber(T.Rchild);
        }

        //返回每个节点的左右节点个数加上本身
        return Lchildnum + Rchildnum + 1;
    }

    //求叶子节点个数
    public int leafChild(BTNode T){
        int Lchildnum = 0;//左子树叶子节点个数
        int Rchildnum = 0;//右子树叶子节点个数

        if(T.Lchild != null)
            Lchildnum = leafChild(T.Lchild);
        if(T.Rchild != null)
            Rchildnum = leafChild(T.Rchild);

        //若是叶子节点,返回本身
        if(T.Lchild == null && T.Rchild == null)
            return 1;
        //若不是叶子节点 返回每个结点的左子树的叶子节点加上右子树的叶子节点
        else
            return Lchildnum + Rchildnum;
    }

    // 先序遍历
    public void preOrderTraverse(BTNode T) {
        if (T != null) {
            System.out.print(T.value);
            preOrderTraverse(T.Lchild);
            preOrderTraverse(T.Rchild);
        }
    }

    // 中序遍历
    public void inOrderTraverse(BTNode T) {
        if (T != null) {
            inOrderTraverse(T.Lchild);
            System.out.print(T.value);
            inOrderTraverse(T.Rchild);
        }
    }
    // 后序遍历
    public void postOrderTraverse(BTNode T){
        if(T != null){
            postOrderTraverse(T.Lchild);
            postOrderTraverse(T.Rchild);
            System.out.print(T.value);
        }
    }

    // 输出二叉树
    public void print(BTNode T) {
        System.out.println(T);
    }
}

主方法

public static void main(String[] args) {
        BinaryTree bt = new BinaryTree();
        BTNode T = new BTNode();

        bt.createBinaryTree(T);

        System.out.print("先序遍历:");
        bt.preOrderTraverse(T);
        System.out.println();

        System.out.print("中序遍历:");
        bt.inOrderTraverse(T);
        System.out.println();

        System.out.print("后序遍历:");
        bt.postOrderTraverse(T);
        System.out.println();

        System.out.println("结点个数:" + bt.nodeNumber(T));
        System.out.println("二叉树的深度:" + bt.depth(T));
        System.out.println("叶子结点的个数:" + bt.leafChild(T));
    }

以两个例子运行程序:
NO.1
这里写图片描述
运行结果:
这里写图片描述
NO.2
这里写图片描述
运行结果:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值