- 参考严蔚敏老师的数据结构(c语言版),并用c++加以实现
#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;
}
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);
}
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);
CreateSOSTree(T, ST);
cout << "请输入待查找的字符" << endl;
cin >> key;
if (Search_SOSTree(T, key))
{
cout << key << "的权值是" << T->data.weight << endl;
}
else
{
cout << "不存在此字符" << endl;
}
return 0;
}