hw1.docxCS 211 Homework #1
Please complete the homework problems on the following page using a separate piece of paper. Note that this is an individual assignment and all work must be your own. Be sure to show your work when appropriate. This assignment is due in lab on Monday, October 10, 2016.
1. [3] Given the following pre-order and in-order traversals, reconstruct the appropriate binary tree. NOTE: You must draw a single tree that works for both traversals.
Pre-order: A, E, D, G, B, F, I, C
In-order: D, E, B, G, A, F, I, C
2. [3] Starting with an empty BST, draw each step in the following operation sequence. Assume that all removals come from the left subtree when the node to remove is full.
Insert(5), Insert(10), Insert(2), Insert(9), Insert(1), Insert(3), Remove(5).
3. [3] Starting with an empty BST, draw each step in the following operation sequence. Assume that all removals come from the right subtree when the node to remove is full.
Insert(10), Insert(5), Insert(23), Insert(4), Insert(19), Insert(7), Insert(9), Insert(6), Remove(5).
4. Given the following binary tree:
A. [1] What is the height of the tree?
B. [1] What is the depth of node 90?
C. [1] What is the height of node 90?
D. [3] Give the pre-order, in-order, and post-order traversal of this tree.
5. Given the following two functions:
int f(int n)
{
if(n <= 0)
{
Return 0;
}
return 1 + f(n - 1);
}
int g(int n)
{
int sum = 0;
for(int i = 0; i < n; i++)
{
sum += 1;
}
Return sum;
}
A. [2] State the runtime complexity of both f() and g()
B. [2] State the memory complexity for both f() and g()
C. [4] Write another function called "int h(int n)" that does the same thing but has a more efficient runtime complexity.
Requirements:
This abstract and outline is for your individual paper that you will be handing in on finals week. Same topic as with your team, but you will write a one paragraph abstract describing your topic, and how you plan to treat it. While you will be walking through all the steps of the Systems Process (which I understand we havent covered in full yet) you may in your abstract and outline want to mention parts that will have more emphasis based on your knowledge of the background of your problem. The outline should obviously include all the steps of the systems process with extra elements based your what you think will have heavier emphasis.
Idea:
So as you know, Elon Musk has just announced SpaceX plan to colonize Mars in the upcoming decades and we thought this would be an interesting topic to research through the 13 steps of the systems engineering process.
Links:
Full Video: https://www.youtube.com/watch?v=IAZ-Xbn5hr0
Short Abbreviated: https://www.youtube.com/watch?v=Yzw6_V7LGeY
Our group idea: after people went to Mars, they will build a system
these ideas supposed to be I think or depends on you:
Buildings, spaces to live, water, and other elements required for life, write in an engineering ...
hw1.docxCS 211 Homework #1Please complete the homework problem.docx
1. hw1.docxCS 211 Homework #1
Please complete the homework problems on the following page
using a separate piece of paper. Note that this is an individual
assignment and all work must be your own. Be sure to show
your work when appropriate. This assignment is due in lab on
Monday, October 10, 2016.
1. [3] Given the following pre-order and in-order traversals,
reconstruct the appropriate binary tree. NOTE: You must draw
a single tree that works for both traversals.
Pre-order: A, E, D, G, B, F, I, C
In-order: D, E, B, G, A, F, I, C
2. [3] Starting with an empty BST, draw each step in the
following operation sequence. Assume that all removals come
from the left subtree when the node to remove is full.
Insert(5), Insert(10), Insert(2), Insert(9), Insert(1), Insert(3),
Remove(5).
3. [3] Starting with an empty BST, draw each step in the
following operation sequence. Assume that all removals come
from the right subtree when the node to remove is full.
Insert(10), Insert(5), Insert(23), Insert(4), Insert(19), Insert(7),
Insert(9), Insert(6), Remove(5).
4. Given the following binary tree:
A. [1] What is the height of the tree?
B. [1] What is the depth of node 90?
C. [1] What is the height of node 90?
D. [3] Give the pre-order, in-order, and post-order traversal of
this tree.
5. Given the following two functions:
int f(int n)
2. {
if(n <= 0)
{
Return 0;
}
return 1 + f(n - 1);
}
int g(int n)
{
int sum = 0;
for(int i = 0; i < n; i++)
{
sum += 1;
}
Return sum;
}
A. [2] State the runtime complexity of both f() and g()
B. [2] State the memory complexity for both f() and g()
C. [4] Write another function called "int h(int n)" that does the
same thing but has a more efficient runtime complexity.
Requirements:
This abstract and outline is for your individual paper that you
will be handing in on finals week. Same topic as with your
team, but you will write a one paragraph abstract describing
your topic, and how you plan to treat it. While you will be
walking through all the steps of the Systems Process (which I
understand we havent covered in full yet) you may in your
3. abstract and outline want to mention parts that will have more
emphasis based on your knowledge of the background of your
problem. The outline should obviously include all the steps of
the systems process with extra elements based your what you
think will have heavier emphasis.
Idea:
So as you know, Elon Musk has just announced SpaceX plan to
colonize Mars in the upcoming decades and we thought this
would be an interesting topic to research through the 13 steps of
the systems engineering process.
Links:
Full Video: https://www.youtube.com/watch?v=IAZ-Xbn5hr0
Short
Abbreviated: https://www.youtube.com/watch?v=Yzw6_V7LGe
Y
Our group idea: after people went to Mars, they will build a
system
these ideas supposed to be I think or depends on you:
Buildings, spaces to live, water, and other elements required for
life, write in an engineering way.
13 steps:
MMMM7.cbp
MA7.docxCS 211 Micro Assignment #7
For this micro assignment, you must implement the following
two functions inside BinarySearchTree.h: int getHeight()
4. Returns the height of the tree. Note that you are allowed to
solve this iteratively or recursively. If you go the recursive
route, you'll probably need to create a helper function, similar
to the tree's addElementHelper. Here's the relevant slide from
the powerpoint presentation:
int getSize()
Returns the total number of nodes present in the tree. Note that
I purposely took out the _size variable from the tree's
implementation. You are not allowed to use a counter variable.
Instead, like getHeight, you must calculate the tree's height
either iteratively or recursively.
Grading
Your submission will be graded based on the following:
1. [7] Your solution passes the test cases
2. [3] Your code contains good style. For example,
· You provide meaningful variable names
· You provide sufficient and meaningful comments
· Your code is well structured
Due Date
This assignment must be submitted through Canvas no later than
1:00 PM on Tuesday, October 11, 2016. Submit your solution
to Canvas in a ZIP file that contains all code and project files
(either Visual Studio or Codeblocks).
TreeNode (2).h
#ifndef TREE_NODE_H
#define TREE_NODE_H
template <typename T>
class TreeNode
10. //Breaks apart the supplied text based on the given delimiter
//static function do not affect the internal state
//(e.g. variables) of a given class instance
static queue<string> splitQ(string text, string delimiter)
{
//vectors are dynamically expanding arrays
queue<string> pieces;
//find the first delimiter
int location = text.find(delimiter);
//we are starting at the beginning of our string
int start = 0;
//go until we have no more delimiters
while (location != string::npos)
{
11. //add the current piece to our list of pieces
string piece = text.substr(start, location - start);
pieces.push(piece);
//update our index markers for the next round
start = location + 1;
location = text.find(delimiter, start);
}
//at the end of our loop, we're going to have one
trailing piece to take care of.
//handle that now.
string piece = text.substr(start, location - start);
pieces.push(piece);
//now, return the completed vector
return pieces;
}
12. };
#endif // STRING_SPLITTER_H
BinarySearchTree (2).h
#ifndef BINARY_SEARCH_TREE_H
#define BINARY_SEARCH_TREE_H
#include "TreeNode.h"
/*
Incomplete BST. We will fill this out more in future lectures.
*/
template <typename T>
class BinarySearchTree
{
private:
TreeNode<T> *_root = nullptr;
13. protected:
virtual TreeNode<T> *findLargestRec(TreeNode<T>
*root)
{
//base case: root has no right child or root is null
if (root == nullptr || root->getRightChild() == nullptr)
{
return root;
}
return findLargestRec(root->getRightChild());
}
virtual TreeNode<T> *findLargestIter(TreeNode<T>
*root)
{
while (root != nullptr || root->getRightChild() !=
nullptr)
{
14. root = root->getRightChild();
}
return root;
}
virtual TreeNode<T> * addItemHelper(TreeNode<T> *root,
T item)
{
//BASE CASE: null root
if (root == nullptr)
{
//allocate new space, store value, then return.
root = new TreeNode<T>{};
root->setValue(item);
return root;
}
//RECURSIVE CASE: root is not null
if (item >= root->getValue())
15. {
//CASE 1: belongs on the right side of root
TreeNode<T> *right = addItemHelper(
root->getRightChild(),
item
);
//update right side with reconfigured state
root->setRightChild(right);
}
else
{
//CASE 2: belongs on the left side of root
TreeNode<T> *left = addItemHelper(
root->getLeftChild(),
item
);
root->setLeftChild(left);
16. }
return root;
}
public:
virtual void addItem(T value)
{
_root = addItemHelper(_root, value);
}
//MA #7 TODO: implement!
//Gets the height of the tree.
int getHeight()
{
return 0;
}
17. //MA #7 TODO: implement!
//Note: You cannot use a "counter variable" to track
height. Your function must
//calculate the height manually (probably w/ recursion).
int getSize()
{
return 0;
}
};
#endif // BINARY_SEARCH_TREE_H