码迷,mamicode.com
首页 > 其他好文 > 详细

二叉搜索树

时间:2017-07-02 16:18:47      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:搜索   insert   print   遍历   rri   isp   root   let   else   

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;
    }
}

 

二叉搜索树

标签:搜索   insert   print   遍历   rri   isp   root   let   else   

原文地址:http://www.cnblogs.com/lowseasonwind/p/7106023.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!