MCS 021

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Write an Algorithm and a Program that accepts a Binary Tree as input

and checks whether it is a Height Balanced Tree.

#include <stdio.h>
#include <stdlib.h>
struct tnode
{
int data;
struct tnode *lchild, *rchild;
};
 
/* returns maximum of two integers */
int maxi(int a,int b)
{
int c;
c = (a >= b)? a :b;
return c;
}
 
int isBalanced(struct tnode *root)
{
int lh; /* for height of left subtree */
int rh; /* for height of right subtree */
 
/* If tree is empty then return true */
if(root == NULL)
return 1;
 
/* Get the height of left and right sub trees */
lh = height(root->lchild);
rh = height(root->rchild);
 
if( abs(lh-rh) <= 1 &&
isBalanced(root->lchild) &&
isBalanced(root->rchild))
return 1;
 
/* If we reach here then tree is not height-balanced */
return 0;
}
 
/* The function Compute the "height" of a tree. Height is the
number of nodes along the longest path from the root node
down to the farthest leaf node.*/
int height(struct tnode* node)
{
/* base case tree is empty */
if(node == NULL)
return 0;
 
/* If tree is not empty then height = 1 + max of left
height and right heights */
return 1 + maxi(height(node->lchild), height(node->rchild));
}
 
struct tnode *insert(struct tnode *p,int val)
{
struct tnode *temp1,*temp2;
if(p == NULL)
{
p = (struct tnode *) malloc(sizeof(struct tnode));
/* insert the new node as root node*/
if(p == NULL)
{
printf("Cannot allocate\n");
exit(0);
}
p->data = val;
p->lchild=p->rchild=NULL;
}
else
{
temp1 = p;
/* traverse the tree to get a pointer to that node
whose child will be the newly created node*/
while(temp1 != NULL)
{
temp2 = temp1;
if( temp1 ->data > val)
temp1 = temp1->lchild;
else
temp1 = temp1->rchild;
}
if( temp2->data > val)
{
temp2->lchild = (struct tnode*)malloc(sizeof(struct tnode));
/*inserts the newly created node as left child*/
temp2 = temp2->lchild;
if(temp2 == NULL)
{
printf("Cannot allocate\n");
exit(0);
}
temp2->data = val;
temp2->lchild=temp2->rchild = NULL;
}
else
{
temp2->rchild = (struct tnode*)malloc(sizeof(struct tnode));
/*inserts the newly created node as left child*/
temp2 = temp2->rchild;
if(temp2 == NULL)
{
printf("Cannot allocate\n");
exit(0);
}
temp2->data = val;
temp2->lchild=temp2->rchild = NULL;
}
}
return(p);
}
/* a function to binary tree in preorder */
void preorder(struct tnode *p)
{
if(p != NULL)
{
printf("%d\t",p->data);
preorder(p->lchild);
preorder(p->rchild);
}
}
/* a function to binary tree in inorder */
void inorder(struct tnode *p)
{
if(p != NULL)
{
inorder(p->lchild);
printf("%d\t",p->data);
inorder(p->rchild);
}
}
/* a function to binary tree in postorder */
void postorder(struct tnode *p)
{
if(p != NULL)
{
postorder(p->lchild);
postorder(p->rchild);
printf("%d\t",p->data);
}
}
void main()
{
struct tnode *root = NULL;
int n,x;
clrscr();
printf("\nEnter the number of nodes\n");
scanf("%d",&n);
while(n!=0)
{
printf("Enter the data value\n");
scanf("%d",&x);
root = insert(root,x);
n--;
}
if(isBalanced(root))
printf("\n***THIS TREE IS A BALANCED TREE***\n");
else
printf("\n**THIS TREE IS NOT A BALANCED TREE**\n");
 
printf("\n\nPREORDER OF TREE : \n");
preorder(root);
printf("\n\nINORDER OF TREE : \n");
inorder(root);
printf("\n\nPOSTORDER OF TREE : \n");
postorder(root);
getch();
}

Write an algorithm for the implementation of an AVL tree 

CODE:

#include <stdio.h>
#include <stdlib.h>

struct avl_node_s {
struct avl_node_s *left;
struct avl_node_s *right;
int value;
};

typedef struct avl_node_s avl_node_t;

struct avl_tree_s {
struct avl_node_s *root;
};

typedef struct avl_tree_s avl_tree_t;


/* Create a new AVL tree. */
avl_tree_t *avl_create() {
avl_tree_t *tree = NULL;

if( ( tree = malloc( sizeof( avl_tree_t ) ) ) == NULL ) {


return NULL;
}

tree->root = NULL;

return tree;
}

/* Initialize a new node. */


avl_node_t *avl_create_node() {
avl_node_t *node = NULL;

if( ( node = malloc( sizeof( avl_node_t ) ) ) == NULL ) {


return NULL;
}

node->left = NULL;
node->right = NULL;
node->value = 0;
return node;
}

/* Insert a new node. */


void avl_insert( avl_tree_t *tree, int value ) {
avl_node_t *node = NULL;
avl_node_t *next = NULL;
avl_node_t *last = NULL;

/* Well, there must be a first case */


if( tree->root == NULL ) {
node = avl_create_node();
node->value = value;

tree->root = node;

/* Okay. We have a root already. Where do we put this? */


} else {
next = tree->root;
last = NULL;

while( next != NULL ) {


last = next;

if( value < next->value ) {


next = next->left;

} else if( value > next->value ) {


next = next->right;

/* Have we already inserted this node? */


} else if( value == next->value ) {
/* This shouldn’t happen. */
}
}

node = avl_create_node();
node->value = value;

if( value < last->value ) last->left = node;


if( value > last->value ) last->right = node;
}

/* Find the height of an AVL node recursively */


int avl_node_height( avl_node_t *node ) {
int height_left = 0;
int height_right = 0;

if( node->left ) height_left = avl_node_height( node->left );


if( node->right ) height_right = avl_node_height( node->right );
return height_right > height_left ? ++height_right : ++height_left;
}

/* Find the balance of an AVL node */


int avl_node_balance( avl_node_t *node ) {
int balance = 0;
if( node->left ) balance += avl_node_height( node->left );
if( node->right ) balance -= avl_node_height( node->right );

return balance;
}

/* Do a depth first traverse of a node. */


void avl_traverse_node_dfs( avl_node_t *node, int depth ) {
int i = 0;

if( node->left ) avl_traverse_node_dfs( node->left, depth + 2 );

for( i = 0; i < depth; i++ ) putchar( ‘ ‘ );


printf( “%d: %d\n”, node->value, avl_node_balance( node ) );

if( node->right ) avl_traverse_node_dfs( node->right, depth + 2 );


}

/* Do a depth first traverse of a tree. */


void avl_traverse_dfs( avl_tree_t *tree ) {

avl_traverse_node_dfs( tree->root, 0 );
}

void main()
{
avl_tree_t *tree = NULL;
clrscr();
tree = avl_create();

avl_insert( tree, 30 );
avl_insert( tree, 20 );
avl_insert( tree, 40 );
avl_insert( tree, 10 );
avl_insert( tree, 50 );

avl_traverse_dfs( tree );

getch();
}
Write an algorithm for the implementation of a Stack

#include<iostream>

#define SIZE 100

#define NO_ELEMENT -999999

using namespace std;

class Stack {

int arr[SIZE]; // array to store Stack elements

int top;

public:

Stack() {

top = -1;

void push(int); // push an element into Stack

void pop(); // pop the top element from Stack

int topElement(); // get the top element

void display(); // display Stack elements from top to bottom

};

void Stack :: push(int data) {

if (top == SIZE) { // no space in Stack

cout << "Stack Overflow ... " << endl;

return;

else {

top++; // increment top

arr[top] = data; // insert the data onto top of Stack

}
void Stack :: pop() {

if (top == -1) { // Stack is empty

cout << "Stack is empty ... " << endl;

else {

top--; // decrement top (remove the top element)

int Stack :: topElement() {

if (top == -1) { // Stack is empty

return NO_ELEMENT;

else {

return arr[top];

void Stack :: display() {

cout << "Displaying Stack Elements :- " << endl;

int i;

for (i = top; i >= 0; i--) {

cout << arr[i] << " ";

cout << endl;

int main() {

Stack s;

s.push(12);
s.push(5);

s.push(13);

s.display();

cout << "Top Element : " << s.topElement() << endl;

s.pop(); // removing the top element i.e 13

s.push(7);

s.display();

return 0;

You might also like