Você está na página 1de 24

Midterm Solutions

CS3137
Peter Lucak
Using Induction, prove that the
number of NULL child pointers in a
binary tree of N nodes is N+1

Basis Case: If the size of the tree is just


one i.e. only the root exists - then the root
will have two NULL child pointers as
predicted by the theorem.
Using Induction, prove that the
number of NULL child pointers in a
binary tree of N nodes is N+1

Inductive Hypothesis: If there are N nodes


then there are N+1 NULL child pointers

Inductive Step: If a node is added, we


therefore have N+1 nodes. This means we
have replaced an empty link with a node
which has two empty links. Therefore the
number of NULL links is: N+1-1+2 = N+2, as
predicted by the theorem.
Using algorithms we have studied in
class, name an example of an
algorithm that satisfies each of the
conditions below.

A constant time algorithm - Insert into


hashtable

A log n time algorithm - Binary search

A linear time algorithm - Search linked list

An exponential time algorithm - Towers of


hanoi, the Fibonacci numbers
Given the infix expression
((2*3^5-(8/2))/(2-3*5)
/

- -

* / 2 *

2 ^ 8 2 3 5

3 5
Given the infix expression
((2*3^5-(8/2)/(2-3*5)

Show an equivalent postfix expression:

235^*82/-235*-/
Give pseudocode for a stack based
algorithm that can be used to
evaluate a legal postfix expression

Inputs:

S - a generic stack

expr[] - the array containing the


expression

n - length of the expr


Give pseudocode for a stack based
algorithm that can be used to
evaluate a legal postfix expression
EVALUATE(S, expr, n) {

for i=0 to n {

if expr[i] is operand

then push expr[i] into S

else {

right = S.pop()

left = S.pop()

output = apply operation found in expr[i] to left and right

push output into S

return S.pop();

}
Write a Java method,
isDegenerate(TreeNode root) which
returns true if the binary tree is
degenerate.
public boolean isDegenerate(TreeNode root) {
if(root == NULL)
return true;
if(root.left != null && root.right != null)
return false;
return isDegenerate(root.left) &&
isDegenerate(root.right);
}
Insert into AVL tree:
4, 7, 8, 2, 5, 9
4
Insert into AVL tree:
4, 7, 8, 2, 5, 9
4

7
Insert into AVL tree:
4, 7, 8, 2, 5, 9
4

ROTATE! 7

8
Insert into AVL tree:
4, 7, 8, 2, 5, 9
7

4 8

Single Rotation!
Insert into AVL tree:
4, 7, 8, 2, 5, 9
7

4 8

2
Insert into AVL tree:
4, 7, 8, 2, 5, 9
7

4 8

2 5
Insert into AVL tree:
4, 7, 8, 2, 5, 9
7

4 8

2 5 9
You are given an application that matches
words from song lyrics to song titles. The
current system uses a hash table
implementation. Each word is hashed to
an entry in the table and each entry
would then point to a linked list which
contained the references to every song
that had that word in its lyrics. Assume
that the database has N distinct words
and M songs for which lyrics have been
hashed.
Assuming no collisions in the hash
table, what is the worst case
complexity for a method that will
print ALL the titles of songs that
contain a particular word.

At worst you can imagine there is a


particular word which is contained in all
songs, and therefore one would have to print
every song. Therefore this operation has
complexity O(M).
Assuming no collisions in the hash
table, what is the worst case
complexity of a method that returns
true if a particular word is in a
particular song.
In order to check if a particular word is in a
particular song we must hash that word, get
the linked list of songs and traverse the
linked list.

Worst case, all songs may contain the word


and thus we must traverse a linked list of
size M. The complexity of this is O(M).
How does the previous answer
change if the song titles are stored
using a binary tree rather than a
linked list?

Then in the worst case instead of having a


linked list of size M we have a BST of size
M. Therefore search in a BST the complexity
got search is log M. (Notice I assumed the
tree was balanced)
Now assume there there are
collisions in the hash table. Modify
the hash table use separate chaining
to handle collisions.
Because there might be a collision, we use a linked
list of words which are stored at each location in
the hash table.

When a new word is added to a particular hash


location we simple add it to the linked list located
there.

Then each data element of this linked list contains


another linked list which contains the songs
associated with each word.
Give an O(N) algorithm to determine if a
linked list contains a cycle. You mat use
O(N) space.
Use a Hash Table!

Inputs:

H - hash table

l - the head of the linked list


Cycle Detection

HASCYCLE(H, l) {
i = l; // i is our current node
while not at end of linked list {
if(H contains i) then return true
place i into H
i = i.next // the next node after i
}
return false;
}

Você também pode gostar