Assignment 2
Assignment 2
ASSIGNMENT-2
SAIKAT MONDAL
22/10/MI/003
Pseudocode
for(;i<p1->n;i++)sum->terms[k++]=p1->terms[i];
for(;j<p2->n;j++)sum->terms[k++]=p2->terms[j];
sum->n=k;
return sum;
}
Problem Description: A binary expression tree is a binary tree, where the operators are
stored in the tree’s internal nodes, and the leaves contain constants.
Assume that each node of the binary expression tree has zero or two children. The
supported
operatorsare +(addition), −(subtraction), * (multiplication), /(division) and ^
(exponentiation).
This program uses a binary tree to evaluate an arithmetic expression. The tree is built by
iterating
through each character in the expression and pushing operands onto a stack and operators
onto
the tree as new nodes with the top two operands on the stack as the left and right
children of the
operator node. Once the entire expression has been processed, the top node of the stack
represents the root of the binary tree.
The evaluateTree function recursively evaluates the binary tree by traversing the left and
right
children of each node and performing the corresponding arithmetic operation based on the
operator at the current node
Pseudocode
if c is an operand:
set node = newNode(c)
stack.push(node)
else if c is an operator:
set node = newNode(c)
node.right = stack.top()
stack.pop()
node.left = stack.top()
stack.pop()
stack.push(node)
return stack.top()
Problem Description: This program uses a binary search tree to maintain a priority queue
where elements are inserted with a priority value. Each node in the binary search tree
contains a
value and a priority, with nodes with lower priority values being closer to the root of
the tree. The
insert function recursively traverses the tree to find the correct position for the new
element based on
its priority value. The delete_node function recursively traverses the right subtree of
the binary
search tree to find the node with the highest priority value, removes it from the tree,
and returns its
value.
In the main program, several elements are inserted into the binary search tree using the
insert
function and the element with the highest priority is removed using the delete_node
function.
Represetation of Tree ode:
int data;
struct tree_node* lchild;
struct tree_node* rchild;
}node;
new_node->rchild = NULL;
return new_node;
}
// insert new node at appropriate position in binary tree
//1 if data > root ----insert at right side
//2 if data < root ----insert at left side
if(data > root->data)
root->rchild=insert_node(root->rchild, data);
else
root->lchild=insert_node(root->lchild, data);
return root;
}
else {
/*
* Case 1: Leaf node. Both left and right reference is NULL
* replace the node with NULL by returning NULL to the calling pointer.
* free the node
*/
if (root->lchild == NULL && root->rchild == NULL) {
// free(root);
root=NULL;
}
/*
* Case 2: Node has left child.
* replace the node with root->left and free the left node
*/
}
/*
* Case 3: Node has right child.
* replace the root node with root->right and free the right node
*/
}
/*
* Case 4: Node has both left and right children.
* Find the min value in the right subtree
* replace node value with min.
* And again call the remove function to delete the node which has the min value.
* Since we find the min value from the right subtree call the remove function with root-
>right.
*/
File: Untitled Document 1 Page 6 of 6
else
{
node* temp= get_min(root->rchild);
root->data = temp->data;
root->rchild = delete_node(root->rchild, temp->data);
}}
return root;
//main driver
void main()
{
node *root=NULL;
//Insert node into tree
root= insert_node(root,10);
root= insert_node(root,20);
root= insert_node(root,5);
root= insert_node(root,15);
root= insert_node(root,30);
root= insert_node(root,25);
//display BST
display(root);
// Search node into tree
node *temp = search_node(root, 5);
if (temp)
{
printf("\nSearched node=%d\n", temp->data);
}
else
{
printf("\nData Not found in tree.\n");
}
// delete searched node
root=delete_node(root,temp->data);
printf("\nAfter deletion , the new tree :\n");
display(root);