Sie sind auf Seite 1von 7

Name: Khanh Huynh

Student ID: 0761882


Date: 12/10/2013
2IL65 Algorithms
Assignment 6

Exercise 1:
a) 7 distinct positive integer keys: 2, 3, 5, 6, 8, 9, 11.
Tree of height 2:





Tree of height 5:









Tree of height 6:


6
3
5 2
9
8 11
3
2 5
6
8
9
11













b) Tree of height as large as possible:
















3
5
6
8
9
11
2





































M
E
T
H
U
S
E
L
A
H
T
R
E
E
Tree of height as small as possible:












Exercise 2:
a) Assume node X is a node with 2 children.
X has 2 children X has a non-empty right sub-tree Xs successor Y is in Xs right
sub-tree. If Y has a left child Z. Z is the left child of Y Z is smaller than Y. Z is also in
the right sub-tree of X Z is larger than X. Thus X < Z < Y Z is the successor of X
(contradict with the assumption Y is the successor of X). It follows that Y has no left
child.
X has 2 children X has a non-empty left sub-tree Xs predecessor M is in Xs left
sub-tree. If M has a right child N. N is the right child of M N is larger than M. N is
also in the left sub-tree of X N is smaller than X. Thus M < N < X N is the
predecessor of X (contradict with the assumption M is the successor of X). It follows that
M has no left child.
b) Induction Hypothesis: InorderTreeWalk outputs the nodes of a binary search tree in
sorted order for any tree height h.
Base: h = 0
x has height 0 binary search tree rooted at x has 1 element and both of xs children are
NILs. InorderTreeWalk(left[x]) thus do nothing. Then value of key[x] is print.
InorderTreeWalk(right[x]) also do nothing. This means InorderTreeWalk(x) for x with
height 0 outputs the node x in a sorted order correctly.
Step: h > 0
Assume InorderTreeWalk ouputs the nodes of a binary search tree in a sorted order for
any tree height smaller than h. We will prove that InorderTreeWalk outputs correctly for
binary search tree of height h. Left[x] has a smaller height than x, thus
InorderTreeWalk(Left[x]) outputs the nodes from xs left sub-tree in a sorted order
H
E
T M
S
E L
A
E
T
R H
E
(Induction Hypothesis). Then key[x] is printed. As x is larger or equal to all the elements
in its left sub-tree, the output is still in a sorted order. Then InorderTreeWalk(Right[x])
outputs the nodes in xs right sub-tree in a sorted order (Induction Hypothesis). Since all
elements in xs right sub-tree is at least equal to x, the outputs of InorderTreeWalk(x) is
in sorted order. Additionally, the number of nodes in the binary search tree rooted at x is
equal to the sum of number of nodes in xs left and right sub-trees plus 1. Thus
InorderTreeWalk outputs the nodes of a binary search tree of height h in sorted order.

Exercise 3:
The sequence 3: 2, 395, 390, 211, 276, 382, 381, 250, 363 is not the sequence of nodes
examined. Proof by contradiction: If the sequence is a sequence of nodes examined: The
reason is 250 is smaller than 381, and thus must be the left child of 381. 381 is smaller
than 382, and thus must be the left child of 382. 382 in turn is larger than 276, and thus
must be the right child of 276. This means 250 is in the right sub-tree of 276 250
276 False.
The sequence 4: 929, 268, 345, 623, 291, 395, 360, 363 is not the sequence of nodes
examined. Proof by contradiction: If the sequence is a sequence of nodes examined: The
reason is 291 is smaller than 623, and thus must be the left child of 623. 623 is larger than
345, and thus is the right child of 345. This means 291 is in the right sub-tree of 345
291 345 False.

Exercise 4:
i = 0
TreeDepth(x, i)
if x NIL then
print i
i = i + 1
TreeDepth(Left[x], i)
TreeDepth(Right[x], i)

Proof: By induction.
Induction Hypothesis: TreeDepth outputs the correct depth of all nodes in a tree T with
maximum depth h.
Base: h = 0
Value 0 is printed and this is correct as there is only 1 node at the root with depth 0. The
calls of TreeDepth(Left[x], i) and TreeDepth(Right[x], i) would then do nothing as Left[x]
and Right[x] are NILs (since the tree height is 0). Thus TreeDepth is correct for h = 0.
Step: h > 0
Assume TreeDepth outputs the correct depth of all nodes in any tree T with maximum
depth smaller than h. TreeDepth(x, i) with i = h 1 will then call TreeDepth(Left[x], h)
and TreeDepth(Right[x], h) for all node xs at depth h - 1. This in turns will print the
depth of Left[x] and Right[x] (if they are not NIL) as h, which is correct. Thus TreeDepth
outputs also the correct depth of all nodes in any tree T with depth h.

Running time:
T(n) takes a small, constant amount of time on an empty sub-tree T(0) = c for c > 0.
For n > 0, assume the left sub-tree has k nodes, the right sub-tree thus has n k 1 nodes
T(n) = T(k) + T(n k 1) + d (d is a constant and d > 0)
We will prove that T(n) = (c + d)n + c and thus has running time O(n).
Proof:
Induction Hypothesis: T(n) = (c + d)n + c and thus has running time O(n) for all n 0.
T(0) = c = (c + d) * 0 + c
n > 0: Assume the IH holds for all values < n. We will prove that it holds for n.
T(n) = T(k) + T(n k 1) + d
= (c + d)k + c + (c+ d)(n k 1) + c + d
= (c + d)n + c
Thus the algorithm has running time O(n).

Exercise 5:
a) Red-black trees is a balanced binary search tree, thus its height is of (logn).
Using a red-black tree: Search, Insert, Delete take (height) and thus (logn) time. This
is slower than performing these operations on hash tables assuming uniform hashing and
a large enough table. These operations in hash tables would take (1) time on average.
However, a red-black tree supports finding Max, Min, finding successor and predecessor
also in (logn) time. These operations are not efficient in a hash table and also not easy
to be implemented.
b) Algorithm:
For sorting numbers efficiently using a red-black tree, we can do as followed: Build a
red-black tree from scratch using Insert on all elements. When we have the red-black tree,
we can sort the numbers easily using InorderTreeWalk.
Pseudo-code:
SortRed-BlackTree(A)
for i = 1 to A.length do
Insert(T, A[i])
InorderTreeWalk(root[T])

Correctness: As Insert function would insert a new key in the tree T correctly and also fix
the red-black property, the red-black tree is then built correctly and contains all elements
in A. When we have a red-black tree, the function InorderTreeWalk also performs
correctly and outputs all elements in A in a sorted order.

Running time:
As Insert has O(logn) for a red-black tree, building a tree would then take maximum

for a positive constant c.


<

= cnlogn
Thus building a red-black tree has O(nlogn)

>

= cn/2 * (logn 1)
Thus building a red-black tree has (nlogn).
Thus building a red-black tree has (nlogn) running time.
InorderTreeWalk would takes (n) times. Running time for SortRed-BlackTree is then:
T(n) = anlogn + bn (a, b are positive constant)
Thus T(n) has (nlogn) running time.
c) For an imbalanced binary search tree. Building the tree from Insert functions would
then take (n*height) of the tree. In the worst case, the height is of order O(n), thus
building the tree would then takes O(n
2
) time. InorderTreeWalk would still take (n)
time. Thus worst case running time for sorting using a simple (not necessarily balanced)
binary search tree would then take O(n
2
) time.

Das könnte Ihnen auch gefallen