二叉排序树 c

/* ds.h */
/* Some pre define */
#ifndef _HS_H
#define _HS_H

#include <string.h>
#include <ctype.h>
#include <sys/malloc.h>
#include <stdio.h>
#include <stdlib.h>

/* State code */
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW 0

typedef int Status;
typedef int Boolean;

#endif
// bst.c

#include "ds.h"
#include <stdio.h>

typedef int KeyType;

typedef struct BiTNode{
  KeyType key;
  struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;

// Initial a bst
Status InitBST(BiTree *T){
  *T = NULL;
  return OK;
}

// search bst, if successed, p points to the node, return 1
// else, return 0
Status SearchBST(BiTree T, KeyType key, BiTree f, BiTree *p){
  /*
  *p = f, result in bus error: 10??
  */
  if(!T){*p = f; return FALSE;}
  else if(key < T->key)return SearchBST(T->lchild, key, T, p);
  else if(key > T->key)return SearchBST(T->rchild, key, T, p);
  else{
    *p = T;
    return TRUE;
  }
}

// Insert bst, if key exists, return False,
// else, insert it and return true
Status InsertBST(BiTree *T, KeyType key){
  BiTree p;
  // InitBST(p);
  if(!SearchBST(*T, key, NULL, &p)){
    BiTree s;
    s = (BiTree)malloc(sizeof(BiTNode));
    s->key = key;
    s->lchild = s->rchild = NULL;
    if(!p){
      *T = s;
    } // T == NULL
    else if(key < p->key)p->lchild = s;
    else{
      p->rchild = s;
    }
    return TRUE;
  }else{
    return FALSE;
  }
}
// Print bst
void printBiTree(BiTree T, int depth){
  if(T){
    printf("%d", T->key);
    if(T->lchild){
      printf("\n");
      printf("%*.s-", depth+1, "");
      printBiTree(T->lchild, depth+1);
    }
    if(T->rchild){
      printf("\n");
      printf("%*.s+", depth+1, "");
      printBiTree(T->rchild, depth+1);
    }
  }else{
    return;
  }
}

Status Delete(BiTree *T){
  // empty rchild, just connect its lchild
  if(!(*T)->rchild){
    BiTree q;
    q = (*T);
    (*T) = (*T)->lchild;
    free(q);
  }else if(!(*T)->lchild){
    BiTree q;
    q = (*T);
    (*T) = (*T)->rchild;
    free(q);
  }else{
    // find the second biggest node
    BiTree q;
    q = (*T);
    BiTree s;
    s = (*T)->lchild;
    while(s->rchild){
      q = s;
      s = s->rchild;
    }
    (*T)->key = s->key;
    if(q != (*T))q->rchild = s->lchild;
    else{
      q->lchild = s->lchild;
    }
    free(s);
  }
  return TRUE;
}

// delete key
Status DeleteBST(BiTree *T, KeyType key){
  if(!T)return FALSE;
  else{
    if(key == (*T)->key)
      return Delete(T);
    else if(key < (*T)->key)
      return DeleteBST(&(*T)->lchild, key);
    else{
      return DeleteBST(&(*T)->rchild, key);
    }
  }
}



int main(){
  BiTree T;
  InitBST(&T);
  int a[7] = {45, 24, 53, 12, 90, 20, 60};
  for(int i=0; i<7; i++){
    InsertBST(&T, a[i]);
  }
  printBiTree(T, 1);
  printf("\n");


  DeleteBST(&T, 24);
  printBiTree(T, 1);
  printf("\n");
}


./a.out 
45
  -24
   -12
    +20
  +53
   +90
    -60
45
  -12
   +20
  +53
   +90
    -60
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值