#include
#include
#include
#include
#define OK 1
#define ERROR 0
#define FALSE 0
#define TRUE 1
#define OVERFLOW -2
#define LH 1
#define RH -1
#define EH 0
#define GRADE 4
typedef int Status;
typedef int KeyType;
typedef struct
{
KeyType key;
}ElemType;
typedef struct
{
ElemType* elem;
int length;
}SSTable;
typedef ElemType TElemType;
typedef struct BiTNode
{
TElemType data;
struct BiTNode* lchild, * rchild;
}BiTNode, * BiTree;
typedef struct BSTNode
{
TElemType data;
int bf;
struct BSTNode* lchild, * rchild;
}BSTNode, * BSTree;
typedef struct BTNode
{
int keynum;
struct BTNode* parent;
KeyType key[GRADE + 1];
struct BTNode* ptr[GRADE + 2];
ElemType* recptr[GRADE + 1];
}BTNode, * BTree;
typedef struct
{
BTNode* ptr;
int i;
int tag;
}Result;
//查找表元素输入,关键字赋值以及比较
void InputTableElem(ElemType* e)
{
printf("请输入关键字信息\n");
scanf("%d", &(e->key));
}
void KeyWordAssign(KeyType* destination, KeyType source)
{
(*destination) = source;
}
void KeyWordPrint(KeyType key)
{
printf("%c\n", key + 64);
}
void TableElemAssign(ElemType* destination, ElemType source)
{
(*destination).key = source.key;
}
void TableElemPrint(ElemType e)
{
printf("%d\n", e.key);
}
Status EQ(KeyType key1, KeyType key2)
{
if (key1 == key2)
return TRUE;
else
return FALSE;
}
Status LT(KeyType key1, KeyType key2)
{
if (key1 < key2)
return TRUE;
else
return FALSE;
}
Status LE(KeyType key1, KeyType key2)
{
if (key1 <= key2)
return TRUE;
else
return FALSE;
}
Status MT(KeyType key1, KeyType key2)
{
if (key1 > key2)
return TRUE;
else
return FALSE;
}
Status ME(KeyType key1, KeyType key2)
{
if (key1 >= key2)
return TRUE;
else
return FALSE;
}
//静态查找表
Status CreatSSTable(SSTable* T, int n)
{
T->length = n;
T->elem = (ElemType*)malloc(sizeof(ElemType) * (n + 1));
if (!T->elem) exit(OVERFLOW);
for (int i = 1; i <= T->length; i++)
{
printf("请输入第%d个表项信息:\n", i);
InputTableElem(&(T->elem[i]));
}
return OK;
}
int Search_Seq(SSTable ST, KeyType key) //Sequence Search
{
KeyWordAssign(&(ST.elem[0].key), key);
int i = ST.length;
while (!EQ(ST.elem[i].key, key))
i--;
return i;
}
int Search_Bin(SSTable ST, KeyType key) //Binary Search
{
int low = 1, high = ST.length;
int mid = (low + high) / 2;
while (low <= high)
{
if (EQ(ST.elem[mid].key, key))
return mid;
if (MT(ST.elem[mid].key, key))
high = mid - 1;
else
low = mid + 1;
}
return 0;
}
void SecondOptimal(BiTree* T, SSTable S, int* sw, int low, int high)
{
int i = low;
int w = sw[high] + sw[low - 1];
int min = abs(sw[high] - sw[low]);
for (int j = low + 1; j <= high; j++)
{
if (abs(w - sw[j - 1] - sw[j]) < min)
{
i = j;
min = abs(w - sw[i] - sw[i - 1]);
}
}
(*T) = (BiTree)malloc(sizeof(BiTNode));
if (!(*T)) exit(OVERFLOW);
KeyWordAssign(&((*T)->data.key), S.elem[i].key);
if (i == low)
(*T)->lchild = NULL;
else
SecondOptimal(&((*T)->lchild), S, sw, low, i - 1);
if (i == high)
(*T)->rchild = NULL;
else
SecondOptimal(&((*T)->rchild), S, sw, i + 1, high);
}
//队列函数
typedef BiTNode* QElemType;
typedef struct QNode
{
QElemType data;
struct QNode* next;
}QNode, * QueuePtr;
typedef struct
{
QueuePtr head, tail;
}LinkQueue;
Status InitQueue(LinkQueue* Q)
{
Q->head = (QueuePtr)malloc(sizeof(QNode));
if (!Q->head) exit(OVERFLOW);
Q->tail = Q->head;
Q->head->next = NULL;
return OK;
}
Status EnQueue(LinkQueue* Q, QElemType e)
{
QNode* p;
p = (QNode*)malloc(sizeof(QNode));
if (!p) exit(OVERFLOW);
p->data = e;
Q->tail->next = p;
Q->tail = p;
Q->tail->next = NULL;
return OK;
}
Status DeQueue(LinkQueue* Q, QElemType* e)
{
if (Q->head == Q->tail)
return ERROR;
QNode* p = Q->head->next;
(*e) = p->data;
Q->head->next = p->next;
if (p == Q->tail)
Q->tail = Q->head;
free(p);
return OK;
}
Status QueueEmpty(LinkQueue Q)
{
if (Q.head == Q.tail)
return TRUE;
else
return FALSE;
}
//打印二叉树(层序遍历)(打印关键字)
void PrintBiTree(BiTree T)
{
LinkQueue Q;
InitQueue(&Q);
EnQueue(&Q, T);
QElemType p;
while (!QueueEmpty(Q))
{
DeQueue(&Q, &p);
KeyWordPrint(p->data.key);
if (p->lchild)
EnQueue(&Q, p->lchild);
if (p->rchild)
EnQueue(&Q, p->rchild);
}
}
//二叉排序树
Status SearchBST(BiTree T, KeyType key, BiTree f, BiTree* p)
{
if (!T)
{
(*p) = f;
return FALSE;
}
if (EQ(T->data.key, key))
{
(*p) = T;
return TRUE;
}
if (LT(T->data.key, key))
return SearchBST(T->rchild, key, T, p);
else
return SearchBST(T->lchild, key, T, p);
}
Status InsertBST(BiTree* T, ElemType e)
{
BiTNode* q;
q = (BiTNode*)malloc(sizeof(BiTNode));
if (!q) exit(OVERFLOW);
TableElemAssign(&(q->data), e);
q->lchild = NULL;
q->rchild = NULL;
if (MT((*T)->data.key, q->data.key))
(*T)->lchild = q;
else
(*T)->rchild = q;
return OK;
}
Status Delete(BiTree* T, BiTree* parent)
{
BiTree p = (*T);
if (p == *parent)
{
if (!p->rchild)
{
(*T) = p->lchild;
free(p);
}
else
{
if (!p->lchild)
{
(*T) = p->rchild;
free(p);
}
else
{
BiTNode* q1 = p->lchild, * q2 = p;
while (q1->rchild)
{
q2 = q1;
q1 = q1->rchild;
}
TableElemAssign(&(p->data), q1->data);
if (q2 != p)
q2->rchild = q1->lchild;
else
q2->lchild = q1->lchild;
free(q1);
}
}
return OK;
}
int left;
if ((*T) == (*parent)->lchild)
left = TRUE;
else
left = FALSE;
if (p->lchild && !p->rchild)
{
if (left)
(*parent)->lchild = p->lchild;
else
(*parent)->rchild = p->lchild;
free(p);
return OK;
}
if (!p->lchild && p->rchild)
{
if (left)
(*parent)->lchild = p->rchild;
else
(*parent)->rchild = p->rchild;
free(p);
return OK;
}
if (!p->lchild && !p->rchild)
{
if (left)
(*parent)->lchild = NULL;
else
(*parent)->rchild = NULL;
free(p);
return OK;
}
BiTNode* q1 = p->lchild, * q2 = p;
while (q1->rchild)
{
q2 = q1;
q1 = q1->rchild;
}
TableElemAssign(&(p->data), q1->data);
if (q2 != p)
q2->rchild = q1->lchild;
else
q2->lchild = q1->lchild;
free(q1);
return OK;
}
Status DeleteBST(BiTree* T, BiTree* parent, KeyType key)
{
if (!(*T))
return FALSE;
if (EQ((*T)->data.key, key))
{
Delete(T, parent);
return TRUE;
}
if (LT((*T)->data.key, key))
{
(*parent) = (*T);
DeleteBST(&((*T)->rchild), parent, key);
}
else
{
(*parent) = (*T);
DeleteBST(&((*T)->lchild), parent, key);
}
}
主函数:
//次优二叉树主函数
int main()
{
SSTable Table;
int amount;
printf("请输入表项数目\n");
scanf("%d", &amount);
CreatSSTable(&Table, amount);
int* w = (int*)malloc(sizeof(int) * (Table.length + 1));
if (!w) exit(OVERFLOW);
w[0] = 0;
for (int i = 1; i <= Table.length; i++)
{
printf("请输入第%d个表项权值\n", i);
scanf("%d", &(w[i]));
}
int* sw;
sw = (int*)malloc(sizeof(int) * (Table.length + 1));
if (!sw) exit(OVERFLOW);
sw[0] = 0;
for (int i = 1; i <= Table.length; i++)
{
sw[i] = w[i];
sw[i] = sw[i - 1] + w[i];
}
BiTree T;
SecondOptimal(&T, Table, sw, 1, Table.length);
printf("次优二叉树的层序输出为:\n");
PrintBiTree(T);
DeleteBST(&T, &T, 6);
printf("次优二叉树删去6号节点的的层序输出为:\n");
PrintBiTree(T);
return 0;
}
输入:
9
1
2
3
4
5
6
7
8
9
1
1
2
5
3
4
4
3
5
原条件:
关键字 A B C D E F G H I
权值 1 1 2 5 3 4 4 3 5
结果: