Trees n' stuff
PhD Student
Spring 2018
Again practice problems and homework help.
But I want to talk about HW1 first. Then trees
From what I've seen so far, here are some tips.
When a question asks you to prove something, you should:
Don't try to answer a question on the homework without understanding what it is asking.
You are likely wasting your time if you do so.
Understand the solution before you write your answer.
If you don't understand a question or a solution that's OK. Try to draw it out, or work it out on a few examples. Go back and check you understand definitions. Ask myself or the professor for help.
There is no shame in not knowing an answer, and there's nothing wrong with asking for help.
Prove that \(\sum^n_{i=1}i^2 = O(n^3)\), for \(n\) a positive integer.
First, do you understand what this question is asking?
What does it mean to be \(O(n^3)\)?
Prove that \(\sum^n_{i=1}i^2 = O(n^3)\), for \(n\) a positive integer.
We have two possible approaches we can take
Either we show that we can find two constants \(n^\prime, c_1\) such that
\(\sum^n_{i=1}i^2 \leq c_1\cdot n^3, \forall n>n^\prime\)
Or that the limit below converges to a finite constant \(c_2\)
\(\lim_{n\to\infty} \sum^n_{i=1}\frac{i^2}{n^3} = c_2\)
Note: The constants \(c_1\) and \(c_2\) are unrelated
Prove that \(\sum^n_{i=1}i^2 = O(n^3)\), for \(n\) a positive integer.
We have two possible approaches we can take
How do I know which to take?
Try one, then the other.
There is no "formula" for problem solving, it's an art. The more you practice and the more time and effort you invest, the better you will become at it.
As you become better at problem solving, you develop more insight, which helps you solve problems in the future
Prove that \(\sum^n_{i=1}i^2 = O(n^3)\), for \(n\) a positive integer.
Consider the sum \(\sum^n_{i=1}i^2\). Since we know that \(i \leq n\), and \(i\) and \(n\) are integers, we know that \(i^2 \leq n^2\).
We can sum both sides of this inequality to get
\(\sum^n_{i=1}i^2 \leq \sum^n_{i=1}n^2\)
Simplifing the right hand side we have
\(\sum^n_{i=1}i^2 \leq n^3\)
Note that this is for all \(n \ge 0\). This satisfies the definition of \(O(n^3)\)
Don't write any code in Word, Notepad, OpenOffice...
Use an IDE. Eclipse, NetBeans, etc... Or at the very least use a lite editor like Sublime of Notepad++ to write code.
These editors are not appropriate for programming. They don't have code completion, syntax highlighting, auto-spacing, a build environment, etc...
These are features that are designed to help you write code faster and with less mistakes.
You are making your life much harder if you do this.
Always test your code.
Make a new file, compile and run it.
Make a small change, compile and run it.
Writing code is not like writing an essay, you should not write it all at once and test it after you've written it.
Compile and run your code before submitting it.
Make another small change, compile and run it.
Compile and Run it
Write in small increments, adding little by little, testing each time you add something.
Check your outputs either with prints or the debugger
Each element of the list is a container. Each container or \(node\) has a
\(root\)
\(null\)
4
2
6
\(null\)
\(null\)
\(null\)
\(null\)
4
2
6
\(null\)
\(null\)
\(null\)
\(nodes\)
nodes have any number of children, leaves have no children
\(leaves\)
\(null\)
4
2
6
\(null\)
\(null\)
\(null\)
All children to the right of a node should be greater than (or equal) the value at the node
All children to the left of a node should be less than the value at the node