Java二叉树

package com.guge.test.tree;

import java.util.TreeMap;

/**
 * Created by guugangzhu on 2017/5/2.
 */
public class BinaryTree {
    private Node root;
    private TreeMap treeMap;
    public Node find(int key){
        Node current=root;
        while (current.iData!=key){
            if(keyiData)
                current=current.leftChild;
            else
                current=current.rightChild;
            if(current==null)
                return null;
        }
        return current;
    }

    public void insert(int key,double dd){

        Node newNode=new Node();
        newNode.iData=key;
        newNode.fData=dd;
        if(root==null){
            newNode.isBlack=true;
            root=newNode;
            return;
        }
        Node current=root;
        while (true){
            if(keyiData){
                if(current.leftChild!=null)
                    current=current.leftChild;
                else {
                    current.leftChild=newNode;
                    break;
                }
            }
            else{
                if(current.rightChild!=null)
                    current=current.rightChild;
                else {
                    current.rightChild=newNode;
                    break;
                }

            }


        }
    }
    public boolean delete(int key){
        Node current=root;
        Node parent=root;
        boolean isLeftChild=true;
        while (current.iData!=key){
            parent=current;
            if(keyiData){
                isLeftChild=true;
                current=current.leftChild;
            }else {
                isLeftChild=false;
                current=current.rightChild;
            }
            if(current==null)
                return false;

        }
        if(current.leftChild==null&¤t.rightChild==null){ //没有子树
            if(current==root)
                root=null;
            if(isLeftChild)
                parent.leftChild=null;
            else
                parent.rightChild=null;
        }else if(current.rightChild==null){  //有左子树
            if(current==root)
                root=current.leftChild;
            else if(isLeftChild){
                parent.leftChild=current.leftChild;
            }else {
                parent.rightChild=current.leftChild;
            }
        }else if(current.leftChild==null){ //有右子树
            if(current==root)
                root=current.rightChild;
            else if(isLeftChild){
                parent.leftChild=current.rightChild;
            }else {
                parent.rightChild=current.rightChild;
            }
        }else {  //双子树
            Node successor=getSuccessor(current);
            if(current==root)
                root=successor;
            else if(isLeftChild)
                parent.leftChild=successor;
            else
                parent.rightChild=successor;
            successor.leftChild=current.leftChild;

        }
        return true;
    }

    public boolean delete2(int key){
        Node current=root;
        Node parent=root;
        boolean isLeftChild=false;
        while (current.iData!=key){
            parent=current;
            if(keyiData){
                isLeftChild=true;
                current=current.leftChild;
            }else {
                isLeftChild=false;
                current=current.rightChild;
            }
            if(current==null)
                return false;
        }

        if(current.leftChild==null&¤t.rightChild==null){
            if(current==root)
                root=null;
            if(isLeftChild){
                parent.leftChild=null;
            }else {
                parent.rightChild=null;
            }
        }else if(current.rightChild==null){
            if(current==root)
                root=current.leftChild;
            if(isLeftChild){
                parent.leftChild=current.leftChild;
            }else {
                parent.rightChild=current.leftChild;
            }
        }else if(current.leftChild==null){
            if(current==root)
                root=current.rightChild;
            if(isLeftChild){
                parent.leftChild=current.rightChild;
            }else {
                parent.rightChild=current.rightChild;
            }
        }else {
            Node successor=getSuccessor(current);
            if(current==root)
                root=successor;

            if(isLeftChild)
                parent.leftChild=successor;
            else
                parent.rightChild=successor;
            successor.leftChild=current.leftChild;
        }
        return true;
    }

    private Node getSuccessor(Node delNode){
        Node successorParent=delNode;
        Node successor=delNode;
        Node current=delNode.rightChild;
        while (current!=null){
            successorParent=successor;
            successor=current;
            current=current.leftChild;
        }
        if(delNode.rightChild!=successor){
            successorParent.leftChild=successor.rightChild;
            successor.rightChild=delNode.rightChild;
        }
        return successor;
    }

    public static void main(String[] args) {
        BinaryTree binaryTree=new BinaryTree();
        binaryTree.insert(1,1.1);
        binaryTree.insert(2,1.2);
        binaryTree.insert(3,1.3);
        binaryTree.insert(4,1.4);
        binaryTree.insert(5,1.5);
        binaryTree.insert(6,1.6);

        Node node=binaryTree.find(3);
        if(node!=null)
            System.out.println(node.fData);
    }

}
class Node{
    double fData;
    int iData;
    boolean isBlack=false;
    Node leftChild;
    Node rightChild;
}

你可能感兴趣的:(数据结构与算法)