二叉查找树的类模板实现
来源:互联网 发布:淘宝刷单负债百万 编辑:程序博客网 时间:2024/04/27 21:19
二叉查找树的性质是,对于树中的每一个结点X,它的左子树中所有的项的值小于X中的项,而它的右子树中所有项的值大于X中的项。
#include <iostream>using namespace std;template <typename T>class BinarySearch{public: BinarySearch(); BinarySearch ( const BinarySearch & rhs ); ~BinarySearch(); const T &findMin() const; const T &findMax() const; bool contains ( const T &x ) const; bool isEmpty() const; void printTree() const; void makeEmpty(); void insert ( const T &x ); void remove ( const T &x ); const BinarySearch &operator= ( const BinarySearch &rhs );private: struct BinaryNode { T element; BinaryNode *left; BinaryNode *right; BinaryNode ( const T & e, BinaryNode *l , BinaryNode *r ) : element ( e ), left ( l ), right ( r ) {} }; BinaryNode *root; //头结点 void insert ( const T & x, BinaryNode * &t ) ; void remove ( const T & x, BinaryNode * &t ) ; const T & findMin ( BinaryNode * t ) const; const T & findMax ( BinaryNode * t ) const; bool contain ( const T &x, BinaryNode *t ) const; void printtree ( BinaryNode *t ) const; void makeEmpty ( BinaryNode * &t ); BinaryNode *clone ( BinaryNode *t ) const { if ( t == NULL ) return NULL; return new BinaryNode ( t->element, clone ( t->left ), clone ( t->right ) ); }};template<typename T>BinarySearch<T>::BinarySearch(){ root = NULL;}template<typename T>BinarySearch<T>::BinarySearch ( const BinarySearch &rhs ){ root = clone ( rhs.root );}template<typename T>const BinarySearch<T> & BinarySearch<T>::operator= ( const BinarySearch &rhs ){ if ( this != &rhs ) { makeEmpty(); root = clone ( rhs.root ); } return *this;}template<typename T>BinarySearch<T>::~BinarySearch(){ makeEmpty();}template<typename T>void BinarySearch<T>::makeEmpty(){ return makeEmpty ( root );}template<typename T>void BinarySearch<T>::makeEmpty ( BinaryNode * &t ){ if ( t != NULL ) { makeEmpty ( t->left ); makeEmpty ( t->right ); delete t; } t = NULL;}template <typename T>void BinarySearch<T>::insert ( const T &x ){ insert ( x, root );}template <typename T>void BinarySearch<T>::insert ( const T &x, BinaryNode * &t ){ 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 ;//x已经存在,不做处理}template <typename T>bool BinarySearch<T>::contains ( const T &x ) const{ return contain ( x, root );}template <typename T>bool BinarySearch<T>::contain ( const T &x, BinaryNode *t ) const{ if ( t == NULL ) return false; else if ( x < t->element ) return contain ( x, t->left ); else if ( t->element < x ) return contain ( x , t->right ); else return true;}template <typename T>void BinarySearch<T>::remove ( const T &x ){ remove ( x, root );}template<typename T>void BinarySearch<T>::remove ( const T &x, BinaryNode * &t ){ if ( t == NULL ) return; else if ( x < t->element ) remove ( x, t->left ); else if ( t->element < x ) remove ( x, t->right ); else if ( t->left != NULL && t->right != NULL ) { t->element = findMin ( t->right ); remove ( t->element, t->right ); } else { BinaryNode *old = t; t = ( t->left != NULL ) ? t->left : t->right; delete old; }}template<typename T>const T & BinarySearch<T>::findMax() const{ return findMax ( root );}template<typename T>const T & BinarySearch<T>::findMax ( BinaryNode *t ) const{ if ( t != NULL ) while ( t->right != NULL ) t = t->right; return t->element;}template<typename T>const T & BinarySearch<T>::findMin() const{ return findMin ( root );}template<typename T>const T & BinarySearch<T>::findMin ( BinaryNode *t ) const{ if ( t == NULL ) return t->element; if ( t->left == NULL ) return t->element; return findMin ( t->left );}template<typename T>bool BinarySearch<T>::isEmpty() const{ if ( root == NULL ) return true; else return false;}template<typename T>void BinarySearch<T>::printTree() const{ printtree ( root ); cout << endl;}template<typename T>void BinarySearch<T>::printtree ( BinaryNode *t ) const{ /*前序输出*/ //if ( t != NULL ) // cout << t->element << " "; //if ( t->left != NULL ) // printtree ( t->left ); //if ( t->right != NULL ) // printtree ( t->right ); /*中序输出*/ if ( t->left != NULL ) { printtree ( t->left ); } cout << t->element << " "; if ( t->right != NULL ) printtree ( t->right ); /*后序输出*/ //if(t->left!=NULL) // printtree(t->left); //if(t->right!=NULL) // printtree(t->right); //cout<<t->element<<" ";}
0 0
- 二叉查找树的类模板实现
- 二叉查找树的简单模板实现
- 类模板实现二叉查找树
- C++模板实现的二叉排序(查找)树
- 二叉查找树 模板实现 C++
- 二叉查找树及C++模板实现
- 【模板】二叉查找树
- 二叉树类的实现(查找树)
- 类模板实现的基本二叉树
- 二叉树的模板类实现
- 二叉查找树的实现
- 二叉查找树的实现
- 二叉查找树的实现
- 二叉查找树的实现
- 二叉查找树的实现
- 二叉查找树的实现
- 二叉查找树的实现
- 二叉查找树的实现
- super4412下调试cmos驱动是的nternal error: Oops - BUG: 0 [#1] PREEMPT SMP ARM问题解决
- 从SIFT到SURF
- jboss开启远程访问
- 顺序栈的置空入栈出栈和返回栈顶元素
- MySQL导入sql脚本错误:2006 - MySQL server has gone away
- 二叉查找树的类模板实现
- android中的返回键与Activity
- HDU 1513 Palindrome 求回文串
- macbook safari上不了网,但是QQ可以上网的问题
- Eclipse中设置JVM内存
- Codeforces Round #275
- 错误:No resource found that matches the given name 'Theme.AppCompat.Light'解决方法
- C++学习之类的使用(排序算法)
- 墩泰的FT6206电容屏的调试