Repository navigation
Expand file tree
/
Copy pathAvltree.cpp
More file actions
201 lines (166 loc) · 5.17 KB
/
Copy pathAvltree.cpp
File metadata and controls
201 lines (166 loc) · 5.17 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "Avltree.h"
#include <iostream>
#include <fstream>
#include <string>
#include <stdexcept>
using namespace std;
Avltree::Node::Node(int k) : key(k), left(nullptr), right(nullptr), height(1) {}
int Avltree::height(Node *N) {
if (N == nullptr)
return 0;
return N->height;
}
int Avltree::max(int a, int b) {
return (a > b) ? a : b;
}
Avltree::Node* Avltree::rightRotate(Node *y) {
Node *x = y->left;
Node *T2 = x->right;
// Εκτέλεση περιστροφής
x->right = y;
y->left = T2;
// Ενημέρωση ύψους
y->height = max(height(y->left), height(y->right)) + 1;
x->height = max(height(x->left), height(x->right)) + 1;
return x;
}
Avltree::Node* Avltree::leftRotate(Node *x) {
Node *y = x->right;
Node *T2 = y->left;
// Εκτέλεση περιστροφής
y->left = x;
x->right = T2;
// Ενημέρωση ύψους
x->height = max(height(x->left), height(x->right)) + 1;
y->height = max(height(y->left), height(y->right)) + 1;
return y;
}
int Avltree::getBalance(Node *N) {
if (N == nullptr)
return 0;
return height(N->left) - height(N->right);
}
Avltree::Node* Avltree::insert(Node* node, int key) {
if (node == nullptr)
return(new Node(key));
if (key < node->key)
node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
else
return node;
node->height = 1 + max(height(node->left), height(node->right));
int balance = getBalance(node);
if (balance > 1 && key < node->left->key)
return rightRotate(node);
if (balance < -1 && key > node->right->key)
return leftRotate(node);
if (balance > 1 && key > node->left->key) {
node->left = leftRotate(node->left);
return rightRotate(node);
}
if (balance < -1 && key < node->right->key) {
node->right = rightRotate(node->right);
return leftRotate(node);
}
return node;
}
Avltree::Node* Avltree::minValueNode(Node* node) {
if (node == nullptr) return nullptr;
Node* current = node;
while (current->left != nullptr)
current = current->left;
return current;
}
Avltree::Node* Avltree::deleteNode(Node* root, int key) {
if (root == nullptr)
return root;
if ( key < root->key )
root->left = deleteNode(root->left, key);
else if( key > root->key )
root->right = deleteNode(root->right, key);
else {
if( (root->left == nullptr) || (root->right == nullptr) ) {
Node *temp = root->left ? root->left : root->right;
if (temp == nullptr) {
temp = root;
root = nullptr;
} else
*root = *temp;
delete temp;
} else {
Node* temp = minValueNode(root->right);
root->key = temp->key;
root->right = deleteNode(root->right, temp->key);
}
}
if (root == nullptr)
return root;
root->height = 1 + max(height(root->left), height(root->right));
int balance = getBalance(root);
if (balance > 1 && getBalance(root->left) >= 0)
return rightRotate(root);
if (balance > 1 && getBalance(root->left) < 0) {
root->left = leftRotate(root->left);
return rightRotate(root);
}
if (balance < -1 && getBalance(root->right) <= 0)
return leftRotate(root);
if (balance < -1 && getBalance(root->right) > 0) {
root->right = rightRotate(root->right);
return leftRotate(root);
}
return root;
}
Avltree::Node* Avltree::search(Node* root, int key) {
if (root == nullptr || root->key == key)
return root;
if (root->key < key)
return search(root->right, key);
return search(root->left, key);
}
int Avltree::size(Node* node) {
if (node == nullptr)
return 0;
return 1 + size(node->left) + size(node->right);
}
Avltree::Avltree() : root(nullptr) {}
Avltree::Avltree(const std::string& filename) : Avltree() {
std::ifstream file(filename.c_str());
if (!file) throw std::runtime_error("Cannot open " + filename);
try {
int key;
while (file >> key) {
if (key < 0) throw std::invalid_argument("Values must be nonnegative");
root = insert(root, key);
}
if (!file.eof()) throw std::runtime_error("Invalid integer in " + filename);
} catch (...) { destroy(root); root = nullptr; throw; }
}
void Avltree::destroy(Node* node) {
if (!node) return;
destroy(node->left);
destroy(node->right);
delete node;
}
Avltree::~Avltree() { destroy(root); }
int Avltree::getSize() {
return size(root);
}
int Avltree::findMin() {
Node* node = minValueNode(root);
if (node == nullptr)
throw std::runtime_error("Το δέντρο είναι άδειο.");
return node->key;
}
std::string Avltree::search(int key) {
Node* result = search(root, key);
return result ? "SUCCESS" : "FAILURE";
}
void Avltree::insert(int key) {
if (key < 0) throw std::invalid_argument("Values must be nonnegative");
root = insert(root, key);
}
void Avltree::deleteAvl(int key) {
root = deleteNode(root, key);
}