0% found this document useful (0 votes)
9 views10 pages

Prasad Gade PR-5

Uploaded by

prasadgade469
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

Prasad Gade PR-5

Uploaded by

prasadgade469
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Bharatiya Vidya Bhavan’s

Sardar Patel Institute of Technology


(Autonomous Institute Affiliated to University of Mumbai)

UID: 2024301006

Name of student: Prasad Vijay Gade

Class : SE-CE-D Batch: D

Exp.No.: 5

Aim: Binary Tree Creation

Problem Statement:
1-Creation of binary tree and display using any one traversal
2- counting no. of nodes in a binary tree
3- counting no of leaf nodes in a binary tree
4- counting height of a given node in a binary tree
5- create an Arithmetic expression tree from given postfix expression

Program:

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

struct Node
{
int data;
struct Node *left;
struct Node *right;
};

struct Node *createNode(int data)


{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
Bharatiya Vidya Bhavan’s
Sardar Patel Institute of Technology
(Autonomous Institute Affiliated to University of Mumbai)
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}

struct Node *insertLevelOrder(int arr[], int index, int totalNodes)


{
if (index >= totalNodes)
return NULL;

struct Node *root = createNode(arr[index]);


root->left = insertLevelOrder(arr, 2 * index + 1, totalNodes);
root->right = insertLevelOrder(arr, 2 * index + 2, totalNodes);
return root;
}

void preorderTraversal(struct Node *root)


{
if (root == NULL)
return;
printf("%d ", root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
}

void postorderTraversal(struct Node *root)


{
if (root == NULL)
return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ", root->data);
}

void inorderTraversal(struct Node *root)


{
if (root == NULL)
return;
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}

int countNodes(struct Node *root)


{
if (root == NULL)
return 0;
return 1 + countNodes(root->left) + countNodes(root->right);
}

int countLeafNodes(struct Node *root)


{
if (root == NULL)
Bharatiya Vidya Bhavan’s
Sardar Patel Institute of Technology
(Autonomous Institute Affiliated to University of Mumbai)
return 0;
if (root->left == NULL && root->right == NULL)
return 1;
return countLeafNodes(root->left) + countLeafNodes(root->right);
}

int calculateHeight(struct Node *root)


{
if (root == NULL)
return -1; // empty tree
int leftHeight = calculateHeight(root->left);
int rightHeight = calculateHeight(root->right);
return 1 + (leftHeight > rightHeight ? leftHeight : rightHeight);
}

struct Node *findNode(struct Node *root, int value) // finds the node whose height is to be
found
{
if (root == NULL)
return NULL;
if (root->data == value)
return root;

struct Node *leftSearch = findNode(root->left, value);


if (leftSearch != NULL)
return leftSearch;

return findNode(root->right, value);


}

int calculateHeightOfNode(struct Node *root, int value)


{
struct Node *targetNode = findNode(root, value);
if (targetNode == NULL)
return -1;
return calculateHeight(targetNode);
}

int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
int totalNodes = sizeof(arr) / sizeof(arr[0]);

struct Node *root = insertLevelOrder(arr, 0, totalNodes);

printf("Preorder : ");
preorderTraversal(root);
printf("\n");
printf("Postorder : ");
postorderTraversal(root);
printf("\n");
printf("Inorder : ");
inorderTraversal(root);
printf("\n");
Bharatiya Vidya Bhavan’s
Sardar Patel Institute of Technology
(Autonomous Institute Affiliated to University of Mumbai)

int calcHeightOf = 1;
printf("Total number of nodes: %d\n", countNodes(root));
printf("Total number of leaf nodes: %d\n", countLeafNodes(root));
printf("Height of the %d: %d\n", calcHeightOf, calculateHeightOfNode(root, calcHeightOf));

return 0;
}
//POST ORDER TRAVERSAL TO EXPRESSION TREE
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

struct Node
{
char data;
struct Node *lchild;
struct Node *rchild;
};

struct Node *createNode(char data)


{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = data;
newNode->lchild = NULL;
newNode->rchild = NULL;
return newNode;
}

struct Stack
{
int top;
int size;
struct Node **array;
};

struct Stack *createStack(int size)


{
struct Stack *stack = (struct Stack *)malloc(sizeof(struct Stack));
stack->top = -1;
stack->size = size;
stack->array = (struct Node **)malloc(stack->size * sizeof(struct Node *));
return stack;
}

void push(struct Stack *stack, struct Node *node)


{
stack->top++;
stack->array[stack->top] = node;
}

struct Node *pop(struct Stack *stack)


{
struct Node *popped = stack->array[stack->top];
Bharatiya Vidya Bhavan’s
Sardar Patel Institute of Technology
(Autonomous Institute Affiliated to University of Mumbai)
stack->top--;
return popped;
}

struct Node *createExpressionTree(char postfix[])


{
struct Stack *stack = createStack(20);
struct Node *t, *t1, *t2;

for (int i = 0; postfix[i] != '\0'; i++)


{
if (isalpha(postfix[i])) // operand
{
t = createNode(postfix[i]);
push(stack, t);
printf("\nPUSHED %c to stack", postfix[i]);
}
else // operator
{
printf("\nOperator %c", postfix[i]);
t = createNode(postfix[i]);
t2 = pop(stack); // rchild
t1 = pop(stack); // lchild
printf("\nPOPPED %c", t2->data);
printf("\nPOPPED %c", t1->data);
t->rchild = t2;
t->lchild = t1;
printf("\nPUSHED %c <-- %c --> %c", t1->data, postfix[i], t2->data);
push(stack, t);
}
}
t = pop(stack); // root of tree
return t;
}

void inOrderTraversal(struct Node *root)


{
if (root)
{
inOrderTraversal(root->lchild);
printf("%c ", root->data);
inOrderTraversal(root->rchild);
}
}

void postOrderTraversal(struct Node *root)


{
if (root)
{
postOrderTraversal(root->lchild);
postOrderTraversal(root->rchild);
printf("%c ", root->data);
}
}
Bharatiya Vidya Bhavan’s
Sardar Patel Institute of Technology
(Autonomous Institute Affiliated to University of Mumbai)

void preOrderTraversal(struct Node *root)


{
if (root)
{
printf("%c ", root->data);
preOrderTraversal(root->lchild);
preOrderTraversal(root->rchild);
}
}

int main()
{
char postfix[] = "AB+C*DE/F+G*H-/";

struct Node *root = createExpressionTree(postfix);

printf("In-order : ");
inOrderTraversal(root);
printf("\n");
printf("Pre-order : ");
preOrderTraversal(root);
printf("\n");
printf("Post-order : ");
postOrderTraversal(root);
printf("\n");

return 0;
}
Bharatiya Vidya Bhavan’s
Sardar Patel Institute of Technology
(Autonomous Institute Affiliated to University of Mumbai)
Output:

Created a binary tree with 15 nodes for this example and performed all traversals on it, counted
the total number of nodes, counted number of leaf nodes, found height of any given node.

Output-2 Expression Tree

Depicted above is the output of conversion of a postfix traversal to a expression tree.


Bharatiya Vidya Bhavan’s
Sardar Patel Institute of Technology
(Autonomous Institute Affiliated to University of Mumbai)

Handwritten Assignment:
Bharatiya Vidya Bhavan’s
Sardar Patel Institute of Technology
(Autonomous Institute Affiliated to University of Mumbai)
Bharatiya Vidya Bhavan’s
Sardar Patel Institute of Technology
(Autonomous Institute Affiliated to University of Mumbai)
Conclusion:

Implemented Binary Tree data structure and performed operations such as displaying
preorder traversal, counting the number of nodes in the tree, counting no of leaf nodes in the
tree, counting the height of the any node of the tree. Created expression tree from a given
post-order traversal.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy