首页 > 代码库 > 二叉搜索树

二叉搜索树

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="http://www.mamicode.com/+this.iData+";fData="http://www.mamicode.com/+this.fData+"]";
14     }
15     
16     public Node(int iData, double fData) {
17         this.iData =http://www.mamicode.com/ iData;
18         this.fData =http://www.mamicode.com/ 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;
    }
}

 

二叉搜索树