-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbalance_checking_bst.c
More file actions
31 lines (30 loc) · 963 Bytes
/
balance_checking_bst.c
File metadata and controls
31 lines (30 loc) · 963 Bytes
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
#include<stdio.h>
#include<stdlib.h>
struct Node* insert(struct Node* node, int link)
{
if (node == NULL)
return(newNode(link));
if (link < node->link) //creating bst
node->left = insert(node->left, link);
else if (link > node->link)
node->right = insert(node->right, link);
else
return node;
node->height = 1 + max(height(node->left),height(node->right));
int balance = getBalance(node);
if (balance > 1 && link < node->left->link)
return rightRotate(node);
if (balance < -1 && link > node->right->link)
return leftRotate(node);
if (balance > 1 && link > node->left->link)
{
node->left = leftRotate(node->left);
return rightRotate(node);
}
if (balance < -1 && link < node->right->link)
{
node->right = rightRotate(node->right);
return leftRotate(node);
}
return node;
}