版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、/ linked binary tree implementation of a binary search tree/ implements all dictionary and bsTree methods#ifndef indexedBinarySearchTree_#define indexedBinarySearchTree_#include "indexedBSTree.h"#include "linkedBinaryTree.h"using namespace std;template<class K, class E>clas
2、s indexedBinarySearchTree : public indexedBSTree<K,E>, public linkedBinaryTree<pair<const K, E> > public: / methods of dictionary bool empty() const return this->treeSize = 0; int size() const return this->treeSize; pair<const K, E>* get(int ) const; void insert(const p
3、air<const K, E>& thePair); void erase(const K& theKey); void delete2(int); / additional method of bsTree void ascend() this->inOrderOutput(); / void makeBst(const int a); void eraseMax();/删除关键字最大的元素;template<class K, class E>pair<const K, E>* indexedBinarySearchTree<K
4、,E>:get(int Index)const binaryTreeNode<pair<const K, E> > *p =this->root; while (p != NULL) if (Index < p->leftSize) p = p->leftChild; else if (Index > p->leftSize) Index = Index-(p->leftSize+1); p = p->rightChild; else / found matching pair return &p->e
5、lement; / no matching pair return NULL;template<class K, class E>void indexedBinarySearchTree<K,E>:insert(const pair<const K, E>& thePair)/ Insert thePair into the tree. Overwrite existing / pair, if any, with same key. / find place to insert binaryTreeNode<pair<const K,
6、E> > *p = this->root, *pp = NULL; while (p != NULL) / examine p->element pp = p; / pp作为p的双亲结点 if (thePair.first < p->element.first) p->leftSize+;/索引加1 p = p->leftChild; else if (thePair.first > p->element.first) p = p->rightChild; else/如果相同 /替换原来的value p->element.
7、second = thePair.second;/不对 return; / get a node for thePair and attach to pp binaryTreeNode<pair<const K, E> > *newNode = new binaryTreeNode<pair<const K, E> > (thePair); if (this->root != NULL) / the tree is not empty if (thePair.first < pp->element.first) pp->l
8、eftChild = newNode; /pp->leftSize = 1;/索引为1 else pp->rightChild = newNode; else this->root = newNode; /this->root->leftSize = 0;/索引为0 / insertion into empty tree this->treeSize+;template<class K, class E>void indexedBinarySearchTree<K,E>:erase(const K& theKey)/根据关键字
9、删除/ Delete the pair, if any, whose key equals theKey. / search for node with key theKey binaryTreeNode<pair<const K, E> > *p = this->root, *pp = NULL; while (p != NULL && p->element.first != theKey) / move to a child of p pp = p; if (theKey < p->element.first) p = p-&
10、gt;leftChild; else p = p->rightChild; if (p = NULL) return; / no pair with key theKey / restructure tree / handle case when p has two children if (p->leftChild != NULL && p->rightChild != NULL) / two children / convert to zero or one child case / find largest element in left subtree
11、 of p binaryTreeNode<pair<const K, E> > *s = p->leftChild, *ps = p; / parent of s while (s->rightChild != NULL) / move to larger element ps = s; s = s->rightChild; / move largest from s to p, can't do a simple move / p->element = s->element as key is const binaryTreeNo
12、de<pair<const K, E> > *q = new binaryTreeNode<pair<const K, E> > (s->element, p->leftChild, p->rightChild); if (pp = NULL) this->root = q; else if (p = pp->leftChild) pp->leftChild = q; else pp->rightChild = q; if (ps = p) pp = q; else pp = ps; delete p;
13、p = s; / p has at most one child / save child pointer in c binaryTreeNode<pair<const K, E> > *c; if (p->leftChild != NULL) c = p->leftChild; else c = p->rightChild; / delete p if (p = this->root) this->root = c; else / is p left or right child of pp? if (p = pp->leftChi
14、ld) pp->leftChild = c; else pp->rightChild = c; this->treeSize-; delete p;template<class K, class E>void indexedBinarySearchTree<K,E>:delete2(int Index)/ Delete the pair, if any, whose key equals theKey. / search for node with key theKey binaryTreeNode<pair<const K, E>
15、> *p = this->root, *pp = NULL;/* if (p = NULL) return; / no pair with key theKey*/ if(Index>this->treeSize-1)/如果索引值大于节点数减1,则不存在该索引值 return ; while (p&& p->leftSize != Index)/寻找索引值为Index的结点 / move to a child of p /*pp = p; if (theKey < p->element.first) p = p->leftChil
16、d; else p = p->rightChild;*/ if (Index < p->leftSize) pp = p;/p的父节点 p->leftSize-;/每向左移动一次索引值减1 p = p->leftChild; else if (Index > p->leftSize) pp = p;/p的父节点 Index = Index-(p->leftSize+1); p = p->rightChild; /*else / found matching pair return &p->element;*/ if (p-&g
17、t;leftChild != NULL && p->rightChild != NULL)/存在左孩子和右孩子的情况 / two children / convert to zero or one child case / find largest element in left subtree of p binaryTreeNode<pair<const K, E> > *s = p->leftChild, *ps = p; / parent of s while (s->rightChild != NULL)/寻找左子树中最大的结点
18、 / move to larger element ps = s; /ps->leftSize-;/索引减1 s = s->rightChild;/s为要替换的值 / 将最大元素s移动到p但不是简单的移动 binaryTreeNode<pair<const K, E> > *q = new binaryTreeNode<pair<const K, E> > (s->element, p->leftChild, p->rightChild); if (pp = NULL)/如果要删除的结点是根节点,则直接替换根节点 t
19、his->root = q; else if (p = pp->leftChild) pp->leftChild = q; else pp->rightChild = q; if (ps = p) pp = q; else pp = ps; delete p; p = s; / p has at most one child / save child pointer in c binaryTreeNode<pair<const K, E> > *c; if (p->leftChild != NULL) c = p->leftChild
20、; else c = p->rightChild; / delete p if (p = this->root) this->root = c; else / is p left or right child of pp? if (p = pp->leftChild) pp->leftChild = c; else pp->rightChild = c; this->treeSize-; delete p;/ overload << for pairtemplate <class K, class E>ostream&
21、operator<<(ostream& out, const pair<K, E>& x) out << x.first << ' ' << x.second; return out;template <class K,class E>void indexedBinarySearchTree<K,E>:eraseMax()binaryTreeNode<pair<const K, E> > *p =this->root, *pp = NULL;whil
22、e(p != NULL)pp = p;p = p->rightChild;if(p->leftChild != NULL)pp->leftChild = p->leftChild;this->treeSize-;delete p;#endif/测试代码/ test binary search tree class#include <iostream>#include "indexedBinarySearchTree.h"using namespace std;int main(void) indexedBinarySearchTre
23、e<int,char> y; y.insert(pair<int, char>(1, 'a'); y.insert(pair<int, char>(6, 'c'); y.insert(pair<int, char>(4, 'b'); y.insert(pair<int, char>(8, 'd'); cout << "Tree size is " << y.size() << endl; cout <<
24、"Elements in ascending order are" << endl; y.ascend(); pair<const int, char> *s = y.get(3);/查找索引为3的结点 cout << "Search for 3 index " << endl; cout << s->first << ' ' << s->second << endl; / y.erase(4); y.delete2(2);/删除索
25、引为2的结点 cout << "2 index deleted " << endl; cout << "Tree size is " << y.size() << endl; cout << "Elements in ascending order are" << endl; y.ascend(); s = y.get(2);/查找索引为2的结点 cout << "Search for 2 index " << endl; /cout << s->first << ' ' << s->second << endl; y.delete2(2);/删除索引为2的结点 cout << "2 deleted " << endl; cout << "Tree size is " << y.size() << endl; cout &
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026天津市安定医院招聘第三批派遣制人员3人备考题库【网校专用】附答案详解
- 2026新疆第四师总医院春季招聘88人备考题库【历年真题】附答案详解
- 2026云南农业大学后勤服务有限公司第一批就业见习人员招聘15人备考题库【培优】附答案详解
- 2026上半年四川成都市卫生健康委员会所属部分事业单位招聘166人备考题库含答案详解(a卷)
- 2026清华大学基础医学院彭敏实验室招聘科研助理2人备考题库及完整答案详解【名师系列】
- 2026春季海南电网有限责任公司校园招聘备考题库带答案详解(考试直接用)
- 2026中智贵阳人力资本科技有限公司招聘备考题库及参考答案详解(模拟题)
- 四川大学华西厦门医院耳鼻咽喉-头颈外科招聘1人备考题库含答案详解ab卷
- 2026国家统计局琼中调查队招聘公益性岗位人员1人备考题库附参考答案详解【研优卷】
- 2026陕西蒲城高新医院招聘25人备考题库带答案详解(夺分金卷)
- 2026年湖北生态工程职业技术学院单招综合素质考试题库带答案详解
- 《特大型突发地质灾害隐患点认定与核销管理办法(试行)》
- XX街道中学初中部2026年春季家长会中期筹备工作方案:筹备家长会搭建沟通平台
- 2025年时事政治必考试题库(附含答案)
- 2026年汽车制造机器人自动化率提升:趋势、技术与实践
- 作业条件危险性评价方法LEC及案例分析
- 初中英语中考短文填空题型考点精析与知识清单
- 城市公共交通运营与服务规范
- 2026年1月浙江省高考首考英语试卷真题完整版(含答案+听力)
- 2026年国轩高科行测笔试题库
- 2025年研究生政治复试笔试题库及答案
评论
0/150
提交评论