计蒜课上面说SBTree说SBTree删除的时候不需要调整,所以删除我没有作调整。
具体为什么不需要作调整,我询问了一些大佬,大概是因为插入删除查找需要的平均时间复杂度是一样的。所以调不调都行。如果有哪位大佬有更详细的资料还望告诉一声,在此谢谢。
这里是AVLTree的实现和SBTree差不多,就是调整策略条件改改
#include <stdlib.h>
#include <stdio.h>
typedef struct Node {
int data;
int size;
struct Node* lchild, *rchild;
} Node;
Node __NIL;
#define NIL (&__NIL)
int init_nil() {
NIL->lchild = NIL->rchild = NIL;
NIL->data = 0;
NIL->size = 0;
return 0;
}
int __________temp = init_nil();
Node* getNewNode(int val) {
Node* node = (Node*)malloc(sizeof(Node));
node->lchild = node->rchild = NIL;
node->data = val;
node->size = 1;
return node;
}
void UP(Node* root) {
root->size = (root->lchild->size > root->rchild->size ? root->lchild->size : root->rchild->size) + 1;
}
Node* rotate_left(Node* root) {
Node* temp = root->rchild;
root->rchild = temp->lchild;
temp->lchild = root;
UP(root);
UP(temp);
return temp;
}
Node* rotate_right(Node* root) {
Node* temp = root->lchild;
root->lchild = temp->rchild;
temp->rchild = root;
UP(root);
UP(temp);
return temp;
}
Node* maintain(Node* root, int flag) {
if (root == NIL) return root;
if (flag) {
if (root->lchild->rchild->size > root->rchild->size) {
root->lchild = rotate_left(root->lchild);
}
if (root->lchild->lchild->size > root->rchild->size) {
root = rotate_right(root);
}
} else {
if (root->rchild->lchild->size > root->lchild->size) {
root->rchild = rotate_right(root->rchild);
}
if (root->rchild->rchild->size > root->lchild->size) {
root = rotate_left(root);
}
}
return root;
}
Node* processor(Node* root) {
Node* temp = root->lchild;
while (temp->rchild != NIL) {
temp = temp->rchild;
}
return temp;
}
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);
}
UP(root);
root = maintain(root, root->data > val);
return root;
}
Node *delete_node(Node* node, int val) {
if (node == NIL) return node;
if (node->data > val) {
node->lchild = delete_node(node->lchild, val);
} else if (node->data < val) {
node->rchild = delete_node(node->rchild, val);
} else {
if (node->lchild == NIL && node->rchild == NIL) {
free(node);
return NIL;
} else if (node->lchild == NIL || node->rchild == NIL) {
Node* temp = node->lchild == NIL ? node->rchild : node->lchild;
free(node);
return temp;
} else {
Node* temp = processor(node);
node->data = temp->data;
node->lchild = delete_node(node->lchild, temp->data);
}
}
UP(node);
return node;
}
void output(Node* root) {
if (root == NIL) return;
output(root->lchild);
printf("%d (%d %d)\n", root->data, root->lchild->data, root->rchild->data);
output(root->rchild);
}
void clear(Node* root) {
if (root == NIL) return;
clear(root->lchild);
clear(root->rchild);
free(root);
}
int main() {
Node* root = NIL;
int n, m;
while (scanf("%d %d", &n, &m) != EOF) {
switch (n) {
case 1:
root = insert(root, m);
break;
case 2:
root = delete_node(root, m);
break;
case 3:
output(root);
}
output(root);
}
clear(root);
return 0;
}