有序表的详解

目录

有序表的介绍

树的左旋和右旋操作

AVL树的详解

SB树的详解

红黑树的介绍

SkipList的详解

有序表的介绍

       有序表是除具备哈希表所具备的功能外,有序表中的内容都是按照key有序排列的,并且增删改查等操作的时间复杂度都是O\left ( logN \right ),红黑树,AVL树,SB树,SkipList等结构都可以实现有序表,并且时间复杂度都是O\left ( logN \right ),差距仅仅在于常数方面且比较小。

树的左旋和右旋操作

        树的左旋指的是将树的头节点的右子树向左边旋转,使右子树的根节点成为头节点,然后右子树的左子树放到原来头节点的右子树,其它的不变。树的右旋就是树的头节点向右旋转,然后左子树的根节点成为头节点 ,然后左子树的右子树放到原来头节点的左子树,其它的不变。

        通过树的左旋和右旋操作可以使不平衡的树变得平衡。

AVL树的详解

       AVL树是具备左旋和右旋操作的搜索二叉树,能够自身保持平衡性。AVL树对于新加入的节点,它会从加入的位置开始出发,依次检查以当前节点为头节点的树是不是具有平衡性。删除一个节点也是同样的操作,从替换节点的上一个节点出发,当平衡性不满足时使用左旋或者右旋操作使它恢复平衡性。接下来对每种具体的平衡性被破坏的情况进行分析,有以下四种类型:

        LL型:即左子树的左边部分失衡,此时采用右旋调整;

        RR型:即右子树的右边部分失衡,此时采用左旋调整;

        LR型:即左子树的右边部分失衡,此时先将左子树的右边部分左旋,然后再将它右旋,使失衡部分的根节点成为头节点;

        RL型:即右子树的左边部分失衡,此时先将右子树的左边部分右旋,然后再将它左旋,使失衡部分的根节点成为头节点。

        而对于AVL树对于各种失衡类型的判断,在下面代码部分详细分析:

   private void rebalance(AVLNode node) {
        while (node != null) {
            
            Node parent = node.parent;//记录头节点
            
            int leftHeight = (node.left == null) ? -1 : ((AVLNode) node.left).height;//求左边高度,如果头节点的左子树为空,返回-1,否则返回左边的高度
            int rightHeight = (node.right == null) ? -1 : ((AVLNode) node.right).height;//求右边高度,如果头节点的右子树为空,返回-1,否则返回右边的高度
            int nodeBalance = rightHeight - leftHeight;//计算两边高度差
            // rebalance (-2 means left subtree outgrow, 2 means right subtree)
            if (nodeBalance == 2) {//如果右边失衡
                if (node.right.right != null) {//如果为RR型
                    node = (AVLNode)avlRotateLeft(node);//左旋调整
                    break;
                } else {//RL型
                    node = (AVLNode)doubleRotateRightLeft(node);//先右旋再左旋调整
                    break;
                }
            } else if (nodeBalance == -2) {//如果左边失衡
                if (node.left.left != null) {//如果为LL型
                    node = (AVLNode)avlRotateRight(node);//右旋调整
                    break;
                } else {//LR型
                    node = (AVLNode)doubleRotateLeftRight(node);//先左旋再右旋调整
                    break;
                }
            } else {
                updateHeight(node);//返回树的高度
            }
            
            node = (AVLNode)parent;//记录当前树的头节点
        }
    }
    private Node avlRotateLeft(Node node) {//左旋
        Node temp = super.rotateLeft(node);
        
        updateHeight((AVLNode)temp.left);
        updateHeight((AVLNode)temp);
        return temp;
    }
    private Node avlRotateRight(Node node) {//右旋
        Node temp = super.rotateRight(node);

        updateHeight((AVLNode)temp.right);
        updateHeight((AVLNode)temp);
        return temp;
    }

    protected Node doubleRotateRightLeft(Node node) {//先右旋再左旋
        node.right = avlRotateRight(node.right);
        return avlRotateLeft(node);
    }
    protected Node doubleRotateLeftRight(Node node) {//先左旋再右旋
        node.left = avlRotateLeft(node.left);
        return avlRotateRight(node);
    }
    private static final void updateHeight(AVLNode node) {//求树的高度
        int leftHeight = (node.left == null) ? -1 : ((AVLNode) node.left).height;
        int rightHeight = (node.right == null) ? -1 : ((AVLNode) node.right).height;
        node.height = 1 + Math.max(leftHeight, rightHeight);
    }

SB树的详解

       SB树的增删改查操作还有平衡型结构的选择方式等和AVL树是相同的。SB树对平衡性的定义有所不同:每棵子树的大小,不小于其兄弟的子树大小,既每棵叔叔树的大小,不小于其任何侄子树的大小。对于失衡状态的调整有些区别。

        LL型:左子树的左边的侄子节点大于它的叔叔树的大小,此时先右旋,然后对发生改动的节点进行平衡性检查。
        RR型:右子树的右边的侄子节点大于它的叔叔树的大小,此时先左旋,然后对发生改动的节点进行平衡性检查。

        LR型:左子树的右边的侄子节点大于它的叔叔树的大小,此时先将左子树的右边的侄子节点左旋,然后对调整后的侄子节点继续右旋,接着对发生改动的节点进行平衡性检查。

        RL型:右子树的左边的侄子节点大于它的叔叔树的大小,此时先将右子树的左边的侄子节点右旋,然后对调整后的侄子节点继续左旋,接着对发生改动的节点进行平衡性检查。

        private SBTNode<K, V> matain(SBTNode<K, V> cur) {
			if (cur == null) {
				return null;
			}
			if (cur.l != null && cur.l.l != null && cur.r != null && cur.l.l.size > cur.r.size) {//LL型
				cur = rightRotate(cur);//右旋调整
				cur.r = matain(cur.r);//检查
				cur = matain(cur);//检查
			} else if (cur.l != null && cur.l.r != null && cur.r != null && cur.l.r.size > cur.r.size) {//LR型
				cur.l = leftRotate(cur.l);//左旋
				cur = rightRotate(cur);//右旋
				cur.l = matain(cur.l);
				cur.r = matain(cur.r);
				cur = matain(cur);
			} else if (cur.r != null && cur.r.r != null && cur.l != null && cur.r.r.size > cur.l.size) {//RR型
				cur = leftRotate(cur);//左旋
				cur.l = matain(cur.l);
				cur = matain(cur);
			} else if (cur.r != null && cur.r.l != null && cur.l != null && cur.r.l.size > cur.l.size) {//RL型
				cur.r = rightRotate(cur.r);//右旋
				cur = leftRotate(cur);//左旋
				cur.l = matain(cur.l);
				cur.r = matain(cur.r);
				cur = matain(cur);
			}
			return cur;
		}
        private SBTNode<K, V> rightRotate(SBTNode<K, V> cur) {//右旋
			SBTNode<K, V> leftNode = cur.l;
			cur.l = leftNode.r;
			leftNode.r = cur;
			leftNode.size = cur.size;
			cur.size = (cur.l != null ? cur.l.size : 0) + (cur.r != null ? cur.r.size : 0) + 1;
			return leftNode;
		}

		private SBTNode<K, V> leftRotate(SBTNode<K, V> cur) {//左旋
			SBTNode<K, V> rightNode = cur.r;
			cur.r = rightNode.l;
			rightNode.l = cur;
			rightNode.size = cur.size;
			cur.size = (cur.l != null ? cur.l.size : 0) + (cur.r != null ? cur.r.size : 0) + 1;
			return rightNode;
		}

红黑树的介绍

       红黑树的要求:(1)红黑树上面的每一个点不是红就是黑;(2)头节点和叶节点都是黑;(3)红点不相邻;(4)cur从任何一个子树的头部出发到它叶节点的每一条路径,要求黑节点数一样多。其实它的这些条件保证的是红黑树中最长的路和最短的路相差没有超过两倍,因为红点不相邻,所以最长的路也就是红黑交替,而最短的路就是全是黑,然而又需要每一条路径黑节点数一样多。红黑树的结构弊端比较大,一般不使用,了解即可。

SkipList的详解

       SkipList利用随机函数打破输入规律,首先有一个默认节点,每一个节点上面有指针,而只有默认节点的指针数量可以增加,对于节点的加入,先通过随机函数确定它上面的指针的数量,根据第一个加入节点的指针的数量在默认节点上面增加指针数量,对于后续节点的加入,如果后续的节点的指针数比默认节点上的指针数多,那么在默认节点上增加指针数,如果比默认节点的指针数少,多的不需要。对于新加入的节点,从默认节点的最高层开始一层一层的选择,如果不需要增加,那么右移寻找,如果需要增加,那么增加指针数。

      SkipList的这个结构便于查询和删除操作,查询时同样从默认节点的最高层出发,通过一层一层,从左到右的筛选,可以找到需要的值,进行查询和删除操作。对于添加同样也是从默认节点的最高层出发,一层一层,从左到右筛选。

       这个结构时间复杂度为O\left ( logN \right ),因为每一个节点的指针数利用概率随机产生,也就是后续每一个节点的操作数都是前一个的一半,整体相当于一棵完全二叉树,时间复杂度就是O\left ( logN \right )

   public static class SkipListNode<K extends Comparable<K>, V> {//SkipList的建立
		public K key;
		public V val;
		public ArrayList<SkipListNode<K, V>> nextNodes;

		public SkipListNode(K k, V v) {
			key = k;
			val = v;
			nextNodes = new ArrayList<SkipListNode<K, V>>();
		}

		public boolean isKeyLess(K otherKey) {
			return otherKey != null && (key == null || key.compareTo(otherKey) < 0);
		}

		public boolean isKeyEqual(K otherKey) {
			return (key == null && otherKey == null)
					|| (key != null && otherKey != null && key.compareTo(otherKey) == 0);
		}

	}

	public static class SkipListMap<K extends Comparable<K>, V> {
		private static final double PROBABILITY = 0.5;
		private SkipListNode<K, V> head;
		private int size;
		private int maxLevel;

		public SkipListMap() {
			head = new SkipListNode<K, V>(null, null);
			head.nextNodes.add(null);
			size = 0;
			maxLevel = 0;
		}

		private SkipListNode<K, V> mostRightLessNodeInTree(K key) {
			if (key == null) {
				return null;
			}
			int level = maxLevel;
			SkipListNode<K, V> cur = head;
			while (level >= 0) {
				cur = mostRightLessNodeInLevel(key, cur, level--);
			}
			return cur;
		}

		private SkipListNode<K, V> mostRightLessNodeInLevel(K key, SkipListNode<K, V> cur, int level) {
			SkipListNode<K, V> next = cur.nextNodes.get(level);
			while (next != null && next.isKeyLess(key)) {
				cur = next;
				next = cur.nextNodes.get(level);
			}
			return cur;
		}

		public boolean containsKey(K key) {
			if (key == null) {
				return false;
			}
			SkipListNode<K, V> less = mostRightLessNodeInTree(key);
			SkipListNode<K, V> next = less.nextNodes.get(0);
			return next != null && next.isKeyEqual(key);
		}

		public void put(K key, V value) {
			if (key == null) {
				return;
			}
			SkipListNode<K, V> less = mostRightLessNodeInTree(key);
			SkipListNode<K, V> find = less.nextNodes.get(0);
			if (find != null && find.isKeyEqual(key)) {
				find.val = value;
			} else {
				size++;
				int newNodeLevel = 0;
				while (Math.random() < PROBABILITY) {
					newNodeLevel++;
				}
				while (newNodeLevel > maxLevel) {
					head.nextNodes.add(null);
					maxLevel++;
				}
				SkipListNode<K, V> newNode = new SkipListNode<K, V>(key, value);
				for (int i = 0; i <= newNodeLevel; i++) {
					newNode.nextNodes.add(null);
				}
				int level = maxLevel;
				SkipListNode<K, V> pre = head;
				while (level >= 0) {
					pre = mostRightLessNodeInLevel(key, pre, level);
					if (level <= newNodeLevel) {
						newNode.nextNodes.set(level, pre.nextNodes.get(level));
						pre.nextNodes.set(level, newNode);
					}
					level--;
				}
			}
		}

		public V get(K key) {
			if (key == null) {
				return null;
			}
			SkipListNode<K, V> less = mostRightLessNodeInTree(key);
			SkipListNode<K, V> next = less.nextNodes.get(0);
			return next != null && next.isKeyEqual(key) ? next.val : null;
		}

		public void remove(K key) {
			if (containsKey(key)) {
				size--;
				int level = maxLevel;
				SkipListNode<K, V> pre = head;
				while (level >= 0) {
					pre = mostRightLessNodeInLevel(key, pre, level);
					SkipListNode<K, V> next = pre.nextNodes.get(level);
					if (next != null && next.isKeyEqual(key)) {
						// free delete node memory -> C++
						pre.nextNodes.set(level, next.nextNodes.get(level));
					}
					if (level != 0 && pre == head && pre.nextNodes.get(level) == null) {
						head.nextNodes.remove(level);
						maxLevel--;
					}
					level--;
				}
			}
		}

		public K firstKey() {
			return head.nextNodes.get(0) != null ? head.nextNodes.get(0).key : null;
		}

		public K lastKey() {
			int level = maxLevel;
			SkipListNode<K, V> cur = head;
			while (level >= 0) {
				SkipListNode<K, V> next = cur.nextNodes.get(level);
				while (next != null) {
					cur = next;
					next = cur.nextNodes.get(level);
				}
				level--;
			}
			return cur.key;
		}

		public K ceillingKey(K key) {
			if (key == null) {
				return null;
			}
			SkipListNode<K, V> less = mostRightLessNodeInTree(key);
			SkipListNode<K, V> next = less.nextNodes.get(0);
			return next != null ? next.key : null;
		}

		public K floorKey(K key) {
			if (key == null) {
				return null;
			}
			SkipListNode<K, V> less = mostRightLessNodeInTree(key);
			SkipListNode<K, V> next = less.nextNodes.get(0);
			return next != null && next.isKeyEqual(key) ? next.key : less.key;
		}

		public int size() {
			return size;
		}

	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

互联网的猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值