-
Notifications
You must be signed in to change notification settings - Fork 0
/
self_balancing_tree.cpp
49 lines (43 loc) · 988 Bytes
/
self_balancing_tree.cpp
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
/*
Problem Link https://www.hackerrank.com/challenges/self-balancing-tree/problem
*/
/*
struct node
{
int val; //value
struct node* left; //left child
struct node* right; //right child
int ht; //height of the node
} node;
*/
node *insert(node *root, int val)
{
node *search = root;
while (search != NULL) {
if (val < search->val) {
if (search->left != NULL) {
serach = serach->left;
}
}
else { //Every val is unique
if (search->right != NULL) {
serach = serach->right;
}
}
}
node *element = new node{val, NULL, NULL, 0};
if (search->ht > 0) {
if (val < serach->val) {
search->left = element;
}
else {
search->right = element;
}
return root;
}
else {
node *tmp = search;
if (val < search->val) {
}
}
}