数据结构与算法复习AVL树插入过程

环境

$ cat /proc/version

Linux version 6.8.0-45-generic (buildd@lcy02-amd64-115) (x86_64-linux-gnu-gcc-13 (Ubuntu 13.2.0-23ubuntu4) 13.2.0, GNU ld (GNU Binutils for Ubuntu) 2.42) #45-Ubuntu SMP PREEMPT_DYNAMIC Fri Aug 30 12:02:04 UTC 2024

复制代码
#include <stdio.h>
#include <stdlib.h>

struct node
{
	int key;
	int height;
	struct node *left;
	struct node *right;
};

int height(struct node *node)
{
	if(node == NULL)
	{
		return 0;
	}
	return node->height;
}

int max(int a, int b)
{
	return a > b ? a : b;
}
struct node *leftRotate(struct node *node)
{
/*
       1
        \
         3
        /
       2
   
         3
        /
       1
        \
         2
*/
	struct node *root = node->right;
	node->right = root->left;
	root->left = node;

	node->height = max(height(node->left), height(node->right));
	root->height = max(height(root->left), height(root->right));

	return root;
}
struct node *rightRotate(struct node *node)
{
	struct node *root = node->left;
	node->left = root->right;
	root->right = node;

	node->height = max(height(node->left), height(node->right));
	root->height = max(height(root->left), height(root->right));

	return root;
}
struct node *insertNode(struct node *root, int key)
{
#if 0
	if(root == NULL)
	{
		root = malloc(sizeof(*root));
		root->key = key;
		root->height = 1;
		root->left = NULL;
		root->right = NULL;
		return root;
	}
	if(key < root->key)
	{
		root->left = insertNode(root->left, key);
	}
	else if(key > root->key)
	{
		root->right = insertNode(root->right, key);
	}
	else
	{
		return root;
	}
#else
	struct node **tmp = &root;

	while(1)
	{
		if((*tmp) == NULL)
		{
			(*tmp) = malloc(sizeof(*root));
			(*tmp)->key = key;
			(*tmp)->height = 1;
			(*tmp)->left = NULL;
			(*tmp)->right = NULL;
			break;
		}
		else if(key < (*tmp)->key)
		{
			tmp = &(*tmp)->left;
		}
		else if(key > (*tmp)->key)
		{
			tmp = &(*tmp)->right;
		}
		else
		{
			return root;
		}
	}
#endif
	root->height = max(height(root->left), height(root->right));

	int bf = height(root->left) - height(root->right);
/*
        3
       /
      2
     /
    1
*/
	if(bf > 1 && key < root->left->key)
	{
		return rightRotate(root);
	}
	else if(bf < -1 && key > root->right->key)
	{
		return leftRotate(root);
	}
/*
        3
       /
      1
       \
        2
*/
	else if(bf > 1 && key > root->left->key)
	{
		root->left = leftRotate(root->left);
		return rightRotate(root);
	}
	else if(bf < -1 && key < root->right->key)
	{
		root->right = rightRotate(root->right);
		return leftRotate(root);
	}
	else
	{}
	
	return root;
}

void inOrder(struct node *node)
{
	if(node == NULL)
	{
		return;
	}
	inOrder(node->left);
	printf("%d ", node->key);
	inOrder(node->right);
}
int main(int argc, char *argv[])
{
	struct node *root = NULL;

	root = insertNode(root, 0);
	root = insertNode(root, 1);
/*
        0
         \
          1
*/
	root = insertNode(root, 3);
/*
        0       0             1
         \       \           / \
          1       1         0   3
                   \
                    3
*/
	root = insertNode(root, 9);
/*
        0       0             1         1
         \       \           / \       / \
          1       1         0   3     0   3
                   \                       \
                    3                       9
*/
	root = insertNode(root, 2);
/*
        0       0             1         1             1
         \       \           / \       / \           / \
          1       1         0   3     0   3         0   3
                   \                       \           / \
                    3                       9         2   9
*/
	root = insertNode(root, 8);
/*
        0       0             1         1             1             1                  1                       3
         \       \           / \       / \           / \           / \                / \                     / \
          1       1         0   3     0   3         0   3         0   3              0   3                   1   8
                   \                       \           / \           / \                / \                 / \   \
                    3                       9         2   9         2   9              2   8               0   2   9
                                                                       /                    \
                                                                      8                      9
*/
	inOrder(root);
	printf("\n");
	return 0;
}

<完>

相关推荐
??tobenewyorker23 分钟前
力扣打卡第二十一天 中后遍历+中前遍历 构造二叉树
数据结构·c++·算法·leetcode
蓝澈112131 分钟前
迪杰斯特拉算法之解决单源最短路径问题
java·数据结构
贾全44 分钟前
第十章:HIL-SERL 真实机器人训练实战
人工智能·深度学习·算法·机器学习·机器人
GIS小天1 小时前
AI+预测3D新模型百十个定位预测+胆码预测+去和尾2025年7月4日第128弹
人工智能·算法·机器学习·彩票
满分观察网友z1 小时前
开发者的“右”眼:一个树问题如何拯救我的UI设计(199. 二叉树的右视图)
算法
森焱森3 小时前
无人机三轴稳定化控制(1)____飞机的稳定控制逻辑
c语言·单片机·算法·无人机
循环过三天3 小时前
3-1 PID算法改进(积分部分)
笔记·stm32·单片机·学习·算法·pid
呆瑜nuage3 小时前
数据结构——堆
数据结构
蓝澈11213 小时前
弗洛伊德(Floyd)算法-各个顶点之间的最短路径问题
java·数据结构·动态规划
zl_dfq3 小时前
数据结构 之 【堆】(堆的概念及结构、大根堆的实现、向上调整法、向下调整法)(C语言实现)
数据结构