| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2090 人关注过本帖
标题:谁给我创建棵二叉排序树,我给他3000金币
只看楼主 加入收藏
论坛
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:1372
专家分:0
注 册:2006-3-27
收藏
 问题点数:0 回复次数:6 
谁给我创建棵二叉排序树,我给他3000金币

typedef struct node
{
  char data;
  struct node *lchild;
  struct node *rchild;
}*Tree, Tnode;

static void CreateTree(Tree *T);

题目:二叉排序树

就一个函数:创建函数.

条件是:CreateTree()函数只能有一个参数,那就是"根".不能有同数据域相同类型的参数

创建成功者给3000金币

搜索更多相关主题的帖子: 金币 node struct 函数 Tree 
2006-05-18 00:04
独角龙
Rank: 1
等 级:新手上路
帖 子:221
专家分:0
注 册:2006-5-5
收藏
得分:0 
俺想要金币,可惜俺还没学到树呢,过几个月再说吧!!

奋斗改变一切!!
2006-05-18 08:48
coolg
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2006-5-21
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册


2006-05-21 21:26
wangqihui200
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2006-5-12
收藏
得分:0 
看看这个行不?我也没学多久,说的不好请见凉!!!

BiTree SearchBST(BiTree T,KeyType key)
{
//在根指针T所指二叉排序树中递归地查找某关键字等于key的数据元素,
//若查找成功,则返回指向该数据元素结点的指针,否则返回空指针
if ((!T)||EQ(key,T->data.key)) return(T); //查找结束

else if LT(key,T->data.key) return(SearchBST(T->lchild,key)); //在左子树中继续查找

else return(SearchBST(T->rchild,key)); //在右子树中继续查找
} //SearchBST
2006-05-21 22:22
lzrzhao
Rank: 1
来 自:BJ
等 级:新手上路
帖 子:37
专家分:5
注 册:2006-5-22
收藏
得分:0 

#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


收金币喽!!~~


路漫漫其修远,吾将上下而求索
2006-05-22 15:00
weikie
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2006-4-19
收藏
得分:0 

#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);//释放空间
}



自己改下就可以了
我没时间改了

2006-06-03 16:36
weikie
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2006-4-19
收藏
得分:0 
这是以前的作业
用C++写的
你应该可以看懂的了
2006-06-03 16:37
快速回复:谁给我创建棵二叉排序树,我给他3000金币
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020905 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved