0% found this document useful (0 votes)
193 views

Cormen Solution

This document contains summaries of several assignments and concepts related to data structures and algorithms. It discusses the time complexities of operations on stacks, counters, and binary trees under different conditions. It also provides pseudocode for non-recursive and recursive tree traversal algorithms, procedures for finding the minimum/maximum and predecessor of a node in a binary search tree, and describes how node rotations affect depths in a binary search tree.

Uploaded by

Javeria Akbar
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
193 views

Cormen Solution

This document contains summaries of several assignments and concepts related to data structures and algorithms. It discusses the time complexities of operations on stacks, counters, and binary trees under different conditions. It also provides pseudocode for non-recursive and recursive tree traversal algorithms, procedures for finding the minimum/maximum and predecessor of a node in a binary search tree, and describes how node rotations affect depths in a binary search tree.

Uploaded by

Javeria Akbar
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

CSCI 3110 Assignment 9

Summer 2010
Katie Fraser
CSID: kfraser
======
17.1-1
======
No. We achieved that result by observing that we can only pop as many
elements from the stack as we have pushed onto it. Since we could only push
O(n) elements in n operations, we could only pop/multipop n elements as well,
for an amortized cost of O(n/n) = O(1).
If we add the multi-push operation, then we can add or remove O(n) elements
in each successive operation, so the running time is O(n^2) for n operations.
The amortized cost would be O(n^2/n) = O(n).
======
17.1-2
======
Suppose we add a DECREMENT operation to the k-bit counter. With k bits,
the INCREMENT operation is THETA(k) in the worst case (flip all ones to
zeroes). With DECREMENT, the worst case is also THETA(k) (flip all zeroes
to ones).
So given a sequence of n operations consisting of alternating INCREMENT
and DECREMENT operations, the time would be THETA(nk).
======
12.1-3
======
Non-recursive algorithm for inorder tree walk:
ITERATIVE-INORDER-WALK(x)
check=true;
Stack s;
push(s,x);
while( s is not empty ) {
while(left[x]!=NIL) { //if there are left-children, put them
push(s,left[x]);//on the stack
x=left[x];
}
while(check==true){
//always true the first time
x=pop(s);
//pop and print top element
print(x);
if(right[x]!=NIL )
//if there is a right-child,
push(s,right[x]); //put on stack and start over
x=right[x];
check=false;
}
//otherwise keep popping the stack
check=true;
//set check back to true
}
======
12.1-4
======

(Note: Used the recursive INORDER pseudocode as a template.)


Preorder: Prints the root before the values in either subtree
PREORDER-TREE-WALK(x)
1 if x != NIL
2
then print key[x]
3
PREORDER-TREE-WALK(left[x])
4
PREORDER-TREE-WALK(right[x])
Postorder: Prints the root after the values in its subtrees.
POSTORDER-TREE-WALK(x)
1 if x != NIL
2
then POSTORDER-TREE-WALK(left[x])
3
POSTORDER-TREE-WALK(right[x])
4
print key[x]
As with the INORDER-TREE-WALK algorithm, the algorithms are called
recursively twice for each node in the tree, so it takes THETA(n) time
to walk a tree with n nodes.
======
12.2-2
======
Write recursive versions of the TREE-MINIMUM and TREE-MAXIMUM procedures.
TREE-MINIMUM(x)
if (left[x]==NIL)
return x;
else
return TREE-MINIMUM(left[x]);
TREE-MAXIMUM(x)
if (right[x]==NIL)
return x;
else
return TREE-MAXIMUM(right[x]);
======
12.2-3
======
Write the TREE-PREDECESSOR procedure.
TREE-PREDECESSOR(x)
if (left[x] != NIL)
return TREE-MAXIMUM(left[x]);
y=p[x];
while(y!=NIL && x=left[y])
x=y;
y=p[y];
return y;
======
13.1-1
======

Complete binary search tree (N=NIL):


8
/

12
/ \
2 6
10 14
/\ /\
/\
/\
1 3 5 7 9 11 13 15
/\ /\ /\ /\ /\ /\ /\ /\
NN NN NN NN NN NN NN NN
/ \

Colouring such that black-height of the root is 2:


8: black
14, 12: red
2, 6, 10, 14: black
1, 3, 4, 7, 9, 11, 13, 15 : red
NILs: black
Colouring such that black-height of the root is 3:
8: black
14, 12: black
2, 6, 10, 14: black
1, 3, 4, 7, 9, 11, 13, 15 : red
NILs: black
Colouring such that black-height of the root is 4:
8: black
14, 12: black
2, 6, 10, 14: black
1, 3, 4, 7, 9, 11, 13, 15 : black
NILs: black
======
13.2-2
======
For every n-node binary search tree, there are exactly n-1 possible rotations.
Argument: Every rotation requires two nodes, a parent and a child. Every node
in the tree has one parent, except for the root. So every node except for the
root can be involved in a rotation with its parent. Thus there are n-1
possible rotations.
======
13.2-3
======
Let a, b, and c be arbitrary nodes in subtrees alpha, beta, and gamma,
respectively, in the left tree of Figure 13.2. How do the depths of a,
b, and c change when a left rotation is performed on node x in the figure?
x becomes a child of y, so its depth increases by one. Alpha is a subtree
of x. Thus the depth of a will increase by one.
y becomes a parent of x, so its depth decreases by one. Gamma is a subtree

of y. Thus the depth of c will decrease by one.


The subtree beta was originally a subtree of y (child of x) but is now a
subtree of x (child of y). So the depth of b will stay the same.

You might also like