• .7 二叉查找树的 建立 insert search remove 操作


      1 //二叉查找树的建立,插入,删除操作
      2 #include<stdio.h>
      3 #include<stdlib.h>
      4 
      5 typedef struct bsTree
      6 {
      7     int  k ;
      8     struct bsTree * lchild , * rchild ;
      9 }bstnode , * bstree ;
     10 
     11 void create_tree(bstree *T,int k1)
     12 {
     13      (*T) = (bstree)malloc(sizeof(bstnode));
     14      if(!(*T)){
     15         fprintf(stderr , "malloc error!
    ");
     16         exit(EXIT_FAILURE) ;
     17      }
     18      (*T)->k = k1 ;
     19      (*T)->lchild = (*T)->rchild = NULL;
     20 }
     21 
     22 bstree search(bstree T , int k )  //递归搜寻某个关键字为k 的节点
     23 {
     24     if(!T) return NULL;
     25     if(k == T->k) return T ;
     26     if(k < T->k) return search(T->lchild , k ) ;
     27     if(k > T->k) return search(T->rchild , k ) ;
     28 }
     29 
     30 bstnode *itersearch(bstree T , int k ) //非递归搜寻某个关键字为k 的节点
     31 {
     32     while(T){
     33         if(k == T->k) return T ;
     34         if(k < T->k) T = T->lchild  ;
     35         else T = T->rchild ;
     36     }
     37     return NULL;
     38 }
     39 
     40 bstree modifiedSearch(bstree T , int k1 ) //改进的搜寻 返回要insert的位置的上一个节点,用于插入操作
     41 {
     42     bstree pre  = T ;
     43     while(T){
     44         if(k1 == T->k) return T ;
     45         pre = T ;
     46         if(k1 < T->k) T = T->lchild  ;
     47         else T = T->rchild ;
     48     }
     49     return pre;
     50 }
     51 
     52 bstree modifiedSearch2(bstree T , int k1 ) //改进的搜寻 返回要insert的位置的上一个节点 用于删除操作
     53 {
     54     bstree pre  = T ;
     55     while(T){
     56         if(k1 == T->k) return pre ;
     57         pre = T ;
     58         if(k1 < T->k) T = T->lchild  ;
     59         else T = T->rchild ;
     60     }
     61     return NULL;
     62 }
     63 
     64 void insert(bstree T , bstree * tmp ,int k1 ) //插入操作  要用到modifiedSearch()函数
     65 {
     66     bstree ptr ;
     67       (*tmp) = modifiedSearch(T,k1);
     68     if( *tmp ){
     69         ptr = (bstree)malloc(sizeof(bstnode)) ;
     70         ptr->k = k1 ;
     71         ptr->lchild = ptr->rchild  = NULL ;
     72             if(k1 < (*tmp)->k) (*tmp)->lchild = ptr ;
     73             else (*tmp)->rchild = ptr ;
     74             }
     75 }
     76 
     77 void in_order_traverse(bstree T) // 中序遍历 查找树
     78 {
     79     if(T){
     80 
     81         in_order_traverse(T->lchild) ;
     82         printf("%d ",T->k) ;
     83         in_order_traverse(T->rchild) ;
     84     }
     85 }
     86 
     87 bstree remove1( bstree T , int k1)
     88 {
     89     int f ;bstree Rf  = T,R  = NULL;
     90      Rf = modifiedSearch2(T,k1) ;
     91      if(k1 > Rf->k ) {
     92         f = 1 ; R = Rf->rchild ;
     93      }else{
     94         f = 0 ; R = Rf->lchild ;
     95      }
     96 
     97     if(!R->lchild && !R->rchild){
     98         if(f) Rf->rchild = NULL ;else Rf->lchild = NULL ;
     99     }
    100     else if(R->lchild || R->rchild ){
    101                 if(f) Rf->rchild = NULL ;else Rf->lchild = NULL ;
    102     }
    103     else {
    104         bstree tmp  = R;
    105         while( tmp->lchild)
    106             tmp = R->lchild ;
    107     R->k = tmp->k ;
    108     R->lchild = NULL ;
    109     }
    110     return R ;
    111 }
    112 
    113 int main()
    114 {
    115     bstree T ;
    116     int i ;
    117     create_tree(&T , 30) ;
    118         bstree tmp ;
    119        tmp = (bstree)malloc(sizeof(bstnode)) ;
    120        insert(T , &tmp ,5) ;
    121     bstree tmp1 ;
    122        tmp1 = (bstree)malloc(sizeof(bstnode)) ;
    123        insert(T , &tmp1 ,40) ;
    124        bstree tmp2 ;
    125        tmp2 = (bstree)malloc(sizeof(bstnode)) ;
    126        insert(T , &tmp2 ,35) ;
    127        bstree tmp3 ;
    128        tmp3 = (bstree)malloc(sizeof(bstnode)) ;
    129        insert(T , &tmp2 ,45) ;
    130        remove1(T,40);
    131     in_order_traverse(T);
    132     return 0 ;
    133 }
  • 相关阅读:
    ASP.NET MVC5(二):控制器、视图与模型
    LINUX重启MYSQL的命令
    mysql unrecognized service问题解决
    UML类图几种关系的总结
    java用org.apache.poi包操作excel
    struts2.xml 中result type属性说明
    MySql Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts' 解决方法
    报错:1130-host ... is not allowed to connect to this MySql server 开放mysql远程连接 不使用localhost
    linux下使用yum安装mysql
    关于LINUX权限-bash: ./startup.sh: Permission denied
  • 原文地址:https://www.cnblogs.com/shaughn/p/3487930.html
Copyright © 2020-2023  润新知