-
Notifications
You must be signed in to change notification settings - Fork 0
/
121-avl_insert.c
110 lines (99 loc) · 2.92 KB
/
121-avl_insert.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "binary_trees.h"
size_t height(const binary_tree_t *tree);
int balance(const binary_tree_t *tree);
avl_t *avl_insert_recursive(avl_t **tree, avl_t *parent,
avl_t **new, int value);
avl_t *avl_insert(avl_t **tree, int value);
/**
* height - Measures the height of a binary tree.
* @tree: A pointer to the root node of the tree to measure the height.
*
* Return: If tree is NULL, your function must return 0, else return height.
*/
size_t height(const binary_tree_t *tree)
{
if (tree != NULL)
{
size_t l = 0, r = 0;
l = tree->left ? 1 + binary_tree_height(tree->left) : 1;
r = tree->right ? 1 + binary_tree_height(tree->right) : 1;
return ((l > r) ? l : r);
}
return (0);
}
/**
* balance - Measures the balance factor of a binary tree.
* @tree: A pointer to the root node of the tree to measure the balance factor.
*
* Return: If tree is NULL, return 0, else return balance factor.
*/
int balance(const binary_tree_t *tree)
{
return (tree != NULL ? height(tree->left) - height(tree->right) : 0);
}
/**
* avl_insert_recursive - Inserts a value into an AVL tree recursively.
* @tree: A double pointer to the root node of the AVL tree to insert into.
* @parent: The parent node of the current working node.
* @new: A double pointer to store the new node.
* @value: The value to insert into the AVL tree.
*
* Return: A pointer to the new root after insertion, or NULL on failure.
*/
avl_t *avl_insert_recursive(avl_t **tree, avl_t *parent,
avl_t **new, int value)
{
int bfactor;
if (*tree == NULL)
return (*new = binary_tree_node(parent, value));
if ((*tree)->n > value)
{
(*tree)->left = avl_insert_recursive(&(*tree)->left, *tree, new, value);
if ((*tree)->left == NULL)
return (NULL);
}
else if ((*tree)->n < value)
{
(*tree)->right = avl_insert_recursive(&(*tree)->right, *tree, new, value);
if ((*tree)->right == NULL)
return (NULL);
}
else
return (*tree);
bfactor = balance(*tree);
if (bfactor > 1 && (*tree)->left->n > value)
*tree = binary_tree_rotate_right(*tree);
else if (bfactor < -1 && (*tree)->right->n < value)
*tree = binary_tree_rotate_left(*tree);
else if (bfactor > 1 && (*tree)->left->n < value)
{
(*tree)->left = binary_tree_rotate_left((*tree)->left);
*tree = binary_tree_rotate_right(*tree);
}
else if (bfactor < -1 && (*tree)->right->n > value)
{
(*tree)->right = binary_tree_rotate_right((*tree)->right);
*tree = binary_tree_rotate_left(*tree);
}
return (*tree);
}
/**
* avl_insert - Inserts a value into an AVL tree.
* @tree: A double pointer to the root node of the AVL tree to insert into.
* @value: The value to insert into the AVL tree.
*
* Return: A pointer to the inserted node, or NULL on failure.
*/
avl_t *avl_insert(avl_t **tree, int value)
{
avl_t *new = NULL;
if (tree == NULL)
return (NULL);
if (*tree == NULL)
{
*tree = binary_tree_node(NULL, value);
return (*tree);
}
avl_insert_recursive(tree, *tree, &new, value);
return (new);
}