演算法導論紅黑樹C 實現

2021-08-04 11:32:27 字數 3290 閱讀 2723

類封裝了紅黑樹的幾個屬性,水平有限,歡迎批評指正

#include "header.h"

enum nodecolor ;

struct rbtreenode

; rbtreenode(const int &a) };

class rbtree

;rbtree::rbtree()

rbtree::~rbtree() }

void rbtree::rbtreeinorder(rbtreenode *root) //輸出中序遍歷結果

}rbtreenode* rbtree::treemin(rbtreenode *root)

return node;

}void rbtree::rbinsertnode(rbtreenode* z)

else

}z->parents = y;

if (y == nil)

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

else

z->leftchild = nil;

z->rightchild = nil;

z->color = red;

rbinsertfixup(z);

}void rbtree::leftrotate(rbtreenode* node)

y->parents = node->parents;

if (node->parents == nil)

else if (node == node->parents->leftchild)

else

y->leftchild = node;

node->parents = y;

}void rbtree::rightrotate(rbtreenode* node)

/ y->parents = node->parents;

if (node->parents == nil) /已修改,等號寫成賦值號了

else if (node == node->parents->leftchild)

else

y->rightchild = node;

node->parents = y;

}void rbtree::rbinsertfixup(rbtreenode *z)

else

///情形3

z->parents->color = black;

z->parents->parents->color = red;

rightrotate(z->parents->parents);

} } else///z的父親是z的祖父的右兒子

else

z->parents->color = black;

z->parents->parents->color = red;

leftrotate(z->parents->parents);

}} }

root->color = black;

}void rbtree::rbtrasplant(rbtreenode* u, rbtreenode* v)

else if (u == u->parents->leftchild)

else

v->parents = u->parents;

}void rbtree::rbdelete(rbtreenode* z)

else if (z->rightchild == nil)

else //用z的後繼替代z,並整合z的屬性(key, color, left, right, parents)然後維護樹的性質

else

rbtrasplant(z, y);

y->leftchild = z->leftchild;

y->leftchild->parents = y;

y->color = z->color;

} if (y_orignal_color == black) }

void rbtree::rbtdeletefixup(rbtreenode* x)

else

w->color = x->parents->color;

x->parents->color = black;

w->rightchild->color = black;

leftrotate(x->parents);

x = root;}}

delete w;

} else

else

w->color = x->parents->color;

x->parents->color = black;

w->leftchild->color = black;

rightrotate(x->parents);

x = root;}}

delete w;

} }}void testrbtree()

; rbtreenode node[7] = {};

for (int i = 0; i < 7; i++)

cout << rbt.root->key << endl;

cout << rbt.root->color << endl;

cout << rbt.root->leftchild->rightchild->key << endl;

cout << rbt.root->rightchild->key << endl;

if (rbt.root->parents == rbt.nil)

cout << "生成的紅黑樹(中序遍歷顯示):" << endl;

rbt.rbtreeinorder(rbt.root);

rbtreenode node1(13);

rbt.rbinsertnode(&node1);

rbtreenode node2(17);

rbt.rbinsertnode(&node2);

rbtreenode node3(19);

rbt.rbinsertnode(&node3);

rbtreenode node4(23);

rbt.rbinsertnode(&node4);

cout << "插入4個節點之後(中序遍歷顯示):" << endl;

rbt.rbtreeinorder(rbt.root);

cout << "刪除乙個節點之後(中序遍歷顯示):" << endl;

rbt.rbdelete(rbt.root);

rbt.rbtreeinorder(rbt.root);

}

演算法導論紅黑樹的C 實現

按照演算法導論裡的紅黑樹偽 用c 實現了一下,並附帶一些測試 include include using namespace std enum color 顏色標記 templateclass binary tree node 紅黑樹節點 binary tree node parent 父節點 bi...

紅黑樹(演算法導論)

測試 所用的例子為算導第三版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.如果乙個結...