-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ#01_Lab9.cpp
More file actions
126 lines (105 loc) · 3.12 KB
/
Q#01_Lab9.cpp
File metadata and controls
126 lines (105 loc) · 3.12 KB
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
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* leftChild;
Node* rightChild;
int height;
Node(int value) {
data = value;
leftChild = NULL;
rightChild = NULL;
height = 1;
}
};
int calculateHeight(Node* node) {
if (node == NULL) {
return 0;
}
int leftHeight = calculateHeight(node->leftChild);
int rightHeight = calculateHeight(node->rightChild);
return 1 + max(leftHeight, rightHeight);
}
int calculateBalanceFactor(Node* node) {
if (node == NULL) {
return 0;
}
int leftHeight = calculateHeight(node->leftChild);
int rightHeight = calculateHeight(node->rightChild);
return leftHeight - rightHeight;
}
Node* performLLRotation(Node* node) {
Node* leftSubtreeRoot = node->leftChild;
Node* leftRightSubtree = leftSubtreeRoot->rightChild;
leftSubtreeRoot->rightChild = node;
node->leftChild = leftRightSubtree;
node->height = calculateHeight(node);
leftSubtreeRoot->height = calculateHeight(leftSubtreeRoot);
return leftSubtreeRoot;
}
Node* performRRRotation(Node* node) {
Node* rightSubtreeRoot = node->rightChild;
Node* rightLeftSubtree = rightSubtreeRoot->leftChild;
rightSubtreeRoot->leftChild = node;
node->rightChild = rightLeftSubtree;
node->height = calculateHeight(node);
rightSubtreeRoot->height = calculateHeight(rightSubtreeRoot);
return rightSubtreeRoot;
}
Node* performLRRotation(Node* node) {
node->leftChild = performRRRotation(node->leftChild);
return performLLRotation(node);
}
Node* performRLRotation(Node* node) {
node->rightChild = performLLRotation(node->rightChild);
return performRRRotation(node);
}
Node* insertNode(Node* root, int value) {
if (root == NULL) {
return new Node(value);
}
if (value < root->data) {
root->leftChild = insertNode(root->leftChild, value);
} else if (value > root->data) {
root->rightChild = insertNode(root->rightChild, value);
} else {
return root;
}
root->height = calculateHeight(root);
int balance = calculateBalanceFactor(root);
if (balance > 1 && value < root->leftChild->data) {
return performLLRotation(root);
}
if (balance < -1 && value > root->rightChild->data) {
return performRRRotation(root);
}
if (balance > 1 && value > root->leftChild->data) {
return performLRRotation(root);
}
if (balance < -1 && value < root->rightChild->data) {
return performRLRotation(root);
}
return root;
}
void inorderTraversal(Node* root) {
if (root == NULL) {
return;
}
inorderTraversal(root->leftChild);
cout << root->data << " ";
inorderTraversal(root->rightChild);
}
int main() {
Node* root = NULL;
root = insertNode(root, 1);
root = insertNode(root, 2);
root = insertNode(root, 3);
root = insertNode(root, 4);
root = insertNode(root, 5);
root = insertNode(root, 6);
root = insertNode(root, 7);
cout << "AVL Tree created successfully!" << endl;
cout << "Inorder Traversal: " << endl;
inorderTraversal(root);
}