/**
* 紅黑樹
** 部分**參考自treemap原始碼
*/public
class
redblacktree
public redblacktree(comparator<? super
t> comparator)
public
void
insert(t
key)
while (x != null)
z.parent = y;
cmp = comparekey(z.key, y.key);
if (cmp < 0)
y.left = z;
else
y.right = z;
z.left = z.right = null;
fixafterinsertion(z);
size++;
}public
tremove(t
key)
public
boolean
isempty()
public
intsize()
public
treenode
firstnode()
public
treenode
lastnode()
public
treenode
find(t
t) return
null;
}public
set> nodeset()
public
static
final
class
treenode
public
tgetkey()
public
boolean
equals(object
o) public
string
tostring()
}private
static
treenodesuccessor(treenodet)
return p;}}
private
static
treenodepredecessor(treenodet)
return p;}}
private
static
treenodegetfirstnode(treenodet)
private
static
treenodegetlastnode(treenodet)
private
static
boolean
keyequals(object
o1, object
o2)
private
intcomparekey(t
key1, t
key2)
return cmp;
}private
void
rotateleft(treenodep)
}private
void
rotateright(treenodep)
}private
void
fixafterinsertion(treenodex) else
setcolor(parentof(x), black);
setcolor(parentof(parentof(x)), red);
rotateright(parentof(parentof(x)));
}} else else
setcolor(parentof(x), black);
setcolor(parentof(parentof(x)), red);
rotateleft(parentof(parentof(x)));}}
}root.color = black;
}private
void
deletenode(treenodep) else
if (rightof(p) == null) else
transplant(p, y);
y.left = leftof(p);
y.left.parent = y;
y.color = colorof(p);
}if (y_original_color == black)
fixafterdeletion(x);
}private
void
fixafterdeletion(treenodex)
if (colorof(leftof(sib)) == black
&& colorof(rightof(sib)) == black) else
setcolor(sib, colorof(parentof(x)));
setcolor(parentof(x), black);
setcolor(rightof(sib), black);
rotateleft(parentof(x));
x = root;
}} else
if (colorof(rightof(sib)) == black
&& colorof(leftof(sib)) == black) else
setcolor(sib, colorof(parentof(x)));
setcolor(parentof(x), black);
setcolor(leftof(sib), black);
rotateright(parentof(x));
x = root;}}
}setcolor(x, black);
}private
void
transplant(treenodeu, treenodev)
/*** 樹的平衡操作
* * 樹的實現沒有使用哨兵元素,而是使用下列方法處理null的情況
*/private
static
boolean colorof(treenodep)
private
static
treenodeparentof(treenodep)
private
static
void setcolor(treenodep, boolean
c) private
static
treenodeleftof(treenodep)
private
static
treenoderightof(treenodep)
final
class
nodeset
extends
abstractset
>
public
intsize()
}/**
* 紅黑樹的迭代器
*/final
class
nodeiterator
extends
privatenodeiterator
>
public
treenode
next()
public
void
remove()
}abstract
class
privatenodeiterator
implements
iterator
public
boolean
hasnext()
final
treenode
nextnode()
}}
紅黑樹(演算法導論)
測試 所用的例子為算導第三版p179圖13 4 include using namespace std const bool black 0 黑色 const bool red 1 紅色 struct node 結點結構 class rb tree 初始化nil結點和root node left r...
演算法導論 紅黑樹
原文 組內培訓,講紅黑樹,找出演算法導論,啃了乙個週末,其中插入結點很簡單,刪除結點有點複雜,但跟著演算法導論上一步一步來沒有什麼問題。不想備份blog的,所以沒有把上穿。可直接察看ppt。紅黑樹性質 1.每個節點或是紅的,或是黑的 2.根節點是黑的 3.每個葉結點 nil 都是黑的 4.如果乙個結...
演算法導論學習筆記 紅黑樹
紅黑樹的5個性質 1 每個結點要麼是紅的,要麼是黑的。2 根結點是黑的。3 每個葉結點,即空結點 nil 是黑的。4 如果乙個結點是紅的,那麼它的倆個兒子都是黑的。5 對每個結點,從該結點到其子孫結點的所有路徑上包含相同數目的黑結點。public class rbtree 當在某個結點nodex上,...