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

Laboratory 3

The document is a laboratory report on recursion and binary trees. It includes source code for recursive functions to calculate the sum of squares from 0 to a given number, and to swap left and right subtrees in a binary tree. The student found the recursion part easy but struggled with coding binary trees, having to search online for help with functions like inorder traversal, height, leaves count, and subtree swapping. While difficult, coding binary trees was a learning experience. The student prefers Dev C++ to the MindTap IDE and is still getting used to coding in VS.

Uploaded by

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

Laboratory 3

The document is a laboratory report on recursion and binary trees. It includes source code for recursive functions to calculate the sum of squares from 0 to a given number, and to swap left and right subtrees in a binary tree. The student found the recursion part easy but struggled with coding binary trees, having to search online for help with functions like inorder traversal, height, leaves count, and subtree swapping. While difficult, coding binary trees was a learning experience. The student prefers Dev C++ to the MindTap IDE and is still getting used to coding in VS.

Uploaded by

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

Laboratory Report 3

RECURSION AND BINARY TREES


Name: Lintag, Arthur Jacob V. Date: 18/1/2024
Program: CPE Section: E01

A. Instructions:
(Please include the following: 1. Screenshots of the output from MindTap or Dev-C++, 2. Screenshots of the
IDE from MindTap or Dev-C++, and 3. The source code from MindTap or Dev-C++.)

Screenshot of Output
(Insert images here from the output of the console window of MindTap)

Resize the image if necessary.

Screenshot of Codes
(Insert images here)

Screenshot of your code in MindTap. Use the snipping tool to properly capture the codes.

Resize the image properly so that it fits in the table. Use a whole page for this part if necessary.

Source Code
(insert Source Code here)

Paste the actual code from MindTap here. Use the following format: Courier New 9

#include <iostream>

using namespace std;

int main() {
cout << "Hello World" << endl;
return 0;
}

_________________________________________________________________________________________________
CPE104L Data Structures and Algorithms
School of Electrical, Electronics, and Computer Engineering Page | 1
Laboratory Report 3
RECURSION AND BINARY TREES

_________________________________________________________________________________________________
CPE104L Data Structures and Algorithms
School of Electrical, Electronics, and Computer Engineering Page | 2
Laboratory Report 3
RECURSION AND BINARY TREES
B. Coding Exercises:
1. Write a recursive function named sumSquares that returns the sum of the squares of the numbers from 0 to
num, in which num is a nonnegative int variable. Do not use global variables; use the appropriate parameters.
Also write a program to test your function.

Screenshot of Output

Screenshot of Codes

Source Code

#include <iostream>

int sumSquares(int num) {

if (num == 0) {
return 0;
}

else {
return num * num + sumSquares(num - 1);
}
}

int main() {
int num;
std::cout << "Enter a nonnegative integer: ";
std::cin >> num;

_________________________________________________________________________________________________
CPE104L Data Structures and Algorithms
School of Electrical, Electronics, and Computer Engineering Page | 3
Laboratory Report 3
RECURSION AND BINARY TREES
int result = sumSquares(num);
std::cout << "The sum of squares from 0 to " << num << " is: " << result <<
std::endl;

return 0;
}

2. Write a function swapSubtrees that swaps all the left and right subtrees of a binary tree. Add this function to
the class binaryTreeType and create a program to test this function.

Screenshot of Output

Screenshot of Codes

_________________________________________________________________________________________________
CPE104L Data Structures and Algorithms
School of Electrical, Electronics, and Computer Engineering Page | 4
Laboratory Report 3
RECURSION AND BINARY TREES

_________________________________________________________________________________________________
CPE104L Data Structures and Algorithms
School of Electrical, Electronics, and Computer Engineering Page | 5
Laboratory Report 3
RECURSION AND BINARY TREES

Source Code

#include<iostream>
#include<stdlib.h>
using namespace std;

class binaryTreeType{
private:
struct node
{
int key;
struct node *left, *right;
};

struct node*root;

struct node* insert(struct node* Node , struct node*newNode)


{
if (Node == NULL) return newNode;

if (newNode->key < Node->key)


Node->left = insert(Node->left, newNode);

_________________________________________________________________________________________________
CPE104L Data Structures and Algorithms
School of Electrical, Electronics, and Computer Engineering Page | 6
Laboratory Report 3
RECURSION AND BINARY TREES
else if (newNode->key > Node->key)
Node->right = insert(Node->right, newNode);

else{
cout<<"The itrm to be inserted is already in the tree -- duplicates are not allowed.\n";
}
return Node;
}

void recInorder(struct node* Node){


if (Node==NULL)return;
recInorder(Node->left);
cout<<Node->key<<" ";
recInorder(Node->right);
}

int recHeight(struct node*Node){


if (Node==NULL)return 0;
else{

int lHeight = recHeight(Node->left);


int rHeight = recHeight(Node->right);

if (lHeight > rHeight)


return(lHeight + 1);
else return(rHeight + 1);

}
}

void recSwap(struct node*Node)


{
if (Node == NULL)
return;
else
{
struct node* temp;
recSwap(Node->left);
recSwap(Node->right);
temp = Node->left;
Node->left = Node->right;
Node->right = temp;
}
}

int recNodeCount(struct node*Node){


if(Node==NULL)return 0;
return 1+recNodeCount(Node->left)+recNodeCount(Node->right);
}

int recLeavesCount(struct node*Node){


if(Node == NULL) return 0;
if(Node->left == NULL && Node->right == NULL) return 1;
else
return recLeavesCount(Node->left)+recLeavesCount(Node->right);
}

public:
binaryTreeType(){
root=NULL;
}

void Insert(int item)


{
struct node *temp = new struct node;
temp->key = item;
temp->left =NULL;
temp->right = NULL;

_________________________________________________________________________________________________
CPE104L Data Structures and Algorithms
School of Electrical, Electronics, and Computer Engineering Page | 7
Laboratory Report 3
RECURSION AND BINARY TREES
root= insert(root,temp);
}

void Inorder(){
return recInorder(root);
}
int height(){
return recHeight(root);
}

int NodeCount(){
return recNodeCount(root);
}
int LeavesCount(){
return recLeavesCount(root);
}
void swapSubtrees(){
recSwap(root);
}

};

int main(){
int data;
binaryTreeType tree;
cout<<"Enter the elements ending with -1\n";
while(1){
cin>>data;
if(data==-1)break;
tree.Insert(data);
}
cout<<"\n\n\n";

cout<<"The elements in an inorder : ";


tree.Inorder();

cout<<"\nThe number of nodes in the binary tree : "<<tree.NodeCount()<<"\n";


cout<<"The number of leaves in the binary tree : "<<tree.LeavesCount()<<"\n\n";
cout<<"The tree height is "<<tree.height()<<"\n";

tree.swapSubtrees();
cout<<"After swapping subtrees the tree elements in the order : ";
tree.Inorder();
cout<<"\nThe tree height is "<<tree.height()<<"\n";

return 0;
}

_________________________________________________________________________________________________
CPE104L Data Structures and Algorithms
School of Electrical, Electronics, and Computer Engineering Page | 8
Laboratory Report 3
RECURSION AND BINARY TREES
C. Findings, Observations, and Comments:
Guiding Questions :
1. Did you solve the coding exercise/s?
2. Are there any difficulties during coding or set-up that you encountered?
3. What are your findings, observations, and comments in the coding exercise?

(Please type your findings, observations, and comments according to the guide questions in a paragraph
format.)
1. The first part of the laboratory which is the recusion is quite easy to solve and
understandable and probably the quickest lesson I’ve learned so far in this course. But on
the second part of the experiment was quite the opposite it is very hard for me to finish and
I need to use dev c++ because the code can’t be accepted in cengage so I tried different
ways and I use the STL to simplify the code.
2. The first part like I said, was quite easy and understandable. But the second part got me
stuck finishing the code. In fact, I didn’t finish it in cengage but I used the dev c++ and STL
to work the code. I’m not quite familiar with Binary trees and swapping sub trees is
challenging I need to search in the internet how to do the inorder height leaves count and
the swapping the subtrees itself to work the code.
3. I learned a lot in Binary trees and I still find it difficult to finish the code but either way it was
fun doing the code, I observed doing the code in binary trees felt like doing the code in
queues and stacks. Furthermore, I also find it hard to code in VS but I’m still learning how to
use and I started using it from now. But I still prefer coding in Dev c++ but maybe using
other language will be okay for me.

_________________________________________________________________________________________________
CPE104L Data Structures and Algorithms
School of Electrical, Electronics, and Computer Engineering Page | 9
Laboratory Report 3
RECURSION AND BINARY TREES
D. Score Sheet

Poor Fair Good Excellent


Criteria Score
(25%) (50%) (75%) (100%)
The report was The report was The report was The report was
incomplete, complete, complete, neat, complete, neat,
I. Completeness,
messy, and had messy, and had and had 1 or 2 and all the
Documentation, and
many many unanswered questions had
Organization of Report
unanswered unanswered questions. been answered.
questions. questions.
The program The program The program The program
had had corrected had corrected had appropriate
inappropriate indention, but indention, elements and
elements and inappropriate adequate structures,
II. Coding Design and
structures, elements and program correct
Patterns
incorrect structures and documentation, indention, and
indention, and poor program but inappropriate excellent
poor program documentation. elements and program
documentation. structures. documentation.
The findings. The findings. The findings. The findings.
observations, observations, observations, observations,
and comments and comments and comments and comments
were not based based on the were based on were based on
on the gathered gathered data the gathered the gathered
III. Findings, Observations, data and results. and results but data and results data and results
and Comments All thoughts were not and were mostly and were
were elaborated. Not elaborated. Most completely
inconsistent and all thoughts were of the thoughts elaborated. All
unclear. consistent and were consistent the thoughts
clear. and clear. were consistent
and clear.
The words used The words used The words used The words used
were were were were
inappropriate, appropriate; appropriate, had appropriate, had
IV. Grammar and wrong grammar however, bad proper grammar proper grammar
Composition usage, and poor grammar and usage, but poor usage, and
sentence poor sentence sentence excellent
construction was construction construction was sentence
observed. were observed. observed. construction.

SCORE:

CHECKED
Signature
Rating
Date

_________________________________________________________________________________________________
CPE104L Data Structures and Algorithms
School of Electrical, Electronics, and Computer Engineering Page |
10

You might also like