根据算法导论实现的红黑树

根据算法导论实现了一下红黑树,红黑树结合哈希表查询速度还是很快的,只是实现理论未考虑效率问题如下源码

typedef struct _StRBItem{
#define RBNodeTy_Red   1
#define RBNodeTy_Black 0
_StRBItem * ptrLeftChild;
_StRBItem * ptrRightChild;
_StRBItem * ptrParent;
int iNodeTy;
int iKey;
char pData[255];
int iDSize;
}RBItem;
class RedBlackTree
{
public:
RedBlackTree(void);
~RedBlackTree(void);
bool InsertNode(int iKey,char * ptrData,int iDSize);
bool DelNode(int ikey);
RBItem * GetRootNode();
protected:
void Release();
void DeleteTree(RBItem * ptrNode);
void adjustNode(RBItem * ptrNode);
void adjustNode_Del(RBItem * ptrNode_in,RBItem * ptrParentNode_in);
public:
RBItem * m_pRoot;


};



RedBlackTree::RedBlackTree(void)
{
m_pRoot = NULL;
}


RedBlackTree::~RedBlackTree(void)
{
Release();
}
bool RedBlackTree::InsertNode(int iKey,char * ptrData,int iDSize){
RBItem * ptrNode;
RBItem * ptrINode;
ptrNode = (RBItem *)malloc(sizeof(RBItem));
if(ptrNode == NULL)
return false;
memset(ptrNode,0x0,sizeof(RBItem));
ptrNode->iNodeTy = RBNodeTy_Red;
ptrNode->iDSize =iDSize;
if(iDSize>0 && ptrData!=NULL){
memcpy(ptrNode->pData,ptrData,iDSize);
}
ptrNode->iKey = iKey;
if(m_pRoot!=NULL){
ptrINode = m_pRoot;
while(ptrINode!=NULL){
if(ptrINode->iKey < iKey){
if(ptrINode->ptrRightChild == NULL){
ptrINode->ptrRightChild = ptrNode;
ptrNode->ptrParent = ptrINode;
ptrNode->ptrLeftChild = NULL;
ptrNode->ptrRightChild = NULL;
break;
}
ptrINode = ptrINode->ptrRightChild;
}else{
if(ptrINode->ptrLeftChild == NULL){
ptrINode->ptrLeftChild = ptrNode;
ptrNode->ptrParent = ptrINode;
ptrNode->ptrLeftChild = NULL;
ptrNode->ptrRightChild = NULL;
break;
}
ptrINode = ptrINode->ptrLeftChild;
}
}


}else{
m_pRoot = ptrNode;
m_pRoot->ptrParent = NULL;
}


adjustNode(ptrNode);
return true;
}
void RedBlackTree::adjustNode(RBItem * ptrNode){
RBItem * ptrParent;
RBItem * ptrUpParent;
RBItem * ptrBrParent;
RBItem * ptrSwap;


while((ptrNode->ptrParent != NULL) && (ptrNode->ptrParent->iNodeTy == RBNodeTy_Red) && (ptrNode->iNodeTy == RBNodeTy_Red)){
ptrParent = ptrNode->ptrParent;
ptrUpParent = ptrParent->ptrParent;
if(ptrUpParent->ptrLeftChild == ptrParent){


if(ptrUpParent->ptrRightChild!=NULL && ptrUpParent->ptrRightChild->iNodeTy == RBNodeTy_Red){
ptrUpParent->ptrRightChild->iNodeTy = RBNodeTy_Black;
ptrParent->iNodeTy = RBNodeTy_Black;
ptrNode = ptrUpParent;
ptrNode->iNodeTy = RBNodeTy_Red;
}else{
if(ptrNode == ptrParent->ptrLeftChild){
ptrUpParent->iNodeTy = RBNodeTy_Red;
ptrUpParent->ptrLeftChild = ptrParent->ptrRightChild;
if(ptrUpParent->ptrLeftChild!=NULL)
ptrUpParent->ptrLeftChild->ptrParent = ptrUpParent;
ptrParent->ptrParent = ptrUpParent->ptrParent;
if(ptrUpParent->ptrParent!=NULL){
if(ptrUpParent->ptrParent->ptrLeftChild == ptrUpParent){
ptrUpParent->ptrParent->ptrLeftChild = ptrParent;
}else{
ptrUpParent->ptrParent->ptrRightChild = ptrParent;
}
}
ptrUpParent->ptrParent = ptrParent;
ptrParent->ptrRightChild = ptrUpParent;
ptrParent->iNodeTy = RBNodeTy_Black;
}else{
ptrUpParent->iNodeTy = RBNodeTy_Red;
ptrNode->ptrParent = ptrUpParent->ptrParent;
if(ptrUpParent->ptrParent!=NULL){
if(ptrUpParent->ptrParent->ptrLeftChild==ptrUpParent){
ptrUpParent->ptrParent->ptrLeftChild = ptrNode;
}else{
ptrUpParent->ptrParent->ptrRightChild = ptrNode;
}
}
ptrUpParent->ptrLeftChild = ptrNode->ptrRightChild;
if(ptrUpParent->ptrLeftChild!=NULL)
ptrUpParent->ptrLeftChild->ptrParent = ptrUpParent;
ptrUpParent->ptrParent = ptrNode;
ptrNode->iNodeTy = RBNodeTy_Black;
ptrNode->ptrRightChild = ptrUpParent;
ptrParent->ptrRightChild = ptrNode->ptrLeftChild;
if(ptrParent->ptrRightChild!=NULL)
ptrParent->ptrRightChild->ptrParent = ptrParent;
ptrNode->ptrLeftChild = ptrParent;
ptrParent->ptrParent = ptrNode;
}
}
}else{
if(ptrUpParent->ptrLeftChild!=NULL && ptrUpParent->ptrLeftChild->iNodeTy == RBNodeTy_Red){
ptrUpParent->ptrLeftChild->iNodeTy = RBNodeTy_Black;
ptrParent->iNodeTy = RBNodeTy_Black;
ptrNode = ptrUpParent;
ptrNode->iNodeTy = RBNodeTy_Red;
}else{
if(ptrNode == ptrParent->ptrLeftChild){
ptrUpParent->iNodeTy = RBNodeTy_Red;
ptrNode->ptrParent = ptrUpParent->ptrParent;
if(ptrUpParent->ptrParent!=NULL){
if(ptrUpParent->ptrParent->ptrLeftChild==ptrUpParent){
ptrUpParent->ptrParent->ptrLeftChild = ptrNode;
}else{
ptrUpParent->ptrParent->ptrRightChild = ptrNode;
}
}
ptrParent->ptrLeftChild = ptrNode->ptrRightChild;
if(ptrParent->ptrLeftChild!=NULL)
ptrParent->ptrLeftChild->ptrParent = ptrParent;
ptrNode->ptrRightChild = ptrParent;
ptrNode->ptrRightChild->ptrParent = ptrNode;
ptrNode->iNodeTy = RBNodeTy_Black;

ptrUpParent->ptrRightChild = ptrNode->ptrLeftChild;
if(ptrUpParent->ptrRightChild!=NULL)
ptrUpParent->ptrRightChild->ptrParent = ptrUpParent;
ptrUpParent->ptrParent = ptrNode;

ptrUpParent->ptrParent = ptrNode;
ptrNode->ptrLeftChild = ptrUpParent;




}else{
ptrUpParent->iNodeTy = RBNodeTy_Red;
ptrUpParent->ptrRightChild = ptrParent->ptrLeftChild;
if(ptrUpParent->ptrRightChild!=NULL)
ptrUpParent->ptrRightChild->ptrParent = ptrUpParent;
ptrParent->ptrParent = ptrUpParent->ptrParent;
if(ptrUpParent->ptrParent!=NULL){
if(ptrUpParent->ptrParent->ptrLeftChild==ptrUpParent){
ptrUpParent->ptrParent->ptrLeftChild = ptrParent;
}else{
ptrUpParent->ptrParent->ptrRightChild = ptrParent;
}
}
ptrUpParent->ptrParent = ptrParent;
ptrParent->ptrLeftChild = ptrUpParent;
ptrParent->iNodeTy = RBNodeTy_Black;
}
}
}
}
if(ptrNode->ptrParent == NULL)
m_pRoot = ptrNode;
if(ptrNode==m_pRoot){
ptrNode->iNodeTy = RBNodeTy_Black;
}


}
bool RedBlackTree::DelNode(int ikey){
RBItem * ptrNode;
RBItem * ptrINode;
RBItem * ptrSeachNode;
RBItem * ptrChildNode;
int      iNodeTy;
if(m_pRoot == NULL){
return false;
}

ptrSeachNode = m_pRoot;
ptrINode =NULL;
while(ptrSeachNode!=NULL){
if(ptrSeachNode->iKey == ikey){
ptrINode = ptrSeachNode;
break;
}
ptrSeachNode = ptrSeachNode->iKey > ikey ? ptrSeachNode->ptrLeftChild : ptrSeachNode->ptrRightChild;
}


if(ptrINode == NULL)
return false;
if(ptrINode->ptrLeftChild!=NULL){
ptrNode = ptrINode->ptrLeftChild;
while(ptrNode->ptrRightChild!=NULL){
ptrNode = ptrNode->ptrRightChild;
}
}else if (ptrINode->ptrRightChild!=NULL){
ptrNode = ptrINode->ptrRightChild;
while(ptrNode->ptrLeftChild!=NULL){
ptrNode = ptrNode->ptrLeftChild;
}
}else{
ptrNode = ptrINode;
}


if(ptrNode != ptrINode){
ptrINode->iDSize = ptrNode->iDSize;
::memcpy(ptrINode->pData,ptrNode->pData,255); 
ptrINode->iKey = ptrNode->iKey;
}
if(ptrNode==m_pRoot){
m_pRoot = NULL;
::free((void *)ptrNode);
}
ptrINode = ptrNode->ptrParent;
ptrChildNode = ptrNode->ptrLeftChild!=NULL ?  ptrNode->ptrLeftChild : ptrNode->ptrRightChild;



if(ptrINode->ptrLeftChild==ptrNode){
ptrINode->ptrLeftChild = ptrChildNode;
}else{
ptrINode->ptrRightChild = ptrChildNode;
}
if(ptrChildNode!=NULL){
ptrChildNode->ptrParent = ptrINode;
}
iNodeTy = ptrNode->iNodeTy;
::free((void *)ptrNode);
if(iNodeTy==RBNodeTy_Red){
return true;
}
// if(ptrChildNode!=NULL){
// adjustNode_Del(ptrChildNode);
// }else{
// adjustNode_DelParent(ptrINode);
// }
adjustNode_Del(ptrChildNode,ptrINode);
return true;
}
void RedBlackTree::adjustNode_Del(RBItem * ptrNode_in,RBItem * ptrParentNode_in){
RBItem * ptrNode;
RBItem * ptrParent;
RBItem * ptrOtherNode;
RBItem * ptrLeftNode;
RBItem * ptrRigithNode;
ptrNode = ptrNode_in;
ptrParent = ptrParentNode_in;
ptrOtherNode = ptrParent->ptrLeftChild == ptrNode ? ptrNode->ptrRightChild: ptrNode->ptrLeftChild;
while(ptrNode==NULL || (ptrNode->iDSize == RBNodeTy_Black && ptrNode != m_pRoot)){
if(ptrParent->ptrLeftChild == ptrNode){
ptrOtherNode = ptrParent->ptrRightChild;
if(ptrOtherNode->iNodeTy == RBNodeTy_Red){
ptrParent->ptrParent = ptrOtherNode->ptrParent;
if(ptrParent->ptrParent!=NULL){
if(ptrParent->ptrParent->ptrLeftChild == ptrParent){
ptrParent->ptrParent->ptrLeftChild = ptrOtherNode; 
}else{
ptrParent->ptrParent->ptrRightChild = ptrOtherNode;
}
}
ptrParent->ptrRightChild = ptrOtherNode->ptrLeftChild;
ptrParent->ptrParent = ptrOtherNode;
ptrParent->ptrRightChild->ptrParent = ptrParent;
ptrParent->iNodeTy = RBNodeTy_Red;
ptrOtherNode->iNodeTy = RBNodeTy_Black;
}else{
ptrLeftNode = ptrOtherNode->ptrLeftChild;
ptrRigithNode = ptrOtherNode->ptrRightChild;
if(ptrRigithNode!=NULL && ptrRigithNode->iNodeTy == RBNodeTy_Red){
ptrOtherNode->iNodeTy = ptrParent->iNodeTy;
ptrParent->ptrRightChild = ptrLeftNode;
if(ptrLeftNode!=NULL)
ptrLeftNode->ptrRightChild = ptrParent;
ptrRigithNode->iNodeTy = RBNodeTy_Black;
ptrOtherNode->ptrParent = ptrParent->ptrParent;
if(ptrParent->ptrParent!=NULL){
if(ptrParent->ptrParent->ptrLeftChild==ptrParent){
ptrParent->ptrParent->ptrRightChild = ptrOtherNode;
}else{
ptrParent->ptrParent->ptrLeftChild = ptrOtherNode;
}
}
ptrParent->ptrParent = ptrOtherNode;
ptrOtherNode->ptrLeftChild = ptrParent;
}else if(ptrLeftNode!=NULL && ptrLeftNode->iNodeTy == RBNodeTy_Red){
ptrLeftNode->ptrParent = ptrOtherNode->ptrParent;
ptrParent->ptrRightChild = ptrLeftNode;
ptrOtherNode->ptrLeftChild = ptrLeftNode->ptrRightChild;
if(ptrLeftNode->ptrRightChild!=NULL)
ptrLeftNode->ptrRightChild->ptrParent = ptrOtherNode;
ptrLeftNode->ptrRightChild = ptrOtherNode;
ptrOtherNode->ptrParent = ptrLeftNode;
ptrOtherNode->iNodeTy = RBNodeTy_Red;
ptrLeftNode->iNodeTy = RBNodeTy_Black;
}else{
ptrOtherNode->iNodeTy = RBNodeTy_Red;
ptrNode = ptrParent;
ptrParent = ptrNode->ptrParent;
}
}
}else{
ptrOtherNode = ptrParent->ptrLeftChild;
if(ptrOtherNode->iNodeTy == RBNodeTy_Red){
ptrParent->ptrParent = ptrOtherNode->ptrParent;
if(ptrParent->ptrParent!=NULL){
if(ptrParent->ptrParent->ptrLeftChild == ptrParent){
ptrParent->ptrParent->ptrLeftChild = ptrOtherNode; 
}else{
ptrParent->ptrParent->ptrRightChild = ptrOtherNode;
}
}
ptrParent->ptrLeftChild = ptrOtherNode->ptrRightChild;
ptrParent->ptrParent = ptrOtherNode;
ptrParent->ptrLeftChild->ptrParent = ptrParent;
ptrParent->iNodeTy = RBNodeTy_Red;
ptrOtherNode->iNodeTy = RBNodeTy_Black;
}else{
ptrLeftNode = ptrOtherNode->ptrLeftChild;
ptrRigithNode = ptrOtherNode->ptrRightChild;
if(ptrLeftNode!=NULL && ptrLeftNode->iNodeTy == RBNodeTy_Red){
ptrOtherNode->ptrParent = ptrParent->ptrParent;
ptrOtherNode->iNodeTy = ptrParent->iNodeTy;
if(ptrParent->ptrParent!=NULL){
if(ptrParent->ptrParent->ptrLeftChild == ptrParent){
ptrParent->ptrParent->ptrLeftChild = ptrOtherNode;
}else{
ptrParent->ptrParent->ptrRightChild = ptrOtherNode;
}
}
ptrParent->ptrParent = ptrOtherNode;
ptrLeftNode->iNodeTy = RBNodeTy_Black;
ptrOtherNode->ptrRightChild = ptrParent;
ptrParent->ptrLeftChild = ptrRigithNode;
if(ptrRigithNode!=NULL)
ptrRigithNode->ptrParent = ptrParent;
}else if(ptrRigithNode!=NULL && ptrRigithNode->iNodeTy == RBNodeTy_Red){
ptrRigithNode->ptrParent = ptrOtherNode->ptrParent;
ptrParent->ptrLeftChild = ptrRigithNode;
ptrOtherNode->ptrRightChild = ptrRigithNode->ptrLeftChild;
if(ptrOtherNode->ptrRightChild!=NULL)
ptrRigithNode->ptrLeftChild->ptrParent = ptrOtherNode;
ptrOtherNode->ptrParent = ptrRigithNode;
ptrRigithNode->ptrLeftChild = ptrOtherNode;
ptrRigithNode->iNodeTy = RBNodeTy_Black;
ptrOtherNode->iNodeTy = RBNodeTy_Red;
}else{
ptrOtherNode->iNodeTy = RBNodeTy_Red;
ptrNode = ptrParent;
ptrParent = ptrNode->ptrParent;
}
}


}
}
if(ptrNode!=NULL && ptrNode->iNodeTy == RBNodeTy_Red)
ptrNode->iNodeTy = RBNodeTy_Black;


}
void RedBlackTree::Release(){
RBItem * ptrItem;
if(m_pRoot!=NULL){
DeleteTree(m_pRoot);
m_pRoot = NULL;
}
}
void RedBlackTree::DeleteTree(RBItem * ptrNode){
if(ptrNode->ptrLeftChild!=NULL){
DeleteTree(ptrNode->ptrLeftChild);
ptrNode->ptrLeftChild = NULL;
}else if(ptrNode->ptrRightChild!=NULL){
DeleteTree(ptrNode->ptrRightChild);
ptrNode->ptrRightChild = NULL;
}
free(ptrNode);

}
RBItem * RedBlackTree::GetRootNode(){
return m_pRoot;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值