typedef struct node
{
char data;
struct node *lchild;
struct node *rchild;
}*Tree, Tnode;
static void CreateTree(Tree *T);
题目:二叉排序树
就一个函数:创建函数.
条件是:CreateTree()函数只能有一个参数,那就是"根".不能有同数据域相同类型的参数
创建成功者给3000金币
typedef struct node
{
char data;
struct node *lchild;
struct node *rchild;
}*Tree, Tnode;
static void CreateTree(Tree *T);
题目:二叉排序树
就一个函数:创建函数.
条件是:CreateTree()函数只能有一个参数,那就是"根".不能有同数据域相同类型的参数
创建成功者给3000金币
#include <stdlib.h>
#include <stdio.h>
#define NULL 0
typedef int KeyType;
typedef struct{
KeyType key;
}ElemType; //元素类型
typedef struct BiTNode{
ElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
BiTree find(BiTree root,KeyType key){ //在二叉排序树中查找其关键字等于给定值的结点是否存在,并输出相应信息
BiTNode *p;
p=root;
if (p==NULL) return NULL;
else if (p->data.key==key) return p;
else if (key<p->data.key) return find(p->lchild,key);
else return find(p->rchild,key);
}
void Insert(BiTree *p,BiTree t){ //在二叉排序树中插入一个新结点
if (*p==NULL) *p=t;
else if(t->data.key<(*p)->data.key) Insert(&((*p)->lchild),t);
else if(t->data.key>(*p)->data.key) Insert(&((*p)->rchild),t);
}
void inorder(BiTree p){ //中序遍历所建二叉排序树,将得到一个按关键字有序的元素序列
if(p!=NULL){
inorder(p->lchild);
printf("%5d",(p->data).key);
inorder(p->rchild);
}
}
void main(){
char ch;
KeyType key;
BiTree p,s;
int i=0;
printf("Please input datas (9999:end):\n");//建立一棵二叉排序树,元素值从键盘输入,直到输入关键字等于9999为止
scanf("%4d",&key);
p=NULL;
while(key!=9999){
s=(BiTree)malloc(sizeof(BiTNode));
(s->data).key=key;
s->lchild=s->rchild=NULL;
Insert(&p,s);
scanf("%d",&key);
}
printf("Create is completed\n");
inorder(p); //中序遍历已建立的二叉排序树
printf("\n");
do{ //二叉排序树的查找,可多次查找,并输出查找的结果
printf("Input the key you want to search:");
scanf("%4d",&key);
s=find(p,key);
if (s!=NULL) printf("success,the value is %4d ",s->data.key);
else printf("unsuccess");
printf("\ncontinue?y:n\n");getchar();
ch=getchar();
}while(ch=='y'||ch=='Y');
}
运行结果:
Please input datas (9999:end):
0 65 45 78 52 12 35 46 879 584 213 9999
Create is completed
0 12 35 45 46 52 65 78 213 584 879
Input the key you want to search:0
success,the value is 0
continue?y:n
y
Input the key you want to search:65
success,the value is 65
continue?y:n
y
Input the key you want to search:66
unsuccess
continue?y:n
n
Press any key to continue
收金币喽!!~~
#include<iostream>
using namespace std;
struct node
{
int info;
struct node *Llink,*Rlink;
};
void change(struct node *t)
{
struct node *s;
s=t->Llink;
t->Llink=t->Rlink;
t->Rlink=s;
if(t->Llink!=0)change(t->Llink);
if(t->Rlink!=0)change(t->Rlink);
}
void make(struct node *s,struct node *t)//s是单个结点,t是树
{
if(s->info<=t->info)//s->info<=t->就放在t左边,否则就放在t的右边
{
if(t->Llink==0)
t->Llink=s;
else
make(s,t->Llink);
}
else
{
if(t->Rlink==0)
t->Rlink=s;
else
make(s,t->Rlink);
}
}
void circuit(struct node *t)
{
if(t->Llink!=0)circuit(t->Llink);
cout<<t->info<<" ";
if(t->Rlink!=0)circuit(t->Rlink);
}
void dele(struct node *t)
{
if(t->Llink!=0)dele(t->Llink);
if(t->Rlink!=0)dele(t->Rlink);
delete t;
}
void main()
{
int a[50],i,j;
cout<<"请输入一组数,以0结尾:"<<endl;
for(i=0,j=0;i<100;i++,j++)
{
cin>>a[i];
if(a[i]==0)break;
}
struct node *s,*t;//t为树根
t=new struct node;
t->info=a[0];
t->Llink=0;
t->Rlink=0;
for(i=1;i<j;i++)//构造二叉树
{
s=new struct node;
s->info=a[i];
s->Llink=0;
s->Rlink=0;
make(s,t);
}
cout<<"交换前中序周游顺序:"<<endl;
circuit(t);
cout<<endl;
change(t);
cout<<"交换后中序周游顺序:"<<endl;
circuit(t);
cout<<endl;
dele(t);//释放空间
}
自己改下就可以了
我没时间改了