数据结构练手——template之二叉查找树
2018-02-03 14:29
190 查看
template <typename Comparable>
class BinarySearchTree
{
public:
BinarySearchTree()
:root(NULL)
{
}
BinarySearchTree(const BinarySearchTree &rhs)
{
if (this != &rhs)
{
MakeEmpty();
this = &rhs;
}
return *this;
}
~BinarySearchTree()
{
MakeEmpty();
}
const BinarySearchTree &operator= (const BinarySearchTree &rhs)
{
if ( this != &rhs )
{
MakeEmpty();
root = Clone(rhs.root);
}
return *this;
}
const Comparable &FindMin() const
{
return FindMin(root)->element;
};
const Comparable &FindMax() const
{
return FindMax(root)->element;
};
bool Contains(const Comparable &x) const
{
return Contains(x,root);
};
bool IsEmpty() const
{
return root == NULL;
}
void PrintTree( ostream & out = cout ) const
{
if ( IsEmpty() )
{
out << "Empty Tree"<< endl;
}
else
{
PrintTree(root, out);
}
}
void MakeEmpty()
{
MakeEmpty(root);
}
void Insert(const Comparable &x)
{
Insert(x, root);
};
void Remove(const Comparable &x)
{
Remove(x, root);
};
int Height(BinaryNode *t) const
{
if ( t == NULL )
{
return -1;
}
else
{
return 1 + Max(Height(t->left), Height(t->right));
}
}
int Max(int a, int b) const
{
return (a > b) ? (a) : (b);
}
private:
struct BinaryNode
{
Comparable element;
BinaryNode *left;
BinaryNode *right;
BinaryNode(const Comparable &theElement, BinaryNode *lt, BinaryNode *rt)
:element(theElement),left(lt),right(rt)
{
}
};
BinaryNode *root;
void Insert(const Comparable &x, BinaryNode* &t) const
{
if ( t == NULL )
{
t = new BinaryNode(x, NULL, NULL);
}
else if( x < t->element )
{
Insert(x, t->left);
}
else if ( t->element < x )
{
Insert(x, t->right);
}
else
{
// do nothing
}
}
void Remove(const Comparable &x, BinaryNode* &t) const
{
if (t == NULL)
{
return;
}
if ( x < t->element)
{
return Remove(x, t->left);
}
else if ( t->element < x)
{
return Remove(x, t->right);
}
else if ( t->left != NULL && t->right != NULL)
{
t->element = FindMin(t->right)->element;
Remove(t->element, t->right);
}
else
{
BinaryNode *oldNode = t;
t = (t->left != NULL) ? t->left : t->right;
delete oldNode;
}
}
BinaryNode *FindMin(BinaryNode *t) const
{
if (t == NULL)
{
return NULL;
}
if (t->left == NULL)
{
return t;
}
return FindMin(t->left);
};
BinaryNode *FindMax(BinaryNode *t) const
{
if (t != NULL)
{
while (t->right != NULL)
{
t = t->right;
}
}
return t;
};
bool Contains(const Comparable &x, BinaryNode *t) const
{
if (t == NULL)
{
return false;
}
else if(x < t->element)
{
return Contains(x, t->left);
}
else if (t->element < x)
{
return Contains(x, t->right);
}
else
{
return true;
}
}
void MakeEmpty(BinaryNode* &t)
{
if ( t != NULL )
{
MakeEmpty( t->left );
MakeEmpty( t->right );
delete t;
}
t = NULL;
}
void PrintTree(BinaryNode *t, ostream &out) const
{
if ( t != NULL)
{
PrintTree(t->left, out);
out<< t->element <<endl;
PrintTree(t->right, out);
}
}
BinaryNode *Clone(BinaryNode *t) const
{
if ( t == NULL)
{
return NULL;
}
return new BinaryNode(t->element, Clone(t->left), Clone(t->right));
}
};
class BinarySearchTree
{
public:
BinarySearchTree()
:root(NULL)
{
}
BinarySearchTree(const BinarySearchTree &rhs)
{
if (this != &rhs)
{
MakeEmpty();
this = &rhs;
}
return *this;
}
~BinarySearchTree()
{
MakeEmpty();
}
const BinarySearchTree &operator= (const BinarySearchTree &rhs)
{
if ( this != &rhs )
{
MakeEmpty();
root = Clone(rhs.root);
}
return *this;
}
const Comparable &FindMin() const
{
return FindMin(root)->element;
};
const Comparable &FindMax() const
{
return FindMax(root)->element;
};
bool Contains(const Comparable &x) const
{
return Contains(x,root);
};
bool IsEmpty() const
{
return root == NULL;
}
void PrintTree( ostream & out = cout ) const
{
if ( IsEmpty() )
{
out << "Empty Tree"<< endl;
}
else
{
PrintTree(root, out);
}
}
void MakeEmpty()
{
MakeEmpty(root);
}
void Insert(const Comparable &x)
{
Insert(x, root);
};
void Remove(const Comparable &x)
{
Remove(x, root);
};
int Height(BinaryNode *t) const
{
if ( t == NULL )
{
return -1;
}
else
{
return 1 + Max(Height(t->left), Height(t->right));
}
}
int Max(int a, int b) const
{
return (a > b) ? (a) : (b);
}
private:
struct BinaryNode
{
Comparable element;
BinaryNode *left;
BinaryNode *right;
BinaryNode(const Comparable &theElement, BinaryNode *lt, BinaryNode *rt)
:element(theElement),left(lt),right(rt)
{
}
};
BinaryNode *root;
void Insert(const Comparable &x, BinaryNode* &t) const
{
if ( t == NULL )
{
t = new BinaryNode(x, NULL, NULL);
}
else if( x < t->element )
{
Insert(x, t->left);
}
else if ( t->element < x )
{
Insert(x, t->right);
}
else
{
// do nothing
}
}
void Remove(const Comparable &x, BinaryNode* &t) const
{
if (t == NULL)
{
return;
}
if ( x < t->element)
{
return Remove(x, t->left);
}
else if ( t->element < x)
{
return Remove(x, t->right);
}
else if ( t->left != NULL && t->right != NULL)
{
t->element = FindMin(t->right)->element;
Remove(t->element, t->right);
}
else
{
BinaryNode *oldNode = t;
t = (t->left != NULL) ? t->left : t->right;
delete oldNode;
}
}
BinaryNode *FindMin(BinaryNode *t) const
{
if (t == NULL)
{
return NULL;
}
if (t->left == NULL)
{
return t;
}
return FindMin(t->left);
};
BinaryNode *FindMax(BinaryNode *t) const
{
if (t != NULL)
{
while (t->right != NULL)
{
t = t->right;
}
}
return t;
};
bool Contains(const Comparable &x, BinaryNode *t) const
{
if (t == NULL)
{
return false;
}
else if(x < t->element)
{
return Contains(x, t->left);
}
else if (t->element < x)
{
return Contains(x, t->right);
}
else
{
return true;
}
}
void MakeEmpty(BinaryNode* &t)
{
if ( t != NULL )
{
MakeEmpty( t->left );
MakeEmpty( t->right );
delete t;
}
t = NULL;
}
void PrintTree(BinaryNode *t, ostream &out) const
{
if ( t != NULL)
{
PrintTree(t->left, out);
out<< t->element <<endl;
PrintTree(t->right, out);
}
}
BinaryNode *Clone(BinaryNode *t) const
{
if ( t == NULL)
{
return NULL;
}
return new BinaryNode(t->element, Clone(t->left), Clone(t->right));
}
};
相关文章推荐
- 数据结构练手——template之AVL树
- 数据结构与算法4:二叉树1(二叉查找树)
- 数据结构-自平衡二叉查找树(AVL)详解
- 如何使用RedisTemplate访问Redis数据结构
- 基本数据结构之二叉查找树
- 【数据结构】有向图、无向图以及最短路(Dijkstra)算法的C#实现(Template模式)
- ※数据结构※→☆非线性结构(tree)☆============二叉搜索树(二叉查找树) 链式存储结构(tree Binary Search list)(二十五)
- 浅谈算法和数据结构: 七 二叉查找树
- 算法与数据结构 其五 二叉查找树
- 数据结构之二叉查找树
- 二叉查找树的数据结构以及实现(JAVA)
- 数据结构快速回顾——二叉查找树
- Java数据结构-二叉查找树续以及平衡二叉查找树
- 数据结构(scheme) -- 抽象数据类型(ADT) -- 二叉查找树
- Pku acm 2075 Tangled in Cables数据结构题目解题报告(十一)最小生成树:prim算法&amp;amp;二叉查找树
- 设计一个算法,找出二叉树中某两个结点的第一个公共祖先.。不得将额外的结点存储在另外的数据结构中。注意:这不一定是二叉查找树。
- 数据结构--二叉查找树
- RedisTemplate访问Redis数据结构(前言)
- 数据结构实战java实现二叉查找树
- 数据结构---二叉查找树