C语言——二叉折半查找 折半查找 顺序查找

 

#include<iostream>

using namespace std;

#define Num 10

typedef int ElemType;

#define MaxInt 0x3f3f3f3f//最大值

ElemType a[Num] = { 5,4,6,3,2,8,9,7,1,10 };

struct LNode

{

    ElemType date;

    struct LNode* next;

};



typedef struct BSTNode//二叉排序树

{

    ElemType date;

    BSTNode* lchild, * rchild;

}BSTNode, * BSTree;



void zuse()//清屏

{

    cout << endl;

    system("pause");

    system("cls");

}



int Menu()

{

    int key = -1;

    cout << "________________________________________" << endl;

    cout << "      0.数据自定义(已初始化)            " << endl;

    cout << "      1.顺序查找(顺序表)                " << endl;

    cout << "      2.顺序查找(链表)                  " << endl;

    cout << "      3.折半查找(顺序表)                " << endl;

    cout << "      4.二叉排序树查找                  " << endl;

    cout << "      5.哈希查找                        " << endl;

    cout << "————————————————————" << endl;

    do

    {

        cout << "输入对应功能数字(0-5):";

        cin >> key;

    } while (key < 0 || key>5);

    return key;

}



void CreatList()

{

    for (int i = 0; i < Num; i++)

    {

        cout << "输入第" << i + 1 << "个数据:";

        cin >> a[i];

    }

}



void CreatLinkList(LNode* chead)

{

    LNode* p = chead;

    for (int i = 0; i < Num; i++)

    {

        LNode* pNewNode = new LNode;

        pNewNode->date = a[i]; // 将新节点的值赋值为a[i]

        pNewNode->next = NULL;

        p->next = pNewNode; // 上一个节点指向这个新建立的节点

        p = pNewNode; // p节点指向这个新的节点

    }

}



void ShunSunSearchList()

{

    int shaobing;//烧饼

    cout << "请输入查找的值:";

    cin >> shaobing;

    for (int i = 0; i < Num; i++)

    {

        if (shaobing == a[i])

        {

            cout << "查找到该数据:" << a[i] << endl;

            return;

        }

        else if (i > Num)

        {

            cout << "未查找到" << endl;

            return;

        }

    }

}



void ShunSunSearchLinkList(LNode* shead)

{

    LNode* p = shead->next;

    int shaobing;//烧饼

    cout << "请输入查找的值:";

    cin >> shaobing;

    for (int i = 0; i < Num; i++)

    {

        if (shaobing == p->date)

        {

            cout << "查找到该数据:" << p->date << endl;

            return;

        }

        else if (p->next == NULL)

        {

            cout << "未查找到" << endl;

            return;

        }

        p = p->next;

    }

}



void ZheBanSearch()

{

    int mid = 0, low = 0, high = Num, key = 0;

    int b[Num + 1];

    int num;

    for (int j = 0; j < Num; j++)

        b[j] = a[j];

    for (int i = 0; i < Num - 1; i++)//冒泡排序

    {

        for (int n = 0; n < Num - 1 - i; n++)

        {

            if (b[n] > b[n + 1])

            {

                num = b[n];

                b[n] = b[n + 1];

                b[n + 1] = num;

            }

        }

    }

    cout << "请输入查找的值:";

    cin >> key;

    while (low <= high)

    {

        mid = (low + high) / 2;

        if (key == b[mid])

        {

            cout << "查找到该数据:" << b[mid];

            return;

        }

        else if (key < b[mid])

            high = mid - 1;

        else

            low = mid + 1;

    }

    cout << "未查找到" << endl;

}

BSTree SearchBST(BSTree T, char key) {

    //在根指针T所指二叉排序树中递归地查找某关键字等于key的数据元素

    //若查找成功,则返回指向该数据元素结点的指针,否则返回空指针

    if ((!T) || key == T->date) return T;                    //查找结束

    else if (key < T->date)  return SearchBST(T->lchild, key);   //在左子树中继续查找

    else return SearchBST(T->rchild, key);                      //在右子树中继续查找

} // SearchBST



void InsertBST(BSTree& T, ElemType e)

{

    //当二叉排序树T中不存在关键字等于e.key的数据元素时,则插入该元素

    if (!T)

    {                             //找到插入位置,递归结束

        BSTree S = new BSTNode;                //生成新结点*S

        S->date = e;                       //新结点*S的数据域置为e  

        S->lchild = S->rchild = NULL; //新结点*S作为叶子结点

        T = S;                            //把新结点*S链接到已找到的插入位置

    }

    else if (e < T->date)

        InsertBST(T->lchild, e);          //将*S插入左子树

    else if (e > T->date)

        InsertBST(T->rchild, e);          //将*S插入右子树

}// InsertBST



void CreateBST(BSTree& T)

{

    //依次读入一个关键字为key的结点,将此结点插入二叉排序树T中

    T = NULL;

    for (int i = 0; i < Num; i++)

        InsertBST(T, a[i]);//将此结点插入二叉排序树T中

}



void InOrderTraverse(BSTree& T)

{

    if (T)

    {

        InOrderTraverse(T->lchild);

        cout << T->date;

        InOrderTraverse(T->rchild);

    }

}

void DeleteBST(BSTree& T, ElemType key) {

    //从二叉排序树T中删除关键字等于key的结点

    BSTree p = T; BSTree f = NULL;                               //初始化

    BSTree q = NULL;

    BSTree s;

    /*------------下面的while循环从根开始查找关键字等于key的结点*p-------------*/

    while (p) {

        if (p->date == key) break;            //找到关键字等于key的结点*p,结束循环

        f = p;                                          //*f为*p的双亲结点

        if (p->date > key)  p = p->lchild;     //在*p的左子树中继续查找

        else p = p->rchild;                            //在*p的右子树中继续查找

    }//while

    if (!p) return;                                 //找不到被删结点则返回

    /*―考虑三种情况实现p所指子树内部的处理:*p左右子树均不空、无右子树、无左子树―*/

    if ((p->lchild) && (p->rchild)) {           //被删结点*p左右子树均不空

        q = p;

        s = p->lchild;

        while (s->rchild)                          //在*p的左子树中继续查找其前驱结点,即最右下结点

        {

            q = s; s = s->rchild;

        }                    //向右到尽头

        p->date = s->date;                         //s指向被删结点的“前驱”

        if (q != p) {

            q->rchild = s->lchild;         //重接*q的右子树

        }

        else q->lchild = s->lchild;            //重接*q的左子树

        delete s;

    }//if

    else {

        if (!p->rchild) {                      //被删结点*p无右子树,只需重接其左子树

            q = p; p = p->lchild;

        }//else if

        else if (!p->lchild) {                      //被删结点*p无左子树,只需重接其右子树

            q = p; p = p->rchild;

        }//else if

      /*――――――――――将p所指的子树挂接到其双亲结点*f相应的位置――――――――*/

        if (!f) T = p;                                  //被删结点为根结点

        else if (q == f->lchild) f->lchild = p;     //挂接到*f的左子树位置

        else f->rchild = p;                        //挂接到*f的右子树位置

        delete q;

    }

}



int main()

{

    cout << "" << endl;

    BSTree result;

    LNode* head = new LNode;

    BSTNode* T = NULL;

    head->next = NULL;

    head->date = Num;

    ElemType key;

    CreatLinkList(head);

    while (true)

    {

        switch (Menu())

        {

        case 0:

            CreatList();

            CreatLinkList(head);

            break;

        case 1:

            ShunSunSearchList();

            break;

        case 2:

            ShunSunSearchLinkList(head);

            break;

        case 3:

            ZheBanSearch();

            break;

        case 4:

            CreateBST(T);

            cout << "当前二叉排序树中序遍历的结果为:" << endl;

            InOrderTraverse(T);//输出

            cout << "输入待查找的数据" << endl;

            cin >> key;

            result = SearchBST(T, key);

            if (result)

                cout << "找到该字符:" << key << endl;

            else

                cout << "未找到该字符" << endl;

            cout << "请输入待删除的字符" << endl;

            cin >> key;

            DeleteBST(T, key);

            cout << "当前有序二叉树中序遍历结果为:" << endl;

            InOrderTraverse(T);

            break;

        //case 5:

            //HaXiChuShiHua();

            //break;

        default:

            break;

        }

        zuse();

    }

    return 0;

}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值