Documente Academic
Documente Profesional
Documente Cultură
CS3137
Peter Lucak
Using Induction, prove that the
number of NULL child pointers in a
binary tree of N nodes is N+1
- -
* / 2 *
2 ^ 8 2 3 5
3 5
Given the infix expression
((2*3^5-(8/2)/(2-3*5)
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
for i=0 to n {
if expr[i] is operand
else {
right = S.pop()
left = S.pop()
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.
Inputs:
H - hash table
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;
}