-
Notifications
You must be signed in to change notification settings - Fork 0
/
133-heap_extract.c
196 lines (179 loc) · 3.42 KB
/
133-heap_extract.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "binary_trees.h"
#define INIT_NODE {0, NULL, NULL, NULL}
#define CONVERT "0123456789ABCDEF"
#define SETUP_NODE_BLOC { \
tmp = *root; \
size = binary_tree_size(*root); \
binary = &buffer[49]; \
*binary = 0; \
}
#define FREE_NODE_BLOC { \
res = tmp->n; \
free(tmp); \
*root = NULL; \
}
#define SWAP_HEAD_BLOC { \
head = *root; \
head = swap_head(head, tmp); \
res = head->n; \
free(head); \
*root = tmp; \
tmp = perc_down(tmp); \
*root = tmp; \
}
#define CONVERT_LOOP { \
*--binary = CONVERT[size % 2]; \
size /= 2; \
}
/**
* swap - swaps two nodes in binary tree
* @a: first node
* @b: second node
* Return: pointer to root
*/
bst_t *swap(bst_t *a, bst_t *b)
{
bst_t a_copy = INIT_NODE;
a_copy.n = a->n;
a_copy.parent = a->parent;
a_copy.left = a->left;
a_copy.right = a->right;
a->parent = b;
a->left = b->left;
a->right = b->right;
if (b->left)
b->left->parent = a;
if (b->right)
b->right->parent = a;
b->parent = a_copy.parent;
if (a_copy.parent)
{
if (a == a_copy.parent->left)
a_copy.parent->left = b;
else
a_copy.parent->right = b;
}
if (b == a_copy.left)
{
b->left = a;
b->right = a_copy.right;
if (a_copy.right)
a_copy.right->parent = b;
}
else if (b == a_copy.right)
{
b->right = a;
b->left = a_copy.left;
if (a_copy.left)
a_copy.left->parent = b;
}
while (b->parent)
b = b->parent;
return (b);
}
/**
* binary_tree_size - measures the size of a binary tree
* @tree: input binary tree
* Return: number of descendant child nodes
*/
size_t binary_tree_size(const binary_tree_t *tree)
{
if (!tree)
return (0);
return (1 + binary_tree_size(tree->left) + binary_tree_size(tree->right));
}
/**
* swap_head - helper func to swap head and node
* @head: pointer to head
* @node: pointer to node
* Return: pointer to severed head of tree
*/
heap_t *swap_head(heap_t *head, heap_t *node)
{
if (node->parent->left == node)
{
node->parent->left = NULL;
} else
node->parent->right = NULL;
node->parent = NULL;
node->left = head->left;
node->right = head->right;
if (head->left)
head->left->parent = node;
if (head->right)
head->right->parent = node;
return (head);
}
/**
* perc_down - percolate head into correct position
* @node: pointer to head
* Return: pointer to head of tree
*/
heap_t *perc_down(heap_t *node)
{
int max;
heap_t *next = node;
if (!node)
return (NULL);
max = node->n;
if (node->left)
max = MAX(node->left->n, max);
if (node->right)
max = MAX(node->right->n, max);
if (node->left && max == node->left->n)
next = node->left;
else if (node->right && max == node->right->n)
next = node->right;
if (next != node)
{
swap(node, next);
perc_down(node);
}
return (next);
}
/**
* heap_extract - extracts the root node of a Max Binary Heap
* @root: double pointer to root of tree
* Return: value stored in the root node
*/
int heap_extract(heap_t **root)
{
size_t size, i;
char *binary, c, buffer[50];
int res;
heap_t *tmp, *head;
if (!root || !*root)
return (0);
SETUP_NODE_BLOC;
if (size == 1)
{
FREE_NODE_BLOC;
return (res);
}
do {
CONVERT_LOOP;
} while (size);
for (i = 1; i < strlen(binary); i++)
{
c = binary[i];
if (i == strlen(binary) - 1)
{
if (c == '1')
{
tmp = tmp->right;
break;
}
else if (c == '0')
{
tmp = tmp->left;
break;
}
}
if (c == '1')
tmp = tmp->right;
else if (c == '0')
tmp = tmp->left;
}
SWAP_HEAD_BLOC;
return (res);
}