資源簡(jiǎn)介
數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)-二叉排序樹(附詳細(xì)文檔說明).rar

代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
using?namespace?std;
struct?node{
int?num;
node?*chl*chr;
};
bool?flagdel;
ifstream?in(“sort.txt“);
void?Insert(const?int?&tempnode?**root)
{
if(?*root==NULL?)
{
*root?=?new?node;
(*root)->chl?=?(*root)->chr?=?NULL;
(*root)->num?=?temp;
}
else?if(?(*root)->num Insert(temp&(*root)->chr);
else
Insert(temp&(*root)->chl);
}
void?Delete(const?int?&keynode?**p)
{
if(?*p==NULL?)
{
cout<<“刪除錯(cuò)誤,不存在該元素!“< flagdel?=?false;
return;
}
node?*temp=NULL;
if(?(*p)->num==key?)
{
if(?(*p)->chl==NULL?)
{
temp?=?*p;
*p?=?(*p)->chr;
delete?temp;
}
else?if(?(*p)->chr==NULL?)
{
temp?=?*p;
*p?=?(*p)->chl;
delete?temp;
}
else
{
temp?=?(*p)->chl;
node?*front=NULL;
while(?temp->chr?)
{
front?=?temp;
temp?=?temp->chr;
}
front->num?=?temp->num;
if(?front!=temp?)
front->chr?=?temp->chl;
else
front->chl?=?temp->chl;
delete?temp;
}
}
else?if(?(*p)->num>key?)
Delete(key&(*p)->chl);
else
Delete(key&(*p)->chr);
}
bool?Find(const?int?&keynode?*pnode?*netint?&depth)
{
if(?p==NULL?)
return?false;
depth++;
net?=?p;
if(?p->num==key?)
return?true;
else?if(?p->num>key?)
return?Find(keyp->chlnetdepth);
else
return?Find(keyp->chrnetdepth);
}
void?Print(const?node?*p)
{
if(?p==NULL?)
return;
cout<num<<‘?‘;
Print(p->chl);
Print(p->chr);
}
bool?Menu(node?**root)
{
int?choicenumdepth;
node?*net;
bool?suc;
cout< cout< cout<<“\t?????1.插入????????\t2.查找“< cout<<“\t?????3.遍歷????????\t4.刪除“< cout<<“\t?????5.退出菜單“< cout<<“輸入選項(xiàng):“;
cin>>choice;
if(?choice==5?)
return?false;
????cout< switch(?choice?)
{
case?1:
cout<<“輸入要插入的數(shù)字:“< cin>>num;
Insert(num&(*root));
cout<<“插入成功!“< break;
case?2:
cout<<“輸入查找元素:“< cin>>num;
if(?*root==NULL?)
cout<<“二叉樹為空,查找失敗!“< else
{
depth?=?0;
net?=?NULL;
suc?=?Find(num*rootnetdepth);
if(?suc?)
cout<<“查找成功。“< else
cout<<“查找失敗,沒有該元素。“<
cout<<“查找深度?:?“< }
break;
case?3:
cout<<“中序遍歷二叉排序樹“< if(?*root==NULL?)
cout<<“二叉樹為空!“< else
Print(*root);
break;
case?4:
cout<<“輸入要?jiǎng)h除的數(shù)字:“< cin>>num;
flagdel?=?true;
Delete(num&(*root));
if(?flagdel?)
????????????????cout<<“刪除成功!“< break;
????????case?5:
????????????return?false;
default:
cout<<“錯(cuò)誤選擇。“<
}
cout< return?true;
}
int?main()
{
int?i;
int?numdepth;
bool?first=true;
cout<<“\t\t\t\t*****初始化*****“<<
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件?????169984??2012-02-10?21:05??二叉排序樹?.doc
?????文件???????3577??2011-09-28?13:44??二叉排序樹.cpp
?????文件?????918755??2011-09-28?13:44??二叉排序樹.exe
-----------?---------??----------?-----??----
??????????????1092316????????????????????3
評(píng)論
共有 條評(píng)論