二叉搜索树

Posted 淡季的风

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二叉搜索树相关的知识,希望对你有一定的参考价值。

Java

Node.class

 1 class Node {
 2     int iData;
 3     double fData;
 4     Node leftChild;
 5     Node rightChild;
 6     
 7     public void displayNode(){
 8         System.out.println(this.toString());
 9     }
10     
11     @Override
12     public String toString() {
13         return "<Node>:[iData="+this.iData+";fData="+this.fData+"]";
14     }
15     
16     public Node(int iData, double fData) {
17         this.iData = iData;
18         this.fData = fData;
19     }
20 }

Tree.class

public class Tree {
    
    private Node root;
    
    public Tree(){
        root =null;
    }
    
    public Node find(int key){
        Node current = root;
        if(root==null){
            return root;
        }
        while(current.iData!=key){
            if(current.iData>key){
                current = current.leftChild;
            }else{
                current = current.rightChild;
            }
            if(current==null){
                return null;
            }
        }
        return current;
    }
    
    public void insert(int iData, double fData){
        Node newNode = new Node(iData, fData);
        
        if(root==null){
            root = newNode;
        }else{
            Node current = root;
            while(true){
                Node parent = current;
                if(iData<current.iData){
                    current = current.leftChild;
                    if(current==null){
                        parent.leftChild = newNode;
                        return;
                    }
                }else{
                    current = current.rightChild;
                    if(current==null){
                        parent.rightChild = newNode;
                        return;
                    }
                }
            }
        }
    }
    
    public boolean delete(int key){
        Node current = root;
        Node parent = root;
        boolean isLeftChild = true;
        while(current.iData!=key){
            parent =current;
            if(current.iData>key){
                current = current.leftChild;
                isLeftChild = true;
            }else{
                current = current.rightChild;
                isLeftChild = false;
            }
            if(current==null){
                return false;
            }
        }
        if(current.leftChild==null && current.rightChild==null){
            if(current==root){
                root = null;
            }else if(isLeftChild){
                parent.leftChild = null;
            }else{
                parent.rightChild = null;
            }
        }else if(current.leftChild==null){
            if(current==root){
                root = root.rightChild;
            }else if(isLeftChild){
                parent.leftChild = current.rightChild;
            }else{
                parent.rightChild = current.rightChild;
            }
        }else if(current.rightChild==null){
            if(current==root){
                root = root.leftChild;
            }else if(isLeftChild){
                parent.leftChild = current.leftChild;
            }else{
                parent.rightChild = current.leftChild;
            }
            
        }else{
            Node suceesstor = getSuceesstor(current);
            if(current==root){
                root = suceesstor;
            }else if(isLeftChild){
                parent.leftChild = suceesstor;
            }else{
                parent.rightChild = suceesstor;
            }
            suceesstor.leftChild = current.leftChild;
        }
        return true;    
    }
    
    private Node getSuceesstor(Node delNode){
        Node successerParent = delNode;
        Node successer = delNode;
        Node current = delNode.rightChild;
        while(current!=null){
            successerParent = successer;
            successer = current;
            current = current.leftChild;
        }
        if(successer!=delNode.rightChild){
            successerParent.leftChild = successer.rightChild;
            successer.rightChild = delNode.rightChild;
        }
        return successer;
    }
    
    /**
     * 前序遍历
     * @param localRoot
     */
    public void preOrder(Node localRoot){
        if(localRoot!=null){
            System.out.println(localRoot.toString());
            preOrder(localRoot.leftChild);
            preOrder(localRoot.rightChild);
        }
    }
    
    public void inOrder(Node localRoot){
        if(localRoot!=null){
            inOrder(localRoot.leftChild);
            System.out.println(localRoot.toString());
            inOrder(localRoot.rightChild);
        }
    }
    
    public void outOrder(Node localRoot){
        if(localRoot!=null){
            outOrder(localRoot.leftChild);
            outOrder(localRoot.rightChild);
            System.out.println(localRoot.toString());
        }
    }
    
    public Node getRoot() {
        return root;
    }

    public void setRoot(Node root) {
        this.root = root;
    }
}

 

以上是关于二叉搜索树的主要内容,如果未能解决你的问题,请参考以下文章

代码题(10)— 二叉搜索树

代码随想录Day20-Leetcode654.最大二叉树,617.合并二叉树,700.二叉搜索树中的搜索,98.验证二叉搜索树

二叉搜索树(KV模型,二叉搜索树删除节点)

代码随想录算法训练营第14天|530.二叉搜索树的最小绝对差501.二叉搜索树中的众数236.二叉树的最近公共祖先

二叉树之二叉搜索树(BSTree)

c++:二叉搜索树BinarySortTree