avltree.cpp File Reference

A simple tree implementation using nodes. More...

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

## Classes

class | node< Kind >

## Typedefs

typedef struct node | node

## Functions

node * | createNode (int data)
int | height (node *root)
int | getBalance (node *root)
node * | rightRotate (node *root)
node * | leftRotate (node *root)
node * | minValue (node *root)
node * | insert (node *root, int item)
node * | deleteNode (node *root, int key)
void | levelOrder (node *root)
int | main ()

## 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.

## Function Documentation

## createNode()

node* createNode | ( | int | data | ) |

Create and return a new Node

{
node *nn = new node();
nn->data = data;
nn->height = 0;
nn->left = NULL;
nn->right = NULL;
return nn;
}
data
int data[MAX]
test data
Definition: hash_search.cpp:24
node
struct list node
node
Definition: avltree.cpp:13

## deleteNode()

node* deleteNode | ( | node * | root,
|  | int | key
| ) |  |

Balanced Deletion

{
if (root == NULL)
return root;
if (key < root->data)
root->left = deleteNode(root->left, key);
else if (key > root->data)
root->right = deleteNode(root->right, key);
else {
// Node to be deleted is leaf node or have only one Child
if (!root->right) {
node *temp = root->left;
delete (root);
root = NULL;
return temp;
} else if (!root->left) {
node *temp = root->right;
delete (root);
root = NULL;
return temp;
}
// Node to be deleted have both left and right subtrees
node *temp = minValue(root->right);
root->data = temp->data;
root->right = deleteNode(root->right, temp->data);
}
// Balancing Tree after deletion
return root;
}
deleteNode
node * deleteNode(node *root, int key)
Definition: avltree.cpp:88
minValue
node * minValue(node *root)
Definition: avltree.cpp:59
std::vector::data
T data(T... args)

## getBalance()

int getBalance | ( | node * | root | ) |

Returns difference between height of left and right subtree

{ return height(root->left) - height(root->right); }
height
int height(node *root)
Definition: avltree.cpp:31

## height()

int height | ( | node * | root | ) |

Returns height of tree

{
if (root == NULL)
return 0;
return 1 + std::max(height(root->left), height(root->right));
}
std::max
T max(T... args)

## insert()

node* insert | ( | node * | root,
|  | int | item
| ) |  |

Balanced Insertion

{
node *nn = createNode(item);
if (root == NULL)
return nn;
if (item < root->data)
root->left = insert(root->left, item);
else
root->right = insert(root->right, item);
int b = getBalance(root);
if (b > 1) {
if (getBalance(root->left) < 0)
root->left = leftRotate(root->left); // Left-Right Case
return rightRotate(root); // Left-Left Case
} else if (b < -1) {
if (getBalance(root->right) > 0)
root->right = rightRotate(root->right); // Right-Left Case
return leftRotate(root); // Right-Right Case
}
return root;
}
createNode
node * createNode(int data)
Definition: avltree.cpp:21
rightRotate
node * rightRotate(node *root)
Definition: avltree.cpp:41
insert
node * insert(node *root, int item)
Definition: avltree.cpp:66
getBalance
int getBalance(node *root)
Definition: avltree.cpp:38
leftRotate
node * leftRotate(node *root)
Definition: avltree.cpp:50

## leftRotate()

node* leftRotate | ( | node * | root | ) |

Returns Node after Left Rotation

{
node *t = root->right;
node *u = t->left;
t->left = root;
root->right = u;
return t;
}

## levelOrder()

void levelOrder | ( | node * | root | ) |

LevelOrder (Breadth First Search)

{
std::queue<node *> q;
q.push(root);
while (!q.empty()) {
root = q.front();
std::cout << root->data << " ";
q.pop();
if (root->left)
q.push(root->left);
if (root->right)
q.push(root->right);
}
}
std::cout
std::queue

## main()

int main | ( | void |  | ) |

Main function

{
// Testing AVL Tree
node *root = NULL;
int i;
for (i = 1; i <= 7; i++) root = insert(root, i);
std::cout << "LevelOrder: ";
levelOrder(root);
root = deleteNode(root, 1); // Deleting key with value 1
std::cout << "\nLevelOrder: ";
levelOrder(root);
root = deleteNode(root, 4); // Deletin key with value 4
std::cout << "\nLevelOrder: ";
levelOrder(root);
return 0;
}
levelOrder
void levelOrder(node *root)
Definition: avltree.cpp:119

## minValue()

node* minValue | ( | node * | root | ) |

Returns node with minimum value in the tree

{
if (root->left == NULL)
return root;
return minValue(root->left);
}

## rightRotate()

node* rightRotate | ( | node * | root | ) |

Returns Node after Right Rotation

{
node *t = root->left;
node *u = t->right;
t->right = root;
root->left = u;
return t;
}