Algorithms_in_C++ 1.0.0
Set of 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:

Typedefs

using node = node { int data
 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.
 

Variables

int height
 
struct nodeleft
 
struct noderight
 

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.

Typedef Documentation

◆ node

using node = node { int 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
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}
node { int data node
for std::queue
Definition: avltree.cpp:14
int data[MAX]
test data
Definition: hash_search.cpp:24
Definition: binary_search_tree.cpp:11

◆ deleteAllNodes()

void deleteAllNodes ( const node *const  root)

calls delete on every node

Parameters
rootof the tree
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
Here is the call graph for this function:

◆ 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
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
T data(T... args)
Here is the call graph for this function:

◆ getBalance()

int getBalance ( node root)
Parameters
[in]rootof the tree
Returns
difference between height of left and right subtree
49{ return height(root->left) - height(root->right); }

◆ height()

int height ( node root)
Parameters
[in]rootthe root of the tree
Returns
height of tree
38 {
39 if (root == nullptr) {
40 return 0;
41 }
42 return 1 + std::max(height(root->left), height(root->right));
43}
T max(T... args)
Here is the call graph for this function:

◆ 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
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
Here is the call graph for this function:

◆ leftRotate()

node * leftRotate ( node root)
Parameters
rootof the tree to be rotated
Returns
node after left rotation
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
163 {
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}
Here is the call graph for this function:

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
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
Here is the call graph for this function:

◆ minValue()

node * minValue ( node root)
Parameters
rootof the tree
Returns
node with minimum value in the tree
79 {
80 if (root->left == nullptr) {
81 return root;
82 }
83 return minValue(root->left);
84}
Here is the call graph for this function:

◆ rightRotate()

node * rightRotate ( node root)
Parameters
rootof the tree to be rotated
Returns
node after right rotation
55 {
56 node *t = root->left;
57 node *u = t->right;
58 t->right = root;
59 root->left = u;
60 return t;
61}