因為紅黑樹首先是二叉所搜樹,所以有了二叉搜尋樹的實現,我們就可以重用部分二叉搜尋樹的類的介面了,rbt.h:
#ifndef __rbt_h__
#define __rbt_h__
#include "bst.h"
template class credblacktree : public cbinarysorttree
virtual cbinarytreenode* insert(const t& key);
virtual void delete(cbinarytreenode** del);
virtual void inorderwalk() const;
private:
void insertfixup(cbinarytreenode* node);
void deletefixup(cbinarytreenode* node);
void leftrotate(cbinarytreenode* node);
void rightrotate(cbinarytreenode* node);
void inorderwalk(const cbinarytreenode* node) const;
};template credblacktree::credblacktree() : cbinarysorttree()
template cbinarytreenode* credblacktree::insert(const t& key)
template void credblacktree::delete(cbinarytreenode** del)
else
fixup->parent = parent; // 'cause sentinel's parent points to itself
} else
temp = node;
} else if (node->left == &cbinarysorttree::sentinel && node->right != &cbinarysorttree::sentinel) else if (node->left != &cbinarysorttree::sentinel && node->right == &cbinarysorttree::sentinel) else else
fixup->parent = successor; // 'cause sentinel's parent points to itself
temp = this->transplant(node, successor);
successor->left = node->left;
successor->left->parent = successor;
successor->color = (*del)->color; // remain the original color
} if (original_color == cbinarytreenode::black && this->root != &cbinarysorttree::sentinel)
deletefixup(fixup);
delete temp;
}template void credblacktree::inorderwalk() const
template void credblacktree::inorderwalk(const cbinarytreenode* node) const
}template void credblacktree::insertfixup(cbinarytreenode* node)
else
node->parent->color = cbinarytreenode::black;
node->parent->parent->color = cbinarytreenode::red;
rightrotate(node->parent->parent);
}} else else
node->parent->color = cbinarytreenode::black;
node->parent->parent->color = cbinarytreenode::red;
leftrotate(node->parent->parent);
}} }
this->root->color = cbinarytreenode::black;
}template void credblacktree::deletefixup(cbinarytreenode* node)
if (w->left->color == cbinarytreenode::black && w->right->color == cbinarytreenode::black) else
w->color = node->parent->color;
w->right->color = cbinarytreenode::black;
node->parent->color = cbinarytreenode::black;
leftrotate(node->parent);
node = this->root;
}} else
if (w->left->color == cbinarytreenode::black && w->right->color == cbinarytreenode::black) else
w->color = node->parent->color;
w->left->color = cbinarytreenode::black;
node->parent->color = cbinarytreenode::black;
rightrotate(node->parent);
node = this->root;
}} }
node->color = cbinarytreenode::black;
}template void credblacktree::leftrotate(cbinarytreenode* node)
template void credblacktree::rightrotate(cbinarytreenode* node)
#endif
這裡簡要說明一下,紅黑樹在二叉搜尋樹的插入和刪除操作基礎上還有修正操作,而對insert的修正操作比較簡單,只需要操作被插入的結點,所以直接重用了二叉搜尋樹的insert,而對delete的修正操作就比較複雜些,所以這裡重新實現了delete操作,其實邏輯跟二叉搜尋樹的刪除操作大體上相同。
最後,測試紅黑樹的**,rbt_test.cpp:
#include "rbt.h"
int main(int argc, const char **argv)
; credblacktreerbt;
cout << "original key: " << endl;
for (size_t i = 0; i < sizeof(key) / sizeof(int); ++i)
cout << key[i] << ' ';
cout << endl;
for (size_t i = 0; i < sizeof(key) / sizeof(int); ++i)
cout << endl;
for (size_t i = 0; i < sizeof(key) / sizeof(int); ++i)
} return 0;
}
同樣,驗證時使用到如下**的,在此表示感謝:
附:c++的一大特性是多型,由於二叉搜尋樹類是紅黑樹類的父類,所以,我們可以使用二叉搜尋樹類的指標或者引用在執行時動態繫結紅黑樹的成員函式,實現多型,rbt_test.cpp:
#include "rbt.h"
using std::runtime_error;
int main(int argc, const char **argv)
; cbinarysorttree* pbst = null;
try
cout << endl;
for (size_t i = 0; i < sizeof(key) / sizeof(int); ++i)
} delete pbst;
} catch (...)
pbst = null;
return 0;
}
紅黑樹(c實現)
學習紅黑樹原理,網路上有很多文章。學習紅黑樹的具體實現,個人推薦去看jdk中的treemap原始碼。因為該原始碼很簡潔,並且很容易改為其它語言的實現,最重要的是該份實現得到世人的認可,可以保證是沒問題的 下面是我根據其實現,使用c語言改寫的紅黑樹實現,目前只有紅黑樹的插入實現。include inc...
紅黑樹C 實現
完整實現了一遍紅黑樹,程式就當作自己的筆記好了 紅黑樹,顧名思義,就是紅黑相間的樹,它借由紅黑規則實現了二叉排序樹的平衡。紅黑規則如下 1.每個節點不是紅就是黑 2.根總是黑色 3.若節點為紅色,它的子節點必須為黑色 4.從根到葉的每條路徑,必須包含相同數目的黑色節點 插入和刪除節點時都要遵循紅黑規...
紅黑樹的C 實現
ifndef rbtree h define rbtree h include using namespace std define red 0 define black 1 template typename t class treenode treenode t value constructo...