设为首页 加入收藏

TOP

红黑树的介绍和实现(二)
2014-11-23 21:46:03 来源: 作者: 【 】 浏览:11
Tags:介绍 实现

//file RBTree.h

//written by saturnman

#ifndef _RB_TREE_H_

#define _RB_TREE_H_

#include

#include

#include

#include

using namespace std;

template

class RB_Tree

{

private:

RB_Tree(const RB_Tree& input){}

const RB_Tree& operator=(const RB_Tree& input){}

private:

enum COLOR{RED,BLACK};

class RB_Node

{

public:

RB_Node()

{

RB_COLOR = BLACK;

right = NULL;

left = NULL;

parent = NULL;

}

COLOR RB_COLOR;

RB_Node* right;

RB_Node* left;

RB_Node* parent;

KEY key;

U data;

};

public:

RB_Tree()

{

this->m_nullNode = new RB_Node();

this->m_root = m_nullNode;

this->m_nullNode->right = this->m_root;

this->m_nullNode->left = this->m_root;

this->m_nullNode->parent = this->m_root;

this->m_nullNode->RB_COLOR = BLACK;

}

bool Empty()

{

if(this->m_root == this->m_nullNode)

{

return true;

}

else

{

return false;

}

}

//find node whos key equals to key,else find the insert point;

RB_Node* find(KEY key)

{

RB_Node* index = m_root;

while(index != m_nullNode)

{

if(keykey)

{

index = index->left;

}

else if(key>index->key)

{

index = index->right;

}

else

{

break;

}

}

return index;

}

bool Insert(KEY key,U data)

{

RB_Node* insert_point = m_nullNode;

RB_Node* index = m_root;

while(index!=m_nullNode)

{

insert_point = index;

if(keykey)

{

index = index->left;

}

else if(key>index->key)

{

index = index->right;

}

else

&

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇示例,红黑树插入和删除过程 下一篇红黑树算法的层层剖析与逐步实现

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: