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

2021-06-05 15:39:09 字數 4849 閱讀 7369

按照演算法導論裡的紅黑樹偽**用c++實現了一下,

並附帶一些測試**。

#include #include using namespace std;

enum color;//顏色標記

templateclass binary_tree_node//紅黑樹節點

binary_tree_node* parent;//*/父節點*/

binary_tree_node* leftchild;//左子樹

binary_tree_node* rightchild;//後子樹

t key;//鍵值

color c;//顏色標記

};templateclass binary_tree//紅黑樹結構

binary_tree_node* root;//根節點

};templatevoid inorder_tree_walk( binary_tree_node* a)//順序列印樹

}templatebinary_tree_node* tree_search( binary_tree_node* a,const t & search_key)//遞迴查詢

if (a->keyrightchild,search_key);

} else }

templatebinary_tree_node* interative_tree_search( binary_tree_node* a,const t & search_key )//非遞迴查詢

else

}return temp;

}templatebinary_tree_node* tree_min(binary_tree_node* a)//求節點下的最小值

return temprt;

}templatebinary_tree_node* tree_max(binary_tree_node* b)//求節點下的最大值

return temprt;

}templatebinary_tree_node* successor(binary_tree_node* a)//求比節點的大的後乙個值

binary_tree_node* y=temp->parent;

while(y!=null && temp==y->rightchild)

return y;

} else }

templatebinary_tree_node* predecessor(binary_tree_node* a)//求比節點小的前乙個值

binary_tree_node* y=temp->parent;

while(y!=null && temp==y->leftchild)

return y;

} else }

templatevoid rb_tree_insert(binary_tree& tree,const t & insertkey)//紅黑樹插入

else

}binary_tree_node* newnode=new binary_tree_node;

newnode->key=insertkey;

newnode->parent=y;

if (y==null)

else

else

}newnode->c=red;

rb_tree_insert_fixup(tree,newnode);//插入後修復紅黑樹屬性

}templatevoid rb_tree_insert_fixup(binary_tree& tree,binary_tree_node* newnode)

else

else

}} else

else

else

}} }

tree.root->c=black;

}templatevoid tree_insert(binary_tree& tree,const t & insertkey)//普通二叉樹插入

else

}binary_tree_node* newnode=new binary_tree_node;

newnode->key=insertkey;

newnode->parent=y;

if (y==null)

else

else

}return ;

}templatevoid rb_tree_delete(binary_tree& tree,binary_tree_node* deletenode)//紅黑樹刪除

else

if (y->leftchild!=null)

else

if (x==null)

x->parent=y->parent;

if (y->parent==null)

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

else

if (y!=deletenode)

if (y->c==black)

if (nil.parent!=null)//將臨時插入的nil還原為null指標

else if (&nil==nil.parent->rightchild)

}if (tree.root==&nil)

delete y;

return;

} else }

templatevoid rb_tree_delete_fixup(binary_tree& tree,binary_tree_node* x)

if (w==null||(w!=null&&(w->leftchild==null||w->leftchild->c==black)

&&(w->rightchild==null||w->rightchild->c==black)))

x=x->parent;

continue;

}else if(w->rightchild==null||w->rightchild->c==black)

w->c=x->parent->c;

x->parent->c=black;

w->rightchild->c=black;

left_roation(tree,x->parent);

x=tree.root;

} else

if (w==null||(w!=null&&(w->leftchild==null||w->leftchild->c==black)

&&(w->rightchild==null||w->rightchild->c==black)))

x=x->parent;

continue;

}else if (w->leftchild==null||w->leftchild->c==black)

w->c=x->parent->c;

x->parent->c=black;

w->leftchild->c=black;

right_roation(tree,x->parent);

x=tree.root;

} }x->c=black;

return;

}templatevoid tree_delete(binary_tree& tree,binary_tree_node* deletenode)//普通二叉樹刪除

else

if (y->leftchild!=null)

else

if (x!=null)

if (y->parent==null)

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

else

if (y!=deletenode)

delete y;

return;

} else }

templatevoid left_roation(binary_tree& tree,binary_tree_node* node)//左旋

else

y->parent=node->parent;

if (node->parent==null)

else

else

}y->leftchild=node;

node->parent=y;

} return;

}templatevoid right_roation(binary_tree& tree,binary_tree_node* node)//右旋

else

y->parent=node->parent;

if (node->parent==null)

else

else

}y->rightchild=node;

node->parent=y;

} return;

}#include int * randintgernate(int num,bool output=true)//生成隨機測試陣列

int * rt=new int[num];

for (int i=0;itypedef paircount_pair;

templatevoid count_path_color(binary_tree_node* node,count_pair pathcount=count_pair(0,0))

//列印紅黑樹的節點分布情況,檢測紅黑樹是否正確

else

count_path_color(node->leftchild,pathcount);

count_path_color(node->rightchild,pathcount);

} else

else }

void dotest(bool output=true)//測試

if (a==0)//查詢

}}}

演算法導論紅黑樹C 實現

類封裝了紅黑樹的幾個屬性,水平有限,歡迎批評指正 include header.h enum nodecolor struct rbtreenode rbtreenode const int a class rbtree rbtree rbtree rbtree rbtree void rbtree...

紅黑樹(演算法導論)

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