二叉树

二叉树:树的每个节点最多只能有两个子节点

  上图的第一幅图B节点有DEF三个子节点,就不是二叉树,称为多路树;而第二幅图每个节点最多只有两个节点,是二叉树,并且二叉树的子节点称为“左子节点”和“右子节点”。上图的D,E分别是B的左子节点和右子节点。

  如果我们给二叉树加一个额外的条件,就可以得到一种被称作二叉搜索树(binary search tree)的特殊二叉树。

  二叉搜索树要求:若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 它的左、右子树也分别为二叉排序树。

 

 

/**
     * hash
     * @param key
     * @return
     */
    private static <k> int getHashCode(k key){
        int h;
        //生成的hash值的随机性会增大
        return (h = key.hashCode()) ^ (h >>> 16);
    }

    Node root;
    class Node <k,v>{
        private k key;
        private v value;
        private int hasKey;    //节点数据
        private Node leftChild; //左子节点的引用
        private Node rightChild; //右子节点的引用
        private int isDelete = 0; //是否删除
        public Node(int hasKey,k key,v value){
            this.hasKey = hasKey;
            this.key = key;
            this.value = value;
        }
    }

    /**
     * 查找节点
     * @param key
     * @return
     */
    public Node find(Object key){
        int hashKey = getHashCode(key);
        if (root==null){
            return root;
        }
        Node curr = root;
        while (curr!=null){
            if (curr.hasKey==hashKey){
                return curr;
            }else if (curr.hasKey>hashKey){
                curr = curr.leftChild;
            }else {
                curr = curr.rightChild;
            }
        }
        return null;
    }

    /**
     * 插入新节点
     * @param key
     * @param value
     * @return
     */
    public Node insert(Object key,Object value){
        int hashKey = getHashCode(key);
        Node newNode = new Node(hashKey,key,value);
        if (root==null){
            root = newNode;
            return newNode;
        }
        Node curr = root;
        while (curr!=null){
            Node parentNode = curr;
            if (curr.hasKey>hashKey){
                curr = curr.leftChild;
                if (curr==null){
                    parentNode.leftChild = newNode;
                }
            }else {
                curr = curr.rightChild;
                if (curr==null){
                    parentNode.rightChild = newNode;
                }
            }
        }
        return newNode;
    }

    /**
     * 删除节点
     * @param key
     * @return Node
     */
    public Node delete(int key){
        int hashKey = getHashCode(key);
        Node curr = root;
        while (curr!=null){
            if (curr.hasKey==hashKey){
                curr.isDelete=1;
                return curr;
            }else if (curr.hasKey>hashKey){
                curr = curr.leftChild;
            }else {
                curr = curr.rightChild;
            }
        }
        return null;
    }

 

如果插入的数据是随机的,则效率很高,但是如果插入的数据是有序的,比如从小到大的顺序【10,20,30,40,50】插入到二叉搜索树中,变成了链表,则查找的时间复杂度为O(N)
这个时候就要平衡二叉树

 

 

红-黑树的特征

  1.每个节点不是红色就是黑色的;
  2.根节点总是黑色的;
  3.如果节点是红色的,则它的子节点必须是黑色的(反之不一定),(也就是从每个叶子到根的所有路径上不能有两个连续的红色节点);
  4.从根节点到叶节点或空子节点的每条路径,必须包含相同数目的黑色节点(即相同的黑色高度)。
 
红黑树比二叉树多了一个颜色属性,我们改变颜色也是为了帮助我们判断何时执行什么旋转,而旋转是为了保证树的平衡。光改变节点颜色是不能起到任何作用的,旋转才是关键的操作,在新增节点或者删除节点之后,可能会破坏二叉树的平衡,那么何时执行旋转以及执行什么旋转,这是我们需要重点关注的。
posted @ 2019-11-01 11:47  爱上胡萝卜的猴子  阅读(161)  评论(0编辑  收藏  举报