You are on page 1of 6

File: Untitled Document 1 Page 1 of 6

ASSIGNMENT-2
SAIKAT MONDAL
22/10/MI/003

Problem 1: Write a program using arrays to read and print a polynomial


.Write a program to add and multiply two polynomials.

Problem Description: A polynomial is composed of different terms where each of them


holds a coefficient and an exponent. First of all read and display two polynomial i.e. P1,
P2.
After this we have to perform addition and multiplication on P1 & P2 polynomial.

Represetation of polynomial using array:

typedef struct term


{int coef;
int exp;
}term;

typedef struct polynomial


{ int n;
struct term *t;
}poly;

Pseudocode

//read polynomial P1 & P2

void create(struct Poly *p)


{
int i;
printf("Number of terms?");
scanf("%d",&p->n);
p->terms=(struct Term*)malloc(p->n*sizeof(struct Term));
printf("Enter terms\n");
for(i=0;i<p->n;i++)
scanf("%d%d",&p->terms[i].coeff,&p->terms[i].exp);
}

//display polynomial P1 & P2

void display(struct Poly p)


{
int i;
for(i=0;i<p.n;i++)
printf("%d^%d+",p.terms[i].coeff,p.terms[i].exp);
printf("\n");
}

//Addition of two Polynomial

struct Poly *add(struct Poly *p1,struct Poly *p2)


{
int i,j,k;
struct Poly *sum;

sum=(struct Poly*)malloc(sizeof(struct Poly));


sum->terms=(struct Term *)malloc((p1->n+p2->n)*sizeof(struct Term));
i=j=k=0;
File: Untitled Document 1 Page 2 of 6

while(i<p1->n && j<p2->n)


{
if(p1->terms[i].exp>p2->terms[j].exp)
sum->terms[k++]=p1->terms[i++];
else if(p1->terms[i].exp < p2->terms[j].exp)
sum->terms[k++]=p2->terms[j++];
else
{
sum->terms[k].exp=p1->terms[i].exp;
sum->terms[k++].coeff=p1->terms[i++].coeff+p2->terms[j++].coeff;
}
}

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 4: Write a program to evaluate an arithmetic expression using


binary tree.

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

// Define a union to represent a node in the binary tree


union NodeData {
char op;
int value;
};
struct Node {
union NodeData data;
struct Node* left;
struct Node* right;
};

Pseudocode

// Function to create a new node with given operator


struct Node* newNode(char op, struct Node* left, struct Node* right) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data.op = op;
node->left = left;
node->right = right;
return node;
}

// Function to create a new node with given operands


File: Untitled Document 1 Page 3 of 6

struct Node* newLeaf(int value) {


struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data.value = value;
node->left = NULL;
node->right = NULL;
return node;
}
// Function to parse an arithmetic expression string and build the corresponding
expression
tree

struct Node* buildTree(char* expr) {


set stack = empty stack of TreeNodes
for each character c in expr:

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()

// Function to evaluate a binary tree representing an arithmetic expression

int evaluateTree(TreeNode* root):


if root is a leaf node:
return root.value - '0'
set leftValue = evaluateTree(root.left)
set rightValue = evaluateTree(root.right)
switch (root.value):
case '+': return leftValue + rightValue
case '-': return leftValue - rightValue
case '*': return leftValue * rightValue
case '/': return leftValue / rightValue

Problem 5: Write a program to maintain a priority queue using a binary


search tree.

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:

typedef struct tree_node


{
File: Untitled Document 1 Page 4 of 6

int data;
struct tree_node* lchild;
struct tree_node* rchild;
}node;

node* insert_node(node *root, int data)


{
node *new_node;
if(root==NULL)// if tree is empty, create new node and set it as the root node
{
new_node =(node*)malloc(sizeof(node*));
new_node->data = data;
new_node->lchild = NULL;

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;
}

void display(node *root)


{
//Inorder Traversal "LDR"
if(root!=NULL)
{
display(root->lchild);
printf("%d ",root->data);
display(root->rchild);
}
}

node* search_node(node *root, int key)


{
node *temp=root;
while (temp != NULL)
{
if (key == temp->data)
return temp;
else if (key > temp->data)
temp = temp->rchild;
else
temp = temp->lchild;
}
return NULL;
}
//in-order successor
node* get_min(node* root)
{
//min value should present in the left most node
while (root->lchild != NULL)
{
root = root->lchild;
}
return root;
}

node* delete_node(node* root, int data)


{
/*
File: Untitled Document 1 Page 5 of 6

* If the node becomes NULL, it will return NULL


* Two possible ways which can trigger this case
* 1. If we send the empty tree. i.e root == NULL
* 2. If the given node is not present in the tree.
*/
if (root == NULL) return NULL;
/*
* If root->data < data. data must be present in the right subtree
* So, call the above remove function with root->right
*/
if (data < root->data)
root->lchild= delete_node(root->lchild, data);
/*
* if root->data > data. val must be present in the left subtree
* So, call the above function with root->left
*/
else if (data > root->data)
root->rchild= delete_node(root->rchild,data);
/*
* This part will be executed only if the root->data == data
* The actual removal starts from here
*/

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
*/

else if (root->lchild == NULL)


{
node* temp = root;
root=root->rchild;
//free(temp);

}
/*
* Case 3: Node has right child.
* replace the root node with root->right and free the right node

*/

else if (root->rchild == NULL)


{
node* temp = root;
root=root->rchild;
//free(root);

}
/*
* 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);

You might also like