zoukankan      html  css  js  c++  java
  • 数据结构作业:二叉排序树的实现

    一.编写SearchBST(T,key)与Insert(T,key)的伪代码,并实现。

    1.查找 SearchBST(T,key):

    伪代码:

    if(T=空或T的值=key)    返回T;
    
    if(T的值>key) return SearchBST(T->lchid, key);
    else        return SearchBST(T->rchid, key);
    

    代码:

    BTree SearchBST(BTree &T,int key)
    {
        if(T->data==key) return T;
        if(T->data>key) 
        return SearchBST(T->lchid, key);
        else
        return SearchBST(T->rchid, key);
    }
    

    2.插入 InsertBST(T,key):

    伪代码:

    if(T为空)
    {
        令T指向新节点,并使T->data=key;
        使 T->lchild与 T->rchild指向空;
    }
    if(T的值=key) return;
    if(T的值大于key) return InsertBST(T->lchild,key);
    else return InsertBST(T->rchild, key);
    

    代码:

    int InsertBST(BTree &T,int key)
    {
        if(T==NULL)
        {
            T=(BTree)malloc(sizeof(BNode));
            T->data=key;
            T->lchild=NULL;
            T->rchild=NULL;
            return 1;
        }
        if(T->data==key) return 0;
        if(T->data>key) return InsertBST(T->lchild,key);
        else return InsertBST(T->rchild, key);
    }
    

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

    创建 CreateBST(T):

    伪代码:

    初始化树T;
    for(i=0;i<n;i++)
    {
        读入key;
        InsertBST(T,key)
    }
    返回 T;
    

    代码:

    BTree CreateBST(int n)
    {
        BTree T=NULL;
        for(int key,int i=0;i<n;i++)
        {
            cin>>key;
            InsertBST(T,key);
        }
        return T;
    }
    

    中序输出结果展示:

    三.编写DeleteBST(T, key)的伪代码实现从T中删除关键字key。

    删除 DeleteBST(T, key)

    伪代码:

    if(树为空) return 0;
    else
    {
        if(T的值>key)
        继续递归 DeleteBST(T->lchild,key);
        else if(T的值<key)
        继续递归 DeleteBST(T->rchild,key);
        else 
        调用函数Delete1删除该节点;
    }
    return 1;
    

    Delete1(T)函数伪代码:

    创建一个新节点q;
    if(T的右孩子为空)
    {
        q=T;
        T=T->lchild;//被删除节点的左孩子代替被删除节点的位置
        删除节点q;
    }
    else if(T的左孩子为空)
    {
        q=T;
        T=T->rchild;//被删除节点的右孩子代替被删除节点的位置
        删除节点q;
    }
    else
    {
        当T的左右孩子都存在时,调用函数Delete2;
    }
    

    Delete2(T,r)伪代码:

    创建节点q;
    if(T左子树的右子树不为空)
    {
        递归执行Delete2直至找到T左子树下的最右节点;
    }
    else
    {
        令T的值=r的值;
        q=r;
        使最右下节点代替被删节点的1位置;
        删除节点q;
    }
    

    四.DeleteBST(T,key)的代码实现。

    代码实现:

    int DeleteBST(BTree &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
            {
            Delete1(T);
            return 1;
            }
            
        }
    }
    
    void Delete1(BTree &T)
    {
        BTree q;
        if(T->rchild==NULL)
        {
            q=T;
            T=T->lchild;
            free(q);
        }
        else if(T->lchild==NULL)
        {
            q=T;
            T=T->rchild;
            free(q);
        }
        else Delete2(T,T->lchild);
    }
    
    void Delete2(T,r)
    {
        BTree q;
        if(r->rchild!=NULL)
        {
            Delete2(T,r->rchild);
        }
        else
        {
            T->data=r->data;
            q=r;
            r=r->lchild;
            free(q);
        }
    }
    

    删除结果展示:

  • 相关阅读:
    STL hash_map使用
    STL的 string 类赋值
    STL map使用详解
    下面我使用vector容器为基础来构成一棵树
    MFC中CString.Format的详细用法
    error LNK2001: 无法解析的外部符号 "public: static class stdext::hash_map
    !!! STL的string类如何实现CString的Format功能 这是一个经典问题,记住
    STL map和STL set(转载)
    为什么提示此错误?RunTime Check Failure #2 Stack around the variable 'tch1'was corrupted.
    STL源码剖析
  • 原文地址:https://www.cnblogs.com/qijing-cy/p/12732668.html
Copyright © 2011-2022 走看看