二叉树的实现:
比较有意思的是二叉树的删除


public class Node {
    int iData;
    double fData;

    Node leftChild;
    Node rightChild;

    public void displayNode(){

    }
}
public class Tree {
    private Node root;

    public Node find(int key){
        Node current=root;
        while (current!=null){
            if (current.iData==key){
                break;
            }
            if (current.iData<key){
                current=current.rightChild;
            }else {
                current=current.leftChild;
            }
        }
        return current;
    }
    public void insert(int key,double value){
        Node newNode=new Node();
        newNode.iData=key;
        newNode.fData=value;

        Node current=root;
        if (root==null){
            root=newNode;
            return;
        }
        while (current!=null){
            Node parent=current;
            if (key<current.iData){
                current=current.leftChild;
                if (current==null){
                    parent.leftChild=newNode;
                    return;
                }
            }else {
                current=current.rightChild;
                if (current==null){
                    parent.rightChild=newNode;
                    return;
                }
            }


        }
    }
    /**
     *中序遍历法
     */
    public void inDisplay(Node node){
        if (node!=null){
            inDisplay(node.leftChild);
            System.out.println(node.iData+":"+node.fData);
            inDisplay(node.rightChild);
        }
    }
    public void delete(int id){
        if (this.root!=null){
            Node node=root;
            Node fNode=null;
            while (node.iData!=id){
                fNode=node;
                if (node.iData>id){
                    node=node.leftChild;
                }else {
                    node=node.rightChild;
                }
                if (node==null){
                    return;
                }
            }
            if (fNode==null){
                //需要删除的节点是根节点
                //看有几个子节点。
                if (node.leftChild==null && node.rightChild==null) {
                    //0个,则删除成功后,树是空的
                    this.root=null;
                    return;
                }
                if (node.rightChild==null) {
                    //1个
                    this.root.leftChild=node.leftChild;
                }
                if (node.leftChild==null){
                    //1个
                    this.root.rightChild=node.rightChild;
                }
                if (node.leftChild!=null && node.rightChild!=null){
                    //2个,则将右侧树的最小叶子节点晋升为根节点
                    Node current=node.rightChild;
                    Node fn=node;
                    while (current.leftChild!=null) {
                        fn=current;
                        current = current.leftChild;
                    }
                    if (fn==node){
                        //最小子节点的父节点是即将被删除的节点
                        this.root=current;
                        current.leftChild=node.leftChild;
                    }else {
                        if (current.rightChild != null) {
                            fn.leftChild = current.rightChild;
                        }//上面先把目标树的右侧树的最小节点从原始树中独立出来,
                        else {
                            fn.leftChild = null;//把最小节点冲原始树中独立出来
                        }
                        this.root = current;
                        current.leftChild = node.leftChild;
                        current.rightChild = node.rightChild;
                    }
                }
            }else {//需要删除的不是根节点
                boolean left=false;
                if (fNode.leftChild==node){
                    left=true;//待删除节点是父节点的左节点
                }
                if (node.leftChild==null &&node.rightChild==null){
                    if (left){
                        fNode.leftChild=null;
                    }else {
                        fNode.rightChild=null;
                    }
                    return;
                }
                if (node.rightChild==null){
                    if (left){
                        fNode.leftChild=node.leftChild;
                    }else {
                        fNode.rightChild=node.leftChild;
                    }
                }
                if (node.leftChild==null){
                    if (left){
                        fNode.leftChild=node.rightChild;
                    }else {
                        fNode.rightChild=node.rightChild;
                    }
                }
                if (node.leftChild!=null && node.rightChild!=null){
                    Node current=node.rightChild;
                    Node fn=node;
                    while (current.leftChild!=null) {
                        fn=current;
                        current = current.leftChild;
                    }
                    if (fn==node){
                        //最小子节点的父节点是即将被删除的节点.把current嫁接到fNode的下面
                        if (left){
                            fNode.leftChild=current;
                            current.leftChild=node.leftChild;
                        }else {
                            fNode.rightChild=current;
                            current.leftChild=node.leftChild;
                        }
                    }else {
                        fn.leftChild=current.rightChild;//剥离current节点
                        //current替换node
                        if (left){
                            fNode.leftChild=current;
                        }else {
                            fNode.rightChild=current;
                        }
                        current.leftChild=node.leftChild;
                        current.rightChild=node.rightChild;
                    }
                }
            }


        }
    }

    /**
     * 获取树最大的节点
     * @return
     */
    public Node max(){
        Node node=this.root;
        if (node!=null){
            while (node.rightChild!=null)
                node=node.rightChild;
        }
        return node;
    }

    /**
     * 获取树最小的节点
     * @return
     */
    public Node min(Node rn){
        Node node=rn;
        if (node!=null){
            while (node.leftChild!=null)
                node=node.leftChild;
        }
        return node;
    }

    public static void main(String[] args) {
        Tree theTree=new Tree();

        theTree.insert(50,1.3);
        theTree.insert(25,1.7);
        theTree.insert(75,1.9);
        theTree.insert(60,1.3);
        theTree.insert(15,1.7);
        theTree.insert(85,1.9);

        Node found=theTree.find(25);
        if (found!=null){
            System.out.println("找到 key = 25 的节点");
        }
        theTree.inDisplay(theTree.root);

        System.out.println(theTree.max().iData);
        System.out.println(theTree.min(theTree.root).iData);

        theTree.delete(50);
        theTree.inDisplay(theTree.root);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值