二叉树2

二叉树类:

package binarytreetraversal;

/**
 *
 * @author shootzp
 */
public class BST {

    public static class BSTNode {

        Comparable data;
        BSTNode left;
        BSTNode right;

        public BSTNode() {
        }

        public BSTNode(Comparable obj) {
            this.data = obj;
        }

        public BSTNode(Comparable obj, BSTNode leftChild, BSTNode rightChild) {
            this.data = obj;
            this.left = leftChild;
            this.right = rightChild;
        }

        @Override
        public String toString() {
            return data.toString();
        }
    }
    private BSTNode root;
    private int count;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public BSTNode getRoot() {
        return root;
    }

    public void setRoot(BSTNode root) {
        this.root = root;
    }

    public BST() {
    }

    public void clear() {
        root = null;
        count = 0;
    }

    public void insert(Comparable val) {
        root = insert(root, val);
    }

//    public void delete(Comparable val) {
//        root = delete(root,val);
//    }
    public boolean isEmpty() {
        return root == null;
    }

    public int size() {
        return this.count + 1;
    }

    public boolean contains(Comparable val) {
        return search(root, val) != null;
    }

    private BSTNode insert(BSTNode ref, Comparable val) {
        if (ref == null) {
            count++;
            return new BSTNode(val);
        } else {
            int comp = val.compareTo(ref.data);
            if (comp < 0) {
                ref.left = insert(ref.left, val);
            } else {
                ref.right = insert(ref.right, val);
            }
            return ref;
        }
    }

//    private BSTNode delete(BSTNode root, Comparable val) {
//        throw new UnsupportedOperationException("Not yet implemented");
//    }
    private BSTNode search(BSTNode ref, Comparable val) {
        if (ref == null) {
            return null;
        } else {
            int comp = val.compareTo(ref.data);
            if (comp == 0) {
                return ref;
            } else if (comp < 0) {
                return search(ref.left, val);
            } else {
                return search(ref.right, val);
            }
        }
    }
}
测试类:

package binarytreetraversal;

/**
 *
 * @author shootzp
 */
public class BSTTest {

    public static void main(String[] args) {
        BST b = new BST();
//        BST.BSTNode root = new BSTNode("fox");
//        b.setRoot(root);
        b.insert("ant");
        b.insert("cat");
        b.insert("dog");
        b.insert("fox");
        b.insert("hen");
        b.insert("pig");
        b.insert("rat");
        print(b);
    }

    public static void print2(BST.BSTNode ref, int level) {
        if (ref == null) {
//            System.out.println("空!");
            return;
        }
        /* 逆中序遍历二叉树(右-根-左) */
        print2(ref.right, level + 1);        // 打印右子树
        for (int i = 0; i < level; i++) {     //level控制打印层次缩进
            System.out.print("    ");
        }
        System.out.println(ref.data);       // 打印节点
        print2(ref.left, level + 1);         // 打印左子树
    }

    public static void print(BST b) {
        BST.BSTNode root = b.getRoot();
        if (b.isEmpty()) {
            return;
        }
        print2(root, 0);
    }
}

结果:(试试改变插入顺序查看结果)

                        rat
                    pig
                hen
            fox
        dog
    cat
ant





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值