二叉查找树简单实现
2016-01-27 16:59
295 查看
树是一种简单的数据结构,其大部分操作的运行时间平均为O(logN)。我将《数据结构与算法分析》上的的代码片段加入自己的理解简单实现了该结构:
BinarySearchTree.h源码如下:
再写个测试的主程序:
运行效果:
BinarySearchTree.h源码如下:
#ifndef BINARYSEARCHTREE_H #define BINARYSEARCHTREE_H #include <iostream> template <typename Comparable> class BinarySearchTree { public: BinarySearchTree() { root = NULL; } BinarySearchTree( const BinarySearchTree &rhs ) { operator=( rhs ); } ~BinarySearchTree() { makeEmpty(); } const Comparable &findMin() const { BinaryNode *p = findMin( root ); return p->element; } const Comparable &findMax() const { BinaryNode *p = findMax( root ); return p->element; } bool contains( const Comparable &x ) const { return contains( x,root ); } bool iSEmpty() const { return root == NULL; } void printTree() const { printTree( root ); } void makeEmpty() { makeEmpty( root ); } void insert( const Comparable &x ) { insert( x,root ); } void remove( const Comparable &x ) { remove( x,root ); } const BinarySearchTree &operator=( const BinarySearchTree &rhs ) { if ( this != &rhs ) { makeEmpty(); root = clone( rhs.root ); } return *this; } 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 ( x > t->element ) insert( x,t->right ); else ; } void remove( const Comparable &x,BinaryNode *&t )const { if ( t == NULL ) return ; 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 )->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 )const //先序遍历 { if ( t == NULL ) return ; std::cout << t->element << std::endl; printTree( t->left ); printTree( t->right ); } BinaryNode *clone( BinaryNode *t )const { if ( t == NULL ) return NULL; return new BinaryNode( t->element,clone( t->left ),clone( t->right ) ); } }; #endif // BINARYSEARCHTREE_H
再写个测试的主程序:
#include <iostream> #include "BinarySearchTree.h" using namespace std; int main() { BinarySearchTree<int> BST; if ( BST.iSEmpty() ) cout << "二叉查找树为空!" << endl; cout << "插入数据中。。" << endl; BST.insert( 200 ); BST.insert( 7 ); BST.insert( 1 ); BST.insert( 99 ); BST.insert( 55 ); cout << "打印二叉树:" << endl; BST.printTree(); cout << "最大值为:" << BST.findMax() << endl; cout << "最小值为:" << BST.findMin() << endl; int temp; cout << "插入一个值:" << endl; cin >> temp; BST.insert( temp ); cout << "打印二叉树" << endl; BST.printTree(); return 0; }
运行效果:
相关文章推荐
- 关于lamp环境的搭建
- 解决部分android手机照片被系统自动旋转90度
- 非对称加密原理解析
- windows安装PyQt5
- PHP获取客户端和服务器的IP地址
- 2016-1月面试那点事
- 如何在低版本中适用Android 5.0设计Material风格
- quartz 使用笔记
- Volley框架学习(一)实现Json字符串请求
- php面试题精髓
- 攻破难啃的骨头-正则表达式
- <OpenGL>Viewing
- Bootstrap3 学习笔记
- ios开发中,xib加载view,loadNibNamed方法奔溃原因之一
- Struts2学习二、一个请求的执行流程
- SQLServer数据库如何收缩日志空间?
- zipfile的压缩和解压缩
- Android几种数据传递方法汇总
- MongoDB replSet
- Highcharts 连续的堆积面积图