数据结构 || 次优查找树的c++实现

29 篇文章 5 订阅
27 篇文章 1 订阅
  • 参考严蔚敏老师的数据结构(c语言版),并用c++加以实现
//
//  main.cpp
//  Search_Optimal
//
//  Created by peiyu wang on 2019/3/19.
//  Copyright © 2019 peiyu wang. All rights reserved.
//

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define N 9

typedef char KeyType;

typedef struct {
    KeyType key;
    int weight;
}ElemType;

ElemType r[N] = {{'A',1},{'B',1},{'C',2},{'D',5},{'E',3},{'F',4},{'G',4},{'H',3},{'I',5}};

float sw[N + 1]; // 累计权值

typedef struct {
    ElemType *elem;
    int length;
}SSTable;

typedef struct BiTNode
{
    ElemType data;
    BiTNode *lchild, *rchild;
}BiTNode, *BiTree;

typedef BiTree SOSTree;

int Create_Table(SSTable &ST, int n)
{
    ST.elem = new ElemType [n + 1];
    ST.length = n;
    if (!ST.elem)
    {
        return ERROR;
    }
    for (int i = 0; i < n; i++)
    {
        ST.elem[i+1] = r[i];
    }
    return OK;
}

int Destory_Table(SSTable &ST)
{
    delete [] ST.elem;
    ST.elem = NULL;
    ST.length = 0;
    return OK;
}

//用于sort函数的排序方式函数
int Cmp(const ElemType &a, const ElemType &b)
{
    return a.key < b.key;
}

// 自己实现的快速排序
int QuickSort(int left, int right, ElemType a[])
{
    if (left > right)
    {
        return 0;
    }
    int i = left;
    int j = right;
    while (i != j)
    {
        while (a[j].key >= a[left].key && j > i)
        {
            j--;
        }
        while (a[i].key <= a[left].key && j > i)
        {
            i++;
        }
        if (i != j)
        {
            ElemType temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
    }
    ElemType temp1 = a[i];
    a[i] = a[left];
    a[left] = temp1;
    QuickSort(left, i - 1 , a);
    QuickSort(i + 1, right, a);
    return 1;
}

// 构造一个顺序表
int Create_Order_Table(SSTable &ST, int n)
{
    int flag;
    flag = Create_Table(ST, n);
    int flag1 = 1;
    if (flag)
    {
        sort(ST.elem + 1, ST.elem + N + 1, Cmp);
        //flag1 = QuickSort(1, n, ST.elem);
    }
    return (flag and flag1);
}

int Search_Table(SSTable ST, KeyType key)
{
    ST.elem[0].key = key;
    int i = ST.length;
    for (; ST.elem[i].key != key; i--);
    return i;
}

int Search_Bin(SSTable ST, KeyType key)
{
    int low, high, mid;
    low = 1;
    high = ST.length;
    while (low <= high)
    {
        mid = (low+high) / 2;
        if (ST.elem[mid].key == key)
        {
            return mid;
        }
        else if (ST.elem[mid].key > key)
        {
            high = mid - 1;
        }
        else {
            low = mid + 1;
        }
    }
    return 0;
}

int Traverse(SSTable ST)
{
    for (int i = 1; i <= ST.length; i++)
    {
        cout << ST.elem[i].key << endl;
    }
    return 1;
}

void SecondOptimal(BiTree &T, ElemType R[], float sw[], int low, int high)
{
    int i = low;
    double min, dw;
    min = fabs(sw[high] - sw[low]);
    dw = sw[high] + sw[low - 1];
    for (int j = low + 1; j <= high; ++j)
    {
        if (fabs(dw - sw[j] - sw[j-1]) < min)
        {
            i = j;
            min = fabs(dw - sw[j] - sw[j-1]);
        }
    }
    T = new BiTNode;
    T->data = R[i];
    if (i == low)
    {
        T->lchild = NULL;
    }
    else
    {
        SecondOptimal(T->lchild, R, sw, low, i - 1);
    }
    if (i == high)
    {
        T->rchild = NULL;
    }
    else
    {
        SecondOptimal(T->rchild, R, sw, i + 1, high);
    }
}

int CreateSOSTree(SOSTree &T, SSTable ST)
{
    if (ST.length == 0)
    {
        T = NULL;
    }
    else
    {
        sw[0] = 0;
        for (int i = 1; i <= ST.length; i++)
        {
            sw[i] = sw[i - 1] + ST.elem[i].weight;
        }
        SecondOptimal(T, ST.elem, sw, 1, ST.length);
    }
    return OK;
}

int Search_SOSTree(SOSTree &T, KeyType key)
{
    while (T)
    {
        if (T->data.key == key)
            return OK;
        else if (T->data.key > key)
            T = T->lchild;
        else
            T = T->rchild;
    }
    return ERROR;
}

int main(int argc, const char * argv[]) {
    SSTable ST;
    SOSTree T;
    KeyType key;
    Create_Order_Table(ST, N);
    //cout << Search_Table(ST, 'C') << endl;
    //cout << Search_Bin(ST, 'D') << endl;
    //Traverse(ST);
    CreateSOSTree(T, ST);
    cout << "请输入待查找的字符" << endl;
    cin >> key;
    if (Search_SOSTree(T, key))
    {
        cout << key << "的权值是" << T->data.weight << endl;
    }
    else
    {
        cout << "不存在此字符" << endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值