You are on page 1of 8

Lab Journal – Lab 10

Data Structures and Algorithms

Lab Journal - Lab 10

Name: IFFA SHAH

Enrollment #: 01-134181-026

Class/Section: BS (CS)-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, fi nd 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 for Traversal(root, 5):

Data Structures and Algorithms Page 1


Lab Journal – Lab 10

2 State what the following function is intended to compute.


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

return 1 + A_Recursive_Counter(root->left) + A_Recursive_Counter(root->right);


}

Answer: int A_Recursive_Counter(TNode *root)is counting the nodes in the tree


including the root node.

3 Compute answ ers 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.

total number of nodes in the giv en tree=65535


the number of leaf nodes in the giv en tree=32768

ii. A c o m p l e t e b i n a r y t r e e h a s 2 5 n o d e s . W h a t i s d e p t h o f t h e g i v e n t r e e ?

Depth: 4

Task 2 :

Implement the following exercises.

Exercise 1

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 node Curr
3. Get a symbol E from the expression.
4. If the symbol is an operand
Set this operand as data member of the node Curr
Push the node

Data Structures and Algorithms Page 2


Lab Journal – Lab 10

on the stack
5. If the symbol is an operator
T2 = Pop()
T1 = Pop()
Attach T1 to the left and T2 to the right of Curr
Set the operator as data member of the node Curr
Push Curr (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


class TNode{
public:
char data;
TNode * left;
TNode * right;

};
Implement the function TNode * constructTree(string postfix) 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 expression: "ab+ef*g*-"


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

CODE

TNode.h

#pragma once

#include<iostream>
#include<conio.h>

using namespace std;


class TNode
{
public:
char data;
TNode *left;

Data Structures and Algorithms Page 3


Lab Journal – Lab 10

TNode *right;
TNode(char value)
{
data = value;
left = right = NULL;
}
};

void postorder(TNode *p)


{
if (p == NULL)
return;
else
{
postorder(p->left);
postorder(p->right);
cout << p->data;
}
}

main.cpp

#pragma once
#include "TNode.h"
#include <stack>
#include <string>

using namespace std;

bool isoperator(char ch);


TNode * constructTree(string postfix);

int main()
{
string s = "ab+ef*g*-";
TNode *root = constructTree(s);
cout << "\n\n\n\n\n\n\n\n\n\n\t\t\t EXPRESSION: ";
postorder(root);
_getch();
return 0;
}

bool isoperator(char ch)


{
if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^')
return true;
else
return false;
}
TNode * constructTree(string postfix)
{
stack <TNode *> s;
TNode *curr = NULL;
int size = postfix.length();
int i = 0;
while (i != size)
{

Data Structures and Algorithms Page 4


Lab Journal – Lab 10

if (!isoperator(postfix[i]))
{
curr = new TNode(postfix[i]);
s.push(curr);
}
else
{
curr = new TNode(postfix[i]);
curr->right = s.top();
s.pop();
curr->left = s.top();
s.pop();
s.push(curr);
}
i++;
}
curr = s.top();
s.pop();
return curr;
}

OUTPUT :

Data Structures and Algorithms Page 5


Lab Journal – Lab 10

Exercise 2

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

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

bool search(TNode *root, int key)


{
bool flag = false;
if (root == NULL)
return false;
else if (root->data == key)
return true;

Data Structures and Algorithms Page 6


Lab Journal – Lab 10

else
{
flag = search(root->right, key);
flag = search(root->left, key);
}
}

b) int count_leaf(TNode *root) ;

int count_leaf(TNode *root)


{
int count = 0;
if (root == NULL)
{
return 0;
}
else
{
count = count_leaf(root->right);
count = count_leaf(root->left);
count++;
}
return count;
}

c) int count_nonleaf(TNode *root) ;

int count_nonleaf(TNode *root)


{
int count = 0;
if (root == NULL)
{
return 0;
}
else
{
count = count_nonleaf(root->left);
count = count_nonleaf(root->right);
count++;
}
return count;
}

d) bool isleaf(TNode *root, int value) ;

bool isleaf(TNode *t, int value)

{
bool flag = false;
if (t == NULL)
return false;
else if (t->data == value)
{
if (t->left == NULL && t->right == NULL)

Data Structures and Algorithms Page 7


Lab Journal – Lab 10

{
return true;
}
else
{
return false;
}
}
else
{
flag = isleaf(t->right, value);
flag = isleaf(t->left, value);
}
}

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 8

You might also like