Please implement an AVL tree with four basic operations, which are "insert", "inorder", and "preorder".
You may use the code template below and replace /* TODO */ section with your code.
Template.cpp
// C++ program to insert a node in AVL tree
#include <iostream>
using namespace std;
// An AVL tree node
class Node
{
public:
int key;
Node *left;
Node *right;
int height;
};
// A utility function to get maximum
// of two integers
int max(int a, int b);
// A utility function to get the
// height of the tree
int height(Node *N)
{
if (N == NULL)
return 0;
return N->height;
}
// A utility function to get maximum
// of two integers
int max(int a, int b)
{
return (a > b)? a : b;
}
/* Helper function that allocates a
new node with the given key and
NULL left and right pointers. */
Node* newNode(int key)
{
Node* node = new Node();
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1; // new node is initially
// added at leaf
return(node);
}
// A utility function to right
// rotate subtree rooted with y
// See the diagram given above.
Node *rightRotate(Node *y)
{
Node *x = y->left;
Node *T2 = x->right;
// Perform rotation
x->right = y;
y->left = T2;
// Update heights
y->height = max(height(y->left),
height(y->right)) + 1;
x->height = max(height(x->left),
height(x->right)) + 1;
// Return new root
return x;
}
// A utility function to left
// rotate subtree rooted with x
// See the diagram given above.
Node *leftRotate(Node *x)
{
/* TODO */
// Perform rotation
/* TODO */
// Update heights
/* TODO */
// Return new root
return y;
}
// Get Balance factor of node N
int getBalance(Node *N)
{
if (N == NULL)
return 0;
return height(N->left) - height(N->right);
}
// Recursive function to insert a key
// in the subtree rooted with node and
// returns the new root of the subtree.
Node* insert(Node* node, int key)
{
/* 1. Perform the normal BST insertion */
if (node == NULL)
return(newNode(key));
if (key < node->key)
node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
else // Equal keys are not allowed in BST
return node;
/* 2. Update height of this ancestor node */
node->height = 1 + max(height(node->left),
height(node->right));
/* 3. Get the balance factor of this ancestor
node to check whether this node became
unbalanced */
int balance = getBalance(node);
// If this node becomes unbalanced, then
// there are 4 cases
// Left Left Case
if (balance > 1 && key < node->left->key)
{
/* TODO */
}
// Right Right Case
if (balance < -1 && key > node->right->key)
{
/* TODO */
}
// Left Right Case
if (balance > 1 && key > node->left->key)
{
/* TODO */
}
// Right Left Case
if (balance < -1 && key < node->right->key)
{
/* TODO */
}
/* return the (unchanged) node pointer */
return node;
}
// A utility function to print preorder
// traversal of the tree.
// The function also prints height
// of every node
void preOrder(Node *root)
{
if(root != NULL)
{
cout << root->key << endl;
preOrder(root->left);
preOrder(root->right);
}
}
void inOrder(Node *root)
{
if(root != NULL)
{
inOrder(root->left);
cout << root->key << endl;
inOrder(root->right);
}
}
// Driver code
int main()
{
Node *root = NULL;
string cmd;
while(cin >> cmd){
if(cmd == "insert"){
int x;
cin >> x;
root = insert(root,x);
}else if(cmd == "preorder"){
preOrder(root);
}else if(cmd == "inorder"){
inOrder(root);
}else if(cmd == "exit"){
return 0;
}
}
return 0;
}
The inputs are constructed as follows:
insert i
: Insert the integer i into the AVL tree.
inorder
: Print the inorder traversal representation of the AVL tree.
preorder
: Print the preorder traversal representation of the AVL tree.
Note:
The integers would not be repeated in the inputs.
The corresponding outputs should be constructed as follows:
For inorder
and preorder
, print the integers separated by new line characters.
Note: You should append a newline character(\n
) after each output.