二叉搜索树 简单函数归纳

//搜索函数//————递归

struct node *Find(int x, struct node *t)
    {
     if (t != NULL) 
     return NULL;         // 没有找到x
     if (x > t -> data)
       return Find(x, t -> right);  //右子树寻找
     else
        if (x < t -> data)
       return Find(x, t -> left);    //左子树寻找
     else
        if (x == t -> data)
           return t;       //找到 x
}

//搜索函数//——————迭代

struct node *Find(int x, struct node *t)
    {
     while(t)         
     {
     if (x > t -> data)
       t = t -> right;  //右子树寻找
     else
        if (x < t -> data)
       t = t -> left;    //左子树寻找
     else
        if (x == t -> data)
           return t;       //找到 x
           }
    return NULL;  // 没有找到x
}

//查找最小元素//——————递归

struct node *findmin(x, struct node *t)
 {
   if (t == NULL) return NULL;
   else
      if (t -> left == NULL) 
      return t;
    else
       return findmin(x, t -> left);

 }

//查找最小元素//——————迭代

struct node *findmin(x, struct node *t)
 {
   if (t != BULL)
      {
        while(t -> left != NULL)
          t = t -> left;
      }
    return t;
 }

//查找最大元素//————递归

struct node *findmax(x, struct node *t)
 {
   if (t == NULL) return NULL;
   else
      if (t -> right == NULL) 
      return t;
    else
       return findmin(x, t -> right);

 }

//查找最大元素//————迭代

struct node *findmin(x, struct node *t)
 {
   if (t != BULL)
      {
        while(t -> right != NULL)
          t = t -> right;
      }
    return t;
 }

//插入函数//

struct node *Insert (int x, struct node *t)
  {
    if (t == NULL)   //进行插入操作//
    {
      t = (struct node *)malloc(sizeof(struct node));
      t -> data = x;
      t -> left = NULL;
      t -> right = NULL;
    }
    else
      if (x < t -> data)
        t -> left = Insert(x, t -> left);
        else
          if (x > t -> data)
            t -> right = Insert(x, t -> right);
  return t;
  }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值