• 大小: 0.28M
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-10
  • 语言: 其他
  • 标签: 其他  

资源简介

数据结构课程设计-二叉排序树(附详细文档说明).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<<“删除错误,不存在该元素!“< 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<<“输入选项:“;
cin>>choice;
if( choice==5 )
return false;
    cout< switch( choice )
{
case 1:
cout<<“输入要插入的数字:“< 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<<“输入要删除的数字:“< cin>>num;
flagdel = true;
Delete(num&(*root));
if( flagdel )
                cout<<“删除成功!“< break;
        case 5:
            return false;
default:
cout<<“错误选择。“<
}
cout< return true;
}
int main()
{

int i;
int numdepth;

bool first=true;
cout<<“\t\t\t\t*****初始化*****“<<

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件     169984  2012-02-10 21:05  二叉排序树 .doc

     文件       3577  2011-09-28 13:44  二叉排序树.cpp

     文件     918755  2011-09-28 13:44  二叉排序树.exe

----------- ---------  ---------- -----  ----

              1092316                    3


评论

共有 条评论