索引二叉搜索树.docx
《索引二叉搜索树.docx》由会员分享,可在线阅读,更多相关《索引二叉搜索树.docx(13页珍藏版)》请在冰豆网上搜索。
索引二叉搜索树
//linkedbinarytreeimplementationofabinarysearchtree
//implementsalldictionaryandbsTreemethods
#ifndefindexedBinarySearchTree_
#defineindexedBinarySearchTree_
#include"indexedBSTree.h"
#include"linkedBinaryTree.h"
usingnamespacestd;
template
classindexedBinarySearchTree:
publicindexedBSTree,
publiclinkedBinaryTree>
{
public:
//methodsofdictionary
boolempty()const{returnthis->treeSize==0;}
intsize()const{returnthis->treeSize;}
pair*get(int)const;
voidinsert(constpair&thePair);
voiderase(constK&theKey);
voiddelete2(int);
//additionalmethodofbsTree
voidascend(){this->inOrderOutput();}
//voidmakeBst(constinta[]);
voideraseMax();//删除关键字最大的元素
};
template
pair*indexedBinarySearchTree:
:
get(intIndex)const
{
binaryTreeNode>*p=this->root;
while(p!
=NULL)
{
if(IndexleftSize)
{
p=p->leftChild;
}
else
{
if(Index>p->leftSize)
{
Index=Index-(p->leftSize+1);
p=p->rightChild;
}
else//foundmatchingpair
return&p->element;
}
}
//nomatchingpair
returnNULL;
}
template
voidindexedBinarySearchTree:
:
insert(constpair&thePair)
{//InsertthePairintothetree.Overwriteexisting
//pair,ifany,withsamekey.
//findplacetoinsert
binaryTreeNode>*p=this->root,*pp=NULL;
while(p!
=NULL)
{//examinep->element
pp=p;
//pp作为p的双亲结点
if(thePair.firstelement.first)
{
p->leftSize++;//索引加1
p=p->leftChild;
}
else
{
if(thePair.first>p->element.first)
p=p->rightChild;
else//如果相同
{//替换原来的value
p->element.second=thePair.second;//不对
return;
}
}
}
//getanodeforthePairandattachtopp
binaryTreeNode>*newNode
=newbinaryTreeNode>(thePair);
if(this->root!
=NULL)//thetreeisnotempty
{
if(thePair.firstelement.first)
{
pp->leftChild=newNode;
//pp->leftSize=1;//索引为1
}
else
pp->rightChild=newNode;
}
else
{
this->root=newNode;
//this->root->leftSize=0;//索引为0
}//insertionintoemptytree
this->treeSize++;
}
template
voidindexedBinarySearchTree:
:
erase(constK&theKey)//根据关键字删除
{//Deletethepair,ifany,whosekeyequalstheKey.
//searchfornodewithkeytheKey
binaryTreeNode>*p=this->root,
*pp=NULL;
while(p!
=NULL&&p->element.first!
=theKey)
{//movetoachildofp
pp=p;
if(theKeyelement.first)
p=p->leftChild;
else
p=p->rightChild;
}
if(p==NULL)
return;//nopairwithkeytheKey
//restructuretree
//handlecasewhenphastwochildren
if(p->leftChild!
=NULL&&p->rightChild!
=NULL)
{//twochildren
//converttozerooronechildcase
//findlargestelementinleftsubtreeofp
binaryTreeNode>*s=p->leftChild,
*ps=p;//parentofs
while(s->rightChild!
=NULL)
{//movetolargerelement
ps=s;
s=s->rightChild;
}
//movelargestfromstop,can'tdoasimplemove
//p->element=s->elementaskeyisconst
binaryTreeNode>*q=
newbinaryTreeNode>
(s->element,p->leftChild,p->rightChild);
if(pp==NULL)
this->root=q;
elseif(p==pp->leftChild)
pp->leftChild=q;
else
pp->rightChild=q;
if(ps==p)pp=q;
elsepp=ps;
deletep;
p=s;
}
//phasatmostonechild
//savechildpointerinc
binaryTreeNode>*c;
if(p->leftChild!
=NULL)
c=p->leftChild;
else
c=p->rightChild;
//deletep
if(p==this->root)
this->root=c;
else
{//ispleftorrightchildofpp?
if(p==pp->leftChild)
pp->leftChild=c;
elsepp->rightChild=c;
}
this->treeSize--;
deletep;
}
template
voidindexedBinarySearchTree:
:
delete2(intIndex)
{//Deletethepair,ifany,whosekeyequalstheKey.
//searchfornodewithkeytheKey
binaryTreeNode>*p=this->root,
*pp=NULL;
/*if(p==NULL)
return;//nopairwithkeytheKey*/
if(Index>this->treeSize-1)//如果索引值大于节点数减1,则不存在该索引值
return;
while(p&&p->leftSize!
=Index)//寻找索引值为Index的结点
{//movetoachildofp
/*pp=p;
if(theKeyelement.first)
p=p->leftChild;
else
p=p->rightChild;*/
if(IndexleftSize)
{
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//foundmatchingpair
return&p->element;*/
}
}
if(p->leftChild!
=NULL&&p->rightChild!
=NULL)//存在左孩子和右孩子的情况
{//twochildren
//converttozerooronechildcase
//findlargestelementinleftsubtreeofp
binaryTreeNode>*s=p->leftChild,
*ps=p;//parentofs
while(s->rightChild!
=NULL)//寻找左子树中最大的结点
{//movetolargerelement
ps=s;
//ps->leftSize--;//索引减1
s=s->rightChild;//s为要替换的值
}
//将最大元素s移动到p但不是简单的移动
binaryTreeNode>*q=
newbinaryTreeNode>
(s->element,p->leftChild,p->rightChild);
if(pp==NULL)//如果要删除的结点是根节点,则直接替换根节点
this->root=q;
elseif(p==pp->leftChild)
pp->leftChild=q;
else
pp->rightChild=q;
if(ps==p)pp=q;
elsepp=ps;
deletep;
p=s;
}
//phasatmostonechild
//savechildpointerinc
binaryTreeNode>*c;
if(p->leftChild!
=NULL)
c=p->leftChild;
else
c=p->rightChild;
//deletep
if(p==this->root)
this->root=c;
else
{//ispleftorrightchildofpp?
if(p==pp->leftChild)
pp->leftChild=c;
elsepp->rightChild=c;
}
this->treeSize--;
deletep;
}
//overload<template
ostream&operator<<(ostream&out,constpair&x)
{out<template
voidindexedBinarySearchTree:
:
eraseMax()
{
binaryTreeNode>*p=this->root,
*pp=NULL;
while(p!
=NULL)
{
pp=p;
p=p->rightChild;
}
if(p->leftChild!
=NULL)
{
pp->leftChild=p->leftChild;
}
this->treeSize--;
deletep;
}
#endif
//测试代码
//testbinarysearchtreeclass
#include
#include"indexedBinarySearchTree.h"
usingnamespacestd;
intmain(void)
{
indexedBinarySearchTreey;
y.insert(pair(1,'a'));
y.insert(pair(6,'c'));
y.insert(pair(4,'b'));
y.insert(pair(8,'d'));
cout<<"Treesizeis"<cout<<"Elementsinascendingorderare"<y.ascend();
pair*s=y.get(3);//查找索引为3的结点
cout<<"Searchfor3index"<cout<first<<''<second<//y.erase(4);
y.delete2
(2);//删除索引为2的结点
cout<<"2indexdeleted"<cout<<"Treesizeis"<cout<<"Elementsinascendingorderare"<y.ascend();
s=y.get
(2);//查找索引为2的结点
cout<<"Searchfor2index"<//cout<first<<''<second<y.delete2
(2);//删除索引为2的结点
cout<<"2deleted"<cout<<"Treesizeis"<cout<<"Elementsinascendingorderare"<y.ascend();
/*//s=y.find(6);
cout<<"Searchfor6succeeds"<//cout<first<<''<second<//y.erase(6);
cout<<"6deleted"<cout<<"Treesizeis"<cout<<"Elementsinascendingorderare"<y.ascend();
//s=y.find
(1);
cout<<"Searchfor1succeeds"<//cout<first<<''<second<y.erase
(1);
cout<<"1deleted"<cout<<"Treesizeis"<cout<<"Elementsinascendingorderare"<y.ascend();*/
}