数据结构:二叉搜索树

二叉搜索树我先了解 了解了一天,就是看原理,看网课 ,然后再写的代码,感觉挺难的,但是打完了感觉也不是这么难,就是删除情况有点多,有四种。用JAVA写了一个。

下面的代码只有插入,查找,删除,前序后序中序遍历这几种操作。

package com.company;

public class Main {
    public node root;
    public node find(int key){    // 查找
        if(root == null){
            System.out.println("现在的二叉树是一颗空树,找不到任何数据");
            return null;
        }
        node current = root;
        while(current.key != key){
            if(current.key < key)
                current=current.leftchild;
            else current = current.rightchild;
            if(current == null)
                return null;
        }
        return current;
    }
    public boolean insert(node data){  //插入节点
        if(root == null){
            root = data;
            return true;
        }
        if(this.find(data.key) != null){
            System.out.println("The number has already exists");
            return false;
        }
        node current = root;
        while(current != null){
            while(current != null){
                if(data.key > current.key){
                    if(current.rightchild == null){
                        current.rightchild = data;
                        return true;
                    }
                    current = current.rightchild;
                }
                else{
                    if(current.leftchild == null){
                        current.leftchild = data;
                        return true;
                    }
                    current = current.leftchild;
                }
            }
        }
        return false;
    }
    public void inorder_iterator(node data){   // 中序遍历
        if(data.leftchild != null)
            this.inorder_iterator(data.leftchild);
        System.out.print(data.key+ " ");
        if(data.rightchild != null)
            this.inorder_iterator(data.rightchild);
    }
    public void preorder_iterator(node data) {  // 前序遍历
        System.out.print(data.key + " ");
        if (data.leftchild != null)
            this.preorder_iterator(data.leftchild);
        if (data.rightchild != null)
            this.preorder_iterator(data.rightchild);
    }
    public void postorder_iterator(node data){ // 后序遍历
        if(data.leftchild != null)
            this.postorder_iterator(data.leftchild);
        if(data.rightchild != null)
            this.postorder_iterator(data.rightchild);
        System.out.print(data.key + " ");
    }
    public node getMinNode(node data){   //获得最小值
        if(data.leftchild == null){
            return data;
        }
        node current = data.leftchild;
        while(current.leftchild != null){
            current = current.leftchild;
        }
        return current;
    }
    public node getMaxNode(node data){  // 获得最大值
        if(data.rightchild == null)
            return data;
        node current = data.rightchild;
        while(current.rightchild != null){
            current = current.rightchild;
        }
        return current;
    }
    public boolean delete(int key){
        if(root == null){
            System.out.println("这棵树是一颗空树");
            return false;
        }
        node targetParent = root;
        node target = root;
        boolean isLeftChild = true;
        while(target.key != key){
            if(key < target.key){
                targetParent=target;
                target = target.leftchild;
                isLeftChild = true;
            }
            else{
                targetParent =target;
                target = target.rightchild;
                isLeftChild = false;
            }
            if(target == null)
                break;
        }
        if(target == null){
            System.out.println("未发现该值!");
            return false;
        }
        //第一种情况 被删除的节点是叶节点
        if(target.leftchild == null && target.rightchild == null){
            if(root.key == target.key) {
                root = null;
                return true;
            }
            if(isLeftChild)
                targetParent.leftchild = null;
            else
                targetParent.rightchild = null;
        }
        //第二种情况 被删除的节点只有一个子节点 且这个节点是右节点
        else if(target.leftchild == null && target.rightchild != null){
            if(target.key == root.key) {
                root = root.rightchild;
                return false;
            }
            if(isLeftChild)
                targetParent.leftchild = target.rightchild;
            else
                targetParent.rightchild = target.rightchild;
        }
        //第三种情况 被删除的节点只有一个子节点 且这个节点是 左节点
        else if(target.leftchild != null && target.rightchild == null){
            if(target.key == root.key){
                root = root.leftchild;
                return true;
            }
            if(isLeftChild)
                targetParent.leftchild = target.leftchild;
            else
                targetParent.rightchild = target.leftchild;
        }
        //第四种情况 被删除的节点有两个子节点
        else {
            node nextNode = this.getNextNode(target);
            if(target.key == root.key)
                root=nextNode;
            else if(isLeftChild)
                targetParent.leftchild=nextNode;
            else
                targetParent.rightchild=nextNode;
            nextNode.leftchild=target.leftchild;
            nextNode.rightchild=target.rightchild;
        }
        return true;
    }
    public node getNextNode(node data){   // 找后继节点,即第一个比要删除的节点大的点
        node parent = data;
        node current = data.rightchild;
        while(current.leftchild != null){
            parent = current;
            current = current.leftchild;
        }
        if(data.rightchild.key == current.key)
            parent.rightchild = current.rightchild;
        else
            parent.leftchild = current.rightchild;
        return current;
    }
    public static void main(String[] args) {
        Main bst = new Main();
        node t1 = new node(10);
        node t2 = new node(11);
        node t3 = new node(8);
        node t4 = new node(9);
        node t5 = new node(12);
        bst.insert(t1);
        bst.insert(t2);
        bst.insert(t3);
        bst.insert(t4);
        bst.insert(t5);
        bst.inorder_iterator(bst.root);
        bst.delete(10);
        System.out.println();
        bst.inorder_iterator(bst.root);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40941611/article/details/82818976
今日推荐