摘要:在上一節(jié)中,在中用了鏈表和紅黑樹兩種方式解決沖突,在中也是用紅黑樹存儲(chǔ)的。其中節(jié)點(diǎn)顏色為黑色紅黑樹的左旋和右旋紅黑樹的插入和刪除,都有可能破壞其特性,就不是一棵紅黑樹了,所以要調(diào)整。
在上一節(jié)中,HashMap在jdk 1.8中用了鏈表和紅黑樹兩種方式解決沖突,在TreeMap中也是用紅黑樹存儲(chǔ)的。下面分析一下紅黑樹的結(jié)構(gòu)和基本操作。一、紅黑樹的特征和基本操作
上一節(jié)中已經(jīng)描述了紅黑樹的基本概念和特征,下面直接通過(guò)一個(gè)例子分析紅黑樹的構(gòu)造和調(diào)整方法。1、紅黑樹的數(shù)據(jù)結(jié)構(gòu)
紅黑樹是一棵二叉查找樹,在二叉樹的基礎(chǔ)上增加了節(jié)點(diǎn)的顏色,下面是TreeMap中的紅黑樹定義:
private static final boolean RED = false; private static final boolean BLACK = true; static final class Entry2、紅黑樹的左旋和右旋implements Map.Entry { K key; V value; Entry left; Entry right; Entry parent; boolean color = BLACK; /** * 給定key、value和父節(jié)點(diǎn),構(gòu)造一個(gè)新的。其中節(jié)點(diǎn)顏色為黑色 */ Entry(K key, V value, Entry parent) { this.key = key; this.value = value; this.parent = parent; } }
紅黑樹的插入和刪除,都有可能破壞其特性,就不是一棵紅黑樹了,所以要調(diào)整。調(diào)整的方法又兩種,一種是改變某個(gè)節(jié)點(diǎn)的顏色,另外一種是結(jié)構(gòu)調(diào)整,包括左旋和右旋。 左旋:將X的節(jié)點(diǎn)的右兒子節(jié)點(diǎn)Y變?yōu)槠涓腹?jié)點(diǎn),并且將Y的左子樹變?yōu)閄的右子樹,變換過(guò)程入下圖
右旋:將X的節(jié)點(diǎn)的左兒子節(jié)點(diǎn)Y變?yōu)槠涓腹?jié)點(diǎn),并且將Y的右子樹變?yōu)閄的左子樹,變換過(guò)程入下圖3、插入節(jié)點(diǎn)后調(diào)整紅黑樹
當(dāng)在紅黑樹中插入一個(gè)節(jié)點(diǎn)后,可能會(huì)破壞紅黑樹的規(guī)則,首先再回顧一下紅黑數(shù)的特點(diǎn):
節(jié)點(diǎn)是紅色或黑色。
根節(jié)點(diǎn)是黑色。
每個(gè)葉子節(jié)點(diǎn)都是黑色的空節(jié)點(diǎn)(NIL節(jié)點(diǎn))。
每個(gè)紅色節(jié)點(diǎn)的兩個(gè)子節(jié)點(diǎn)都是黑色。(從每個(gè)葉子到根的所有路徑上不能有兩個(gè)連續(xù)的紅色節(jié)點(diǎn))
從任一節(jié)點(diǎn)到其每個(gè)葉子的所有路徑都包含相同數(shù)目的黑色節(jié)點(diǎn)。
從上面的條件可以看出,a肯定是不會(huì)違背的。插入的節(jié)點(diǎn)不在根節(jié)點(diǎn)處,所以b也不會(huì)違背。插入的節(jié)點(diǎn)時(shí)非空節(jié)點(diǎn),c也不會(huì)違背。最有可能違背的就是d和e。而在我們插入節(jié)點(diǎn)時(shí),先將要插入的節(jié)點(diǎn)顏色設(shè)置為紅色,這樣也就不會(huì)違背e。所以,插入后只需要調(diào)整不違背e就可以。
插入后調(diào)整需要分三種情況來(lái)處理:
插入的是根節(jié)點(diǎn):
處理方法是直接將根節(jié)點(diǎn)顏色設(shè)置為黑色
插入節(jié)點(diǎn)的父節(jié)點(diǎn)為黑色節(jié)點(diǎn)或父節(jié)點(diǎn)為根節(jié)點(diǎn)
不需要處理
插入節(jié)點(diǎn)的父節(jié)點(diǎn)時(shí)紅色節(jié)點(diǎn)
這種又分為三種情況
下面假設(shè)插入節(jié)點(diǎn)為x,父節(jié)點(diǎn)為xp,祖父節(jié)點(diǎn)為xpp,祖父節(jié)點(diǎn)的左兒子為xppl,祖父節(jié)點(diǎn)的右兒子為xppr
S1:當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn)xp是紅色,且當(dāng)前節(jié)點(diǎn)的祖父節(jié)xpp點(diǎn)的另一個(gè)子節(jié)點(diǎn)(xppl或者xppr)也是紅色
處理邏輯:將父節(jié)點(diǎn)xp設(shè)為紅色,祖父節(jié)點(diǎn)的兒子節(jié)點(diǎn)(xppl或者xppr)設(shè)為黑色,將祖父節(jié)點(diǎn)xpp設(shè)為紅色,將祖父節(jié)點(diǎn)xpp設(shè)為當(dāng)前節(jié)點(diǎn),繼續(xù)處理。
S2:當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn)xp是紅色,祖父節(jié)點(diǎn)的兒子節(jié)點(diǎn)(xppl或者xppr)是黑色,且當(dāng)前節(jié)點(diǎn)x是其父節(jié)點(diǎn)xp的右孩子
處理邏輯:父節(jié)點(diǎn)xp作為當(dāng)前節(jié)點(diǎn)x, 以當(dāng)前節(jié)點(diǎn)x為支點(diǎn)進(jìn)行左旋。
S3:當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn)xp是紅色,祖父節(jié)點(diǎn)的兒子節(jié)點(diǎn)(xppl或者xppr)是黑色,且當(dāng)前節(jié)點(diǎn)是其父節(jié)點(diǎn)xp的左孩子
處理邏輯:將父節(jié)點(diǎn)xp設(shè)置為黑色,祖父節(jié)點(diǎn)xpp設(shè)置為紅色,以祖父節(jié)點(diǎn)xpp為支點(diǎn)進(jìn)行右旋
4、刪除節(jié)點(diǎn)后調(diào)整紅黑樹未完,待續(xù)。。。
3、構(gòu)造一棵紅黑樹通過(guò)插入節(jié)點(diǎn),構(gòu)造紅黑樹
現(xiàn)在給定節(jié)點(diǎn)8 5 3 9 12 1 4 2,依次插入紅黑樹中,具體流程見(jiàn)下圖:
在紅黑樹中刪除節(jié)點(diǎn)
未完,待續(xù)。。。
二、HashMap中的紅黑樹相關(guān)源碼static final class TreeNode三、總結(jié)extends LinkedHashMap.Entry { TreeNode parent; // red-black tree links TreeNode left; TreeNode right; //在節(jié)點(diǎn)刪除后,需解除鏈接 TreeNode prev; boolean red; TreeNode(int hash, K key, V val, Node next) { super(hash, key, val, next); } /** * 返回根節(jié)點(diǎn) */ final TreeNode root() { for (TreeNode r = this, p;;) { if ((p = r.parent) == null) return r; r = p; } } /** * 確保根節(jié)點(diǎn)就是第一個(gè)節(jié)點(diǎn) */ static void moveRootToFront(Node [] tab, TreeNode root) { int n; if (root != null && tab != null && (n = tab.length) > 0) { int index = (n - 1) & root.hash; TreeNode first = (TreeNode )tab[index]; //如果根節(jié)點(diǎn)不是第一個(gè)節(jié)點(diǎn),進(jìn)行調(diào)整 if (root != first) { Node rn; tab[index] = root; TreeNode rp = root.prev; if ((rn = root.next) != null) ((TreeNode )rn).prev = rp; if (rp != null) rp.next = rn; if (first != null) first.prev = root; root.next = first; root.prev = null; } assert checkInvariants(root); } } /** * 根據(jù)hash值和key查詢節(jié)點(diǎn) */ final TreeNode find(int h, Object k, Class> kc) { TreeNode p = this; do { int ph, dir; K pk; TreeNode pl = p.left, pr = p.right, q; if ((ph = p.hash) > h) p = pl; else if (ph < h) p = pr; else if ((pk = p.key) == k || (k != null && k.equals(pk))) return p; else if (pl == null) p = pr; else if (pr == null) p = pl; else if ((kc != null || (kc = comparableClassFor(k)) != null) && (dir = compareComparables(kc, k, pk)) != 0) p = (dir < 0) ? pl : pr; else if ((q = pr.find(h, k, kc)) != null) return q; else p = pl; } while (p != null); return null; } /** * 根據(jù)hash值和key查詢節(jié)點(diǎn) */ final TreeNode getTreeNode(int h, Object k) { return ((parent != null) ? root() : this).find(h, k, null); } /** * 將鏈表轉(zhuǎn)換為紅黑樹 */ final void treeify(Node [] tab) { TreeNode root = null; //從第一個(gè)節(jié)點(diǎn)開始 for (TreeNode x = this, next; x != null; x = next) { next = (TreeNode )x.next; x.left = x.right = null; //如果root節(jié)點(diǎn)為null,x為根節(jié)點(diǎn),此節(jié)點(diǎn)為黑色,父節(jié)點(diǎn)為null if (root == null) { x.parent = null; x.red = false; root = x; } else { //x的key值 K k = x.key; //x的hash值 int h = x.hash; Class> kc = null; for (TreeNode p = root;;) { int dir, ph; K pk = p.key; //左邊 if ((ph = p.hash) > h) dir = -1; //右邊 else if (ph < h) dir = 1; //通過(guò)仲裁方法判斷 else if ((kc == null && (kc = comparableClassFor(k)) == null) || (dir = compareComparables(kc, k, pk)) == 0) dir = tieBreakOrder(k, pk); TreeNode xp = p; //dir <=0 左子樹搜索,并且判斷左兒子是否為空,表示是否到葉子節(jié)點(diǎn) if ((p = (dir <= 0) ? p.left : p.right) == null) { x.parent = xp; if (dir <= 0) xp.left = x; else xp.right = x; //插入元素,判斷是否平衡,并且調(diào)整 root = balanceInsertion(root, x); break; } } } } //確保根節(jié)點(diǎn)就是第一個(gè)節(jié)點(diǎn) moveRootToFront(tab, root); } /** * 紅黑樹轉(zhuǎn)換為鏈表 */ final Node untreeify(HashMap map) { Node hd = null, tl = null; for (Node q = this; q != null; q = q.next) { Node p = map.replacementNode(q, null); if (tl == null) hd = p; else tl.next = p; tl = p; } return hd; } /** * 插入一個(gè)節(jié)點(diǎn) */ final TreeNode putTreeVal(HashMap map, Node [] tab, int h, K k, V v) { Class> kc = null; boolean searched = false; TreeNode root = (parent != null) ? root() : this; //從根據(jù)點(diǎn)開始,和當(dāng)前搜索節(jié)點(diǎn)的hash比較 for (TreeNode p = root;;) { int dir, ph; K pk; if ((ph = p.hash) > h) dir = -1; else if (ph < h) dir = 1; //hash和key都一致 else if ((pk = p.key) == k || (k != null && k.equals(pk))) return p; else if ((kc == null && (kc = comparableClassFor(k)) == null) || (dir = compareComparables(kc, k, pk)) == 0) { if (!searched) { TreeNode q, ch; searched = true; if (((ch = p.left) != null && (q = ch.find(h, k, kc)) != null) || ((ch = p.right) != null && (q = ch.find(h, k, kc)) != null)) return q; } dir = tieBreakOrder(k, pk); } TreeNode xp = p; if ((p = (dir <= 0) ? p.left : p.right) == null) { Node xpn = xp.next; //新建節(jié)點(diǎn) TreeNode x = map.newTreeNode(h, k, v, xpn); if (dir <= 0) xp.left = x; else xp.right = x; xp.next = x; x.parent = x.prev = xp; if (xpn != null) ((TreeNode )xpn).prev = x; //插入元素,判斷是否平衡,并且調(diào)整。確保根節(jié)點(diǎn)就是第一個(gè)節(jié)點(diǎn) moveRootToFront(tab, balanceInsertion(root, x)); return null; } } } /** * Removes the given node, that must be present before this call. * This is messier than typical red-black deletion code because we * cannot swap the contents of an interior node with a leaf * successor that is pinned by "next" pointers that are accessible * independently during traversal. So instead we swap the tree * linkages. If the current tree appears to have too few nodes, * the bin is converted back to a plain bin. (The test triggers * somewhere between 2 and 6 nodes, depending on tree structure). */ final void removeTreeNode(HashMap map, Node [] tab, boolean movable) { int n; if (tab == null || (n = tab.length) == 0) return; int index = (n - 1) & hash; TreeNode first = (TreeNode )tab[index], root = first, rl; TreeNode succ = (TreeNode )next, pred = prev; if (pred == null) tab[index] = first = succ; else pred.next = succ; if (succ != null) succ.prev = pred; if (first == null) return; if (root.parent != null) root = root.root(); if (root == null || root.right == null || (rl = root.left) == null || rl.left == null) { tab[index] = first.untreeify(map); // too small return; } TreeNode p = this, pl = left, pr = right, replacement; if (pl != null && pr != null) { TreeNode s = pr, sl; while ((sl = s.left) != null) // find successor s = sl; boolean c = s.red; s.red = p.red; p.red = c; // swap colors TreeNode sr = s.right; TreeNode pp = p.parent; if (s == pr) { // p was s"s direct parent p.parent = s; s.right = p; } else { TreeNode sp = s.parent; if ((p.parent = sp) != null) { if (s == sp.left) sp.left = p; else sp.right = p; } if ((s.right = pr) != null) pr.parent = s; } p.left = null; if ((p.right = sr) != null) sr.parent = p; if ((s.left = pl) != null) pl.parent = s; if ((s.parent = pp) == null) root = s; else if (p == pp.left) pp.left = s; else pp.right = s; if (sr != null) replacement = sr; else replacement = p; } else if (pl != null) replacement = pl; else if (pr != null) replacement = pr; else replacement = p; if (replacement != p) { TreeNode pp = replacement.parent = p.parent; if (pp == null) root = replacement; else if (p == pp.left) pp.left = replacement; else pp.right = replacement; p.left = p.right = p.parent = null; } TreeNode r = p.red ? root : balanceDeletion(root, replacement); if (replacement == p) { // detach TreeNode pp = p.parent; p.parent = null; if (pp != null) { if (p == pp.left) pp.left = null; else if (p == pp.right) pp.right = null; } } if (movable) moveRootToFront(tab, r); } /* ------------------------------------------------------------ */ // Red-black tree methods, all adapted from CLR //左旋 static TreeNode rotateLeft(TreeNode root, TreeNode p) { TreeNode r, pp, rl; //以p為左旋支點(diǎn),且p不為空,右兒子不為空 if (p != null && (r = p.right) != null) { //將p的右兒子r的左兒子rl變?yōu)閜的右兒子 if ((rl = p.right = r.left) != null) rl.parent = p; //處理p、l和p父節(jié)點(diǎn)的關(guān)系 if ((pp = r.parent = p.parent) == null) (root = r).red = false; else if (pp.left == p) pp.left = r; else pp.right = r; //處理p和r的關(guān)系 r.left = p; p.parent = r; } return root; } //右旋 static TreeNode rotateRight(TreeNode root, TreeNode p) { TreeNode l, pp, lr; //p:右旋支點(diǎn),不為空,p的左兒子l不為空 if (p != null && (l = p.left) != null) { //將左兒子的右子樹變?yōu)閜的左子樹 if ((lr = p.left = l.right) != null) lr.parent = p; //p的父節(jié)點(diǎn)變?yōu)閘的父節(jié)點(diǎn) if ((pp = l.parent = p.parent) == null) (root = l).red = false; //如果p為右兒子,則p的父節(jié)點(diǎn)的右兒子變?yōu)閘,否則左兒子變?yōu)閘 else if (pp.right == p) pp.right = l; else pp.left = l; //p變?yōu)閘的右兒子 l.right = p; p.parent = l; } return root; } static TreeNode balanceInsertion(TreeNode root, TreeNode x) { //插入節(jié)點(diǎn)初始化為紅色 x.red = true; //xp:父節(jié)點(diǎn),xpp:祖父節(jié)點(diǎn), xppl:祖父節(jié)點(diǎn)的左兒子,xppr:祖父節(jié)點(diǎn)的右兒子 //循環(huán)遍歷 for (TreeNode xp, xpp, xppl, xppr;;) { //插入的節(jié)點(diǎn)為根節(jié)點(diǎn),節(jié)點(diǎn)顏色轉(zhuǎn)換為黑色 if ((xp = x.parent) == null) { x.red = false; return x; } //當(dāng)前節(jié)點(diǎn)的父為黑色節(jié)點(diǎn)或者父節(jié)點(diǎn)為根節(jié)點(diǎn),直接返回 else if (!xp.red || (xpp = xp.parent) == null) return root; //祖父節(jié)點(diǎn)的左兒子是父節(jié)點(diǎn) if (xp == (xppl = xpp.left)) { //S1:當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn)xp是紅色,且當(dāng)前節(jié)點(diǎn)的祖父節(jié)xpp點(diǎn)的另一個(gè)子節(jié)點(diǎn)(xppl或者xppr)也是紅色 if ((xppr = xpp.right) != null && xppr.red) { xppr.red = false; xp.red = false; xpp.red = true; x = xpp; } else { //S2:當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn)xp是紅色,祖父節(jié)點(diǎn)的兒子節(jié)點(diǎn)(xppl或者xppr)是黑色,且當(dāng)前節(jié)點(diǎn)x是其父節(jié)點(diǎn)xp的右孩子 if (x == xp.right) { root = rotateLeft(root, x = xp); xpp = (xp = x.parent) == null ? null : xp.parent; } //S3:當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn)xp是紅色,祖父節(jié)點(diǎn)的兒子節(jié)點(diǎn)(xppl或者xppr)是黑色,且當(dāng)前節(jié)點(diǎn)是其父節(jié)點(diǎn)xp的左孩子 if (xp != null) { xp.red = false; if (xpp != null) { xpp.red = true; root = rotateRight(root, xpp); } } } } else { //S1:當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn)xp是紅色,且當(dāng)前節(jié)點(diǎn)的祖父節(jié)xpp點(diǎn)的另一個(gè)子節(jié)點(diǎn)(xppl或者xppr)也是紅色 if (xppl != null && xppl.red) { xppl.red = false; xp.red = false; xpp.red = true; x = xpp; } else { //S2:當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn)xp是紅色,祖父節(jié)點(diǎn)的兒子節(jié)點(diǎn)(xppl或者xppr)是黑色,且當(dāng)前節(jié)點(diǎn)x是其父節(jié)點(diǎn)xp的右孩子 if (x == xp.left) { root = rotateRight(root, x = xp); xpp = (xp = x.parent) == null ? null : xp.parent; } //S3:當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn)xp是紅色,祖父節(jié)點(diǎn)的兒子節(jié)點(diǎn)(xppl或者xppr)是黑色,且當(dāng)前節(jié)點(diǎn)是其父節(jié)點(diǎn)xp的左孩子 if (xp != null) { xp.red = false; if (xpp != null) { xpp.red = true; root = rotateLeft(root, xpp); } } } } } } static TreeNode balanceDeletion(TreeNode root, TreeNode x) { for (TreeNode xp, xpl, xpr;;) { if (x == null || x == root) return root; else if ((xp = x.parent) == null) { x.red = false; return x; } else if (x.red) { x.red = false; return root; } else if ((xpl = xp.left) == x) { if ((xpr = xp.right) != null && xpr.red) { xpr.red = false; xp.red = true; root = rotateLeft(root, xp); xpr = (xp = x.parent) == null ? null : xp.right; } if (xpr == null) x = xp; else { TreeNode sl = xpr.left, sr = xpr.right; if ((sr == null || !sr.red) && (sl == null || !sl.red)) { xpr.red = true; x = xp; } else { if (sr == null || !sr.red) { if (sl != null) sl.red = false; xpr.red = true; root = rotateRight(root, xpr); xpr = (xp = x.parent) == null ? null : xp.right; } if (xpr != null) { xpr.red = (xp == null) ? false : xp.red; if ((sr = xpr.right) != null) sr.red = false; } if (xp != null) { xp.red = false; root = rotateLeft(root, xp); } x = root; } } } else { // symmetric if (xpl != null && xpl.red) { xpl.red = false; xp.red = true; root = rotateRight(root, xp); xpl = (xp = x.parent) == null ? null : xp.left; } if (xpl == null) x = xp; else { TreeNode sl = xpl.left, sr = xpl.right; if ((sl == null || !sl.red) && (sr == null || !sr.red)) { xpl.red = true; x = xp; } else { if (sl == null || !sl.red) { if (sr != null) sr.red = false; xpl.red = true; root = rotateLeft(root, xpl); xpl = (xp = x.parent) == null ? null : xp.left; } if (xpl != null) { xpl.red = (xp == null) ? false : xp.red; if ((sl = xpl.left) != null) sl.red = false; } if (xp != null) { xp.red = false; root = rotateRight(root, xp); } x = root; } } } } } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/72103.html
摘要:關(guān)于的源碼分析,本文并不打算展開講了。大家可以參考我之前的一篇文章源碼詳細(xì)分析。在刪除節(jié)點(diǎn)時(shí),父類的刪除邏輯并不會(huì)修復(fù)所維護(hù)的雙向鏈表,這不是它的職責(zé)。在節(jié)分析鏈表建立過(guò)程時(shí),我故意忽略了部分源碼分析。 1. 概述 LinkedHashMap 繼承自 HashMap,在 HashMap 基礎(chǔ)上,通過(guò)維護(hù)一條雙向鏈表,解決了 HashMap 不能隨時(shí)保持遍歷順序和插入順序一致的問(wèn)題。除此...
摘要:很多文章或書籍在介紹紅黑樹的時(shí)候直接上來(lái)就是紅黑樹的個(gè)基本性質(zhì)插入刪除操作等。這也不奇怪,算法的作者就是紅黑樹的作者之一。所以,理解樹對(duì)掌握紅黑樹是至關(guān)重要的。 本文主要包括以下內(nèi)容: 什么是2-3樹 2-3樹的插入操作 紅黑樹與2-3樹的等價(jià)關(guān)系 《算法4》和《算法導(dǎo)論》上關(guān)于紅黑樹的差異 紅黑樹的5條基本性質(zhì)的分析 紅黑樹與2-3-4樹的等價(jià)關(guān)系 紅黑樹的插入、刪除操作 JDK ...
摘要:若遇到哈希沖突,則將沖突的值加到鏈表中即可。之后相比于之前的版本,之后在解決哈希沖突時(shí)有了較大的變化,當(dāng)鏈表長(zhǎng)度大于閾值默認(rèn)為時(shí),將鏈表轉(zhuǎn)化為紅黑樹,以減少搜索時(shí)間。有序,唯一紅黑樹自平衡的排序二叉樹。 本文是最最最常見(jiàn)Java面試題總結(jié)系列第三周的文章。主要內(nèi)容: Arraylist 與 LinkedList 異同 ArrayList 與 Vector 區(qū)別 HashMap的底層...
摘要:訪問(wèn)順序調(diào)用過(guò)訪問(wèn)的元素會(huì)放到鏈尾,迭代會(huì)從鏈?zhǔn)组_始插入順序按插入順序迭代出來(lái)內(nèi)部是基于紅黑樹實(shí)現(xiàn)的,并且默認(rèn)會(huì)通過(guò)按照類型進(jìn)行自然排序。 對(duì)于常用的集合大家都不陌生,但是深入到內(nèi)部原理可能都是一知半解,通過(guò)閱讀源碼理解如下。 ArrayList ArrayList內(nèi)部就是一個(gè)默認(rèn)大小為10的動(dòng)態(tài)對(duì)象數(shù)組容器,每當(dāng)add一個(gè)新數(shù)據(jù)的時(shí)候,如果大于原來(lái)的容器大小,則會(huì)通過(guò)Arrays.c...
摘要:源碼剖析由于紅黑樹的操作我這里不說(shuō)了,所以這里基本上也就沒(méi)什么源碼可以講了,因?yàn)檫@里面重要的算法都是,這里的是指,他們是算法導(dǎo)論的作者,也就是說(shuō)里面算法都是參照算法導(dǎo)論的偽代碼。因?yàn)榧t黑樹是平衡的二叉搜索樹,所以其包含操作的時(shí)間復(fù)雜度都為。 本文章首發(fā)于個(gè)人博客,鑒于sf博客樣式具有賞心悅目的美感,遂發(fā)表于此,供大家學(xué)習(xí)、批評(píng)。本文還在不斷更新中,最新版可移至個(gè)人博客。? 繼上篇文章...
閱讀 3036·2021-10-15 09:41
閱讀 1696·2021-09-22 15:56
閱讀 2174·2021-08-10 09:43
閱讀 3343·2019-08-30 13:56
閱讀 1849·2019-08-30 12:47
閱讀 716·2019-08-30 11:17
閱讀 2842·2019-08-30 11:09
閱讀 2238·2019-08-29 16:19