-
Notifications
You must be signed in to change notification settings - Fork 0
/
131-heap_insert.c
163 lines (150 loc) · 2.88 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "binary_trees.h"
#include<stdlib.h>
#include<string.h>
#define INIT_NODE {0, NULL, NULL, NULL}
/**
* 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);
}
/**
* convert - converts number and base into string
* @num: input number
* @base: input base
* @lowercase: flag if hexa values need to be lowercase
* Return: result string
*/
char *convert(unsigned long int num, int base, int lowercase)
{
static char *rep;
static char buffer[50];
char *ptr;
rep = (lowercase)
? "0123456789abcdef"
: "0123456789ABCDEF";
ptr = &buffer[49];
*ptr = 0;
do {
*--ptr = rep[num % base];
num /= base;
} while (num);
return (ptr);
}
/**
* 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));
}
/**
* insert - helper func to insert node to correct location
* @root: double pointer to root of max heap
* @node: node to insert
*/
void insert(heap_t **root, heap_t *node)
{
heap_t *tmp;
int size;
unsigned int i;
char *binary;
char c;
tmp = *root;
size = binary_tree_size(tmp) + 1;
binary = convert(size, 2, 1);
for (i = 1; i < strlen(binary); i++)
{
c = binary[i];
if (i == strlen(binary) - 1)
{
if (c == '1')
{
node->parent = tmp;
tmp->right = node;
break;
}
else if (c == '0')
{
node->parent = tmp;
tmp->left = node;
break;
}
}
if (c == '1')
tmp = tmp->right;
else if (c == '0')
tmp = tmp->left;
}
}
/**
* heap_insert - inserts a value in Max Binary Heap
* @root: double pointer to root of tree
* @value: input value
* Return: pointer to the created node, or NULL on failure
*/
heap_t *heap_insert(heap_t **root, int value)
{
heap_t *ht = NULL, *ret;
if (!root)
return (NULL);
ht = calloc(1, sizeof(heap_t));
ht->n = value;
if (!*root)
{
*root = ht;
return (ht);
}
insert(root, ht);
while (ht->parent && ht->n > ht->parent->n)
{
ret = swap(ht->parent, ht);
if (ret)
*root = ret;
}
return (ht);
}