TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
avltree.cpp File Reference

A simple tree implementation using nodes. More...

#include <algorithm>
#include <iostream>
#include <queue>
Include dependency graph for avltree.cpp:

Go to the source code of this file.

Typedefs

using node
 for std::queue
 

Functions

nodecreateNode (int data)
 creates and returns a new node
 
int height (node *root)
 
int getBalance (node *root)
 
noderightRotate (node *root)
 
nodeleftRotate (node *root)
 
nodeminValue (node *root)
 
nodeinsert (node *root, int item)
 inserts a new element into AVL tree
 
nodedeleteNode (node *root, int element)
 removes a given element from AVL tree
 
void deleteAllNodes (const node *const root)
 calls delete on every node
 
void levelOrder (node *root)
 prints given tree in the LevelOrder
 
int main ()
 Main function.
 

Detailed Description

A simple tree implementation using nodes.

Todo
update code to use C++ STL library features and OO structure
Warning
This program is a poor implementation and does not utilize any of the C++ STL features.

Definition in file avltree.cpp.

Typedef Documentation

◆ node

using node
Initial value:
struct node {
int data;
int height;
struct node *left;
struct node *right;
}
int height(node *root)
Definition avltree.cpp:38

for std::queue

for std::max for std::cout

Definition at line 13 of file avltree.cpp.

13 {
14 int data;
15 int height;
16 struct node *left;
17 struct node *right;
18};
int data[MAX]
test data

Function Documentation

◆ createNode()

node * createNode ( int data)

creates and returns a new node

Parameters
[in]datavalue stored in the node
Returns
newly created node

Definition at line 25 of file avltree.cpp.

25 {
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}
struct node { int data; int height; struct node *left; struct node *right;} node
for std::queue
Definition avltree.cpp:13

◆ deleteAllNodes()

void deleteAllNodes ( const node *const root)

calls delete on every node

Parameters
rootof the tree

Definition at line 151 of file avltree.cpp.

151 {
152 if (root) {
153 deleteAllNodes(root->left);
154 deleteAllNodes(root->right);
155 delete root;
156 }
157}
void deleteAllNodes(const node *const root)
calls delete on every node
Definition avltree.cpp:151

◆ deleteNode()

node * deleteNode ( node * root,
int element )

removes a given element from AVL tree

Parameters
rootof the tree
[in]elementthe element to be deleted from the tree
Returns
root of the updated tree

Definition at line 122 of file avltree.cpp.

122 {
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}
node * minValue(node *root)
Definition avltree.cpp:79
node * deleteNode(node *root, int element)
removes a given element from AVL tree
Definition avltree.cpp:122

◆ getBalance()

int getBalance ( node * root)
Parameters
[in]rootof the tree
Returns
difference between height of left and right subtree

Definition at line 49 of file avltree.cpp.

49{ return height(root->left) - height(root->right); }

◆ height()

int height ( node * root)
Parameters
[in]rootthe root of the tree
Returns
height of tree

Definition at line 38 of file avltree.cpp.

38 {
39 if (root == nullptr) {
40 return 0;
41 }
42 return 1 + std::max(height(root->left), height(root->right));
43}

◆ insert()

node * insert ( node * root,
int item )

inserts a new element into AVL tree

Parameters
rootof the tree
[in]itemthe element to be insterted into the tree
Returns
root of the updated tree

Definition at line 92 of file avltree.cpp.

92 {
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}
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
int getBalance(node *root)
Definition avltree.cpp:49
node * rightRotate(node *root)
Definition avltree.cpp:55

◆ leftRotate()

node * leftRotate ( node * root)
Parameters
rootof the tree to be rotated
Returns
node after left rotation

Definition at line 67 of file avltree.cpp.

67 {
68 node *t = root->right;
69 node *u = t->left;
70 t->left = root;
71 root->right = u;
72 return t;
73}

◆ levelOrder()

void levelOrder ( node * root)

prints given tree in the LevelOrder

Parameters
[in]rootof the tree

Definition at line 163 of file avltree.cpp.

163 {
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}

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 183 of file avltree.cpp.

183 {
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}
void levelOrder(node *root)
prints given tree in the LevelOrder
Definition avltree.cpp:163

◆ minValue()

node * minValue ( node * root)
Parameters
rootof the tree
Returns
node with minimum value in the tree

Definition at line 79 of file avltree.cpp.

79 {
80 if (root->left == nullptr) {
81 return root;
82 }
83 return minValue(root->left);
84}

◆ rightRotate()

node * rightRotate ( node * root)
Parameters
rootof the tree to be rotated
Returns
node after right rotation

Definition at line 55 of file avltree.cpp.

55 {
56 node *t = root->left;
57 node *u = t->right;
58 t->right = root;
59 root->left = u;
60 return t;
61}