数据结构互评作业2

一.

SearchBST(T, key)

{

if (T为空 || T->data == s)

return T

else if (T->data > s)

return SearchBST(T->lchild, s)

else

return SearchBST(T->rchild, s)

}

 

InsertBST(T, key)

{

 if(T为空)

{

   建立新

   点的值为key

新结点左右子树为NULL

 }

 if(T==key)返回T

 if(T->data>key)InsertBST(T->lchild,key)

 if(T->data<key)InsertBST(T->rchild,key)

}

BiTree SearchBST(BiTree T,int s)
{
    if (T == NULL || T->data == s)
        return T;
    else if (T->data > s)
        return SearchBST(T->lchild, s);
    else
        return SearchBST(T->rchild, s);
}
BiTree InsertBST(BiTree T, int key)
{
    if (T == NULL)
    {
        T = new BiTNode;
        T->data = key;
        T->lchild = T->rchild = NULL;
    }
    else if (T->data > key)
        T->lchild = InsertBST(T->lchild, key);
    else if (T->data < key)
        T->rchild = InsertBST(T->rchild, key);
    else
        cout << "该结点已存在";
    return T;
}

二.

2.CreateBST(T,key)

{

if (T为空)

{

创建新结点T

T->data=key

T左右子树为空

}

else

{

if (T->data > x)

{

T->lchild = CreateBST(T->lchild, x)

}

if (T->data < x)

{

T->rchild = CreateBST(T->rchild, x)

}

}

return T

}

 

 三.

int DeleteBST(&T, key)
{
 if (T 为空)
  return 0;
 else
 {
  if (T->data > key)
   return DeleteBST(T->lchild, key);
  else if (T->data < key)
   return DeleteBST(T->rchild, key);
  else
  {
   Delete(T);
   return 1;
  }
 }
}
Delete(&T)
{
 新建结构体指针p, q
 if (T->rchild为空)
 {
  p = T
  T = T->lchild
  释放p
 }
 else if (T->lchild为空)
 {
  p = T
  T = T->rchild
  释放p
 }
 else
 {
  p = T
  q = T->lchild
  while (q->rchild不为空)
  {
   p = q
   q = q->rchild
  }
  T->data = q->data
  if (p != T)
   p->rchild = q->lchild
  else
   p->lchild = q->lchild
  释放q
 }
}

四.

void Delete(BiTree& T)
{
    BiTree p, q;
    if (T->rchild == NULL)
    {
        p = T;
        T = T->lchild;
        free(p);
    }
    else if (T->lchild == NULL)
    {
        p = T;
        T = T->rchild;
        free(p);
    }
    else
    {
        p = T;
        q = T->lchild;
        while (q->rchild)
        {
            p = q;
            q = q->rchild;
        }
        T->data = q->data;
        if (p != T)
            p->rchild = q->lchild;
        else
            p->lchild = q->lchild;
        free(q);
    }
}
int DeleteBST(BiTree &T,int key)
{
    if (T == NULL)
        return 0;
    else
    {
        if (T->data > key)
            return DeleteBST(T->lchild, key);
        else if (T->data < key)
            return DeleteBST(T->rchild, key);
        else
        {
            Delete(T);
            return 1;
        }
    }
}

最后是全部代码

#include<iostream>
using namespace std;
typedef int ElementType;
typedef struct bNode
{
    ElementType data;
    struct bNode* lchild;
    struct bNode* rchild;
}BiTNode,*BiTree;
BiTree CreateBST(BiTree T,int x)
{
    if (T == NULL)
    {
        T = new BiTNode;
        T->data = x;
        T->lchild = T->rchild = NULL;
    }
    else
    {
        if (T->data > x)
        {
            T->lchild = CreateBST(T->lchild,x);
        }
        if (T->data < x)
        {
            T->rchild = CreateBST(T->rchild, x);
        }
    }
    return T;
}
void inordertraverse(BiTree T)
{
    if (T)
    {
        inordertraverse(T->lchild);
        cout << T->data << " ";
        inordertraverse(T->rchild);
    }
}

BiTree SearchBST(BiTree T,int s)
{
    if (T == NULL || T->data == s)
        return T;
    else if (T->data > s)
        return SearchBST(T->lchild, s);
    else
        return SearchBST(T->rchild, s);
}
BiTree InsertBST(BiTree T, int key)
{
    if (T == NULL)
    {
        T = new BiTNode;
        T->data = key;
        T->lchild = T->rchild = NULL;
    }
    else if (T->data > key)
        T->lchild = InsertBST(T->lchild, key);
    else if (T->data < key)
        T->rchild = InsertBST(T->rchild, key);
    else
        cout << "该结点已存在";
    return T;
}
void Delete(BiTree& T)
{
    BiTree p, q;
    if (T->rchild == NULL)
    {
        p = T;
        T = T->lchild;
        free(p);
    }
    else if (T->lchild == NULL)
    {
        p = T;
        T = T->rchild;
        free(p);
    }
    else
    {
        p = T;
        q = T->lchild;
        while (q->rchild)
        {
            p = q;
            q = q->rchild;
        }
        T->data = q->data;
        if (p != T)
            p->rchild = q->lchild;
        else
            p->lchild = q->lchild;
        free(q);
    }
}
int DeleteBST(BiTree &T,int key)
{
    if (T == NULL)
        return 0;
    else
    {
        if (T->data > key)
            return DeleteBST(T->lchild, key);
        else if (T->data < key)
            return DeleteBST(T->rchild, key);
        else
        {
            Delete(T);
            return 1;
        }
    }
}
int main()
{
    BiTree root=NULL,T;
    int n, x,s;
    int j,k;
    cin >> n;
    for (int i = 0;i < n;i++)
    {
        cin >> x;
        root = CreateBST(root, x);
    }
    cout << "中序:";
    inordertraverse(root);
    cin >> s;
    T=SearchBST(root, s);
    if (T)
        cout << "该结点存在"<<endl;
    else
        cout << "该结点不存在"<<endl;
    cin >> j;
    root = InsertBST(root, j);
    cin >> k;
    if (DeleteBST(root, k))
        cout << "删除成功"<<endl;
    else
        cout << "删除失败" << endl;
}

 

posted @ 2020-04-19 12:36  GGG(郭其松)  阅读(135)  评论(0编辑  收藏  举报