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

Bstproblems

The document discusses binary trees and binary search trees. It provides several problems and exercises related to operations on binary trees like counting nodes, leaves, distances from root, traversing trees, and numbering nodes. It also discusses operations on binary search trees like finding heights, inserting elements, and finding elements within ranges. The solutions to the problems are expected to have efficient time complexities like O(n) or O(h) where h is the height of the tree.

Uploaded by

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

Bstproblems

The document discusses binary trees and binary search trees. It provides several problems and exercises related to operations on binary trees like counting nodes, leaves, distances from root, traversing trees, and numbering nodes. It also discusses operations on binary search trees like finding heights, inserting elements, and finding elements within ranges. The solutions to the problems are expected to have efficient time complexities like O(n) or O(h) where h is the height of the tree.

Uploaded by

Random Swag
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CS 213 Problem Set 1 Abhiram Ranade

1 Vectors and amortized analysis


1. Consider a vector class which has indexing, the usual push back and a pop back operation, which causes the
last element to be removed from the vector. Show that any sequence of n operations can be implemented in
O(n) time total, i.e. such that the amortized complexity of each operation is O(1). You must ensure that
at all times the allocated memory must be at most some α times the memory in use. You can choose any
constant α. Note that your algorithm has to say when you will increase the memory given to the vector,
and when you will take memory away. A simple strategy could be to double the allocation when the current
allocation is exhausted, and halve it when the current allocation is more than half empty. This does not
work, i.e. there is a sequence of n operations for which the total work will be proportional to n2 rather than
to n.
2. Suppose I use an array of length n of ints to represent an n bit number. Thus each element of the array will
only take values 0 or 1, and the ith element will represent the ith least significant bit. Initially the number
is initialized to all zeroes. Two operations are allowed: increment which increments the value stored, and
print which prints the value. Note that when you increment, there will be carries and in general many bits
will change. Show that starting at zero if you perform N increment operations, the total work you do is
O(N ), i.e. the amortized cost of each increment operation is O(1). What is the maximum possible cost
(time taken) for an increment operation?

2 Binary trees and binary search trees


Some of the problems below are about binary trees (rather than binary search trees), but you are expected to be
able to solve them.
The description will often say, input is “a tree” – which really means the input is a pointer to the root of the
tree. Also, unless specified, we will mean binary trees, i.e. trees in which each vertex has at most two children.
Most problems will be solved using an appropriate recursive function: to solve the problem on the tree recurse
on one or both the subtrees. If you indeed use such recursive functions in your solution, you should state precisely
what your function does, when it is called on any node, not just the root. You should write down preconditions for
the function to work correctly, if any such preconditions are necessary.
In the exercises below, n will denote the number of nodes in the tree, and h the height of the tree.
Some of the exercises ask you to add members to the struct Node. If need be you may yourself add more
members if your computation needs it.

1. Write a function which given a binary tree, returns n the number of nodes. Your function should run in time
O(n).
2. Write a function which given a binary tree, returns the number L of leaves in it. Remember that a node is a
leaf if it has no children. Your function should run in time O(L + n).
3. Write a function which given a tree returns a vector v such that v[i] gives the number of nodes at a distance
i from the root. You might find it useful to have two function, one function which the rest of the world calls,
but which in turn calls a recursive function that does all the work. Be careful in specifying which parameters
are passed by reference, if any. Remember that if you pass a vector by value, it is copied and that takes time
proportional to its length. Your function should run in time O(n).
4. An important generic procedure that can be performed on a tree is to traverse it. Here is the skeleton of a
possible traverse function.

void traverse(Node* r){


if(r == NULL) return;
traverse(r->left);
traverse(r->right);
}
Of course, to do something useful the function will have to have additional code to print messages or modify
data members of *r. It is also possible to have the function return a value.
By calling traverse(r) where r is the root of a tree, we will cause traverse(u) to be called for vertices u
in the tree. The execution of traverse(r) is said to cause a traversal of the tree. Tree traversal can be used
to perform important functions related to a tree.
One such function is numbering the nodes of an n node tree from 0 to n − 1 to satisfy certain requirements. In
preorder (postorder) numbering the requirement is that a node receive a smaller (larger) than its descendants.
In inorder numbering, a node must receive a number larger than the numbers of the nodes in its left subtree,
but smaller than the numbers of the nodes in its right subtree.
Suppose struct Node has additional integer members preorder, inorder, postorder. Write a traversal
function that take the tree as argument and sets the values of the members above to reflect the numberings
defined above.
5. Suppose we are given the preorder, inorder, and postorder numbers for each node u. Show that in O(1) time
given (pointers to) two nodes u, v we can determine whether u is an ancestor of v.

6. Write a program which draws the tree such that the x coordinate of each node is proportional to the inorder
number, and y coordinate proportional to the depth. Your function should run in time O(n).
7. Let Rn (i) denote the number obtained by reversing the binary representation of the n bit number i. For
example, R4 (12) is 3, because the binary representations of 12, 3 are 1100, 0011, and thus reverses of each
other. Suppose we have an initially empty search tree into which in step i we insert Rn (i) for i = 1 to
i = 2n − 1. What is the height of this tree?
8. Suppose n distinct numbers are inserted into a binary search tree in a randomly chosen order. What is the
probability that the tree will have height n? Relate this to the operations performed in Quicksort, and assume
that the time for performing Quicksort is O(n log n) if at each stage the pivot is randomly chosen from current
set of keys to be sorted.

9. Suppose we add an integer member size to struct Node. We would like to have the invariant that v.size
is equal to the number of nodes in the subtree beneath node v (including v itself). Show how the insert
function update v.size of relevant nodes suitably so that the invariant is maintained. The insert function
should run in time O(h) as before, where h is the height of the tree.
10. Using the member size as described above, show how to find the rth smallest key in the set, given r and the
root of the tree. Your function should run in time O(h).
11. Give an algorithm that prints the elements in a binary search tree falling in a given interval [a, b]. Your
algorithm should run in time O(h + m), where h is the height of the tree and m is the number of elements in
the set that are in the given interval.

12. Suppose you are given a possibly unbalanced binary search tree on n nodes. Show that in time O(n) you can
construct a binary search tree with the same set of keys having height log n.

3 Binary search tree application


1. In the bin packing problem, you are given m bins each with capacity C, and n items with weights w1 , . . . , wn .
You are to decide if it is possible to distribute the items among the bins such that no bin is assigned items of
total weight more than C.
No fast algorithms are known for the bin packing problem. Here is a commonly used heuristic. Consider
items in decreasing order of weights. Place each item in that bin whose residual capacity is smallest but yet
larger than the weight of the item. The residual capacity of a bin is C − W where W the the total weight of
the items currently in the bin. Write a program which decides if it is possible to place all items into bins using
the above heuristic. Your program must run in time O(n log n). You can assume without loss of generality
that m ≤ n.

You might also like