#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <math.h>
#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("%d\n", key);
}
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 BSTNode* 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 Rotate_L(BSTree* T)
{
BSTNode* rc = (*T)->rchild;
BSTNode* new = (BSTNode*)malloc(sizeof(BSTNode));
if (!new) exit(OVERFLOW);
TableElemAssign(&(new->data), (*T)->data);
new->lchild = (*T)->lchild;
(*T)->lchild = new;
new->rchild = rc->lchild;
(*T)->rchild = rc->rchild;
TableElemAssign(&((*T)->data), rc->data);
(*T)->bf = EH;
new->bf = EH;
free(rc);
}
void Rotate_RL(BSTree* T)
{
BSTNode* rc = (*T)->rchild, * rc_lc = rc->lchild;
BSTNode* new;
//右旋代码
new = (BSTNode*)malloc(sizeof(BSTNode));
if (!new) exit(OVERFLOW);
TableElemAssign(&(new->data), rc->data);
new->rchild = rc->rchild;
new->lchild = rc_lc->rchild;
rc->rchild = new;
TableElemAssign(&(rc->data), rc_lc->data);
rc->lchild = rc_lc->lchild;
int bf_rem = rc_lc->bf;
free(rc_lc);
//左旋代码
new = (BSTree)malloc(sizeof(BSTNode));
if (!new) exit(OVERFLOW);
TableElemAssign(&(new->data), (*T)->data);
new->lchild = (*T)->lchild;
new->rchild = rc->lchild;
(*T)->lchild = new;
TableElemAssign(&((*T)->data), rc->data);
(*T)->rchild = rc->rchild;
free(rc);
switch (bf_rem)
{
case LH:
(*T)->lchild->bf = EH;
(*T)->rchild->bf = RH;
break;
case EH:
(*T)->lchild->bf = EH;
(*T)->rchild->bf = EH;
break;
case RH:
(*T)->lchild->bf = LH;
(*T)->rchild->bf = EH;
break;
}
(*T)->bf = EH;
}
void Rotate_R(BSTree* T)
{
BSTNode* lc = (*T)->lchild;
BSTNode* new = (BSTree)malloc(sizeof(BSTNode));
if (!new) exit(OVERFLOW);
TableElemAssign(&(new->data), (*T)->data);
new->rchild = (*T)->rchild;
new->lchild = lc->rchild;
TableElemAssign(&((*T)->data), lc->data);
(*T)->lchild = lc->lchild;
free(lc);
(*T)->bf = EH;
new->bf = EH;
}
void Rotate_LR(BSTree* T)
{
BSTNode* lc = (*T)->lchild, * lc_rc = (*T)->lchild->rchild;
BSTNode* new = (BSTree)malloc(sizeof(BSTNode));
if (!new) exit(OVERFLOW);
TableElemAssign(&(new->data), lc->data);
new->lchild = lc->lchild;
new->rchild = lc_rc->lchild;
lc->lchild = new;
TableElemAssign(&(lc->data), lc_rc->data);
lc->rchild = lc_rc->rchild;
int bf_rem = lc_rc->bf;
free(lc_rc);
new = (BSTNode*)malloc(sizeof(BSTNode));
if (!new) exit(OVERFLOW);
TableElemAssign(&(new->data), (*T)->data);
new->rchild = (*T)->rchild;
new->lchild = lc->rchild;
(*T)->rchild = new;
TableElemAssign(&((*T)->data), lc->data);
(*T)->lchild = lc->lchild;
free(lc);
switch (bf_rem)
{
case LH:
(*T)->lchild->bf = EH;
(*T)->rchild->bf = RH;
break;
case EH:
(*T)->lchild->bf = EH;
(*T)->rchild->bf = EH;
break;
case RH:
(*T)->lchild->bf = LH;
(*T)->rchild->bf = EH;
break;
}
(*T)->bf = EH;
}
void RightBalance(BSTree* T)
{
BSTNode* rc = (*T)->rchild;
switch (rc->bf)
{
case RH:Rotate_L(T); break;
case LH:Rotate_RL(T); break;
}
}
void LeftBalance(BSTree(*T))
{
BSTNode* lc = (*T)->lchild;
switch (lc->bf)
{
case LH:Rotate_R(T); break;
case RH:Rotate_LR(T); break;
}
}
Status InsertAVL(BSTree* T, ElemType e, int* taller)
{
if (!(*T))
{
*T = (BSTree)malloc(sizeof(BSTNode));
if (!(*T)) exit(OVERFLOW);
TableElemAssign(&((*T)->data), e);
(*T)->lchild = NULL;
(*T)->rchild = NULL;
(*T)->bf = EH;
*taller = TRUE;
}
else
{
if (EQ((*T)->data.key, e.key))
{
(*taller) = FALSE;
return FALSE;
}
else
{
if (LT((*T)->data.key, e.key))
{
if (!InsertAVL(&((*T)->rchild), e, taller))
return FALSE;
if ((*taller) == TRUE)
{
switch ((*T)->bf)
{
case LH:
(*T)->bf = EH;
(*taller) = FALSE;
break;
case EH:
(*T)->bf = RH;
(*taller) = TRUE;
break;
case RH:
RightBalance(T);
(*taller) = FALSE;
break;
}
}
}
else
{
if (!InsertAVL(&((*T)->lchild), e, taller))
return FALSE;
if ((*taller) == TRUE)
{
switch ((*T)->bf)
{
case RH:
(*T)->bf = EH;
(*taller) = FALSE;
break;
case EH:
(*T)->bf = LH;
(*taller) = TRUE;
break;
case LH:
LeftBalance(T);
(*taller) = FALSE;
break;
}
}
}
}
}
return OK;
}
void PrintBSTree(BSTree 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);
}
}
主函数:
//平衡二叉树的插入
int main()
{
BSTree T = NULL;
int taller;
ElemType e[5];
for (int i = 0; i < 5; i++)
{
taller = FALSE;
InputTableElem(&(e[i]));
InsertAVL(&T, e[i], &taller);
}
PrintBSTree(T);
return 0;
}
输入:(P234中的基本左右旋为例)
13
24
37
90
53
输出: