索引二叉搜索树Word下载.docx

上传人:b****8 文档编号:22257432 上传时间:2023-02-03 格式:DOCX 页数:13 大小:16.94KB
下载 相关 举报
索引二叉搜索树Word下载.docx_第1页
第1页 / 共13页
索引二叉搜索树Word下载.docx_第2页
第2页 / 共13页
索引二叉搜索树Word下载.docx_第3页
第3页 / 共13页
索引二叉搜索树Word下载.docx_第4页
第4页 / 共13页
索引二叉搜索树Word下载.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

索引二叉搜索树Word下载.docx

《索引二叉搜索树Word下载.docx》由会员分享,可在线阅读,更多相关《索引二叉搜索树Word下载.docx(13页珍藏版)》请在冰豆网上搜索。

索引二叉搜索树Word下载.docx

*get(int)const;

voidinsert(constpair<

&

thePair);

voiderase(constK&

theKey);

voiddelete2(int);

//additionalmethodofbsTree

voidascend(){this->

inOrderOutput();

//voidmakeBst(constinta[]);

voideraseMax();

//删除关键字最大的元素

};

*indexedBinarySearchTree<

:

get(intIndex)const

binaryTreeNode<

*p=this->

root;

while(p!

=NULL)

{

if(Index<

p->

leftSize)

p=p->

leftChild;

}

else

if(Index>

Index=Index-(p->

leftSize+1);

rightChild;

else//foundmatchingpair

return&

p->

element;

//nomatchingpair

returnNULL;

voidindexedBinarySearchTree<

insert(constpair<

thePair)

{//InsertthePairintothetree.Overwriteexisting

//pair,ifany,withsamekey.

//findplacetoinsert

*p=this->

root,*pp=NULL;

{//examinep->

element

pp=p;

//pp作为p的双亲结点

if(thePair.first<

element.first)

p->

leftSize++;

//索引加1

if(thePair.first>

else//如果相同

{//替换原来的value

element.second=thePair.second;

//不对

return;

//getanodeforthePairandattachtopp

*newNode

=newbinaryTreeNode<

(thePair);

if(this->

root!

=NULL)//thetreeisnotempty

pp->

leftChild=newNode;

//pp->

leftSize=1;

//索引为1

rightChild=newNode;

this->

root=newNode;

//this->

root->

leftSize=0;

//索引为0

}//insertionintoemptytree

treeSize++;

erase(constK&

theKey)//根据关键字删除

{//Deletethepair,ifany,whosekeyequalstheKey.

//searchfornodewithkeytheKey

root,

*pp=NULL;

=NULL&

element.first!

=theKey)

{//movetoachildofp

if(theKey<

if(p==NULL)

//nopairwithkeytheKey

//restructuretree

//handlecasewhenphastwochildren

if(p->

leftChild!

rightChild!

{//twochildren

//converttozerooronechildcase

//findlargestelementinleftsubtreeofp

*s=p->

leftChild,

*ps=p;

//parentofs

while(s->

{//movetolargerelement

ps=s;

s=s->

//movelargestfromstop,can'

tdoasimplemove

//p->

element=s->

elementaskeyisconst

*q=

newbinaryTreeNode<

(s->

element,p->

leftChild,p->

rightChild);

if(pp==NULL)

root=q;

elseif(p==pp->

leftChild)

leftChild=q;

rightChild=q;

if(ps==p)pp=q;

elsepp=ps;

deletep;

p=s;

//phasatmostonechild

//savechildpointerinc

*c;

c=p->

//deletep

if(p==this->

root)

root=c;

{//ispleftorrightchildofpp?

if(p==pp->

leftChild=c;

elsepp->

rightChild=c;

treeSize--;

delete2(intIndex)

/*if(p==NULL)

//nopairwithkeytheKey*/

if(Index>

this->

treeSize-1)//如果索引值大于节点数减1,则不存在该索引值

return;

while(p&

leftSize!

=Index)//寻找索引值为Index的结点

/*pp=p;

*/

if(Index<

{

pp=p;

//p的父节点

p->

leftSize--;

//每向左移动一次索引值减1

p=p->

}

else

if(Index>

pp=p;

/*else//foundmatchingpair

return&

=NULL)//存在左孩子和右孩子的情况

=NULL)//寻找左子树中最大的结点

//ps->

//索引减1

//s为要替换的值

//将最大元素s移动到p但不是简单的移动

if(pp==NULL)//如果要删除的结点是根节点,则直接替换根节点

//overload<

<

forpair

template<

ostream&

operator<

(ostream&

out,constpair<

K,E>

x)

{out<

x.first<

'

<

x.second;

returnout;

classK,classE>

eraseMax()

*pp=NULL;

while(p!

if(p->

pp->

leftChild=p->

#endif

//测试代码

//testbinarysearchtreeclass

#include<

iostream>

indexedBinarySearchTree.h"

 

intmain(void)

indexedBinarySearchTree<

int,char>

y;

y.insert(pair<

int,char>

(1,'

a'

));

(6,'

c'

(4,'

b'

(8,'

d'

cout<

"

Treesizeis"

y.size()<

endl;

Elementsinascendingorderare"

y.ascend();

constint,char>

*s=y.get(3);

//查找索引为3的结点

Searchfor3index"

s->

first<

second<

//y.erase(4);

y.delete2

(2);

//删除索引为2的结点

2indexdeleted"

s=y.get

(2);

//查找索引为2的结点

Searchfor2index"

//cout<

2deleted"

/*//s=y.find(6);

Searchfor6succeeds"

//y.erase(6);

6deleted"

//s=y.find

(1);

Searchfor1succeeds"

//cout<

y.erase

(1);

1deleted"

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 可爱清新

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1