《数据结构与算法分析--C++描述》(第三版)学习笔记系列一:BST的实现
2009-10-04 17:18
946 查看
实现代码: "BST.h": #ifndef __BST_H__ #define __BST_H__ #include <iostream> using namespace std; //*********BST类的接口**************** //void insert(x) 插入 //void remove(x) 删除 //bool contains(x) 检查是否包含 //const Comparable& findMin() 查找最小元素 //const Comparable& findMax() 查找最大元素 //bool isEmpty() 判断是否为空 //void makeEmpty() 清空 //void printTree() 打印 template<typename Comparable> class BST { public: BST():root(NULL) { } BST(const BST& rhs):root(NULL) { *this = rhs; } const BST& operator=(const BST& rhs) { if(this != &rhs) { makeEmpty(); root = clone(rhs.root); } return *this; } ~BST() { makeEmpty(); } //查找最小项 const Comparable& findMin() const { if(isEmpty()) return NULL; return findMin(root)->element; } //查找最大项 const Comparable& findMax() const { if(!isEmpty()) return findMax(root)->element; else return NULL; } //判断x是否在树中 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); } //插入x void insert(const Comparable& x) { insert(x, root); } //删除x void remove(const Comparable& x) { remove(x, root); } private: //节点数据结构 struct BSTNode { Comparable element; BSTNode* left; BSTNode* right; BSTNode(const Comparable & theElement, BSTNode* lp, BSTNode* rp ):element(theElement), left(lp), right(rp) { } }; BSTNode* root; void insert(const Comparable& x, BSTNode* &t) { if(NULL == t) t = new BSTNode(x, NULL, NULL); else if(x < t->element) insert(x, t->left); else if(t->element < x) insert(x, t->right); else ; //已存在,暂无操作 } //insert非递归版本 /******************************************************* 研究后再加入 *********************************************************/ void remove(const Comparable& x, BSTNode*& t) { if(NULL == t) 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)->element; //首先找到右子树中最小的项 remove(t->element, t->right); //其次在右子树中删除最小的项 } else //一个孩子或叶子节点的情况 { BSTNode* oldNode = t; t = (t->left != NULL) ? t->left : t->right; delete oldNode; } } //remove非递归版本 /******************************************************* 研究后再加入 *********************************************************/ BSTNode* findMin(BSTNode* t) const { if(NULL == t) return NULL; if(t->left == NULL) return t; return findMin(t->left); } //findMin非递归版本 /******************************************************* BSTNode* findMin(BSTNode* t) const { if(t != NULL) while(t->left != NULL) t = t->left; return t; } *********************************************************/ BSTNode* findMax(BSTNode* t) const { if(NULL == t) return NULL; if(t->right == NULL) return t; return findMax(t->right); } //findMax非递归版本 /******************************************************* BSTNode* findMax(BSTNode* t) const { if(t != NULL) while(t->right != NULL) t = t->right; return t; } *********************************************************/ bool contains(const Comparable& x, BSTNode* t) const { if(t == NULL) return false; else if(x < t->element) return contains(x, t->left); else if(t->element < x) return t->right; else return true; } //contains非递归版本 /******************************************************* bool contains(const Comparable& x, BSTNode* t) const { while(t != NULL) if(x < t->element) t = t->left; else if(t->element < x) t = t->right; else return true; return false; } *********************************************************/ void makeEmpty(BSTNode* &t) { if(t != NULL) { makeEmpty(t->left); makeEmpty(t->right); delete t; } t = NULL; } void printTree(BSTNode* t, ostream& out) const { if(t != NULL) { printTree(t->left, out); out << t->element << endl; printTree(t->right, out); } } BSTNode* clone(BSTNode* t) const { if(t == NULL) return NULL; else return new BSTNode(t->element, clone(t->left), clone(t->right)); } }; #endif //__BST_H__ 测试代码: "BSTTest.cpp": #include "BST.h" int main() { BST<int> BSTree; cout << "此时树" << (BSTree.isEmpty()?"为":"不为") << "空" << endl; BSTree.insert(10); BSTree.insert(20); BSTree.insert(30); BSTree.insert(15); BSTree.insert(25); BSTree.insert(35); BSTree.insert(13); BSTree.insert(17); BSTree.insert(22); BSTree.insert(29); cout << "依次插入10、20、30、15、25、35、13、17、22、29后输出节点值为: " << endl; BSTree.printTree(); cout << "最小值为: " << BSTree.findMin() << "最大值为: " << BSTree.findMax() << endl; cout << "35" << (BSTree.contains(35)?"在":"不在") << "树中。" << endl; cout << "100" << (BSTree.contains(35)?"在":"不在") << "树中。" << endl; cout << "此时树" << (BSTree.isEmpty()?"为":"不为") << "空" << endl; BSTree.remove(0); BSTree.remove(20); BSTree.remove(40); BSTree.remove(30); BSTree.remove(20); BSTree.remove(10); cout << "依次删除0、20、40、30、20、20、10后输出节点值为: " << endl; BSTree.printTree(); cout << "最小值为: " << BSTree.findMin() << "最大值为: " << BSTree.findMax() << endl; cout << "此时树" << (BSTree.isEmpty()?"为":"不为") << "空" << endl; BSTree.makeEmpty(); cout << "清空后输出节点值为: " << endl; BSTree.printTree(); cout << "最小值为: " << BSTree.findMin() << "最大值为: " << BSTree.findMax() << endl; cout << "此时树" << (BSTree.isEmpty()?"为":"不为") << "空" << endl; return 0; }
相关文章推荐
- 一个无聊男人的疯狂《数据结构与算法分析-C++描述》学习笔记 用C++/lua/python/bash的四重实现(4)二分搜索算法
- 一个无聊男人的疯狂《数据结构与算法分析-C++描述》学习笔记 用C++/lua/python/bash的四重实现(3) 最大子序列和问题
- 一个无聊男人的疯狂《数据结构与算法分析-C++描述》学习笔记 用C++/lua/python/bash的四重实现(7)习题2.8 随机数组的三种生成算法
- 一个无聊男人的疯狂《数据结构与算法分析-C++描述》学习笔记 习题2.8 随机数组的三种生成算法(补) 将bash的实现翻译成比较纯正的bash风格
- 一个无聊男人的疯狂《数据结构与算法分析-C++描述》学习笔记 用C++/lua/python/bash的四重实现(1) f(x) = 2f(x-1) + x^2
- 一个无聊男人的疯狂《数据结构与算法分析-C++描述》学习笔记 用C++/lua/python/bash的四重实现(2) IntCell类
- 一个无聊男人的疯狂《数据结构与算法分析-C++描述》学习笔记 用C++/lua/python/bash的四重实现(5)欧几里得算法欧几里得算法求最大公约数
- 一个无聊男人的疯狂《数据结构与算法分析-C++描述》学习笔记 用C++/lua/python/bash的四重实现(6)高效率的幂运算
- PE文件学习系列笔记四-C++实现PE文件的分析
- 学习笔记---单例的C++实现(从大神那看到的)
- C_PlusPlus学习笔记 - 1_C++简单程序设计 (C++语言程序设计【第三版】 郑莉等,清华大学出版社)
- [C++学习笔记14]动态创建对象(定义静态方法实现在map查找具体类名对应的创建函数,并返回函数指针,map真是一个万能类)good
- c++学习笔记—二叉树基本操作的实现
- 【C++数据结构学习笔记---线性表】用数组实现线性表
- 【C++数据结构学习笔记---线性表】用单链表实现线性表
- C_PlusPlus学习笔记 - 6_继承与派生(C++语言程序设计【第三版】 郑莉等,清华大学出版社)
- 【学习笔记】Thinking in java (第三版)第五章 隐藏实现(Hiding the implementation)
- C++ 学习笔记(二) 多态的实现
- c++学习笔记—单链表基本操作的实现
- 【C++】学习笔记草稿版系列9(指向类成员的指针)