数据结构 课程设计 二叉排序树查找C语言代码1 含测试序列数据结构 课程设计 二叉排序树查找C语言代码1 含测试序列
#include "stdio.h" #include "stdlib.h" #include "malloc.h"
typedef int KeyType; typedef struct
{
KeyType key;
}DataType;
typedef struct node {
DataType data;
struct node *leftChild; struct node *rightChild; }BiTreeNode;
BiTreeN...
数据结构 课程
二叉排序树查找C语言代码1 含测试序列
#include "stdio.h" #include "stdlib.h" #include "malloc.h"
typedef int KeyType; typedef struct
{
KeyType key;
}DataType;
typedef struct node {
DataType data;
struct node *leftChild; struct node *rightChild; }BiTreeNode;
BiTreeNode *root=NULL; DataType a[20],item; int i,n,num=0;
/*查找*/
void Search(BiTreeNode *root,DataType item)
{
BiTreeNode *p;
if(root!=NULL)
{
p=root;
while(p!=NULL)
{
if(p->data.key==item.key) {
num=1;
break;
}
if(item.key>p->data.key) {
p=p->rightChild;
}
else
{
p = p->leftChild;
}
if(p==NULL)
{
num=0;
}
}
}
if(num==1)
{
for(i=0;i
data.key==item.key) return 0;
parent=current;
if(current->data.keyrightChild;
else current=current->leftChild;
}
p=(BiTreeNode *)malloc(sizeof(BiTreeNode));
if(p==NULL)
{
printf("Lack of space!");
exit(1);
}
p->data=item;
p->leftChild=NULL;
p->rightChild=NULL;
if(parent==NULL) *root=p;
else if(item.keydata.key)
parent->leftChild=p;
else
parent->rightChild=p;
return 1;
}
/*插入数组*/
void InsertN()
{
printf("Please enter some integer:\n");
for(i=0;i<20;i++)
{
scanf("%d",&a[i]);
if(a[i].key==-1) /*判断是否非数字*/
{
n=i;
break;
}
else if(a[i].key<=0)
{
printf("Number must >0.\n");
i--;
}
if(i==19)
{
n=20;
break;
}
}
printf("\nThe number you entered are:\n");
for(i=0;ileftChild!=NULL)
InTraverse(root->leftChild);
printf("%d ",root->data.key);
if(root->rightChild!=NULL)
InTraverse(root->rightChild); }
/*删除*/ /*DataType item*/
void Destroy()
{
/*BiTreeNode **root;
if((*root)!=NULL&&(*root)->leftChild!=NULL)
Destroy(&(*root)->leftChild); if((*root)!=NULL&&(*root)->rightChild!=NULL)
Destroy(&(*root)->rightChild); free(*root);
*/printf("Destory has been finished!");
}
/*打印二叉树*/
void PrintBiTree(BiTreeNode *bt,int n) {
if(bt==NULL)return;
PrintBiTree(bt->rightChild,n+1); for(i=0;i=0)
{
printf("---");
printf("%d\n",bt->data);
}
PrintBiTree(bt->leftChild,n+1); }
/*主函数*/
void main()
{
int d,t=0;
do
{
printf("\n");
printf(" 1. Insert\n"); /*y 第二次插入的重复数字没有被记录,记录了新的数字,相同数值被忽略*/
printf(" 2. Search\n");
printf(" 3. Destroy\n");
printf(" 4. Print\n");
printf(" 5. InTraverse\n"); /*y*/
printf(" 6. Exit!\n\n");
printf("Input command number:\n");
scanf("%d",&d);
switch(d)
{
case 1: InsertN(); break;
case 2:
{
printf("\n Search number:\n");
scanf("%d",&item.key);
Search(root,item);
break;
}
case 3: /* 判断是否存在该数值*/
{
printf("Destroy number:\n");
scanf("%d",&item.key);
Search(root,item);
if(num==0)
{
printf("Destroy could not be done.\n");
break;
}
else
Destroy();
break;
}
case 4: PrintBiTree(root,0); break;
case 5: InTraverse(root); break;
case 6: t=1; break;
default: printf("The number should between 1 and 6.\n");
}
} while(!t);
}
测试序列 二叉排序树查找
1. Insert
2. Search
3. Destroy
4. Print
5. InTraverse
6. Exit!
Input command number:
1
Please enter some integer:
10
50
32
5
76
9
40
88
-1
The number you entered are:
10 50 32 5 76 9 40 88
1. Insert
2. Search
3. Destroy
4. Print
5. InTraverse
6. Exit!
Input command number:
4
---88
---76
---50
---40
---32
---10
---9
---5
1. Insert
2. Search
3. Destroy
4. Print
5. InTraverse
6. Exit!
Input command number: 2
Search number: 9
Element 9 exists in a[5]
1. Insert
2. Search
3. Destroy
4. Print
5. InTraverse
6. Exit!
Input command number: 1
Please enter some integer:
120
6
-1
The number you entered are:
120 6
1. Insert
2. Search
3. Destroy
4. Print
5. InTraverse
6. Exit!
Input command number: 4
---120
---88
---76
---50
---40
---32
---10
---9
---6
---5
1. Insert
2. Search
3. Destroy
4. Print
5. InTraverse
6. Exit!
Input command number:
5
5 6 9 10 32 40 50 76 88 120
1. Insert
2. Search
3. Destroy
4. Print
5. InTraverse
6. Exit!
Input command number:
本文档为【数据结构 课程设计 二叉排序树查找C语言代码1 含测试序列】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。