数据结构与算法复习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;
}

<完>

相关推荐
艾莉丝努力练剑10 分钟前
【LeetCode&数据结构】单链表的应用——反转链表问题、链表的中间节点问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表
_殊途2 小时前
《Java HashMap底层原理全解析(源码+性能+面试)》
java·数据结构·算法
珊瑚里的鱼5 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
秋说6 小时前
【PTA数据结构 | C语言版】顺序队列的3个操作
c语言·数据结构·算法
lifallen6 小时前
Kafka 时间轮深度解析:如何O(1)处理定时任务
java·数据结构·分布式·后端·算法·kafka
liupenglove7 小时前
自动驾驶数据仓库:时间片合并算法。
大数据·数据仓库·算法·elasticsearch·自动驾驶
python_tty8 小时前
排序算法(二):插入排序
算法·排序算法
然我8 小时前
面试官:如何判断元素是否出现过?我:三种哈希方法任你选
前端·javascript·算法
risc1234568 小时前
BKD 树(Block KD-Tree)Lucene
java·数据结构·lucene
F_D_Z8 小时前
【EM算法】三硬币模型
算法·机器学习·概率论·em算法·极大似然估计