zoukankan      html  css  js  c++  java
  • 二叉排序树(BST)

    二叉排序树(BST) 二叉排序树是空树或者是满足如下性质的树

    (1)若它的左子树不空,则左子树上所有关键字的值均小于关键字的值

    (2)若它的右子树不空,则右子树上所有关键字的值均大于跟关键字的值。

    (3)左右子树各是一颗二叉排序树。

    说明: 在二叉排序树中插入关键字均为在新建的叶子上,由于找的的插入位置总是空指针域上。

    因此,在空指针域上连接一个新结点必为叶子结点

    #define LOCAL
    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    using namespace std;
    typedef int ElemType;
    const int maxSize=10000;
    typedef struct BTNode
    {
        ElemType key;
        struct BTNode *lChild;
        struct BTNode *rChild;
    }BTNode;
    /*
    插入关键字算法
    二叉排序树是一个查找表,插入一个关键字首先要找到插入位置,对于不存在于二叉
    树种的关键字,其查找不成功的位置即为关键字的插入位置。
    */
    int BSTInsert(BTNode *&bt,int key)
    {
        if(bt==NULL)
        {
            bt=(BTNode *)malloc(sizeof(BTNode));
            bt->lChild=bt->rChild=NULL;
            bt->key=key;
            return 1;
        }
        else
        {
            if(key==bt->key)
            {
                return 0;
            }
            else if(key<bt->key)
            {
                return BSTInsert(bt->lChild,key);
            }
            else
            {
                return BSTInsert(bt->rChild,key);
            }
        }
    }
    BTNode* BSTSearch(BTNode *bt,int key)
    {
        if(bt==NULL)
        {
            return NULL;
        }
        else if(bt->key==key)
        {
            return bt;
        }    
        else if(key<bt->key)
        {
            return BSTSearch(bt->lChild,key);
        }
        else
        {
            return BSTSearch(bt->rChild,key);
        }
    }
    /*
    建立二叉排序树是,只要建立一颗空树,然后将关键字逐个插入到空树中,即可构
    造一颗二叉排序树,算法如下:假设关键字已存入数组key[],且下标是从1开始。
    */
    void CreateBST(BTNode *&bt,int key[],int n)
    {
        int i=0;
        bt=NULL;
        for(i=1;i<=n;i++)
        {
            BSTInsert(bt,key[i]);
        }
    }
    int SearchR(int R[],int k,int n)
    {
        int flag=0;
        for(int i=0;i<n;i++)
        {
            if(R[i]==k)
            {
                flag=1;
                break;
            }
        }
        return flag;
    }
    int main()
    {
    #ifdef LOCAL
        freopen("data.in","r",stdin);
        freopen("data.out","w",stdout);
    #endif    
        int R[maxSize],n,i,key;
        BTNode *bt,*search;
        cin>>n;
        for(i=0;i<n;i++)
        {
            cin>>R[i];
        }  
        CreateBST(bt,R,n);
        cin>>key;
        search=BSTSearch(bt,key);
        if(search!=NULL)
        {
            cout<<search->key<<endl;
        }
        else
        {
            cout<<"0
    ";
        }
        //查数组找验证二叉排序树执行结果是否正确 
        if(SearchR(R,key,n))
        {
            cout<<key<<endl;    
        }
        else{
            cout<<"0
    ";
        }
        return 0;
    }
  • 相关阅读:
    COGS 2075. [ZLXOI2015][异次元圣战III]ZLX的陨落
    51nod 1099 任务执行顺序
    洛谷 P1215 [USACO1.4]母亲的牛奶 Mother's Milk
    洛谷 P3395 路障
    2017.10.25 模拟赛
    COGS 146. [USACO Jan08] 贝茜的晨练计划
    洛谷 P3905 道路重建
    COGS 678. 双重回文数
    洛谷 P1509 找啊找啊找GF
    51nod 1366 贫富差距
  • 原文地址:https://www.cnblogs.com/jianfengyun/p/4020607.html
Copyright © 2011-2022 走看看