daescu/cs3345spr12-hw3sol.docx · web viewcs 3345 – spring 2012 homework 3 solutions q1....

9
CS 3345 – Spring 2012 Homework 3 Solutions Q1. a. Algorithm: preorderNext (node v) Input: the current node v Output: the next node in the preorder traversal of T Pseudocode: if v is internal then return v’s left child else node p = parent of v if v is left child of p then return right child of p else while v is not a left child of p and p is not root do v = p p = p.parent end while return right child of p end if end if 1

Upload: dinhnhu

Post on 22-Apr-2018

219 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: daescu/cs3345spr12-hw3Sol.docx · Web viewCS 3345 – Spring 2012 Homework 3 Solutions Q1. Algorithm: preorderNext (node v) Input: the current node v Output: the next node in the

CS 3345 – Spring 2012Homework 3 Solutions

Q1.

a. Algorithm: preorderNext (node v)Input: the current node vOutput: the next node in the preorder traversal of T

Pseudocode:

if v is internal thenreturn v’s left child

elsenode p = parent of v

if v is left child of p thenreturn right child of p

else while v is not a left child of p and p is not root do

v = pp = p.parent

end whilereturn right child of p

end ifend if

1

Page 2: daescu/cs3345spr12-hw3Sol.docx · Web viewCS 3345 – Spring 2012 Homework 3 Solutions Q1. Algorithm: preorderNext (node v) Input: the current node v Output: the next node in the

b. Algorithm: inOrderNext (node v)Input: the current node vOutput: the next node in the in order traversal of T

Pseudocode:

if v is an internal node thencurrent v.rightwhile current has a left child do

current current.leftreturn current

elsecurrent vp current.parentwhile current is the right child of p do

if p = root thenreturn null

else current pp current.parent

end ifend whilereturn p

end if

c. For this algorithm, we assume that the Boolean methods isRightChild () and isLeftChild () already exist as part of the implementation of a node

2

Page 3: daescu/cs3345spr12-hw3Sol.docx · Web viewCS 3345 – Spring 2012 Homework 3 Solutions Q1. Algorithm: preorderNext (node v) Input: the current node v Output: the next node in the

Algorithm: postOrderNext (node v)Input: the current node vOutput: the next node in the postorder traversal of T

Pseudocode:

if v is an internal node thenif v is a right child then

return v.parentelse

v (v.parent).right

while v is an internal node dov v.left

end whilereturn v

end ifelse

if v is a right child thenreturn v.parent

elsev (v.parent).right

while v is an internal node doif v.left is not null then

v v.leftelse

v v.rightend if

end whilereturn v

end ifend if

Q2. The diameter of the tree will be the maximum of three numbers:

a. The diameter of the subtree with (T.root).left as its root

3

Page 4: daescu/cs3345spr12-hw3Sol.docx · Web viewCS 3345 – Spring 2012 Homework 3 Solutions Q1. Algorithm: preorderNext (node v) Input: the current node v Output: the next node in the

b. The diameter of the subtree with (T.root).right as its rootc. The maximum length of a path between nodes that goes through T.root

This algorithm should be called with T.root. It runs in O(n2) time.

Algorithm: findDiameter (node v)Input: A node vOutput: The diameter of a tree T with v as its root

Pseudocode:

if v = null thenreturn 0

end if

leftHeight getHeight (v.left)rightHeight getHeight (v.right)leftDiameter findDiameter (v.left)rightDiameter finDiameter (v.right)

return max (leftHeight + rightHeight + 1, leftDiameter, rightDiameter)

Q3.

4

Page 5: daescu/cs3345spr12-hw3Sol.docx · Web viewCS 3345 – Spring 2012 Homework 3 Solutions Q1. Algorithm: preorderNext (node v) Input: the current node v Output: the next node in the

5

Page 6: daescu/cs3345spr12-hw3Sol.docx · Web viewCS 3345 – Spring 2012 Homework 3 Solutions Q1. Algorithm: preorderNext (node v) Input: the current node v Output: the next node in the

6

Page 7: daescu/cs3345spr12-hw3Sol.docx · Web viewCS 3345 – Spring 2012 Homework 3 Solutions Q1. Algorithm: preorderNext (node v) Input: the current node v Output: the next node in the

Q4. The following algorithm runs in O(n) time. The BottomUpHeap () method requires O(n) operations and the for loop will take at most log2n operations. This gives us a O(n + log2n) = O(n) run-time.

Algorithm: sortFlyers (A)Input: an array A of size n where A[i] contains the frequent flyer miles of flyer iOutput: a sorted list with the top log n frequent flyers

Pseudocode:

Let L be a new listH BottomUpHeap (A)

for n = 0 to log n doa H.removeMin ()L.insertLast (a)

end forreturn L

Q5.

The hash table using linear probing:

Key 11 43 33 13 14 16 12 3 22 - 10Position 0 1 2 3 4 5 6 7 8 9 10

The hash table using double hashing:

Key 11 43 33 14 13 16 22 12 - 3 10Position 0 1 2 3 4 5 6 7 8 9 10

7

Page 8: daescu/cs3345spr12-hw3Sol.docx · Web viewCS 3345 – Spring 2012 Homework 3 Solutions Q1. Algorithm: preorderNext (node v) Input: the current node v Output: the next node in the

Q6.

8