第十八题、二叉排序树的插入删除和查找(难度系数100)

二叉排序树的插入删除和查找
pre: 前序遍历
in: 中序遍历
post:后序遍历
insert: 插入,本题中不会出现相同的元素
delete: 删除,删除成功输出TRUE,没有该元素则输出FALSE,删除的方法是如果有左子树,以左子树中最大值作为新的树根,否则,以右子树最小值作为树根。
search: 查找,存在该元素输出YES, 否则输出NO
exit:退出
输入:
输入的第一行为整数 N
接下来一行为N个整数,你需要把这N个整数构建出相应的二叉排序树
之后是一组指令。

输出:
根据相应的指令进行输出。说明:遍历各元素之间用一个空格隔开。
样例输入:

10
31 15 9 5 4 7 8 50 42 2
pre
in
post
delete
15
delete
88
in
search
8
search
222
insert
15
pre
in
post
exit

样例输出:

31 15 9 5 4 2 7 8 50 42
2 4 5 7 8 9 15 31 42 50
2 4 8 7 5 9 15 42 50 31
TRUE
FALSE
2 4 5 7 8 9 31 42 50
YES
NO
31 9 5 4 2 7 8 15 50 42
2 4 5 7 8 9 15 31 42 50
2 4 8 7 5 15 9 42 50 31

注意:在样例中,我先把15删除掉了,然后再插入15,观察最开始的三个遍历,和最后的三次遍历,遍历结果是有区别的。

参考:注意本题代码不能有中文,否则过不了=


import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        BTree bt = new BTree();
        for (int i = 0; i < n; i++) {
            bt.insert(sc.nextInt());
        }
        String op = sc.next();
        while (!op.equals("exit")) {
            if (op.equals("pre")) {
                bt.pre_order();
            } else if (op.equals("in")) {
                bt.in_order();
            } else if (op.equals("post")) {
                bt.post_order();
            } else if (op.equals("delete")) {
                if (bt.delete(sc.nextInt())) {
                    System.out.println("TRUE");
                } else {
                    System.out.println("FALSE");
                }
            } else if (op.equals("search")) {
                if (bt.search(sc.nextInt())) {
                    System.out.println("YES");
                } else {
                    System.out.println("NO");
                }
            } else if (op.equals("insert")) {
                bt.insert(sc.nextInt());
            }
            op = sc.next();
        }
    }
}
class BTree{
    static private class Node{
        int value;
        Node lchild;
        Node rchild;

        public Node() {
        }

        public Node(int value) {
            this.value = value;
        }
    }
    Node root = null;
    // 插入元素
    public void insert(int x) {
        root = insert(root, x);
    }
    private Node insert(Node root, int value) {
        if (root == null) {
            root = new Node(value);
        } else if (value > root.value) {
            root.rchild = insert(root.rchild, value);
        } else if (value < root.value) {
            root.lchild = insert(root.lchild, value);
        }
        return root;
    }
    // 用来判断是否删除成功
    boolean has_delete = false;
    // 删除元素
    public boolean delete(int x) {
        has_delete = false;
        root = delete(root, x);
        return has_delete;
    }
    private Node delete(Node root, int value) {
        if (root != null) {
            // 如果找到之后 就删除
            if (root.value == value) {
                has_delete = true;
                // 如果没有子树了,就将该节点删除
                if (root.lchild == null && root.rchild == null) {
                    root = null;
                } else if (root.lchild != null) {
                    // 如果有左孩子,找到最大值并替换
                    Node now = root.lchild;
                    Node last = root.lchild;
                    while (now.rchild != null) {
                        last = now;
                        now = now.rchild;
                    }
                    if (now != last) {
                        last.rchild = now.lchild;
                        root.value = now.value;
                    } else {
                        root.value = now.value;;
                        root.lchild = now.lchild;
                    }
                } else {
                    // 如果有右孩子,找到最小值并替换
                    Node now = root.rchild;
                    Node last = root.rchild;
                    while (now.lchild != null) {
                        last = now;
                        now = now.lchild;
                    }
                    if (last != now) {
                        last.lchild = now.rchild;
                        root.value = now.value;
                    } else {
                        root.value = now.value;
                        root.rchild = now.rchild;
                    }
                }
            } else if (value < root.value) {
                root.lchild = delete(root.lchild, value);
            } else {
                root.rchild = delete(root.rchild, value);
            }
        }
        return root;
    }

    // 查找元素
    public boolean search(int value) {
        Node current = root;
        while (current != null) {
            if (value < current.value) {
                current = current.lchild;
            } else if (value > current.value) {
                current = current.rchild;
            } else {
                break;
            }
        }
        return current != null;
    }

    // 前序遍历
    public void pre_order() {
        pre_order(root);
        System.out.println();
    }
    private void pre_order(Node root) {
        if (root != null) {
            System.out.print(root.value + " ");
            pre_order(root.lchild);
            pre_order(root.rchild);
        }
    }
    // 中序遍历
    public void in_order() {
        in_order(root);
        System.out.println();
    }
    private void in_order(Node root) {
        if (root != null) {
            in_order(root.lchild);
            System.out.print(root.value + " ");
            in_order(root.rchild);
        }
    }
    // 后序遍历
    public void post_order() {
        post_order(root);
        System.out.println();
    }
    private void post_order(Node root) {
        if (root != null) {
            post_order(root.lchild);
            post_order(root.rchild);
            System.out.print(root.value + " ");
        }
    }
}

和上面的代码是一样的,只是多加了点注释(提交的时候记得删除注释)


import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        BTree bt = new BTree();
        for (int i = 0; i < n; i++) {
            bt.insert(sc.nextInt());
        }
        String op = sc.next();
        while (!op.equals("exit")) {
            if (op.equals("pre")) {
                bt.pre_order();
            } else if (op.equals("in")) {
                bt.in_order();
            } else if (op.equals("post")) {
                bt.post_order();
            } else if (op.equals("delete")) {
                if (bt.delete(sc.nextInt())) {
                    System.out.println("TRUE");
                } else {
                    System.out.println("FALSE");
                }
            } else if (op.equals("search")) {
                if (bt.search(sc.nextInt())) {
                    System.out.println("YES");
                } else {
                    System.out.println("NO");
                }
            } else if (op.equals("insert")) {
                bt.insert(sc.nextInt());
            }
            op = sc.next();
        }
    }
}
class BTree{
    static private class Node{
        int value;
        Node lchild;
        Node rchild;

        public Node() {
        }

        public Node(int value) {
            this.value = value;
        }
    }
    Node root = null;
    // 插入元素
    public void insert(int x) {
        root = insert(root, x);
    }
    private Node insert(Node root, int value) {
        if (root == null) {
            root = new Node(value);
        } else if (value > root.value) {
            root.rchild = insert(root.rchild, value);
        } else if (value < root.value) {
            root.lchild = insert(root.lchild, value);
        }
        return root;
    }
    // 用来判断是否删除成功
    boolean has_delete = false;
    // 删除元素
    public boolean delete(int x) {
        has_delete = false;
        root = delete(root, x);
        return has_delete;
    }
    private Node delete(Node root, int value) {
        if (root != null) {
            // 如果找到之后 就删除
            if (root.value == value) {
                has_delete = true;
                // 如果没有子树了,就将该节点删除
                if (root.lchild == null && root.rchild == null) {
                    root = null;
                } else if (root.lchild != null) {
                    // 如果有左孩子,找到最大值并替换
                    // now 记录当前节点
                    Node now = root.lchild;
                    // last 记录当前节点的父节点
                    Node last = root.lchild;
                    // 通过 while 循环不断寻找最大值,即右孩子
                    while (now.rchild != null) {
                        last = now;
                        now = now.rchild;
                    }
                    // while循环后 当前节点一点没有右孩子了,否则无法退出 while 循环
                    root.value = now.value;
                    // 如果 root 的左孩子 不是 左子树的最大值,即左孩子还有右孩子
                    if (now != last) {
                        last.rchild = now.lchild;
                    } else {
                        // 如果 root 的左孩子 就是 左子树的最大值,则左孩子没有右孩子
                        root.lchild = now.lchild;
                    }
                } else {
                    // 如果有右孩子,找到最小值并替换
                    Node now = root.rchild;
                    Node last = root.rchild;
                    while (now.lchild != null) {
                        last = now;
                        now = now.lchild;
                    }
                    root.value = now.value;
                    if (last != now) {
                        last.lchild = now.rchild;
                    } else {
                        root.rchild = now.rchild;
                    }
                }
            } else if (value < root.value) {
                root.lchild = delete(root.lchild, value);
            } else {
                root.rchild = delete(root.rchild, value);
            }
        }
        return root;
    }

    // 查找元素
    public boolean search(int value) {
        Node current = root;
        while (current != null) {
            if (value < current.value) {
                current = current.lchild;
            } else if (value > current.value) {
                current = current.rchild;
            } else {
                break;
            }
        }
        return current != null;
    }

    // 前序遍历
    public void pre_order() {
        pre_order(root);
        System.out.println();
    }
    private void pre_order(Node root) {
        if (root != null) {
            System.out.print(root.value + " ");
            pre_order(root.lchild);
            pre_order(root.rchild);
        }
    }
    // 中序遍历
    public void in_order() {
        in_order(root);
        System.out.println();
    }
    private void in_order(Node root) {
        if (root != null) {
            in_order(root.lchild);
            System.out.print(root.value + " ");
            in_order(root.rchild);
        }
    }
    // 后序遍历
    public void post_order() {
        post_order(root);
        System.out.println();
    }
    private void post_order(Node root) {
        if (root != null) {
            post_order(root.lchild);
            post_order(root.rchild);
            System.out.print(root.value + " ");
        }
    }
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qing影

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值