查找二叉排序树中最大的键值(c代码)

简介: #include#includestruct  tree{ char key; struct tree *Lchild,*Rchild;};struct tree *create_btree(struct tree *t,struct tree *r,char...

#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);};

 }

目录
相关文章
|
1月前
|
存储 算法
【数据结构】— —查找(折半查找,二叉排序树)
【数据结构】— —查找(折半查找,二叉排序树)
26 0
【数据结构】— —查找(折半查找,二叉排序树)
|
9月前
折半(二分)查找、插入相关问题
折半(二分)查找、插入相关问题
|
7月前
|
存储 算法
【查找算法】二叉排序树查找法
【查找算法】二叉排序树查找法
|
11月前
|
存储 算法 索引
【数据结构】先排序后查找的查找
【数据结构】先排序后查找的查找
43 0
单链表的按位查找和按值查找
单链表的按位查找和按值查找的代码实现讲解
319 0
二叉查找树的建立,删除,非递归和递归查找给定元素,非递归和递归查找最大元素结点和最小元素结点
二叉查找树的建立,删除,非递归和递归查找给定元素,非递归和递归查找最大元素结点和最小元素结点
|
存储 算法
查找-之有序表查找
待查找的表是有序排列的
63 0
查找-之有序表查找
|
搜索推荐
查找-之二叉排序树(查找、插入、删除)
有序的线性表采用:折半/二分、插值、斐波那契查找相比顺序查找效率得到提高,但是在插入和删除时效率低(为维持数据的有序性) 在高效实现查找操作时,如何提高插入和删除的效率? 在一些应用场景:在查找时需要插入和删除
87 0
查找-之二叉排序树(查找、插入、删除)
|
算法
算法查找——二分查找
算法查找——二分查找
64 0
算法查找——二分查找
|
存储 算法 数据处理
【数据结构】静态表查找之顺序查找、二分查找、分块查找
【数据结构】静态表查找之顺序查找、二分查找、分块查找
394 0
【数据结构】静态表查找之顺序查找、二分查找、分块查找