Traverse(A,2*i+2);
}
}
voidDelBST(intA[],KeyTypex){
for(i=0;i<40;i++)
if(A[i]==x)
A[i]=-1;
}
voidmain(){
intx;
//intkey;
printf("建立二叉排序树,请输入序列L:
\n");
CreateBST(A);
printf("中序遍历输出序列为:
");
Traverse(A,0);
cin>>x;
if(A[i]!
=-1){
DelBST(A,x);
printf("删除x后中序遍历输出序列为:
");
Traverse(A,0);
}
else
printf("无X\n");
}
五、测试分析
程序运行:
1、用二叉链表存储结构实现:
2、用顺序存储结构实现:
六、总结与体会
通过一周的课程设计,对计算机的应用,数据结构的作用以及C++的使用都有了更深的了解。
在理论学习和上机实践的各个环节中,通过自主学习和请教老师,我收获了不少。
当然也遇到不少的问题,也正是因为这些问题引发的思考给我带了收获。
从当初不喜欢上机写程序到现在能主动写程序,从当初拿着程序不知如何下手到现在知道如何分析问题,如何用专业知识解决实际问题的转变,我发现无论是专业知识还是动手能力,自己都有很大程度的提高。
通过课程设计题目的练习,强化学生对所学知识的掌握及对问题分析和任务定义的理解。
在这段时间里,我遇到过的问题主要就是C语言和C++语言有些混淆,一些用法记不太清楚。
在老师的指导帮助下,同学们课余时间的讨论中,这些问题都一一得到了解决。
在程序的调试能力上,无形中得到了许多的提高。
例如:
头文件的使用,变量和数组的范围问题,定义变量时出现的问题等等。
在实际的上机操作过程中,不仅是让我们了解数据结构的理论知识,更重要的是培养解决实际问题的能力,所以相信通过此次实习可以提高我们分析设计能力和编程能力,为后续课程的学习及实践打下良好的基础。
我的这个是根据你的C语言全部修改成C++的,可以运行,可以答辩,放心吧
1、用二叉链表存储结构实现
#include
usingnamespacestd;
typedefintKeyType;//这个KeyType就是int类型。
typedefstructNode{
KeyTypekey;
structNode*lchild,*rchild;
}BSTNode,*BSTree;
voidInsertBST(BSTree*bst,KeyTypekey){
BSTrees;
if(*bst==NULL)/*递归结束条件*/
{
s=newBSTNode;
s->key=key;
s->lchild=NULL;
s->rchild=NULL;
*bst=s;
}
else
if(key<(*bst)->key)
InsertBST(&((*bst)->lchild),key);/*将s插入左子树*/
else
if(key>(*bst)->key)
InsertBST(&((*bst)->rchild),key);/*将s插入右子树*/
}
voidCreateBST(BSTree*bst){
KeyTypekey;
*bst=NULL;
cin>>key;
while(key!
=0)
{
InsertBST(bst,key);
cin>>key;//这个是输入判断,如果输入0停止构建二叉树
}
}
voidInOrder(BSTreeroot){
if(root!
=NULL)
{
InOrder(root->lchild);
cout<key<<"";//输出根节点的值
InOrder(root->rchild);
}
}
BSTNode*DelBST(BSTreeT,KeyTypex){
BSTNode*p,*f,*s,*q;
p=T;
f=NULL;
while(p)/*查找关键字为x的待删结点p*/
{
if(p->key==x)break;
f=p;/*f指向p结点的双亲结点*/
if(p->key>x)
p=p->lchild;
else
p=p->rchild;
}
if(p==NULL)returnNULL;/*若找不到,返回空*/
if(p->lchild==NULL)/*p无左子树*/
{
if(f==NULL)
T=p->rchild;
else
if(f->lchild==p)
f->lchild=p->rchild;
else
f->rchild=p->rchild;
deletep;
}
else/*p有左子树*/
{
q=p;
s=p->lchild;
while(s->rchild)/*在p的左子树中查找最右下结点*/
{
q=s;
s=s->rchild;
}
if(q==p)
q->lchild=s->lchild;/*将s的左子树链到q上*/
else
q->rchild=s->lchild;
p->key=s->key;
deletes;
}
returnT;
}
voidmain(){
BSTreeT;
intx;
BSTreeresult;
cout<<"建立二叉排序树,请输入序列L:
\n";
CreateBST(&T);
cout<<"中序遍历输出序列为:
";
InOrder(T);
cout<cout<<"请输入要删除的结点"<cin>>x;
result=DelBST(T,x);
if(result!
=NULL){
InOrder(result);
}
else
cout<<"无x\n";
}
2、用顺序存储结构实现:
#include
usingnamespacestd;
typedefintKeyType;
typedefstructNode{
KeyTypekey;
structNode*next;
}BSTNode,*BSTree;
intA[40]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
intj,i=0;
voidInsertBST(intA[],KeyTypekey){
intj=0;
while(A[j]!
=-1){
if(keyj=2*j+1;
else
j=2*j+2;
}
A[j]=key;
}
voidCreateBST(intA[]){
intkey;
cin>>key;
while(key!
=-1)
{
InsertBST(A,key);
cin>>key;
}
}
voidTraverse(intA[],inti){
if(A[i]!
=-1){
Traverse(A,2*i+1);
cout<Traverse(A,2*i+2);
}
}
voidDelBST(intA[],KeyTypex)
{
for(i=0;i<40;i++)
if(A[i]==x)
{A[i]=-1;
break;}
}
voidmain()
{
intx;
cout<<"建立二叉排序树,请输入序列L:
"<CreateBST(A);
cout<<"中序遍历输出序列为:
";
Traverse(A,0);
cout<cout<<"请输入要删除元素"<cin>>x;
DelBST(A,x);
if(A[i]==-1){
cout<<"删除x后中序遍历输出序列为:
";
Traverse(A,0);
}
else
cout<<"无X\n";
}
2、用顺序存储结构实现:
#include
usingnamespacestd;
typedefintKeyType;
typedefstructNode{
KeyTypekey;
structNode*next;
}BSTNode,*BSTree;
intA[40]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
intj,i=0;
voidInsertBST(intA[],KeyTypekey){
intj=0;
while(A[j]!
=-1){
if(keyj=2*j+1;
else
j=2*j+2;
}
A[j]=key;
}
voidCreateBST(intA[]){
intkey;
cin>>key;
while(key!
=-1)
{
InsertBST(A,key);
cin>>key;
}
}
voidTraverse(intA[],inti){
if(A[i]!
=-1){
Traverse(A,2*i+1);
cout<Traverse(A,2*i+2);
}
}
voidDelBST(intA[],KeyTypex)
{
for(i=0;i<40;i++)
if(A[i]==x)
{A[i]=-1;
break;}
}
voidmain()
{
intx;
cout<<"建立二叉排序树,请输入序列L:
"<CreateBST(A);
cout<<"中序遍历输出序列为:
";
Traverse(A,0);
cout<cout<<"请输入要删除元素"<cin>>x;
DelBST(A,x);
if(A[i]==-1){
cout<<"删除x后中序遍历输出序列为:
";
Traverse(A,0);
}
else
cout<<"无X\n";
}