演算法導論第三版 紅黑樹

2022-08-03 07:48:17 字數 3397 閱讀 5270

紅黑樹是滿足如下條件的二叉搜尋樹。

1、每個節點要麼是紅色的,要麼是黑色的。

2、根節點是黑色。

3、每個葉子節點是黑色的。

4、如果乙個節點是紅色的,那麼它的2個子節點是黑色的。

5、對每個節點,從它到它的後代葉子節點的簡單路徑上,均包含相同數目的黑色節點。

練習完這些**,我感覺我把stl的rbtree抄了一遍。。。

1

enum

rb ;

56 template7

struct

rb_node_cls ;

1617 templateusing rbt_node = rb_node_cls;

1819 template20

struct

rb_tree_cls ;

2324 templateusing rb_tree = rb_tree_cls;

2526 template27

void left_rotate(rb_tree& tree, rbt_node*x)

3435 y->parent = x->parent;

36if(x->parent ==nullptr) else

if(x == x->parent->left) else

4344 y->left =x;

45 x->parent =y;46}

4748 template49

void right_rotate(rb_tree& tree, rbt_node*x)

5758 x->parent = y->parent;

59if(y->parent ==nullptr) else

if (y == y->parent->left) else

6667 x->right =y;

68 y->parent =x;69}

7071 template72

void rb_insert_fixup(rb_tree& tree, rbt_node*z) else

if(z == z->parent->right)

85 z->parent->color =black;

86 z->parent->parent->color =red;

87 right_rotate(tree, z->parent->parent);

88 } else

else

if(z == z->parent->left)

99 z->parent->color =black;

100 z->parent->parent->color =red;

101 left_rotate(tree, z->parent->parent);

102}

103}

104 tree.root->color =black;

105}

106107 template108

void rb_insert(rb_tree& tree, rbt_node*z)

116117 z->parent =y;

118if(y ==nullptr) else

if(z->key < y->key) else

125126 z->left =nullptr;

127 z->right =nullptr;

128 z->color =red;

129rb_insert_fixup(tree, z);

130}

131132 template133

void rb_transplant(rb_tree& tree, rbt_node* u, rbt_node*v) else

if (u == u->parent->left) else

141if(v !=nullptr)

142 v->parent = u->parent;

143}

144145 template146 rbt_node* tree_minimum(rbt_node*node)

151152 template153

void rb_delete_fixup(rb_tree& tree, rbt_node*x)

163164

if(w->left->color == black && w->right->color ==black) else

if(w->right->color ==black)

173174 w->color = x->parent->color;

175 x->parent->color =black;

176 w->right->color =black;

177 left_rotate(tree, x->parent);

178 x =tree.root;

179 } else

187188

if(w->right->color == black && w->left->color ==black) else

if (w->left->color ==black)

197198 w->color = x->parent->color;

199 x->parent->color =black;

200 x->left->color =black;

201 right_rotate(tree, x->parent);

202 x =tree.root;

203}

204}

205 x->color =black;

206}

207208 template209

void rb_delete(rb_tree& tree, rbt_node*z) else

if(z->right ==nullptr) else

else

231rb_transplant(tree, z, y);

232 y->left = z->left;

233 y->left->parent =y;

234 y->color = z->color;

235}

236if(y_original_color ==black)

239 }

演算法導論第三版習題5 2

正好僱傭一次說明第一次僱傭的就是所有應聘者中最好的,所以概率為1n 正好僱傭 n 次說明所有應聘者按優秀從低到高依次出現,第一位是最差的,概率為1n 第二位其次,概率為1n 1,所以整體概率為1n 正好僱傭兩次,說明第乙個應聘者不是最好的,概率為n 1n,第二個應聘者是最好的,概率為1n 1,所以概...

演算法導論第三版習題5 3

在進入迴圈前,先將在整個陣列中隨機選擇乙個數至於a 1 即可 permute in place a 1 n a.length 2 swap a 1 with a random 1,n 3 for i 2 to n 4 swap a i with a random i,n 第二步其概率為1n 後面一樣...

演算法導論第三版習題6 3

a a.leng th 9 故從i a.l engt h 2 4開始呼叫max heapify a,i a 4 9 8 故交換a 4 和a 8 得到新的序列a1 b 接下來呼叫max heapify a,3 a 7 3 6 故交換a 3 和a 6 得到a2 c 第三步呼叫max heapify a,...