-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path133-heap_extract.c
117 lines (93 loc) · 1.83 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
#include "binary_trees.h"
/**
* swap_values - Swap values between two nodes.
* @node1: Pointer to the first node.
* @node2: Pointer to the second node.
*/
void swap_values(heap_t *node1, heap_t *node2)
{
int swap = node1->n;
node1->n = node2->n;
node2->n = swap;
}
/**
* get_last_node - Find the last level-order node of the heap.
* @root: Pointer to the root node of the heap.
* Return: Pointer to the last level-order node.
*/
heap_t *get_last_node(heap_t *root)
{
heap_t *queue[1024];
size_t start = 0, rear = 0;
heap_t *lase = NULL;
if (!root)
return (NULL);
queue[rear] = root;
rear++;
while (start < rear)
{
lase = queue[start];
if (lase->left)
{
queue[rear] = lase->left;
rear++;
}
if (lase->right)
{
queue[rear] = lase->right;
rear++;
}
start++;
}
return (lase);
}
/**
* heapify_d - Perform downward heapification after extracting the root.
* @root: Pointer to the root node of the heap.
*/
void heapify_d(heap_t *root)
{
heap_t *max, *left, *right;
if (!root)
return;
max = root;
left = root->left;
right = root->right;
if (left && left->n > max->n)
max = left;
if (right && right->n > max->n)
max = right;
if (max != root)
{
swap_values(root, max);
heapify_d(max);
}
}
/**
* heap_extract - Extract the root node of a Max Binary Heap.
* @root: Double pointer to the root node of the heap.
* Return: The value stored in the extracted root node, or 0 on failure.
*/
int heap_extract(heap_t **root)
{
int xtract;
heap_t *lase;
if (!root || !*root)
return (0);
xtract = (*root)->n;
lase = get_last_node(*root);
if (*root == lase)
{
free(*root);
*root = NULL;
return (xtract);
}
swap_values(*root, lase);
if (lase->parent->left == lase)
lase->parent->left = NULL;
else
lase->parent->right = NULL;
free(lase);
heapify_d(*root);
return (xtract);
}