算法-二叉树

 

 

private  class Node{
        private Key key;
        private Value value;
        public Node left;
        public Node right;

        public Node(Key key, Value value, Node left, Node right) {
            this.key = key;
            this.value = value;
            this.left = left;
            this.right = right;
        }
    }

实现实现:

如果没有任何一个结点,则直接把新节点当成根节点

如果当前根节点不为空,则从根节点开始

package test.tree;

import org.apache.poi.ss.formula.functions.T;

public class BinaryTree <Key extends   Comparable<Key>,Value >{

    //根节点
    private Node root;
    //树的结点个数
    private int N;


    public BinaryTree(){
        root = null;
        this.N = 0;
    }

    private  class Node{
        private Key key;
        private Value value;
        public Node left;
        public Node right;

        public Node(Key key, Value value, Node left, Node right) {
            this.key = key;
            this.value = value;
            this.left = left;
            this.right = right;
        }
    }

   public void put(Key key,Value value){

       put(root, key, value);

   }
   //向指定的树添加key-value,并返回添加后的新树
    public Node put(Node n ,Key key,Value value){
        //如果x子树为null
        if(N==0){
            N++;
           root =  new Node(key,value,null,null);
           return root;
        }
        if(n == null){
            //null就把新节点当成根节点
            N++;
            return new Node(key,value,null,null);

        }
       //如果不为空
       //和当前结点比较
       int cmp =  key.compareTo(n.key);
        if(cmp>0){
            n.right = put(n.right,key,value);
        }else if(cmp<0){
            n.left = put(n.left,key,value);
        }else{
            n.value = value;
        }
        return n;
    }


    public Value get(Key key){

       return  get(root, key);


    }

    public Value get(Node n,Key key){
        if(n==null){
            return null;
        }
        int cmp = key.compareTo(n.key);
        if(cmp>0){
           return get(n.right,key);
        }else if(cmp<0){
            return get(n.left,key);
        }else{
            return n.value;
        }


    }


    public void delete(Key key){
        delete(root,key);
    }
   //删除指定x树的key结点,并返回删除后的新树
    public Node delete(Node n,Key key){
        if(n==null){
            return null;
        }
        int cmp = key.compareTo(n.key);

        if(cmp>0){
           return  delete(n.right,key);
        }else if(cmp<0){
            return delete(n.left,key);
        }else{
            N--;
            if(n.left==null){
                return n.right;
            }
            if(n.right==null){
                return n.left;
            }
            //先找到右结点中最小的结点
            Node minNode =  n.right;
            while(minNode.left!=null){
                minNode =  minNode.left;
            }
            //删除结点,找到最小结点的上一个结点
            Node x =  n.right;
            while(x.left!=null){
                if(x.left.left==null){
                    x.left = null;
                }else{
                    x= x.left;
                }
            }
            minNode.left = n.left;
            minNode.right = n.right;
            n = minNode;


            return n;
        }

    }

    public int size(){
       return N;
    }



}

测试代码

package test.tree;

public class BinaryTreeTest {

    public static void main(String[] args) {
        BinaryTree<Integer,String> tree1 = new BinaryTree<>();
        tree1.put(1,"11");
        tree1.put(2,"22");
        tree1.put(3,"33");
        tree1.put(4,"44");
        tree1.put(5,"55");
        tree1.put(6,"66");
        tree1.put(7,"77");

        System.out.println("插入完毕后元素的个数:"+tree1.size());
        System.out.println("插入完毕后键二对应的元素:"+tree1.get(2));

        tree1.delete(4);
        System.out.println("删除完毕后元素的个数:"+tree1.size());

        System.out.println("删除完毕后键二对应的元素:"+tree1.get(2));

    }

}

执行结果

插入完毕后元素的个数:7
插入完毕后键二对应的元素:22
删除完毕后元素的个数:6
删除完毕后键二对应的元素:22

查找最小键和最大键

 public Key min(){
        return min(root).key;
    }

    private Node min(Node n){
        if(n.left!=null){
            return min(n.left);
        }else{
            return n;
        }

    }

    public Key max(){
        return max(root).key;
    }

    private Node max(Node n){
        if(n.right!=null){
            return max(n.right);
        }else{
            return n;
        }

    }


    public Quene<Key> preErgodic(){
        Quene<Key> key = new Quene<>();
        preErgodic(root,key);
        return key;

    }
    //前序遍历
    public void preErgodic(Node x,Quene<Key> key){
        if(x==null){
            return ;
        }

        //把当前结点的key放入队列
        key.enquence(x.key);
        preErgodic(x.left,key);
        preErgodic(x.right,key);

    }

  //中序遍历
    public Quene<Key> midErgodic(){
        Quene<Key> key = new Quene<>();
        midErgodic(root,key);
        return key;

    }

    public void midErgodic(Node x,Quene<Key> key){
        if(x==null){
            return ;
        }

        //把当前结点的key放入队列

        midErgodic(x.left,key);
        key.enquence(x.key);
        midErgodic(x.right,key);

    }
//后序遍历
    public Quene<Key> afterErgodic(){
        Quene<Key> key = new Quene<>();
        afterErgodic(root,key);
        return key;

    }

    public void afterErgodic(Node x,Quene<Key> key){
        if(x==null){
            return ;
        }

        //把当前结点的key放入队列

        afterErgodic(x.left,key);
        afterErgodic(x.right,key);
        key.enquence(x.key);
    }
最大深度问题:


public int maxDepth(){
    return maxDepth(root);
}

public int maxDepth(Node x){
    if(x==null){
        return 0;
    }
    int max = 0;
    int maxL = 0;
    int maxR = 0;
    if(x.left!=null){
        maxL = maxDepth(x.left);
    }
    if(x.right!=null){
        maxR = maxDepth(x.right);
    }
    max=maxL>maxR?maxL+1:maxR+1;
    return max;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值