-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ#04_Lab9.cpp
More file actions
168 lines (145 loc) · 4.28 KB
/
Q#04_Lab9.cpp
File metadata and controls
168 lines (145 loc) · 4.28 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
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
164
165
166
167
#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;
}
Node* searchNode(Node* root, int value) {
if (root == NULL || root->data == value) {
return root;
}
if (value < root->data) {
return searchNode(root->leftChild, value);
}
return searchNode(root->rightChild, value);
}
void inorderTraversal(Node* root) {
if (root == NULL) {
return;
}
inorderTraversal(root->leftChild);
cout << root->data << " ";
inorderTraversal(root->rightChild);
}
void preorderTraversal(Node* root) {
if (root == NULL) {
return;
}
cout << root->data << " ";
preorderTraversal(root->leftChild);
preorderTraversal(root->rightChild);
}
void postorderTraversal(Node* root) {
if (root == NULL) {
return;
}
postorderTraversal(root->leftChild);
postorderTraversal(root->rightChild);
cout << root->data << " ";
}
int main() {
Node* root = NULL;
int valueToSearch;
cout << "Enter a value to search or insert: ";
cin >> valueToSearch;
root = insertNode(root, 1);
root = insertNode(root, 2);
root = insertNode(root, 3);
root = insertNode(root, 5);
root = insertNode(root, 25);
Node* searchResult = searchNode(root, valueToSearch);
if (searchResult != NULL) {
cout << "Value " << valueToSearch << " found with key (index) " << searchResult->data << endl;
} else {
cout << "Value " << valueToSearch << " not found in the tree. Inserting..." << endl;
root = insertNode(root, valueToSearch);
}
cout << "\nPre-order Traversal: ";
preorderTraversal(root);
cout << endl;
cout << "In-order Traversal: ";
inorderTraversal(root);
cout << endl;
cout << "Post-order Traversal: ";
postorderTraversal(root);
cout << endl;
return 0;
}