紅黑樹基本操作實現

2021-07-31 14:11:22 字數 4036 閱讀 7949

紅黑樹的插入,刪除簡單實現

主要理解的難點在刪除的實現,當刪除節點存在左右子節點時,需選擇右側後續節點,然後替換兩個節點,當被替換節點是葉節點時,需要重新平衡該樹,此時以空節點染黑代替進行平衡操作,再做各種變換,與書上的圖操作相似, 詳見「[balance]」

#ifndef crbtree_h

#define crbtree_h

#include using namespace std;

enum nodecolor;

template struct rbnode

;template rbnode*__initnode(const t& obj)

template class crbtree

~crbtree()

bool empty()

void insert(const value_type &obj)

else}}

void erase(const value_type &obj)

if (_pnode == _erasenode)

else

else

if (headnode == _erasenode)

headnode = _pnode;

else if (_erasenode->parentnode->leftnode == _erasenode)

_erasenode->parentnode->leftnode = _pnode;

else

_erasenode->parentnode->rightnode = _pnode;

_pnode->parentnode = _erasenode->parentnode;

//swap color

nodecolor color = _pnode->color;

_pnode->color = _erasenode->color;

_erasenode->color = color;

}if (_erasenode->color == colorblack)

if ((_unclenode->leftnode == null || _unclenode->leftnode->color == colorblack) &&

(_unclenode->rightnode == null || _unclenode->rightnode->color == colorblack))

else

_unclenode->color = _fixnodeparent->color;

_fixnodeparent->color = colorblack;

if (_unclenode->rightnode != null) _unclenode->rightnode->color = colorblack;

_leftrotate(_fixnodeparent);

break;}}

else

if ((_unclenode->rightnode == null ||_unclenode->rightnode->color == colorblack) &&

(_unclenode->leftnode == null || _unclenode->leftnode->color == colorblack))

else

_unclenode->color = _fixnodeparent->color;

_fixnodeparent->color = colorblack;

if (_unclenode->leftnode) _unclenode->leftnode->color = colorblack;

_rightrotate(_fixnodeparent);

break;}}

}if (_fixnode) _fixnode->color = colorblack;}}

delete _erasenode;}}

class iter

iter operator ++()

}else if (_node->parentnode == null)

else if (_node == _node->parentnode->leftnode)

else if (_node == _node->parentnode->rightnode)

else

}return *this;

}bool operator == (const iter& other)

bool operator != (const iter& other)

nodetype* operator -> () const

nodetype* data() const

private:

nodetype *_node;

};iter begin()

}return iter(_tnode);

}iter end()

void clear()

private:

nodetype *headnode;

void _leftrotate(nodetype *nodea)

nodea->parentnode = nodeb;

}void _rightrotate(nodetype *nodea)

nodea->parentnode = nodeb;

}void _insertnode(nodetype *node)

else

parent = parent->leftnode;

}else

else

parent = parent->rightnode;

}}while(1);

}nodetype* _findnode(nodetype *pnode,const value_type& obj)

else if (_node->obj < obj)

else

}while(1);

}void _recolor(nodetype *node)

else if (_parent->color == colorblack)

else

else

node->parentnode->color = colorblack;

node->parentnode->parentnode->color = colorred;

_rightrotate(node->parentnode->parentnode);}}

else

else

node->parentnode->color = colorblack;

node->parentnode->parentnode->color = colorred;

_leftrotate(node->parentnode->parentnode);}}

}}

void _erase(nodetype *node)

}};#endif // crbtree_h

測試**:

#include #include "rbtree.h"

using namespace std;

void printtree(crbtree& rbtree)

if (blackdepth == 0)

blackdepth = branchdepth;

else if (blackdepth != branchdepth)

cout << "rbtree is not balance now!\n";}}

for ( crbtree::iter it = rbtree.begin(); it != rbtree.end(); ++it)

cout << endl;

}int main()

int arrint = ;

for (unsigned int i=sizeof(arrint)/sizeof(arrint[0]); i>0; i--)

rbtree.clear();

return 0;

}

紅黑樹實現

紅黑樹 是一棵二叉搜尋樹,它在每個節點上增加了乙個儲存位來表示節點的顏色,可以是red或black。通過對任何一條從根到葉子簡單路徑上的 顏色來約束,紅黑樹保證最長路徑不超過最短路徑的兩倍,因而近似於平衡 由於性質中紅色節點不連續,最短路徑可為全是連續黑結點,最長路徑則由於紅節點不連續,則每間隔乙個...

紅黑樹實現

按演算法導論裡的偽 用c 實現了紅黑樹。實現過程中,參考了nginx的紅黑樹實現,特別是右旋的實現,演算法導論裡沒有給出,就借鑑了nginx的實現。對比了nginx的紅黑樹實現和演算法導論裡的偽 插入基本上一樣的,刪除兩者有點差別,但思路應該是一樣的。實現過程中需要注意的是,空葉子結點一定要實現,不...

紅黑樹下 紅黑樹的實現

1.實現紅黑樹的基本思想 實際上,紅黑樹是有固定的平衡過程的 遇到什麼樣的節點分布,我們就對應怎麼去調整。只要按照這些固定的調整規則來操作,就能將乙個非平衡的紅黑樹調整成平衡的。首先,我們需要再來看一下紅黑樹的定義 在插入 刪除節點的過程中,第 三 四點要求可能會被破壞,所以 平衡調整 實際上就是把...