二叉查找树

/*
 *  二叉查找树(binary search/sort tree,BST)是具有如下性质的二叉树:对于二叉树中的任意一个结点,
 *  如果它包含的数据元素为data,那么它的左子树(如果非空)只包含小于data的元素,并且它的右子树
 *  (如果非空)只包含大于或者等于data的元素。它是一种动态树表,树结构在插入元素时生成。
 * 
 * */

public class BSTree {

    // 假设结点值为整数
    private static class BSTNode {
        private int value;
        private BSTNode left;
        private BSTNode right;

        public BSTNode() {
        }

        public BSTNode(int value) {
            this.value = value;
        }

        public int getvalue() {
            return value;
        }

        public void setvalue(int value) {
            this.value = value;
        }

        public BSTNode getLeft() {
            return left;
        }

        public void setLeft(BSTNode left) {
            this.left = left;
        }

        public BSTNode getRight() {
            return right;
        }

        public void setRight(BSTNode right) {
            this.right = right;
        }
    }

    private BSTNode root;
    private int size;

    // 构造空二叉树
    public BSTree() {
        clear();
    }

    // 构造只有一个节点的二叉树
    public BSTree(int value) {
        root = new BSTNode(value);
    }

    public final void clear() {    // 不可覆写
        root = null;
        this.size = 0;
    }

    /*
     *  node:二叉树上的一个节点
     *  value:要操作的数据
     *  
     *  */
    public BSTNode insert(BSTNode node, int value) {
        if (node == null) {
            size++;
            if (root == null) {        // 是否是空二叉树,根节点有些特殊,它没有父节点也是二叉树的起点
                root = new BSTNode(value);
                return root;
            }
            return new BSTNode(value);
        } else {
            if (value < node.value) {
                node.left = insert(node.left, value);
            } else {
                node.right = insert(node.right, value);
            }
        }
        return node;
    }

    // 直接插入二叉树,自动寻找到插入位置
    public void insert(int value) {
        insert(root, value);
    }

    private BSTNode search(BSTNode node, int value) {
        if(node == null) {
            return null;
        } else {
            if(value == node.value) {
                return node;
            } else if(value < node.value){
                return search(node.left, value);
            } else {
                return search(node.right, value);
            }
        }
    }
    
    public boolean contains(int value) {
        return (search(root, value) != null);
    }

    /*
     * 前序:根-左-右
     * 中序:左-根-右,将会得到从小到大排列的结点序列
     * 后序:左-右-根
     * 
     * */
    private void midList(BSTNode node) {
        if (node.left != null) {
            midList(node.left);
        }
        System.out.print(node.value + ",");
        if (node.right != null) {
            midList(node.right);
        }
    }

    public void midList() {
        midList(root);
    }
    
    // 逆中序(右-根-左)打印,打印后的结果顺时针旋转90°就是正常的二叉树形状了
    /*
     * @param  
     *         node: 打印的节点
     *         depth: 打印节点的深度
     * */
    private void print(BSTNode node, final int depth) {
        if(node == null) {
            return;
        }
        print(node.right,depth+1);        // 注意这里不能用depth++或++depth
        for(int i = 0; i < depth; i++) {
            System.out.print("    ");
        }
        System.out.println(node.value);
        print(node.left,depth+1);
    }
    
    // 打印二叉树
    public void print(){
        print(root,0);
    }
    
    public int size() {
        return size;
    }
}
public class BSTreeTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        BSTree bst = new BSTree();
        int[] array = new int[] {6,4,9,1,8,3,10};
        for(int i : array) {
            bst.insert(i);
        }
        System.out.println(bst.contains(1));
        bst.midList();
//        bst.print();
    }

}
原文地址:https://www.cnblogs.com/zhangpeng201211/p/2766991.html