紅黑樹的C 實現

2021-06-09 23:58:39 字數 4871 閱讀 3803

#ifndef rbtree_h

#define rbtree_h

#include using namespace std;

#define red 0

#define black 1

template< typename t>

class treenode

treenode(t value) // constructor

};template < typename t >

class rbtree

;template rbtree::rbtree()

template < typename t >

rbtree::rbtree(t values,int arraysize)

}template< typename t >

void rbtree::leftrotate(treenode* target)

if (target->parent == this->treetail)else if(target->parent->left == target)else

rightchild->left = target;

target->parent = rightchild;

}template< typename t >

void rbtree::rightrotate(treenode* target)

if (target->parent == this->treetail)else if(target->parent->left == target)else

leftchild->right = target;

target->parent = leftchild; }}

template int rbtree::insert(t value)

template int rbtree::insert(treenode* targetparent,treenode* target,t value)

else

else if (value > current->value)

else

else if(value > former->value)

this->insertfixup(newnode);

} this->treesize++;

return 0;

}template void rbtree::insertfixup(treenode*target)

else

target->parent->color= black;

target->parent->parent->color= red;

this->rightrotate(target->parent->parent);

break;}}

else if(target->parent == target->parent->parent->right)else

target->parent->color= black;

target->parent->parent->color= red;

this->leftrotate(target->parent->parent);

break;}}

} this->treeroot->color = black;

}template treenode* rbtree::search(t searchvalue)

else if(current->value > searchvalue)else

} if (find == 1)else

}template int rbtree::deletenode(t value)

this->deletenode(delnode);

cout << "node "<< value <<" has been deleted."<< endl;

return 0;

}template int rbtree::deletenode(treenode*delnode)else if(delnode->left != this->treetail)else if(delnode->right != this->treetail)

treenode*deletetargetchild = this->treetail;

if (deletetarget->left != this->treetail)else if (deletetarget->right != this->treetail)

deletetargetchild->parent = deletetarget->parent;

if (deletetarget->parent == this->treetail)else if ( deletetarget->parent->left == deletetarget)else

if (deletetarget != delnode)

//delete_fixup

if(deletetarget->color == black)else

} this->treesize--;

return 0;

}template void rbtree::deletenodefixup(treenode*fixnode)else if(brother->left->color == black && brother->right->color == black)else

fixnode->parent->color = black;

brother->color = red;

brother->right->color = black;

this->leftrotate(fixnode->parent);

break;}}

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

fixnode->parent->color = black;

brother->color = red;

brother->left->color = black;

this->rightrotate(fixnode->parent);

break;}}

} fixnode->color = black;

}template int rbtree::successor(t value)

treenode*successornode = this->successor(position);

if ( successornode != this->treetail)

cout << value << " \'s successor is:" << successornode->value << endl;

else

cout << value << " has no successor" << endl;

return 0;

}template treenode* rbtree::successor(treenode*target)

treenode* parentnode =target->parent;

while ( parentnode != this->treetail && parentnode->right == target)

return parentnode;

}template int rbtree::predecessor(t value)

template treenode* rbtree::predecessor(treenode*target)

treenode* parentnode =target->parent;

while ( parentnode != this->treetail && parentnode->left == target)

return parentnode;

}template int rbtree::maxvalue()

template treenode* rbtree::maxvalue(treenode*target)

return target;

}template int rbtree::minvalue()

template treenode* rbtree::minvalue(treenode*target)

return target;

}template int rbtree::getheight()

template int rbtree::getheight(treenode*target)

else

}template int rbtree::getsize(t value)

template int rbtree::getsize(treenode*target)

if (target->left == this->treetail && target->left == this->treetail)else

}template void rbtree::inorder()

template void rbtree::inorder(treenode*target)

template void rbtree::inordernorrec()

template void rbtree::inordernorrec(treenode*target)

else

}}template void rbtree::output()

template void rbtree::output(treenode*target,int totalspaces)

else

}else

output(target->left,totalspaces+4);

}};#endif

紅黑樹(c實現)

學習紅黑樹原理,網路上有很多文章。學習紅黑樹的具體實現,個人推薦去看jdk中的treemap原始碼。因為該原始碼很簡潔,並且很容易改為其它語言的實現,最重要的是該份實現得到世人的認可,可以保證是沒問題的 下面是我根據其實現,使用c語言改寫的紅黑樹實現,目前只有紅黑樹的插入實現。include inc...

紅黑樹C 實現

完整實現了一遍紅黑樹,程式就當作自己的筆記好了 紅黑樹,顧名思義,就是紅黑相間的樹,它借由紅黑規則實現了二叉排序樹的平衡。紅黑規則如下 1.每個節點不是紅就是黑 2.根總是黑色 3.若節點為紅色,它的子節點必須為黑色 4.從根到葉的每條路徑,必須包含相同數目的黑色節點 插入和刪除節點時都要遵循紅黑規...

紅黑樹下 紅黑樹的實現

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