-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path131-heap_insert.c
executable file
·148 lines (132 loc) · 3.03 KB
/
131-heap_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "binary_trees.h"
/**
* height - measures the height of a tree
*
* @tree: tree root
* Return: height
*/
int height(const binary_tree_t *tree)
{
int left = 0;
int right = 0;
if (tree == NULL)
return (-1);
left = height(tree->left);
right = height(tree->right);
if (left > right)
return (left + 1);
return (right + 1);
}
/**
* binary_tree_is_perfect - checks if a binary tree is perfect
*
* @tree: tree root
* Return: 1 if tree is perfect, 0 otherwise
*/
int binary_tree_is_perfect(const binary_tree_t *tree)
{
if (tree && height(tree->left) == height(tree->right))
{
if (height(tree->left) == -1)
return (1);
if ((tree->left && !((tree->left)->left) && !((tree->left)->right))
&& (tree->right && !((tree->right)->left) && !((tree->right)->right)))
return (1);
if (tree && tree->left && tree->right)
return (binary_tree_is_perfect(tree->left) &&
binary_tree_is_perfect(tree->right));
}
return (0);
}
/**
* swap - swaps nodes when child is greater than parent
*
* @arg_node: parent node
* @arg_child: child node
* Return: no return
*/
void swap(heap_t **arg_node, heap_t **arg_child)
{
heap_t *node, *child, *node_child, *node_left, *node_right, *parent;
int left_right;
node = *arg_node, child = *arg_child;
if (child->n > node->n)
{
if (child->left)
child->left->parent = node;
if (child->right)
child->right->parent = node;
if (node->left == child)
node_child = node->right, left_right = 0;
else
node_child = node->left, left_right = 1;
node_left = child->left, node_right = child->right;
if (left_right == 0)
{
child->right = node_child;
if (node_child)
node_child->parent = child;
child->left = node;
}
else
{
child->left = node_child;
if (node_child)
node_child->parent = child;
child->right = node;
}
if (node->parent)
{
if (node->parent->left == node)
node->parent->left = child;
else
node->parent->right = child;
}
parent = node->parent, child->parent = parent;
node->parent = child, node->left = node_left;
node->right = node_right, *arg_node = child;
}
}
/**
* heap_insert - function that inserts a value in Max Binary Heap
* @value: value to be inserted
* @root: tree root
* Return: pointer to the created node, or NULL on failure.
*/
heap_t *heap_insert(heap_t **root, int value)
{
heap_t *new_node;
if (*root == NULL)
{
*root = binary_tree_node(NULL, value);
return (*root);
}
if (binary_tree_is_perfect(*root) || !binary_tree_is_perfect((*root)->left))
{
if ((*root)->left)
{
new_node = heap_insert(&((*root)->left), value);
swap(root, &((*root)->left));
return (new_node);
}
else
{
new_node = (*root)->left = binary_tree_node(*root, value);
swap(root, &((*root)->left));
return (new_node);
}
}
if ((*root)->right)
{
new_node = heap_insert(&((*root)->right), value);
swap(root, (&(*root)->right));
return (new_node);
}
else
{
new_node = (*root)->right = binary_tree_node(*root, value);
swap(root, &((*root)->right));
return (new_node);
}
return (NULL);
}