二叉树的简单实现

  1. 定义:二叉树在图论中是这样定义的:二叉树是一个连通的无环图,并且每一个顶点的度不大于3。有根二叉树还要满足根结点的度不大于2。有了根结点之后,每个顶点定义了唯一的父结点,和最多2个子结点。然而,没有足够的信息来区分左结点和右结点。如果不考虑连通性,允许图中有多个连通分量,这样的结构叫做森林。
  2. 二叉树性质
    (1) 在非空二叉树中,第i层的结点总数不超过 , i>=1;
    (2) 深度为h的二叉树最多有 个结点(h>=1),最少有h个结点;
    (3) 对于任意一棵二叉树,如果其叶结点数为N0,而度数为2的结点总数为N2,则N0=N2+1;
    (4) 具有n个结点的完全二叉树的深度为
    (5)有N个结点的完全二叉树各结点如果用顺序方式存储,则结点之间有如下关系:
    若I为结点编号则 如果I>1,则其父结点的编号为I/2;
    如果2*I<=N,则其左儿子(即左子树的根结点)的编号为2*I;若2*I>N,则无左儿子;
    如果2*I+1<=N,则其右儿子的结点编号为2*I+1;若2*I+1>N,则无右儿子。
package BinaryTreeTest;
 class Node{

    int value;
    Node leftchild;
    Node rightchild;
}

 class Tree{
      Node root;

     //查找
     public Node find(int value){
         Node current=root;
         while(current.value!=value){
             if(value<current.value){
                 current=current.leftchild;
             }
             else
                 current=current.rightchild ;
             if(current==null)
                 return null;
         }
         return current;
     }

     //插入
    public void insert(int value){
         Node newNode=new Node();
         newNode.value =value;

         if(root==null)
             root=newNode;
         else{
             Node current=root;
             Node parent;
             while(true){
                 parent=current;
                 if(value<current.value){
                     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 value){
        Node current=root;
        Node parent=root;
        boolean isleftchild=true;

        //找出要被删除的节点
        while(current.value!=value){
            parent=current;
            if(value<current.value){
                isleftchild=true;
                current=current.leftchild;
            }
            else{
                isleftchild=false;
                current=current.rightchild;
            }
            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.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.leftchild;
            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;

     }

    //获取删除时所需要用到被删除节点的中序后继节点
    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(Successor!=delnode.rightchild){
            Successorparent.leftchild=Successor.rightchild;
            Successor.rightchild=delnode.rightchild;
        }


        return Successor;

    }

    //遍历:中序遍历
    public void inOrder(Node root){
        if(root!=null){
            inOrder(root.leftchild);
            System.out.println(root.value );
            inOrder(root.rightchild);
        }
    }

    //遍历:前序遍历
    public void preOrder(Node root){
        if(root!=null){
            System.out.println(root.value );
            inOrder(root.leftchild);
            inOrder(root.rightchild);
        }
    }

    //遍历:后序遍历
    public void posOrder(Node root){
        if(root!=null){

            inOrder(root.leftchild);
            inOrder(root.rightchild);
            System.out.println(root.value );
        }
    }

    //获取最大节点
    public Node maxNode(){
        Node current,Last = null;
        current=root;
        while(current!=null){
            Last=current;
            current=current.rightchild ;

        }
        return Last;
    }

  //获取最小节点
    public Node minNode(){
        Node current,Last = null;
        current=root;
        while(current!=null){
            Last=current;
            current=current.leftchild  ;

        }
        return Last;
    }


}
public class BinaryTree {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Tree tree=new Tree();

        tree.insert(10);
        tree.insert(22);
        tree.insert(13);
        tree.insert(44);
        tree.insert(27);
        tree.insert(5);
        tree.insert(48);

        tree.inOrder(tree.root);
        System.out.println("---------------------------");
        tree.delete(13);
        tree.inOrder(tree.root);
        //System.out.println(tree.maxNode().value);
        //System.out.println(tree.minNode().value);

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值