CS301 Assignment No. 2 Fall2016
CS301 - Data Structures Assignment No. 2 Solution Fall 2016 Due Date: Nov 29, 2016Semester: Fall 2016
CS301 – Data Structures
CS301 Assignment Total Marks: 20
CS301 Assignment Due Date: Tuesday, November 29, 2016
CS301 Assignment Instructions
Please read the following instructions carefully before submitting assignment:It should be clear that your assignment will not get any credit if:
- Assignment is submitted after due date.
- Submitted assignment does not open or file is corrupt.
- Assignment is copied (From internet/ to from students).
Note: Use ONLY Dev-C++ IDE for writing and executing your code.
Also Read : Free Microsoft Office Specialist (MOS) for Virtual University
CS301 Assignment Objectives:
The purpose of this assignment is to make you familiar with following topics:- Tree
- Binary Tree
- Terminologies of a Binary Tree
- Level
- Complete Binary Tree
- Level of a Complete Binary Tree
CS301 Assignment Submission Instructions
You have to submit only .cpp on the Assignments interface of CS301 at VULMS. Assignment submitted in any other format will not be accepted and will be graded zero marks.CS301 Assignment No. 2
CS301 Assignment Question 1:
Write down C++ code to create a complete binary Tree in which user can add any number of node/elements (Numeric). After Creating a complete binary tree , You need to calculate the product of the 2nd lowest level and find the smallest number in the binary tree as given in the example:Product of Elements in 2nd last level (N-1)= 20
Smallest number in Binary Tree = 2
Note: N levels mean any number of levels.
CS301 Assignment Deadline:
Your assignment must be uploaded on or before Tuesday, November 29, 2016. We shall not accept your solution through email after the due date.CS301 Assignment No. 2 Solution Fall 2016
CS301 Assignment No. 2 Solution
#include <iostream>
using namespace std;
struct Node
{
struct Node *left, *right;
};
Node *newNode(int data)
{
Node *temp = new Node;
temp->val = data;
temp->left = temp->right = NULL;
return temp;
}
// A utility function to find deepest leaf node.
// lvl: level of current node.
// maxlvl: pointer to the deepest left leaf node found so far
// isLeft: A bool indicate that this node is left child of its parent
// resPtr: Pointer to the result
void deepestLeftLeafUtil(Node *root, int lvl, int *maxlvl,
bool isLeft, Node **resPtr)
{
// Base case
if (root == NULL)
return;
// Update result if this node is left leaf and its level is more
if (isLeft && !root->left && !root->right && lvl > *maxlvl)
{
*resPtr = root;
*maxlvl = lvl;
return;
}
// Recur for left and right subtrees
deepestLeftLeafUtil(root->left, lvl+1, maxlvl, true, resPtr);
deepestLeftLeafUtil(root->right, lvl+1, maxlvl, false, resPtr);
}
// A wrapper over deepestLeftLeafUtil().
Node* deepestLeftLeaf(Node *root)
{
int maxlevel = 0;
Node *result = NULL;
deepestLeftLeafUtil(root, 0, &maxlevel, false, &result);
return result;
}
Node* insert(Node* node, int val)
{
/* 1. If the tree is empty, return a new,
single node */
if (node == NULL)
return(newNode(val));
else
{
/* 2. Otherwise, recur down the tree */
if (val <= node->val)
node->left = insert(node->left, val);
else
node->right = insert(node->right, val);
return node;
}
}
int minValue(Node* node) {
Node* current = node;
while (current->left != NULL) {
current = current->left;
}
return(current->val);
}
int main()
{
int a, b, c, d, e, f, g, h, i, j;
cout"Enter 10 Numbers in Tree:\n";
cin>>a>>b>>c>>d>>e>>f>>g>>h>>i>>j;
Node* root = newNode(a);
root->left = newNode(b);
root->right = newNode(c);
root->left->left = newNode(d);
root->right->left = newNode(e);
root->right->right = newNode(f);
root->right->left->right = newNode(g);
root->right->right->right = newNode(h);
root->right->left->right->left = newNode(i);
root->right->right->right->right = newNode(j);
root = insert(root, a);
insert(root, b);
insert(root, c);
insert(root, d);
insert(root, e);
insert(root, f);
insert(root, g);
insert(root, h);
insert(root, i);
insert(root, j);
Node *result = deepestLeftLeaf(root);
if (result){
cout "Product of 2nd lowest level of a binary tree is: " result->val * result->val;
}
cout "There is no left leaf in the given tree";
}
return 0;
}
Post a Comment
Click to see the code!
To insert emoticon you must added at least one space before the code.