查找二叉排序树中最大的键值(c代码)
2005-09-03 10:09
309 查看
#include<stdio.h>
#include<conio.h>
struct tree{
char key;
struct tree *Lchild,*Rchild;
};
struct tree *create_btree(struct tree *t,struct tree *r,char key)
{
if (r ==0 )
{
r=new (struct tree);
if ( r == 0)
{
printf("Out of memory/n"); return 0 ;
}
r->Lchild= 0; r->Rchild=0; r->key=key;
if (t)
{
if(key<t->key) t->Lchild=r;
else
t->Rchild=r;
}
else
{
r->Rchild=0; r->Lchild = 0;
}
return r;
}
if (key < r->key)
create_btree(r,r->Lchild,key);
if(key>=r->key)
create_btree(r,r->Rchild,key);
return t;
};
struct tree *search_btree(struct tree *t,char key1)
{ struct tree *p;
p=t;
if (!p)
{ printf("Empty btree/n"); return p; }
while(p->Rchild) {p=p->Rchild;};
printf("Successful search/n key1=%c/n",p->key);
return p;
};
void main()
{
char s[100], c, e,q;
struct tree *t=0, *p;
printf("Input a letter for Creating the Binary_Tree ( Directly press <Enter> to stop ):/n");
while (*s){
printf("/nInput a letter: ");
e=getch(); /*#include<conio.h>*/
putch(e); /*#include<conio.h>*/
if(e==13) break;
if (!t)
t=create_btree(t,t,e);
else
create_btree(t,t,e);
};
search_btree(t,0);
printf("/n");
printf("结束请按q!");
if(getchar()=='q') printf("再见");
else {while(1);};
}
#include<conio.h>
struct tree{
char key;
struct tree *Lchild,*Rchild;
};
struct tree *create_btree(struct tree *t,struct tree *r,char key)
{
if (r ==0 )
{
r=new (struct tree);
if ( r == 0)
{
printf("Out of memory/n"); return 0 ;
}
r->Lchild= 0; r->Rchild=0; r->key=key;
if (t)
{
if(key<t->key) t->Lchild=r;
else
t->Rchild=r;
}
else
{
r->Rchild=0; r->Lchild = 0;
}
return r;
}
if (key < r->key)
create_btree(r,r->Lchild,key);
if(key>=r->key)
create_btree(r,r->Rchild,key);
return t;
};
struct tree *search_btree(struct tree *t,char key1)
{ struct tree *p;
p=t;
if (!p)
{ printf("Empty btree/n"); return p; }
while(p->Rchild) {p=p->Rchild;};
printf("Successful search/n key1=%c/n",p->key);
return p;
};
void main()
{
char s[100], c, e,q;
struct tree *t=0, *p;
printf("Input a letter for Creating the Binary_Tree ( Directly press <Enter> to stop ):/n");
while (*s){
printf("/nInput a letter: ");
e=getch(); /*#include<conio.h>*/
putch(e); /*#include<conio.h>*/
if(e==13) break;
if (!t)
t=create_btree(t,t,e);
else
create_btree(t,t,e);
};
search_btree(t,0);
printf("/n");
printf("结束请按q!");
if(getchar()=='q') printf("再见");
else {while(1);};
}
相关文章推荐
- 【数据结构树表的查找】二叉排序树详解和代码(生成、插入、查找、最大值、最小值、删除、中序遍历、销毁)
- 求一个二叉排序树查找成功的ASL的最大值
- BST二叉排序树的查找和删除的完整C代码
- 动态查找之二叉排序树,C++代码实现
- Java实现查找当前字符串最大回文串代码分享
- 一行代码实现数组中最大值与最小值的查找
- 用二分法查找出满足条件值的最大下标(满足条件的值有多个)
- java中数据类型间的最大值,最小值及转换程序代码实现
- 静态查找的方法:顺序查找、对半查找、分块查找,C++代码实现
- hdu 2025 查找最大元素
- 数据结构编程笔记二十五:第九章 查找 二叉排序树(动态查找表)查找算法的实现
- 用递归实现查找字符串中相同字符连续出现次数的最大值
- 查找最大元素
- 二维数组, 二分法查找数组元素下标 快速查找数组最大值 数组作为实参的问题
- 二叉排序树的查找、插入和删除
- hdu 2025 查找最大元素 解题报告+疑问
- ruby代码将查找数据存入execl表的处理.rb
- 查找服务器中占用空间最大的目录及文件
- 数据结构实验之查找一:二叉排序树
- 二叉排序树的插入创建与查找