0% found this document useful (0 votes)
72 views

Data Structures and Algorithms Lab Journal - Lab 8

This lab journal document describes exercises completed on binary tree data structures. The exercises included: 1) Converting a postfix expression to a binary expression tree using a stack. 2) Creating a sample binary tree and implementing functions to count the leaf nodes, non-leaf nodes, and check if a node is a leaf. 3) The outputs of the functions implemented were: the number of leaf nodes is 1, the number of non-leaf nodes is 5, and the sample checks verified their leaf/non-leaf statuses.

Uploaded by

Qazi Mujtaba
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Data Structures and Algorithms Lab Journal - Lab 8

This lab journal document describes exercises completed on binary tree data structures. The exercises included: 1) Converting a postfix expression to a binary expression tree using a stack. 2) Creating a sample binary tree and implementing functions to count the leaf nodes, non-leaf nodes, and check if a node is a leaf. 3) The outputs of the functions implemented were: the number of leaf nodes is 1, the number of non-leaf nodes is 5, and the sample checks verified their leaf/non-leaf statuses.

Uploaded by

Qazi Mujtaba
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab Journal – Lab 8

Data Structures and Algorithms

Lab Journal - Lab 8


Name: Qazi Mujtaba

Enrollment #: 01-235171-050

Class/Section: BS-IT(3B)

Objective

This is a lab session designed for ‘Binary Tree’ data structure and aims to enhance the
understanding of types of binary trees and their traversals.

Task 1 :
Give answers to the following.

1 For the following tree, find output of the given traversal function.
.
void Traversal(Node* temp, int
count)
{
if(temp!=NULL) {
if (count%2 == 0)
cout<<temp->data;
count++;
Traversal(temp->left, count);
Traversal(temp->right, count);
}
}

Output forTraversal(root, 5):

Data Structures and Algorithms Page 1


Lab Journal – Lab 8

2 State what the following function is intended to compute.


. int A_Recursive_Counter(TNode *root)
{
if(root==NULL
)
return0;
else

return1 + A_Recursive_Counter(root->left) + A_Recursive_Counter(root->right);


}
Answer: This function computes the number of nodes in binary tree.

3 Compute answers of the following:


.
i. A complete binary tree has a depth of 15. What is total number of nodes
in the given tree? Also compute the number of leaf nodes in the given
tree.

ANSWER: Using formula


d+1
N=2 - 1
65535 are nodes in tree with depth 15
32768 are leafs nodes in the tree

ii. A complete binary tree has 25 nodes. What is depth of the given tree?

ANSWER: 4

Task 2 :

Implement the following exercises.

Exercise 1

Data Structures and Algorithms Page 2


Lab Journal – Lab 8

An effective way to represent Mathematical expressions is using the binary trees. The
following algorithm can be employed to generate an expression tree from a given postfix
expression.

1. Scan the postfix expression from left to right.


2. Create a nodeCurr
3. Get a symbol E from the expression.
4. If the symbol is an operand
Set this operand as data member of Curr
the node
Push the node on the stack
5. If the symbol is an operator
T2 = Pop()
T1 = Pop()
Attach T1 to the left and T2 to theCurr
right of
Set the operator as data member of Currthe node
PushCurr(with child nodes attached) onto the stack
6. Repeat Steps 1-5 till end of expression
7. Pop the (only remaining) node from the stack which is a pointer to the root of the
expression tree.

Consider the following class to model a node of the tree


classTNode{
public
:
chardata;
TNode * left;
TNode * right;

};
TNode * constructTree(string postfix)
Implement the function to convert a given
postfix expression into an expression tree. Use the stack class in the STL library and create a
Stack as follows.

stack <TNode *> s;

Call the function constructTree() with the postfix"ab+ef*g*-"


expression:
Traverse the produced tree in postorder to verify its correctness. You should get back the
input string.

CODE :

#include<iostream>
#include<conio.h> using namespace std; struct et

Data Structures and Algorithms Page 3


Lab Journal – Lab 8

{ char value; et* left,


*right;
};
bool isOperator(char c)
{ if (c == '+' || c == '-' || c ==
'*' || c == '/' || c == '^')
return true;
return false;
} void inorder(et
*t)
{ if (t)
{ inorder(t->left); printf("%c
", t->value); inorder(t-
>right);
}
} et* newNode(int
v)
{ et *temp = new et; temp->left =
temp->right = NULL; temp->value = v;
return temp; };
et* constructTree(char postfix[])
{ stack<et *> st; et
*t, *t1, *t2;
for (int i = 0; i<strlen(postfix); i++)
{

if (!isOperator(postfix[i]))
{ t = newNode(postfix[i]);
st.push(t); } else
{ t = newNode(postfix[i]); t1
= st.top(); st.pop(); t2
= st.top(); st.pop(); t-
>right = t1; t->left =
t2; st.push(t);
}
}

t = st.top();
st.pop(); return t; }
int main()
{ char postfix[] = "ab+ef*g*-"; et* r =
constructTree(postfix);
printf("infix expression is \n");
inorder(r); return 0;
}

Data Structures and Algorithms Page 4


Lab Journal – Lab 8

Exercise 2

Create the following tree and implement the following functions and give their outputs:

a) int count_leaf(TNode *root)


; //returns the total number of leaf nodes in the tree
b) int count_nonleaf(TNode *root)
; //returns the total number of non-leaf nodes in the
tree
c) bool isleaf(TNode *root, int value)
; //checks if the queried node is a leaf

CODE :

#include <conio.h>
#include <iostream> using
namespace std; struct
node
{ int data; struct node*
left; struct node*
right;
}; int getLeafCount(struct node*

node) { if (node == NULL) return 0;

if (node->left == NULL && node->right == NULL)


return 1;
else return getLeafCount(node->left) +
getLeafCount(node->right);
}
int countNonleaf(struct Node* root)
{

if (root == NULL || (root->left == NULL &&


root->right == NULL)) return 0;
return 1 + countNonleaf(root->left) +
countNonleaf(root->right);
}
struct node* newNode(int data)

Data Structures and Algorithms Page 5


Lab Journal – Lab 8

{ struct node* node = (struct node*)

node->data = data;
node->left = NULL; node-
>right = NULL;

return(node);
} int
main()
{ struct node *root = newNode(1);
root->left = newNode(2); root-
>right = newNode(3); root-
>left->left = newNode(4); root-
>left->right = newNode(5);
cout << "Leaf count of the tree is : " << getLeafCount(root) << endl;
cout << "Non Leaf count of the tree is : " << countNonleaf(root);
return 0;
}

OUTPUT :

Implement the given exercises and get them checked by your instructor. If you are unable to
complete the tasks in the lab session, deposit this journal alongwith your programs (printed
or handwritten) before the start of the next lab session.

S No. Exercise Checked By:


1. Exercise 1

2. Exercise 2

+++++++++++++++++++++++++

Data Structures and Algorithms Page 6

You might also like