二叉排序树 -- 增删查改

数据结构 -- 树

1. 二叉排序树 -- 定义

2. 二叉排序树 -- 增

3. 二叉排序树 -- 删

4. 二叉排序树 -- 查

5. 二叉排序树 -- 改

6. 二叉排序树 -- 实现

  Class : 

package lime.tree;

import java.util.Arrays;

/**
 * @Author: Lime
 * @Description:
 * @Date:Create in 19:25 2017/10/28
 */
public class BSTTree {
    private Integer value;
    private BSTTree PChild;
    private BSTTree LChild;
    private BSTTree RChild;

    public BSTTree(Integer value) {
        this.value = value;
    }

    public static void showTree(BSTTree root) {
        if (null == root) {
            return;
        }
        showTree(root.getLChild());
        System.out.print(root.getValue() + "  ");
        showTree(root.getRChild());
    }

    public static boolean addTree(BSTTree root, Integer value) {
        if (null == value) {
            return false;
        }
        if (null == root) {
            root = new BSTTree(value);
            return true;
        }
        if (value == root.getValue()) {
            return false;
        } else if (value < root.getValue()) {
            if (null == root.getLChild()) {
                root.setLChild(value);
                root.getLChild().setPChild(root);
                return true;
            } else {
                return addTree(root.getLChild(), value);
            }
        } else {
            if (null == root.getRChild()) {
                root.setRChild(value);
                root.getRChild().setPChild(root);
                return true;
            } else {
                return addTree(root.getRChild(), value);
            }
        }
    }

    public static BSTTree searchTree(BSTTree root, Integer value) {
        if (null == root || null == value) {
            return null;
        }
        if (value == root.getValue()) {
            return root;
        } else if (value < root.getValue()) {
            return searchTree(root.getLChild(), value);
        } else {
            return searchTree(root.getRChild(), value);
        }
    }

    public static void deleteTree(BSTTree root, Integer value) {
        if (null == root || null == value) {
            return;
        }
        root = searchTree(root, value);
        if (null == root) {
            return;
        }

        if (null == root.getRChild() && null == root.getLChild()) {
            if (null == root.getPChild()) {
//                1.只有一个根节点
                root = null;
            } else if (root == root.getPChild().getLChild()) {
//                2. 删除左叶子节点
                root.getPChild().setLChild((BSTTree) null);
            } else {
//                3. 删除右叶子节点
                root.getPChild().setRChild((BSTTree) null);
            }
        } else if ((null == root.getLChild()) ^ (null == root.getRChild())) {
            if (null == root.getPChild()) {
//                1. 根节点下只有一个子树
                root = (null == root.getLChild()) ? root.getRChild() : root.getLChild();
            } else if (root == root.getPChild().getRChild()) {
//                2. root 是父节点的右子树,将root的唯一子树放到父节点的右子树上
                root.getPChild().setRChild(null == root.getLChild() ? root.getRChild() : root.getLChild());
                root.getPChild().getRChild().setPChild(root.getPChild());
            } else {
//                3. root 是父节点的左子树,将root的唯一子树放到父节点的左子树上
                root.getPChild().setLChild(null == root.getRChild() ? root.getLChild() : root.getRChild());
                root.getPChild().getLChild().setPChild(root.getPChild());
            }
        } else {
//            1. 如果删除的节点有两个子节点,则寻找root节点的前驱节点的值替换root节点的值,然后删除前驱节点
            BSTTree newRoot = searchPrecursor(root.getLChild());
            Integer newRootValue = newRoot.getValue();
            deleteTree(root, newRootValue);
            root.setValue(newRootValue);
        }
    }

    private static BSTTree searchPrecursor(BSTTree root) {
        if (null == root.getRChild()) {
            return root;
        } else {
            return searchPrecursor(root.getRChild());
        }
    }

    public Integer getValue() {
        return value;
    }

    public void setValue(Integer value) {
        this.value = value;
    }

    public BSTTree getLChild() {
        return LChild;
    }

    public void setLChild(BSTTree LChild) {
        this.LChild = LChild;
    }

    public void setLChild(Integer value) {
        this.LChild = new BSTTree(value);
    }

    public BSTTree getRChild() {
        return RChild;
    }

    public void setRChild(BSTTree RChild) {
        this.RChild = RChild;
    }

    public void setRChild(Integer value) {
        this.RChild = new BSTTree(value);
    }

    public BSTTree getPChild() {
        return PChild;
    }

    public void setPChild(BSTTree PChild) {
        this.PChild = PChild;
    }

    @Override
    public String toString() {
        return "BSTTree{" +
                "value=" + value +
                '}';
    }

    public static void putTree(BSTTree root, int[] angles) {
        if (null != root) {
            for (int angle : angles) {
                addTree(root, angle);
            }
        }
    }
}

    Class : 

package lime.tree;


/**
 * @Author: Lime
 * @Description:
 * @Date:Create in 19:25 2017/10/28
 */
public class BSTTreeTt {
    public static void main(String[] args){
        BSTTree root = new BSTTree(13);
        int[] angles = {8,17,1,11,15,25,6,22,27,26};
        BSTTree.putTree(root,angles);
        showTree(root);
//        for(int i = 0;i < 10;i++){
//            BSTTree.addTree(root,(int)(Math.random() * 100));
//        }
//        showTree(root);
//        int elf = (int)(Math.random() * 100);
//        System.out.println("search " + elf + " : " + BSTTree.searchTree(root,elf));
        BSTTree.deleteTree(root,11);
        showTree(root);
//        BSTTree.addTree(root,11);
        BSTTree.deleteTree(root,1);
        showTree(root);
//        BSTTree.addTree(root,1);
        BSTTree.deleteTree(root,27);
        showTree(root);
//        BSTTree.addTree(root,27);
        BSTTree.deleteTree(root,17);
        showTree(root);
//        BSTTree.addTree(root,17);
        BSTTree.deleteTree(root,13);
        showTree(root);

    }
    public static void showTree(BSTTree root){
        BSTTree.showTree(root);
        System.out.println();
    }
}

啦啦啦

原文地址:https://www.cnblogs.com/ClassNotFoundException/p/7781032.html