平衡二叉搜尋樹的簡單操作(AVL)
阿新 • • 發佈:2018-12-02
結構體
typedef struct AVLNode * Position;
typedef Position AVLTree;
struct AVLNode {
int data;
AVLTree left;
AVLTree right;
int height;
};
LL
AVLTree singleLeftRotation(AVLTree A) { AVLTree B = A->left; A->left = B->right; B->right = A; A->height = max(getTreeHeight(A->left), getTreeHeight(A->right)) + 1; B->height = max(getTreeHeight(B->left), getTreeHeight(B->right)) + 1; return B; }
RR
AVLTree singleRightRotation(AVLTree A) { AVLTree B = A->right; A->right = B->left; B->left = A; A->height = max(getTreeHeight(A->left), getTreeHeight(A->right)) + 1; B->height = max(getTreeHeight(B->left), getTreeHeight(B->right)) + 1; return B; }
LR
AVLTree doubleLeftRightRotation(AVLTree A) {
A->left = singleRightRotation(A->left);
return singleLeftRotation(A);
}
RL
AVLTree doubleRightRotation(AVLTree A) {
A->right = singleLeftRotation(A->right);
return singleRightRotation(A);
}
int getTreeHeight(AVLTree T) { int HL = 0, HR = 0; int max = 0; if (T) { if (T->left) { HL = getTreeHeight(T->left); } if (T->right) { HR = getTreeHeight(T->right); } max = (HL > HR) ? (HL + 1) : (HR + 1); } return max; }
具體如下
#include<stdio.h>
#include<stdlib.h>
int max(int a, int b) {
return a>b?a:b;
}
int getTreeHeight(AVLTree T)
{
int HL = 0, HR = 0;
int max = 0;
if (T) {
if (T->left) {
HL = getTreeHeight(T->left);
}
if (T->right) {
HR = getTreeHeight(T->right);
}
max = (HL > HR) ? (HL + 1) : (HR + 1);
}
return max;
}
AVLTree singleLeftRotation(AVLTree A) {
AVLTree B = A->left;
A->left = B->right;
B->right = A;
A->height = max(getTreeHeight(A->left), getTreeHeight(A->right)) + 1;
B->height = max(getTreeHeight(B->left), getTreeHeight(B->right)) + 1;
return B;
}
AVLTree singleRightRotation(AVLTree A) {
AVLTree B = A->right;
A->right = B->left;
B->left = A;
A->height = max(getTreeHeight(A->left), getTreeHeight(A->right)) + 1;
B->height = max(getTreeHeight(B->left), getTreeHeight(B->right)) + 1;
return B;
}
AVLTree doubleLeftRightRotation(AVLTree A) {
A->left = singleRightRotation(A->left);
return singleLeftRotation(A);
}
AVLTree doubleRightRotation(AVLTree A) {
A->right = singleLeftRotation(A->right);
return singleRightRotation(A);
}
AVLTree insert(AVLTree T, int x) {
if (!T) {
T = (AVLTree)malloc(sizeof(struct AVLNode));
T->data = x;
T->left = T->right = NULL;
T->height = 0;
} else if (x < T->data) {
T->left = insert(T->left, x);
if (getTreeHeight(T->left)-getTreeHeight(T->right) == 2) {
T = (x < T->left->data)?(singleLeftRotation(T)):(doubleLeftRightRotation(T));
}
} else if (x > T->data) {
T->right = insert(T->right, x);
if (getTreeHeight(T->right) - getTreeHeight(T->left) == 2) {
T = (x > T->right->data)?(singleRightRotation(T)):(doubleRightRotation(T));
}
}
T->height = max(getTreeHeight(T->left), getTreeHeight(T->right)) + 1;
return T;
}
void preOrder(AVLTree BST) {
if (!BST) {
return ;
} else {
printf("%d ", BST->data);
preOrder(BST->left);
preOrder(BST->right);
}
}
int main(void)
{
int N, x;
AVLTree root = NULL;
printf("輸入原始資料個數:\n");
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%d", &x);
root = insert(root, x);
}
/*
函式呼叫省略,很多的找最小,最大,樹高,遍歷的寫法都是和樹是一樣的
*/
return 0;
}