在项目中用了b+树,为了想比较一下性能,今天花了3-4个小时写了平衡二叉树,它能够插入,删除以及查找.可能测试不够全面,有bug,提供出来和大家共享.项目中用了b+树,所以b+树的代码不能够提供
可以给我邮件批评指正tangjiliang@gmail.google.com
结点定义:
#ifndef _AVLTREENODE_H
#define _AVLTREENODE_H
#include < cmath >
using namespace std;
template < class keyType >
struct CAvlTreeNode
{
typedef CAvlTreeNode < keyType > node;
keyType value_node; // the value of the node
node * left_child_node; // the left child of the node
node * right_child_node; // the right child of the node
node * parent_node; // the parent node of the node
int deepth; // the deepth of the node
explicit CAvlTreeNode(keyType value ,node * parent = 0 )
{
value_node = value;
left_child_node = 0 ;
right_child_node = 0 ;
parent_node = parent;
deepth = 0 ;
}
bool isleaf_node() // judge the node is leaf or not
{
return left_child_node == 0 && right_child_node == 0 ;
}
// judge the node is balance,if the deep between right and left node is smaller than one
// the node son tree is balance
bool isBalance_node()
{
int left_deepth = 0 ;
int right_deepth = 0 ;
if (left_child_node != 0 ) // get the deepth of the left tree
{
left_deepth = left_child_node -> deepth + 1 ;
}
if (right_child_node != 0 ) // get the deepth of the right tree
{
right_deepth = right_child_node -> deepth + 1 ;
}
return abs(right_deepth - left_deepth) < 2 ; //
}
// judge the node is the parent which node
bool isLeftNode(node * judge_node)
{
return left_child_node == judge_node;
}
// modify the deepth of the node ,if modify return true
// else return false
bool modify_deepth( bool isleft_node , bool increase);
// adjust the deepth of the node after rotate
void adjust_deepth()
{
if (left_child_node == 0 && right_child_node == 0 ) deepth = 0 ;
else if (left_child_node == 0 ) deepth = right_child_node -> deepth + 1 ;
else if (right_child_node == 0 ) deepth = left_child_node -> deepth + 1 ;
else deepth = (left_child_node -> deepth > right_child_node -> deepth) ?
(left_child_node -> deepth + 1 ):(right_child_node -> deepth + 1 );
}
bool deepth_depend_leftNode()
{
if (left_child_node == 0 )
{
return false ;
}
else if (right_child_node == 0 )
{
return true ;
}
else
{
if (deepth == right_child_node -> deepth + 1 ) return false ;
else return true ;
}
}
~ CAvlTreeNode()
{
if (left_child_node != 0 )
{
delete left_child_node;
}
if (right_child_node != 0 )
{
delete right_child_node;
}
}
};
/// modify_deepth ///
// modify the deepth of the node ,if modify return true
// else return false
template < class typeKey >
bool CAvlTreeNode < typeKey > ::modify_deepth( bool isleft_node , bool increase)
{
if (increase)
{
if (isleft_node)
{
// no need to adjust deepth ,so the tree is balance
if (deepth >= left_child_node -> deepth + 1 )
{
return false ;
}
else
{
deepth ++ ;
}
}
else
{
// no need to adjust deepth ,so the tree is balance
if (deepth >= right_child_node -> deepth + 1 )
{
return false ;
}
else
{
deepth ++ ;
}
}
}
else // it is delete node
{
adjust_deepth();
}
return true ;
}
#endif
#define _AVLTREENODE_H
#include < cmath >
using namespace std;
template < class keyType >
struct CAvlTreeNode
{
typedef CAvlTreeNode < keyType > node;
keyType value_node; // the value of the node
node * left_child_node; // the left child of the node
node * right_child_node; // the right child of the node
node * parent_node; // the parent node of the node
int deepth; // the deepth of the node
explicit CAvlTreeNode(keyType value ,node * parent = 0 )
{
value_node = value;
left_child_node = 0 ;
right_child_node = 0 ;
parent_node = parent;
deepth = 0 ;
}
bool isleaf_node() // judge the node is leaf or not
{
return left_child_node == 0 && right_child_node == 0 ;
}
// judge the node is balance,if the deep between right and left node is smaller than one
// the node son tree is balance
bool isBalance_node()
{
int left_deepth = 0 ;
int right_deepth = 0 ;
if (left_child_node != 0 ) // get the deepth of the left tree
{
left_deepth = left_child_node -> deepth + 1 ;
}
if (right_child_node != 0 ) // get the deepth of the right tree
{
right_deepth = right_child_node -> deepth + 1 ;
}
return abs(right_deepth - left_deepth) < 2 ; //
}
// judge the node is the parent which node
bool isLeftNode(node * judge_node)
{
return left_child_node == judge_node;
}
// modify the deepth of the node ,if modify return true
// else return false
bool modify_deepth( bool isleft_node , bool increase);
// adjust the deepth of the node after rotate
void adjust_deepth()
{
if (left_child_node == 0 && right_child_node == 0 ) deepth = 0 ;
else if (left_child_node == 0 ) deepth = right_child_node -> deepth + 1 ;
else if (right_child_node == 0 ) deepth = left_child_node -> deepth + 1 ;
else deepth = (left_child_node -> deepth > right_child_node -> deepth) ?
(left_child_node -> deepth + 1 ):(right_child_node -> deepth + 1 );
}
bool deepth_depend_leftNode()
{
if (left_child_node == 0 )
{
return false ;
}
else if (right_child_node == 0 )
{
return true ;
}
else
{
if (deepth == right_child_node -> deepth + 1 ) return false ;
else return true ;
}
}
~ CAvlTreeNode()
{
if (left_child_node != 0 )
{
delete left_child_node;
}
if (right_child_node != 0 )
{
delete right_child_node;
}
}
};
/// modify_deepth ///
// modify the deepth of the node ,if modify return true
// else return false
template < class typeKey >
bool CAvlTreeNode < typeKey > ::modify_deepth( bool isleft_node , bool increase)
{
if (increase)
{
if (isleft_node)
{
// no need to adjust deepth ,so the tree is balance
if (deepth >= left_child_node -> deepth + 1 )
{
return false ;
}
else
{
deepth ++ ;
}
}
else
{
// no need to adjust deepth ,so the tree is balance
if (deepth >= right_child_node -> deepth + 1 )
{
return false ;
}
else
{
deepth ++ ;
}
}
}
else // it is delete node
{
adjust_deepth();
}
return true ;
}
#endif
对树的处理
#ifndef _AVLTREEINDEX_H
#define _AVLTREEINDEX_H
#include " avlTreeNode.h "
typedef enum
{
_LEFT_LEFT, // the new node insert in the left tree of the left node
_LEFT_RIGHT, // the new node insert in the left tree of the right node
_RIGHT_RIGHT, // the new node insert in the right tree of the right node
_RIGHT_LEFT, // the new node insert in the right tree of the left node
_BALANCE // the tree is balance
}_NOBALANCE_TYPE;
template < class typeKey >
class CAvlTreeIndex
{
typedef CAvlTreeNode < typeKey > node;
private :
node * head; // the head node of the a v l tree
public :
CAvlTreeIndex(){}
explicit CAvlTreeIndex(typeKey value)
{
head = new node(value);
}
// insert one node into the a v l tree
void insert_Node(typeKey value);
// delete a node whose value is value from the a v l tree
void delete_Node(typeKey value);
// get the location of the node whose value is value
// if the node is in the a v l tree ,return the node
// else return null
CAvlTreeNode < typeKey >* find_key_inTree(typeKey value) const ;
bool is_key_inTree(typeKey value) const ;
// find the parent of the node which will be inserted
inline CAvlTreeNode < typeKey >* getParent_InsertNode(typeKey value , bool & isInLeftChild) const ;
// after insert the node ,adjust the tree to balance
inline void adjust_avl_tree(node * parent , bool isLeftNode , bool increase = true );
// deal with the condition of left - left
inline void rotate_cond_ll(node * rotate_node);
// deal with the condition of left - right
inline void rotate_cond_lr(node * rotate_node);
// deal with the condition of right - left
inline void rotate_cond_rl(node * rotate_node);
// deal with the condition of right - right
inline void rotate_cond_rr(node * rotate_node);
void print()
{
print(head);
}
void print(node * head)
{
if (head != 0 )
{
print(head -> left_child_node);
cout << head -> value_node << " " << head -> deepth << endl;
print(head -> right_child_node);
}
}
~ CAvlTreeIndex();
};
#endif
insert_node /
// insert one node into the a v l tree
template < class typeKey >
void CAvlTreeIndex < typeKey > ::insert_Node(typeKey value)
{
bool isLeftChild;
node * parent_insert = getParent_InsertNode(value,isLeftChild); // get the parent node of insert
node * new_node = new node(value,parent_insert); // create a new node
if (isLeftChild) parent_insert -> left_child_node = new_node;
else parent_insert -> right_child_node = new_node;
// after insert a new node, adjust the tree to be balance again
adjust_avl_tree(parent_insert,isLeftChild);
}
/get_state_tree //
// after insert get the type of the balance
// first modify the deepth of the node ,if the deepth of node
// not need modify , the tree is balance
template < class typeKey >
inline void CAvlTreeIndex < typeKey > ::adjust_avl_tree(node * parent , bool isLeftChild , bool increase)
{
node * child_node = parent;
node * parent_node = parent;
bool isLeftNode = isLeftChild,isLeftTree = isLeftChild;
while (parent_node != 0 )
{
// adjust the deepth of the parent node
// if the deepth of the node is not need to modify
// the tree is balance
if ( ! parent_node -> modify_deepth(isLeftNode,increase))
{
return ;
}
if (parent_node == child_node && increase)
{
parent_node = parent_node -> parent_node;
}
else if (parent_node -> isBalance_node()) // the node is balance
{
isLeftTree = isLeftNode;
isLeftNode = parent_node -> isLeftNode(child_node);
child_node = parent_node;
parent_node = parent_node -> parent_node;
}
else
{
if ( ! increase)
{
isLeftNode = parent_node -> deepth_depend_leftNode();
node * _temp_node = (isLeftNode) ? parent_node -> left_child_node:parent_node -> right_child_node;
isLeftTree = _temp_node -> deepth_depend_leftNode();
}
if (isLeftNode && isLeftTree)
{
rotate_cond_ll(parent_node);
break ;
}
else if (isLeftNode && ! isLeftTree)
{
rotate_cond_lr(parent_node);
break ;
}
else if ( ! isLeftNode && ! isLeftTree)
{
rotate_cond_rr(parent_node);
break ;
}
else
{
rotate_cond_rl(parent_node);
break ;
}
}
}
}
// /rotate_cond_ll //
// deal with the condition of left - left
template < class typeKey >
inline void CAvlTreeIndex < typeKey > ::rotate_cond_ll(node * rotate_node)
{
node * left_node = rotate_node -> left_child_node;
node * _temp_node = left_node -> right_child_node;
left_node -> right_child_node = rotate_node;
rotate_node -> left_child_node = _temp_node;
if (rotate_node == head)
{
head = left_node;
left_node -> parent_node = 0 ;
}
else
{
bool isleftNode = rotate_node -> parent_node -> isLeftNode(rotate_node);
if (isleftNode) rotate_node -> parent_node -> left_child_node = left_node;
else rotate_node -> parent_node -> right_child_node = left_node;
left_node -> parent_node = rotate_node -> parent_node;
}
// adjust the deepth of the rotate node
rotate_node -> adjust_deepth();
left_node -> adjust_deepth();
if (left_node != head)
{
rotate_node -> parent_node -> adjust_deepth();
}
rotate_node -> parent_node = left_node;
}
/// rotate_cond_lr //
// deal with the condition of left - right
template < class typeKey >
inline void CAvlTreeIndex < typeKey > ::rotate_cond_lr(node * rotate_node)
{
node * left_node = rotate_node -> left_child_node;
rotate_cond_rr(left_node); // right rotate
rotate_cond_ll(rotate_node); // left rotate
}
//rotate_cond_rl
// deal with the condition of right - left
template < class typeKey >
inline void CAvlTreeIndex < typeKey > ::rotate_cond_rl(node * rotate_node)
{
node * right_node = rotate_node -> right_child_node;
rotate_cond_ll(right_node); // left rotate
rotate_cond_rr(rotate_node); // right rotate
}
// rotate_cond_rr /// /
// deal with the condition of right - right
template < class typeKey >
inline void CAvlTreeIndex < typeKey > ::rotate_cond_rr(node * rotate_node)
{
node * right_node = rotate_node -> right_child_node;
node * _temp_node = right_node -> left_child_node;
right_node -> left_child_node = rotate_node;
rotate_node -> right_child_node = _temp_node;
if (rotate_node == head)
{
head = right_node;
right_node -> parent_node = 0 ;
}
else
{
bool isLeftNode = rotate_node -> parent_node -> isLeftNode(rotate_node);
if (isLeftNode) rotate_node -> parent_node -> left_child_node = right_node;
else rotate_node -> parent_node -> right_child_node = right_node;
right_node -> parent_node = rotate_node -> parent_node;
}
rotate_node -> adjust_deepth();
right_node -> adjust_deepth();
if (right_node != head)
{
rotate_node -> parent_node -> adjust_deepth();
}
rotate_node -> parent_node = right_node;
}
// //getParent_InsertNode // /
// find the parent of the node which will be inserted
template < class typeKey >
inline CAvlTreeNode < typeKey >* CAvlTreeIndex < typeKey > ::getParent_InsertNode(typeKey value , bool & isLeftChild) const
{
node * _temp_node = head;
while ( ! _temp_node -> isleaf_node())
{
_temp_node = (_temp_node -> value_node > value) ?
_temp_node -> left_child_node:_temp_node -> right_child_node;
}
isLeftChild = (_temp_node -> value_node > value) ? true : false ;
return _temp_node;
}
/// /find_key_inTree // /
// get the location of the node whose value is value
// if the node is in the a v l tree ,return the node
// else return null
template < class typeKey >
CAvlTreeNode < typeKey >* CAvlTreeIndex < typeKey > ::find_key_inTree(typeKey value) const
{
node * _cur_find_node = head;
while (_cur_find_node != 0 )
{
if (_cur_find_node -> value_node == value) // find it
{
return _cur_find_node;
}
else if (_cur_find_node -> value_node > value) // in the right tree
{
_cur_find_node = _cur_find_node -> left_child_node;
}
else // in the left tree
{
_cur_find_node = _cur_find_node -> right_child_node;
}
}
return 0 ;
}
/ /is_key_inTree / /
// find if the key is in tree ,if it is in return true
// else return false
template < class typeKey >
bool CAvlTreeIndex < typeKey > ::is_key_inTree(typeKey value) const
{
if ( ! find_key_inTree(value))
{
return false ;
}
else
{
return true ;
}
}
// /delete_Node /// /
// delete a node whose value is value from the a v l tree
template < class typeKey >
void CAvlTreeIndex < typeKey > ::delete_Node(typeKey value)
{
node * _del_node = find_key_inTree(value);
if (_del_node == 0 ) // if the value is not in the tree
{
return ;
}
else
{
bool isLeftNode = _del_node -> parent_node -> isLeftNode(_del_node);
if (_del_node -> isleaf_node()) // if the delete node is leaf node
{
if (_del_node == head) ;
else if (isLeftNode) _del_node -> parent_node -> left_child_node = 0 ;
else _del_node -> parent_node -> right_child_node = 0 ;
}
else if (_del_node -> left_child_node != 0 && _del_node -> right_child_node != 0 )
{
node * _right_node = _del_node -> right_child_node;
node * _left_node = _del_node -> left_child_node;
_left_node -> parent_node = _del_node -> parent_node;
_del_node -> left_child_node = 0 ;
_del_node -> right_child_node = 0 ;
node * _next_node = _left_node;
while (_next_node -> right_child_node != 0 )
{
_next_node = _next_node -> right_child_node;
}
_next_node -> right_child_node = _right_node;
_right_node -> parent_node = _next_node;
if (_del_node == head) head = _left_node;
else if (isLeftNode) _del_node -> parent_node -> left_child_node = _left_node;
else _del_node -> parent_node -> right_child_node = _left_node;
adjust_avl_tree(_next_node, false , false );
delete _del_node;
return ;
}
else if (_del_node -> left_child_node != 0 )
{
node * _temp_node = _del_node -> left_child_node;
_temp_node -> parent_node = _del_node -> parent_node;
_del_node -> left_child_node = 0 ;
if (_del_node == head) head = _temp_node;
else if (isLeftNode) _del_node -> parent_node -> left_child_node = _temp_node;
else _del_node -> parent_node -> right_child_node = _temp_node;
}
else if (_del_node -> right_child_node != 0 )
{
node * _temp_node = _del_node -> right_child_node;
_temp_node -> parent_node = _del_node -> parent_node;
_del_node -> right_child_node = 0 ;
if (_del_node == head) head = _temp_node;
else if (isLeftNode) _del_node -> parent_node -> left_child_node = _temp_node;
else _del_node -> parent_node -> right_child_node = _temp_node;
}
adjust_avl_tree(_del_node -> parent_node,isLeftNode, false );
delete _del_node;
}
}
/ ~CAvlTreeIndex // //
template < class typeKey >
CAvlTreeIndex < typeKey > :: ~ CAvlTreeIndex()
{
delete head;
}
#define _AVLTREEINDEX_H
#include " avlTreeNode.h "
typedef enum
{
_LEFT_LEFT, // the new node insert in the left tree of the left node
_LEFT_RIGHT, // the new node insert in the left tree of the right node
_RIGHT_RIGHT, // the new node insert in the right tree of the right node
_RIGHT_LEFT, // the new node insert in the right tree of the left node
_BALANCE // the tree is balance
}_NOBALANCE_TYPE;
template < class typeKey >
class CAvlTreeIndex
{
typedef CAvlTreeNode < typeKey > node;
private :
node * head; // the head node of the a v l tree
public :
CAvlTreeIndex(){}
explicit CAvlTreeIndex(typeKey value)
{
head = new node(value);
}
// insert one node into the a v l tree
void insert_Node(typeKey value);
// delete a node whose value is value from the a v l tree
void delete_Node(typeKey value);
// get the location of the node whose value is value
// if the node is in the a v l tree ,return the node
// else return null
CAvlTreeNode < typeKey >* find_key_inTree(typeKey value) const ;
bool is_key_inTree(typeKey value) const ;
// find the parent of the node which will be inserted
inline CAvlTreeNode < typeKey >* getParent_InsertNode(typeKey value , bool & isInLeftChild) const ;
// after insert the node ,adjust the tree to balance
inline void adjust_avl_tree(node * parent , bool isLeftNode , bool increase = true );
// deal with the condition of left - left
inline void rotate_cond_ll(node * rotate_node);
// deal with the condition of left - right
inline void rotate_cond_lr(node * rotate_node);
// deal with the condition of right - left
inline void rotate_cond_rl(node * rotate_node);
// deal with the condition of right - right
inline void rotate_cond_rr(node * rotate_node);
void print()
{
print(head);
}
void print(node * head)
{
if (head != 0 )
{
print(head -> left_child_node);
cout << head -> value_node << " " << head -> deepth << endl;
print(head -> right_child_node);
}
}
~ CAvlTreeIndex();
};
#endif
insert_node /
// insert one node into the a v l tree
template < class typeKey >
void CAvlTreeIndex < typeKey > ::insert_Node(typeKey value)
{
bool isLeftChild;
node * parent_insert = getParent_InsertNode(value,isLeftChild); // get the parent node of insert
node * new_node = new node(value,parent_insert); // create a new node
if (isLeftChild) parent_insert -> left_child_node = new_node;
else parent_insert -> right_child_node = new_node;
// after insert a new node, adjust the tree to be balance again
adjust_avl_tree(parent_insert,isLeftChild);
}
/get_state_tree //
// after insert get the type of the balance
// first modify the deepth of the node ,if the deepth of node
// not need modify , the tree is balance
template < class typeKey >
inline void CAvlTreeIndex < typeKey > ::adjust_avl_tree(node * parent , bool isLeftChild , bool increase)
{
node * child_node = parent;
node * parent_node = parent;
bool isLeftNode = isLeftChild,isLeftTree = isLeftChild;
while (parent_node != 0 )
{
// adjust the deepth of the parent node
// if the deepth of the node is not need to modify
// the tree is balance
if ( ! parent_node -> modify_deepth(isLeftNode,increase))
{
return ;
}
if (parent_node == child_node && increase)
{
parent_node = parent_node -> parent_node;
}
else if (parent_node -> isBalance_node()) // the node is balance
{
isLeftTree = isLeftNode;
isLeftNode = parent_node -> isLeftNode(child_node);
child_node = parent_node;
parent_node = parent_node -> parent_node;
}
else
{
if ( ! increase)
{
isLeftNode = parent_node -> deepth_depend_leftNode();
node * _temp_node = (isLeftNode) ? parent_node -> left_child_node:parent_node -> right_child_node;
isLeftTree = _temp_node -> deepth_depend_leftNode();
}
if (isLeftNode && isLeftTree)
{
rotate_cond_ll(parent_node);
break ;
}
else if (isLeftNode && ! isLeftTree)
{
rotate_cond_lr(parent_node);
break ;
}
else if ( ! isLeftNode && ! isLeftTree)
{
rotate_cond_rr(parent_node);
break ;
}
else
{
rotate_cond_rl(parent_node);
break ;
}
}
}
}
// /rotate_cond_ll //
// deal with the condition of left - left
template < class typeKey >
inline void CAvlTreeIndex < typeKey > ::rotate_cond_ll(node * rotate_node)
{
node * left_node = rotate_node -> left_child_node;
node * _temp_node = left_node -> right_child_node;
left_node -> right_child_node = rotate_node;
rotate_node -> left_child_node = _temp_node;
if (rotate_node == head)
{
head = left_node;
left_node -> parent_node = 0 ;
}
else
{
bool isleftNode = rotate_node -> parent_node -> isLeftNode(rotate_node);
if (isleftNode) rotate_node -> parent_node -> left_child_node = left_node;
else rotate_node -> parent_node -> right_child_node = left_node;
left_node -> parent_node = rotate_node -> parent_node;
}
// adjust the deepth of the rotate node
rotate_node -> adjust_deepth();
left_node -> adjust_deepth();
if (left_node != head)
{
rotate_node -> parent_node -> adjust_deepth();
}
rotate_node -> parent_node = left_node;
}
/// rotate_cond_lr //
// deal with the condition of left - right
template < class typeKey >
inline void CAvlTreeIndex < typeKey > ::rotate_cond_lr(node * rotate_node)
{
node * left_node = rotate_node -> left_child_node;
rotate_cond_rr(left_node); // right rotate
rotate_cond_ll(rotate_node); // left rotate
}
//rotate_cond_rl
// deal with the condition of right - left
template < class typeKey >
inline void CAvlTreeIndex < typeKey > ::rotate_cond_rl(node * rotate_node)
{
node * right_node = rotate_node -> right_child_node;
rotate_cond_ll(right_node); // left rotate
rotate_cond_rr(rotate_node); // right rotate
}
// rotate_cond_rr /// /
// deal with the condition of right - right
template < class typeKey >
inline void CAvlTreeIndex < typeKey > ::rotate_cond_rr(node * rotate_node)
{
node * right_node = rotate_node -> right_child_node;
node * _temp_node = right_node -> left_child_node;
right_node -> left_child_node = rotate_node;
rotate_node -> right_child_node = _temp_node;
if (rotate_node == head)
{
head = right_node;
right_node -> parent_node = 0 ;
}
else
{
bool isLeftNode = rotate_node -> parent_node -> isLeftNode(rotate_node);
if (isLeftNode) rotate_node -> parent_node -> left_child_node = right_node;
else rotate_node -> parent_node -> right_child_node = right_node;
right_node -> parent_node = rotate_node -> parent_node;
}
rotate_node -> adjust_deepth();
right_node -> adjust_deepth();
if (right_node != head)
{
rotate_node -> parent_node -> adjust_deepth();
}
rotate_node -> parent_node = right_node;
}
// //getParent_InsertNode // /
// find the parent of the node which will be inserted
template < class typeKey >
inline CAvlTreeNode < typeKey >* CAvlTreeIndex < typeKey > ::getParent_InsertNode(typeKey value , bool & isLeftChild) const
{
node * _temp_node = head;
while ( ! _temp_node -> isleaf_node())
{
_temp_node = (_temp_node -> value_node > value) ?
_temp_node -> left_child_node:_temp_node -> right_child_node;
}
isLeftChild = (_temp_node -> value_node > value) ? true : false ;
return _temp_node;
}
/// /find_key_inTree // /
// get the location of the node whose value is value
// if the node is in the a v l tree ,return the node
// else return null
template < class typeKey >
CAvlTreeNode < typeKey >* CAvlTreeIndex < typeKey > ::find_key_inTree(typeKey value) const
{
node * _cur_find_node = head;
while (_cur_find_node != 0 )
{
if (_cur_find_node -> value_node == value) // find it
{
return _cur_find_node;
}
else if (_cur_find_node -> value_node > value) // in the right tree
{
_cur_find_node = _cur_find_node -> left_child_node;
}
else // in the left tree
{
_cur_find_node = _cur_find_node -> right_child_node;
}
}
return 0 ;
}
/ /is_key_inTree / /
// find if the key is in tree ,if it is in return true
// else return false
template < class typeKey >
bool CAvlTreeIndex < typeKey > ::is_key_inTree(typeKey value) const
{
if ( ! find_key_inTree(value))
{
return false ;
}
else
{
return true ;
}
}
// /delete_Node /// /
// delete a node whose value is value from the a v l tree
template < class typeKey >
void CAvlTreeIndex < typeKey > ::delete_Node(typeKey value)
{
node * _del_node = find_key_inTree(value);
if (_del_node == 0 ) // if the value is not in the tree
{
return ;
}
else
{
bool isLeftNode = _del_node -> parent_node -> isLeftNode(_del_node);
if (_del_node -> isleaf_node()) // if the delete node is leaf node
{
if (_del_node == head) ;
else if (isLeftNode) _del_node -> parent_node -> left_child_node = 0 ;
else _del_node -> parent_node -> right_child_node = 0 ;
}
else if (_del_node -> left_child_node != 0 && _del_node -> right_child_node != 0 )
{
node * _right_node = _del_node -> right_child_node;
node * _left_node = _del_node -> left_child_node;
_left_node -> parent_node = _del_node -> parent_node;
_del_node -> left_child_node = 0 ;
_del_node -> right_child_node = 0 ;
node * _next_node = _left_node;
while (_next_node -> right_child_node != 0 )
{
_next_node = _next_node -> right_child_node;
}
_next_node -> right_child_node = _right_node;
_right_node -> parent_node = _next_node;
if (_del_node == head) head = _left_node;
else if (isLeftNode) _del_node -> parent_node -> left_child_node = _left_node;
else _del_node -> parent_node -> right_child_node = _left_node;
adjust_avl_tree(_next_node, false , false );
delete _del_node;
return ;
}
else if (_del_node -> left_child_node != 0 )
{
node * _temp_node = _del_node -> left_child_node;
_temp_node -> parent_node = _del_node -> parent_node;
_del_node -> left_child_node = 0 ;
if (_del_node == head) head = _temp_node;
else if (isLeftNode) _del_node -> parent_node -> left_child_node = _temp_node;
else _del_node -> parent_node -> right_child_node = _temp_node;
}
else if (_del_node -> right_child_node != 0 )
{
node * _temp_node = _del_node -> right_child_node;
_temp_node -> parent_node = _del_node -> parent_node;
_del_node -> right_child_node = 0 ;
if (_del_node == head) head = _temp_node;
else if (isLeftNode) _del_node -> parent_node -> left_child_node = _temp_node;
else _del_node -> parent_node -> right_child_node = _temp_node;
}
adjust_avl_tree(_del_node -> parent_node,isLeftNode, false );
delete _del_node;
}
}
/ ~CAvlTreeIndex // //
template < class typeKey >
CAvlTreeIndex < typeKey > :: ~ CAvlTreeIndex()
{
delete head;
}