二叉查找树
来源:互联网 发布:mac版b站 编辑:程序博客网 时间:2024/06/03 18:55
template <typename Comparable>class BinarySearchTree{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; void remove( const Comparable & x, BinaryNode * & t ) const; BinaryNode * findMin( BinaryNode *t ) const; BinaryNode * findMax( BinaryNode *t ) const; void printTree( BinaryNode * t ) const; BinaryNode *clone( BinaryNode *t )const; public: BinarySearchTree( ); BinarySearchTree( const BinarySearchTree & rhs ); ~BinarySearchTree( ); const Comparable & findMin( ) const; const Comparable & findMax( ) const; bool contains( const Comparable & x ) const; bool isEmpty( )const; void printTree( )const; void makeEmpty( ); void insert( const Comparable & x ); void remove( const Comparable & x ); const BinarySearchTree & operator= ( const BinarySearchTree & rhs ); };bool contains( const Comparable & x ) const{return contains( x, root );}void insert( const Comparable & x ){insert( x, root );}void remove( const Comparable & x ){remove( x, root );}bool contains( const Comparable & x, BinaryNode *t )const{if( t == NULL)return false;else if( x < t->element ){return contains( x, t->right );}else if( x > t->element ){return contains( x, t->left );}elsereturn true;}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;}void insert( const Comparable & x,BinaryNode * & t){if( t==NULL ){t = new BinaryNode( x, NULL, NULL );}else if( x < t->left )insert( x,t-left );else if( t->lement < x )insert( x, t->right );else return;}void remove( const Compareble & x, BinaryNode * & t ){if( t == NULL )return;if ( x < t->element )remove( x, t->left );else if( t->element < x )remove( x, t->right );else if( t->letf !=NULL && t->right != NULL ){t->element = findMin( t->right )->element;remove( t->element, t->right );}else{BinaryNode *oleNode = t;t = ( t->left != NULL ) ? t->left:t->right;deleta oldNode;}}~BinarySearchTree( ){makeEmpty( );}void makeEmpty( BinaryNode * & t ){if( t!= NULL ){makeEmpty( t->left );makeEmpty( t->right );delete t;}t = NULL;}BinaryNode *clone( BinaryNode *t ) const{if( t == NULL )return NULL;return new Binary( t->element, clone( t->left ),clone( t->right ) );}
0 0
- 查找--二叉查找树
- 二叉树、二叉查找树
- 二叉树 & 二叉查找树
- 【查找结构】二叉查找树
- 查找之二叉树查找
- 查找之二叉树查找
- 查找:二叉查找树总结
- 二叉树查找树...
- 二叉树查找树
- 查找--遍历二叉树
- 二叉查找树
- 二叉查找树实现
- 二叉查找树
- 动态二叉查找树
- 最优二叉查找树
- 二叉查找树
- 二叉查找树
- 平衡二叉查找树
- 数据结构实验之栈二:一般算术表达式转换成后缀式
- Tomcat配置JVM参数步骤
- SQL Server 2008下轻松调试T-SQL语句
- tomcat启动了但是访问不了8080
- java获取当前类的绝对路径
- 二叉查找树
- 基于ZeroMQ的聊天室设计实现
- Hash 函数及其重要性
- js调用打印预览功能(IE)
- Java Collection Framework扩展Apache Common Collections介绍
- Windows7下配置Eclipse和Tomcat6.0
- BUG_学习随笔(移动数据快捷开关)
- Properties是Hashtable的子类
- strcpy和memcpy的区别