当前位置:网站首页>ALV树(LL LR RL RR)插入删除
ALV树(LL LR RL RR)插入删除
2022-04-23 09:04:00 【yitahutu79】
Windows版本
//VS2022
#include <stdio.h>
#include <stdlib.h>
#define max(a,b) ((a) > (b) ? (a) : (b))
typedef struct Node {
int data, h;
struct Node* lchild, * rchild;
}Node;
Node __NIL;
#define NIL (&__NIL)
#define DEBUG
#ifdef DEBUG
#define LOG(frm, ...) {
\ printf(frm, ##__VA_ARGS__);\ }\
#else
#define LOG(frm, ...) {
}
#endif
void init_NIL() {
NIL->data = NIL->h = 0;
NIL->lchild = NIL->rchild = NIL;
return;
}
Node* getNewNode(int val) {
Node* p = (Node*)malloc(sizeof(Node));
p->data = val;
p->h = 1;
p->lchild = p->rchild = NIL;
return p;
}
void update_height(Node* root) {
root->h = max(root->lchild->h, root->rchild->h) + 1;
return;
}
Node* left_rotate(Node* root) {
LOG("%d left rotate\n", root->data);
Node* new_root = root->rchild;
root->rchild = new_root->lchild;
new_root->lchild = root;
update_height(root);
update_height(new_root);
return new_root;
}
Node* right_rotate(Node* root) {
LOG("%d left rotate\n", root->data);
Node* new_root = root->lchild;
root->lchild = new_root->rchild;
new_root->rchild = root;
update_height(root);
update_height(new_root);
return new_root;
}
const char* type_str[4] = {
"LL", "LR", "RR", "RL" };
Node* maintain(Node* root) {
if (abs(root->lchild->h - root->rchild->h) < 2) return root;
int type = -1;
if (root->lchild->h > root->rchild->h) {
if (root->lchild->rchild->h > root->lchild->lchild->h) {
root->lchild = left_rotate(root->lchild);
type += 1;
}
type += 1;
root = right_rotate(root);
}
else {
type = 1;
if (root->rchild->lchild->h > root->rchild->rchild->h) {
root->rchild = right_rotate(root->rchild);
type += 1;
}
type += 1;
root = left_rotate(root);
}
LOG(" TYPE : %s\n", type_str[type]);
return root;
}
Node* insert(Node* root, int val) {
if (root == NIL) return getNewNode(val);
if (root->data == val) return root;
if (root->data > val) root->lchild = insert(root->lchild, val);
else root->rchild = insert(root->rchild, val);
update_height(root);
return maintain(root);
}
Node* erase(Node* root, int val) {
if (root == NIL) return root;
if (root->data > val) root->lchild = erase(root->lchild, val);
else if (root->data < val) root->rchild = erase(root->rchild, val);
else {
if (root->lchild == NIL || root->rchild == NIL) {
Node* temp = root->lchild != NIL ? root->lchild : root->rchild;
free(root);
return temp;
}
else {
Node* temp = root->lchild;
while (temp->rchild != NIL) temp = temp->rchild;
root->data = temp->data;
root->lchild = erase(root->lchild, temp->data);
}
}
update_height(root);
return maintain(root);
}
void clear(Node* root) {
if (root == NIL) return;
clear(root->lchild);
clear(root->rchild);
free(root);
return;
}
void print_node(Node* root) {
printf("[ %d(%d) | %d, %d]\n", root->data, root->h, root->lchild->data, root->rchild->data);
return;
}
void output(Node* root) {
if (root == NIL) return;
print_node(root);
output(root->lchild);
output(root->rchild);
return;
}
int main() {
Node* root = NIL;
int val;
//insert
while (~scanf_s("%d", &val)) {
if (val == -1) break;
printf("insert %d to AVL Tree\n", val);
root = insert(root, val);
output(root);
}
//erase
while (~scanf_s("%d", &val)) {
if (val == -1) break;
printf("erase %d from AVL Tree\n", val);
root = erase(root, val);
output(root);
}
return 0;
}
Linux版本
#include <stdio.h>
#include <stdlib.h>
#define max(a,b) ((a) > (b) ? (a) : (b))
typedef struct Node {
int data, h;
struct Node* lchild, * rchild;
}Node;
Node __NIL;
#define NIL (&__NIL)
#define DEBUG
#ifdef DEBUG
#define LOG(frm, args...) {
\ printf(frm, ##args);\ }\
#else
#define LOG(frm, args...) {
}
#endif
__attribute__((constructor))
void init_NIL() {
NIL->data = NIL->h = 0;
NIL->lchild = NIL->rchild = NIL;
return;
}
Node* getNewNode(int val) {
Node* p = (Node*)malloc(sizeof(Node));
p->data = val;
p->h = 1;
p->lchild = p->rchild = NIL;
return p;
}
void update_height(Node* root) {
root->h = max(root->lchild->h, root->rchild->h) + 1;
return;
}
Node* left_rotate(Node* root) {
LOG("%d left rotate\n", root->data);
Node* new_root = root->rchild;
root->rchild = new_root->lchild;
new_root->lchild = root;
update_height(root);
update_height(new_root);
return new_root;
}
Node* right_rotate(Node* root) {
LOG("%d left rotate\n", root->data);
Node* new_root = root->lchild;
root->lchild = new_root->rchild;
new_root->rchild = root;
update_height(root);
update_height(new_root);
return new_root;
}
const char* type_str[4] = {
"LL", "LR", "RR", "RL" };
Node* maintain(Node* root) {
if (abs(root->lchild->h - root->rchild->h) < 2) return root;
int type = -1;
if (root->lchild->h > root->rchild->h) {
if (root->lchild->rchild->h > root->lchild->lchild->h) {
root->lchild = left_rotate(root->lchild);
type += 1;
}
type += 1;
root = right_rotate(root);
}
else {
type = 1;
if (root->rchild->lchild->h > root->rchild->rchild->h) {
root->rchild = right_rotate(root->rchild);
type += 1;
}
type += 1;
root = left_rotate(root);
}
LOG(" TYPE : %s\n", type_str[type]);
return root;
}
Node* insert(Node* root, int val) {
if (root == NIL) return getNewNode(val);
if (root->data == val) return root;
if (root->data > val) root->lchild = insert(root->lchild, val);
else root->rchild = insert(root->rchild, val);
update_height(root);
return maintain(root);
}
Node* erase(Node* root, int val) {
if (root == NIL) return root;
if (root->data > val) root->lchild = erase(root->lchild, val);
else if (root->data < val) root->rchild = erase(root->rchild, val);
else {
if (root->lchild == NIL || root->rchild == NIL) {
Node* temp = root->lchild != NIL ? root->lchild : root->rchild;
free(root);
return temp;
}
else {
Node* temp = root->lchild;
while (temp->rchild != NIL) temp = temp->rchild;
root->data = temp->data;
root->lchild = erase(root->lchild, temp->data);
}
}
update_height(root);
return maintain(root);
}
void clear(Node* root) {
if (root == NIL) return;
clear(root->lchild);
clear(root->rchild);
free(root);
return;
}
void print_node(Node* root) {
printf("[ %d(%d) | %d, %d]\n", root->data, root->h, root->lchild->data, root->rchild->data);
return;
}
void output(Node* root) {
if (root == NIL) return;
print_node(root);
output(root->lchild);
output(root->rchild);
return;
}
int main() {
Node* root = NIL;
int val;
//insert
while (~scanf("%d", &val)) {
if (val == -1) break;
printf("insert %d to AVL Tree\n", val);
root = insert(root, val);
output(root);
}
//erase
while (~scanf("%d", &val)) {
if (val == -1) break;
printf("erase %d from AVL Tree\n", val);
root = erase(root, val);
output(root);
}
return 0;
}
版权声明
本文为[yitahutu79]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_40713201/article/details/124357083
边栏推荐
- To remember the composition ~ the pre order traversal of binary tree
- ONEFLOW learning notes: from functor to opexprinter
- Multi view depth estimation by fusing single view depth probability with multi view geometry
- Go language self-study series | golang nested structure
- Get trustedinstaller permission
- 根据后序和中序遍历输出先序遍历 (25 分)
- 数字政府建设中政务中台中的技术创新点
- Common errors of VMware building es8
- 还原二叉树 (25 分)
- L2-024 部落 (25 分)(并查集)
猜你喜欢
Pctp test experience sharing
PLC point table (register address and point table definition) cracking detection scheme -- convenient for industrial Internet data acquisition
Latex paper typesetting operation
Flink SQL realizes the integration of stream and batch
Write down the post order traversal of the ~ binary tree
资源打包关系依赖树
Notes d'apprentissage oneflow: de functor à opexprinterpreter
SAP 101K 411K 库存变化
Study notes of deep learning (8)
LLVM之父Chris Lattner:编译器的黄金时代
随机推荐
Restore binary tree (25 points)
Harbor enterprise image management system
Flink reads MySQL and PgSQL at the same time, and the program will get stuck without logs
完全二叉搜索树 (30 分)
npm ERR! network
ONEFLOW learning notes: from functor to opexprinter
Brush classic topics
Share the office and improve the settled experience
是否同一棵二叉搜索树 (25 分)
企业微信应用授权/静默登录
MATLAB入门资料
Taxable income
PLC的点表(寄存器地址和点表定义)破解探测方案--方便工业互联网数据采集
Flink SQL realizes the integration of stream and batch
L2-3 romantic silhouette (25 points)
To remember the composition ~ the pre order traversal of binary tree
valgrind和kcachegrind使用運行分析
PLC point table (register address and point table definition) cracking detection scheme -- convenient for industrial Internet data acquisition
Pctp test experience sharing
The crawler returns null when parsing with XPath. The reason why the crawler cannot get the corresponding element and the solution