二叉排序树的实现

1.编写SearchBST(T,key)与InsertBST(T,key)的伪代码与实现:

SearchBST(T,key)

伪代码:

SearchBST(T,key)
{
   if(T为空||T的值等于key)返回 T;
   else if(T的值>key)返回 SearchBST(T->lchild,key);
   else 返回 SearchBST(T->rchild,key);  
}

代码:

void Search(BiTree T, char key) {
    if (T == NULL || T->data == key)
       return T;
    else if (T->data > key)
       return Search(T->lchild, key);
    else
       return Search(T->rchild, key);

}

InsertBST(T, key)

伪代码:

InsertBST(T, key)
{
 if (T为空)
       创建新的根节点;
       把key赋予根的值;
    else if (T == key)返回0;
    else if (T的值 > key)返回InsertBST(T->lchild,key);
    else if (T的值 < key)返回InsertBST(T->rchild,key);
}

代码:

InsertBST(T, key)
{
    if (T == NULL) {
        T = new BiTNode;
        T->data = key;
        T->lchild == NULL;
        T->rchild == NULL;
        return 1;
    }
    else if (T->data == key)
       return 0;
    else if (T->data > key)
       return InsertBST(T->lchild, key);
    else if (T->data < key)
       return InsertBST(T->rchild, key);
}

2.编写CreateBST(T)的伪代码实现从控制台输入创建BST树。最后使用代码实现。使用“50 30 80 20 40 90 10 25 35 85 23 88”创建BST,并中序输出该BST:

CreateBST(T)

伪代码:

BiTNode *CreateBST(T)
{
  初始化树T为空树
    int i=0;
    while (i < n) {
        调用函数InsertBST(T,key);
        i++;
    }
    返回T;
}

代码:

BiTNode *CreatBST(int a[], int n) {
    BiTNode *T = NULL;
    int i = 0;
    while (i < n) {
        InsertBST(T, a[i]);
        i++;
        }
    return T;
}

代码运行截图:

3.编写DeleteBST(T, key)的伪代码实现从T中删除关键字key。如果无法编写出来,请写出在BST中删除关键字key所需注意的事项:

DeleteBST(T, key)

伪代码:

DeleteBST(T, key)
{
   if(T为空)返回0
    else {
        if(k<T->data)返回DeleteBST(T->lchild,key)
        else if(k>T->data)返回DeleteBST(T->rchild,key)
        else {
            Delete(T)
            返回1
        }
    }
}

Delete(&T)

伪代码:

Delete(&T)
{
  新建结构指针q,s;
  if(T为叶子节点)
     *T指向空;
  else if(T的左子树为空)
     q=T;
     T=T->rchild;
     释放q;
  else if(T的右子树为空)
     q=T;
     T=T->lchild;
     释放q;
  else
     q=T;
     s=T->lchild;
     while(s->rchild不为空)
           q=s;
           s=s->rchild;
     T的值赋予s;
     if(q!=T)
           q->rchild=s->lchild;
     else
           q->lchild=s->lchild; 
      释放s; 
}

4.选做:使用代码实现DeleteBST(T, key):

DeleteBST(T, key)

代码:

void DeleteBST(BiTNode *&T, char key) {
    if (T == NULL)return;
    else {
        if (key < T->data)
       return DeleteBST(T->lchild, key);
        else if (key > T->data)
       return DeleteBST(T->rchild, key);
        else {
            Delete(T);
        }
    }
}

Delete(&T)

代码:

void Delete(BiTree &T)
{
   BiTree q,s;
   if(!T->lchild&&!T->rchild)
      T=NULL;
   else if(!T->lchild)
      q=T;
      T=T->rchild;
   else if(!T->rchild)
      q=T;
      T=T->lchild;
   else{
       q=T;
       s=T->lchild;
       while(s->rchild);
       {
           q=s;
           s=s->rchild;
       }
       T->data=s->data;
       if(q!=T)
           q->rchild=s->lchild;
       else
           q->lchild=s->lchild;
       free(s);
      }
}
posted @ 2020-04-19 20:41  陈小蛋  阅读(150)  评论(0编辑  收藏  举报