[原创]红黑树源码...
写了个公共的平衡树接口....然后写了个红黑树.....功能不多,只能完成字典操作.....顺便试一下飞燕的高亮新版...不好请多包涵......
PS: 写得好累啊~~~~~
下面是平衡树接口:
/*****************************************************************
** HighlightCodeV3.0 software by yzfy(雨中飞燕) [url]http://[/url] **
*****************************************************************/
enum COLOR { RED,BLACK };
template<typename Ty>
struct node
{
int key;
Ty data;
node<Ty>* leftchild;
node<Ty>* rightchild;
node<Ty>* parent;
COLOR color;
node(const Ty& v,int k):data(v)
{
key = k;
color = RED;
leftchild = rightchild = parent =0;
}
node(){ color = BLACK; }
} ;
template<typename _value>
class BBST
{
public:
virtual ~BBST() = 0;
virtual void Insert(const _value& value,int key)=0;
virtual const node<_value>* Delete(int key) =0;
virtual const node<_value>* Search(int key) const;
bool empty() const ;
int size() const ;
int height() const ;
void InOrder() const ;
void PostOrder() const ;
void PreOrder() const ;
void LevelOrder() const ;
protected:
node<_value>* root;
node<_value>* null;
int countNode;
protected:
virtual void LeftRotate(node<_value>* parent,node<_value>* child) =0;
virtual void RightRotate(node<_value>* parent,node<_value>* child) =0;
node<_value>* successor(node<_value>* current) const;
node<_value>* predecessor(node<_value>* current) const;
node<_value>* search(int skey) const;
private:
int height(const node<_value>* root) const;
void inorder(const node<_value>* root) const ;
void inorder(node<_value>& root) const ;
void postorder(const node<_value>* root) const ;
void postorder(node<_value>& root) const ;
void preorder(const node<_value>* root) const ;
void preorder(node<_value>& root) const ;
node<_value>* Minimum(node<_value>* current) const;
node<_value>* Maximum(node<_value>* current) const;
};
template<typename _Ty>
inline BBST<_Ty>::~BBST() {}
// const node<_Ty>* Search(int key) const
template<typename _Ty>
inline const node<_Ty>* BBST<_Ty>::Search(int skey) const
{
return search(skey);
}
template<typename _Ty>
node<_Ty>* BBST<_Ty>::search(int skey) const
{
for(node<_Ty>* current=root;current!=null; )
{
if(current->key == skey) return current;
if(current->key < skey) current = current->rightchild;
else current = current->leftchild;
}
return null;
}
// Minimum and Maximum
template<typename _Ty>
node<_Ty>* BBST<_Ty>::Minimum(node<_Ty>* _node) const
{
for(node<_Ty>* current=_node;current!=null;)
{
if( current->leftchild!=null) current = current->leftchild;
else return current;
}
return null;
}
template<typename _Ty>
node<_Ty>* BBST<_Ty>::Maximum(node<_Ty>* _node) const
{
for(node<_Ty>* current=_node;current!=null;)
{
if( current->rightchild!=null) current = current->rightchild;
else return current;
}
return null;
}
// successor and predecessor
template<typename _Ty>
node<_Ty>* BBST<_Ty>::successor(node<_Ty>* current) const
{
if( current->rightchild!=null) return Minimum(current->rightchild);
for(node<_Ty>* __node=current->parent;__node!=null;)
{
if(__node->leftchild == current)
{
current = __node;
__node = null;
}
else
{
current = __node;
__node = __node->parent;
}
}
return current;
}
template<typename _Ty>
node<_Ty>* BBST<_Ty>::predecessor(node<_Ty>* current) const
{
if( current->leftchild!=null) return Maximum(current->leftchild);
for(node<_Ty>* __node=current->parent;__node!=null;)
{
if(__node->rightchild == current)
{
current = __node;
__node = null;
}
else
{
current = __node;
__node = __node->parent;
}
}
return current;
}
// bool empty() const
template<typename _Ty>
inline bool BBST<_Ty>::empty() const
{
return root == null;
}
// int size() const
template<typename _Ty>
inline int BBST<_Ty>::size() const
{
return countNode;
}
// int height() const
template<typename _Ty>
inline int BBST<_Ty>::height() const
{
return height(root);
}
// int height(const node<_Ty>* root) const
template<typename _Ty>
int BBST<_Ty>::height(const node<_Ty>* root) const
{
if( root==null ) return 0;
else return height(root->leftchild)>height(root->rightchild)?
height(root->leftchild)+1:height(root->rightchild)+1;
}
// void InOrder() const
template<typename _Ty>
void BBST<_Ty>::InOrder() const
{
// srand((unsigned int)time(0));
inorder(*root);
}
template<typename _Ty >
void BBST<_Ty>::inorder(const node<_Ty>* root) const
{
if( root!=null )
{
inorder(root->leftchild);
std::cout<<current->data<<','
<<current->color<<" ";
inorder(root->rightchild);
}
}
template<typename _Ty>
void BBST<_Ty>::inorder( node<_Ty> & root) const
{
using std::deque;
deque<node<_Ty>*> que;
for(node<_Ty>* current=&root; current!=null|| !que.empty(); )
{
for(;current!=null ; )
{
que.push_front(current);
current = current->leftchild;
}
current = que.front();
que.pop_front();
std::cout<<current->data<<','
<<current->color<<" ";
current = current->rightchild;
}
}
// void PostOrder() const
template<typename _Ty>
void BBST<_Ty>::PostOrder() const
{
// srand((unsigned int)time(0));
postorder(*root);
}
template<typename _Ty>
void BBST<_Ty>::postorder(const node<_Ty>* root) const
{
if( root!=null )
{
postorder(root->leftchild);
postorder(root->rightchild);
std::cout<<current->data<<','
<<current->color<<" ";
}
}
template<typename _Ty>
void BBST<_Ty>::postorder( node<_Ty>& root) const
{
using std::deque;
deque<node<_Ty>* > que;
for(node<_Ty>* current=&root,*visited=0;
current!=null || !que.empty(); )
{
for(; current!=null; )
{
que.push_front(current);
current = current->leftchild;
}
current = que.front();
que.pop_front();
if(current->rightchild!=null&&visited!=current->rightchild)
{
que.push_front(current);
current = current->rightchild;
}
else
{
visited = current;
std::cout<<current->data<<','
<<current->color<<" ";
current = null;
}
}
}
// void PreOrder() const
template<typename _Ty>
void BBST<_Ty>::PreOrder() const
{
// srand((unsigned int) time(0));
preorder(*root);
}
template<typename _Ty>
void BBST<_Ty>::preorder(const node<_Ty>* root) const
{
if( root!=null )
{
std::cout<<current->data<<','
<<current->color<<" ";
preorder(root->leftchild);
preorder(root->rightchild);
}
}
template<typename _Ty>
void BBST<_Ty>::preorder( node<_Ty>& root) const
{
using std::deque;
deque<node<_Ty>*> que;
for(node<_Ty>* current=&root;current!=null || !que.empty();)
{
for( ; current!=null; )
{
std::cout<<current->data<<','
<<current->color<<" ";
que.push_front(current);
current = current->leftchild;
}
current = que.front();
que.pop_front();
current = current->rightchild;
}
}
// void LevelOrder() const
template<typename _Ty>
void BBST<_Ty>::LevelOrder() const
{
using std::deque;
deque<node<_Ty>*> que;
for(node<_Ty>* current=root;current!=null||!que.empty();)
{
std::cout<<current->data<<','
<<current->color<<" ";
if(current->leftchild!=null) que.push_front(current->leftchild);
if(current->rightchild!=null) que.push_front(current->rightchild);
if(!que.empty())
{
current = que.back();
que.pop_back();
}
else current = null;
}
}
** HighlightCodeV3.0 software by yzfy(雨中飞燕) [url]http://[/url] **
*****************************************************************/
enum COLOR { RED,BLACK };
template<typename Ty>
struct node
{
int key;
Ty data;
node<Ty>* leftchild;
node<Ty>* rightchild;
node<Ty>* parent;
COLOR color;
node(const Ty& v,int k):data(v)
{
key = k;
color = RED;
leftchild = rightchild = parent =0;
}
node(){ color = BLACK; }
} ;
template<typename _value>
class BBST
{
public:
virtual ~BBST() = 0;
virtual void Insert(const _value& value,int key)=0;
virtual const node<_value>* Delete(int key) =0;
virtual const node<_value>* Search(int key) const;
bool empty() const ;
int size() const ;
int height() const ;
void InOrder() const ;
void PostOrder() const ;
void PreOrder() const ;
void LevelOrder() const ;
protected:
node<_value>* root;
node<_value>* null;
int countNode;
protected:
virtual void LeftRotate(node<_value>* parent,node<_value>* child) =0;
virtual void RightRotate(node<_value>* parent,node<_value>* child) =0;
node<_value>* successor(node<_value>* current) const;
node<_value>* predecessor(node<_value>* current) const;
node<_value>* search(int skey) const;
private:
int height(const node<_value>* root) const;
void inorder(const node<_value>* root) const ;
void inorder(node<_value>& root) const ;
void postorder(const node<_value>* root) const ;
void postorder(node<_value>& root) const ;
void preorder(const node<_value>* root) const ;
void preorder(node<_value>& root) const ;
node<_value>* Minimum(node<_value>* current) const;
node<_value>* Maximum(node<_value>* current) const;
};
template<typename _Ty>
inline BBST<_Ty>::~BBST() {}
// const node<_Ty>* Search(int key) const
template<typename _Ty>
inline const node<_Ty>* BBST<_Ty>::Search(int skey) const
{
return search(skey);
}
template<typename _Ty>
node<_Ty>* BBST<_Ty>::search(int skey) const
{
for(node<_Ty>* current=root;current!=null; )
{
if(current->key == skey) return current;
if(current->key < skey) current = current->rightchild;
else current = current->leftchild;
}
return null;
}
// Minimum and Maximum
template<typename _Ty>
node<_Ty>* BBST<_Ty>::Minimum(node<_Ty>* _node) const
{
for(node<_Ty>* current=_node;current!=null;)
{
if( current->leftchild!=null) current = current->leftchild;
else return current;
}
return null;
}
template<typename _Ty>
node<_Ty>* BBST<_Ty>::Maximum(node<_Ty>* _node) const
{
for(node<_Ty>* current=_node;current!=null;)
{
if( current->rightchild!=null) current = current->rightchild;
else return current;
}
return null;
}
// successor and predecessor
template<typename _Ty>
node<_Ty>* BBST<_Ty>::successor(node<_Ty>* current) const
{
if( current->rightchild!=null) return Minimum(current->rightchild);
for(node<_Ty>* __node=current->parent;__node!=null;)
{
if(__node->leftchild == current)
{
current = __node;
__node = null;
}
else
{
current = __node;
__node = __node->parent;
}
}
return current;
}
template<typename _Ty>
node<_Ty>* BBST<_Ty>::predecessor(node<_Ty>* current) const
{
if( current->leftchild!=null) return Maximum(current->leftchild);
for(node<_Ty>* __node=current->parent;__node!=null;)
{
if(__node->rightchild == current)
{
current = __node;
__node = null;
}
else
{
current = __node;
__node = __node->parent;
}
}
return current;
}
// bool empty() const
template<typename _Ty>
inline bool BBST<_Ty>::empty() const
{
return root == null;
}
// int size() const
template<typename _Ty>
inline int BBST<_Ty>::size() const
{
return countNode;
}
// int height() const
template<typename _Ty>
inline int BBST<_Ty>::height() const
{
return height(root);
}
// int height(const node<_Ty>* root) const
template<typename _Ty>
int BBST<_Ty>::height(const node<_Ty>* root) const
{
if( root==null ) return 0;
else return height(root->leftchild)>height(root->rightchild)?
height(root->leftchild)+1:height(root->rightchild)+1;
}
// void InOrder() const
template<typename _Ty>
void BBST<_Ty>::InOrder() const
{
// srand((unsigned int)time(0));
inorder(*root);
}
template<typename _Ty >
void BBST<_Ty>::inorder(const node<_Ty>* root) const
{
if( root!=null )
{
inorder(root->leftchild);
std::cout<<current->data<<','
<<current->color<<" ";
inorder(root->rightchild);
}
}
template<typename _Ty>
void BBST<_Ty>::inorder( node<_Ty> & root) const
{
using std::deque;
deque<node<_Ty>*> que;
for(node<_Ty>* current=&root; current!=null|| !que.empty(); )
{
for(;current!=null ; )
{
que.push_front(current);
current = current->leftchild;
}
current = que.front();
que.pop_front();
std::cout<<current->data<<','
<<current->color<<" ";
current = current->rightchild;
}
}
// void PostOrder() const
template<typename _Ty>
void BBST<_Ty>::PostOrder() const
{
// srand((unsigned int)time(0));
postorder(*root);
}
template<typename _Ty>
void BBST<_Ty>::postorder(const node<_Ty>* root) const
{
if( root!=null )
{
postorder(root->leftchild);
postorder(root->rightchild);
std::cout<<current->data<<','
<<current->color<<" ";
}
}
template<typename _Ty>
void BBST<_Ty>::postorder( node<_Ty>& root) const
{
using std::deque;
deque<node<_Ty>* > que;
for(node<_Ty>* current=&root,*visited=0;
current!=null || !que.empty(); )
{
for(; current!=null; )
{
que.push_front(current);
current = current->leftchild;
}
current = que.front();
que.pop_front();
if(current->rightchild!=null&&visited!=current->rightchild)
{
que.push_front(current);
current = current->rightchild;
}
else
{
visited = current;
std::cout<<current->data<<','
<<current->color<<" ";
current = null;
}
}
}
// void PreOrder() const
template<typename _Ty>
void BBST<_Ty>::PreOrder() const
{
// srand((unsigned int) time(0));
preorder(*root);
}
template<typename _Ty>
void BBST<_Ty>::preorder(const node<_Ty>* root) const
{
if( root!=null )
{
std::cout<<current->data<<','
<<current->color<<" ";
preorder(root->leftchild);
preorder(root->rightchild);
}
}
template<typename _Ty>
void BBST<_Ty>::preorder( node<_Ty>& root) const
{
using std::deque;
deque<node<_Ty>*> que;
for(node<_Ty>* current=&root;current!=null || !que.empty();)
{
for( ; current!=null; )
{
std::cout<<current->data<<','
<<current->color<<" ";
que.push_front(current);
current = current->leftchild;
}
current = que.front();
que.pop_front();
current = current->rightchild;
}
}
// void LevelOrder() const
template<typename _Ty>
void BBST<_Ty>::LevelOrder() const
{
using std::deque;
deque<node<_Ty>*> que;
for(node<_Ty>* current=root;current!=null||!que.empty();)
{
std::cout<<current->data<<','
<<current->color<<" ";
if(current->leftchild!=null) que.push_front(current->leftchild);
if(current->rightchild!=null) que.push_front(current->rightchild);
if(!que.empty())
{
current = que.back();
que.pop_back();
}
else current = null;
}
}