Java实现二叉排序树

二叉树 是数据结构里比较重要的一部分。有二叉树还引申出各个分支,如:平衡树、排序树、字典树、最优树等。
对于各种二叉树的特点和算法思想,我觉得这一篇博文讲的非常详细,在此附上链接地址:二叉树的应用详解
下面是Java实现的一个简单二叉排序树,代码如下:

package tree;

public class BinaryTree {
    class Node { //定义树结点
        int value;
        Node left;
        Node right;

        Node(int value) {
            this.value = value;
            this.left = null;
            this.right = null;
        }   
    }

    private Node root;

    public BinaryTree() {
        // 构造空树
    }
    public BinaryTree(Node n) { 
        //构造有一个结点的树
        root.value = n.value;
        root.left = null;
        root.right = null;
    }
    public void addKey(int key) {
        //插入数据
        Node node = new Node(key);
        if(root == null) {
            root = node;
        }
        else {
            Node current = root;
            Node parent = null;
            while(true) {
                if(key < current.value) { //小于则向左孩子遍历
                    parent = current;
                    current = current.left;
                    if(current == null) {
                        parent.left = node;
                        break;
                    }
                }
                else if(key > current.value) { //大于则向右孩子遍历
                    parent = current;
                    current = current.right;
                    if(current == null) {
                        parent.right = node;
                        break;
                    }
                }
                else {
                    System.out.println("不能插入重复值");
                    return;
                }
            }
        }
    }

    public Node findKey(int key) {
        //查找数据 ,与插入类似
        Node current = root;
        while(true) {
            if(current == null) {
                return null;
            }
            if(key == current.value) {
                return current;
            }
            else if(key  < current.value) {
                current = current.left;
            }
            else if(key > current.value) {
                current = current.right;
            }
        }
    }

    //中序遍历
    public void inOrderTraverse(Node node) {
        if(node == null) {
            return;
        }
        inOrderTraverse(node.left);
        System.out.print(node.value);
        inOrderTraverse(node.right);
    }
    //先序遍历
    public void preOrderTraverse(Node node) {
        if(node == null) {
            return;
        }

        System.out.print(node.value);
        preOrderTraverse(node.left);
        preOrderTraverse(node.right);
    }
    //后序遍历
    public void postOrderTraverse(Node node) {
        if(node == null) {
            return;
        }
        postOrderTraverse(node.left);
        postOrderTraverse(node.right);
        System.out.print(node.value);
    }

    public static void main(String[] args) {
        BinaryTree t = new BinaryTree();
        t.addKey(3);
        t.addKey(5);
        t.addKey(4);
        t.addKey(1);
        t.addKey(2);
        Node n = t.findKey(5);
        System.out.println("先序遍历");
        t.preOrderTraverse(t.root);
        System.out.println("\n中序遍历");
        t.inOrderTraverse(t.root);
        System.out.println("\n后序遍历");
        t.postOrderTraverse(t.root);
        System.out.println("\n要找的数据为:"+n.value);
    }
}

测试结果:
测试结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值