-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path131-heap_insert.c
112 lines (92 loc) · 2.09 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
#include "binary_trees.h"
/**
* binary_node - a function that creates a binary tree node
* @parent: pointer to the parent node
* @value: value to put in the new node
* Return: pointer to the new node or NULL on failure
*/
binary_tree_t *binary_node(binary_tree_t *parent, int value)
{
binary_tree_t *newnode;
newnode = (binary_tree_t *)malloc(sizeof(binary_tree_t));
if (!newnode)
return (NULL);
newnode->n = value;
newnode->parent = parent;
newnode->left = NULL;
newnode->right = NULL;
return (newnode);
}
/**
* binary_tree_size - Measures the height of a binary tree
*
* @tree: Pointer to the node to measures the size
*
* Return: The size of the tree starting at @node
*/
size_t binary_tree_size(const binary_tree_t *tree)
{
if (tree == NULL)
return (0);
return (binary_tree_size(tree->left) + 1 +
binary_tree_size(tree->right));
}
/**
* get_parent - Find the parent node for inserting a new node.
* @root: Pointer to the root node of the tree.
* @size: The size of the tree.
* Return: Pointer to the parent node for insertion.
*/
heap_t *get_parent(heap_t *root, size_t size)
{
heap_t *parent = NULL;
size_t idx = size + 1;
while (idx > 1)
{
idx >>= 1;
parent = root;
if (idx & 1)
root = root->right;
else
root = root->left;
}
return (parent);
}
/**
* heap_insert - Insert a value into a Max Binary Heap.
* @root: Double pointer to the root node of the heap.
* @value: The value to insert.
* Return: Pointer to the created node, or NULL on failure.
*/
heap_t *heap_insert(heap_t **root, int value)
{
heap_t *nunode, *parent;
size_t size;
int temp;
if (!root)
return (NULL);
nunode = binary_node(NULL, value);
if (!nunode)
return (NULL);
if (!*root)
{
*root = nunode;
return (nunode);
}
size = binary_tree_size(*root);
parent = get_parent(*root, size);
if (!parent->left)
parent->left = nunode;
else
parent->right = nunode;
nunode->parent = parent;
while (parent && nunode->n > parent->n)
{
temp = nunode->n;
nunode->n = parent->n;
parent->n = temp;
nunode = parent;
parent = parent->parent;
}
return (nunode);
}