AVL二叉平衡树c++代码

#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;
}

void in_order_node(Node* root) {
	if (root == NIL) {
		printf("error!\n");
	}
	in_order_node(root->lchild);
	printf("%d ", root->data);
	in_order_node(root->rchild);
	return;
}
void in_order(Node* root) {
	if (root == NIL)  {
		printf("error!\n");
	}
	printf("in_order: ");
	in_order_node(root->data);
	printf("\n");
	return;
}

int main() {
	Node* root = NIL;
	int val;
	//insert
	printf("please print the val: (-1 remarks finish.)\n");
	while (~scanf_s("%d", &val)) {
		if (val == -1) break;
		printf("insert %d to AVL Tree\n", val);
		root = insert(root, val);
		output(root);
	}
	in_order(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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yitahutu79

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值