TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
avltree.cpp
Go to the documentation of this file.
1
9#include <algorithm>
10#include <iostream>
11#include <queue>
12
13using node = struct node {
14 int data;
15 int height;
16 struct node *left;
17 struct node *right;
18};
19
26 node *nn = new node();
27 nn->data = data;
28 nn->height = 0;
29 nn->left = nullptr;
30 nn->right = nullptr;
31 return nn;
32}
33
38int height(node *root) {
39 if (root == nullptr) {
40 return 0;
41 }
42 return 1 + std::max(height(root->left), height(root->right));
43}
44
49int getBalance(node *root) { return height(root->left) - height(root->right); }
50
56 node *t = root->left;
57 node *u = t->right;
58 t->right = root;
59 root->left = u;
60 return t;
61}
62
68 node *t = root->right;
69 node *u = t->left;
70 t->left = root;
71 root->right = u;
72 return t;
73}
74
80 if (root->left == nullptr) {
81 return root;
82 }
83 return minValue(root->left);
84}
85
92node *insert(node *root, int item) {
93 if (root == nullptr) {
94 return createNode(item);
95 }
96 if (item < root->data) {
97 root->left = insert(root->left, item);
98 } else {
99 root->right = insert(root->right, item);
100 }
101 int b = getBalance(root);
102 if (b > 1) {
103 if (getBalance(root->left) < 0) {
104 root->left = leftRotate(root->left); // Left-Right Case
105 }
106 return rightRotate(root); // Left-Left Case
107 } else if (b < -1) {
108 if (getBalance(root->right) > 0) {
109 root->right = rightRotate(root->right); // Right-Left Case
110 }
111 return leftRotate(root); // Right-Right Case
112 }
113 return root;
114}
115
122node *deleteNode(node *root, int element) {
123 if (root == nullptr) {
124 return root;
125 }
126 if (element < root->data) {
127 root->left = deleteNode(root->left, element);
128 } else if (element > root->data) {
129 root->right = deleteNode(root->right, element);
130
131 } else {
132 // Node to be deleted is leaf node or have only one Child
133 if (!root->right || !root->left) {
134 node *temp = !root->right ? root->left : root->right;
135 delete root;
136 return temp;
137 }
138 // Node to be deleted have both left and right subtrees
139 node *temp = minValue(root->right);
140 root->data = temp->data;
141 root->right = deleteNode(root->right, temp->data);
142 }
143 // Balancing Tree after deletion
144 return root;
145}
146
151void deleteAllNodes(const node *const root) {
152 if (root) {
153 deleteAllNodes(root->left);
154 deleteAllNodes(root->right);
155 delete root;
156 }
157}
158
163void levelOrder(node *root) {
164 std::queue<node *> q;
165 q.push(root);
166 while (!q.empty()) {
167 root = q.front();
168 std::cout << root->data << " ";
169 q.pop();
170 if (root->left) {
171 q.push(root->left);
172 }
173 if (root->right) {
174 q.push(root->right);
175 }
176 }
177}
178
183int main() {
184 // Testing AVL Tree
185 node *root = nullptr;
186 int i = 0;
187 for (i = 1; i <= 7; i++) root = insert(root, i);
188 std::cout << "LevelOrder: ";
189 levelOrder(root);
190 root = deleteNode(root, 1); // Deleting key with value 1
191 std::cout << "\nLevelOrder: ";
192 levelOrder(root);
193 root = deleteNode(root, 4); // Deletin key with value 4
194 std::cout << "\nLevelOrder: ";
195 levelOrder(root);
196 deleteAllNodes(root);
197 return 0;
198}
node * insert(node *root, int item)
inserts a new element into AVL tree
Definition avltree.cpp:92
node * leftRotate(node *root)
Definition avltree.cpp:67
node * createNode(int data)
creates and returns a new node
Definition avltree.cpp:25
node * minValue(node *root)
Definition avltree.cpp:79
struct node { int data; int height; struct node *left; struct node *right;} node
for std::queue
Definition avltree.cpp:13
void deleteAllNodes(const node *const root)
calls delete on every node
Definition avltree.cpp:151
node * deleteNode(node *root, int element)
removes a given element from AVL tree
Definition avltree.cpp:122
int getBalance(node *root)
Definition avltree.cpp:49
node * rightRotate(node *root)
Definition avltree.cpp:55
void levelOrder(node *root)
prints given tree in the LevelOrder
Definition avltree.cpp:163
int height(node *root)
Definition avltree.cpp:38
int main()
Main function.
Definition avltree.cpp:183
int data[MAX]
test data