Laboratory 3
Laboratory 3
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)
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>
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>
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;
_________________________________________________________________________________________________
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;
}
}
}
public:
binaryTreeType(){
root=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";
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
SCORE:
CHECKED
Signature
Rating
Date
_________________________________________________________________________________________________
CPE104L Data Structures and Algorithms
School of Electrical, Electronics, and Computer Engineering Page |
10