CS1102 AY0809 Sem 2

This is an unofficial website for CS1102 created by Melvin. This semester, I'm teaching the following tutorial slots for CS1102Y:

  • Thursday 2-3
  • Thursday 3-4

Feedback

After each tutorial, please take 5mins to fill in this feedback form.

Tutorial 8 - Trees

2b

Instead of creating new arrays to represent the inorder and preorder traversal of the subtrees, it is possible to save space by passing a pair of indices instead. Observe that the inorder/preorder traversal of the left and right subtrees are just contiguous segments of the inorder/preorder traversal of the whole tree. Therefore, we can represent the inorder/preorder traversal of the subtrees using a start and end index.

3a

A variant is to determine whether a given binary tree is complete in O(n) time. What is the simplest algorithm for this problem? Hint: think about the fact that a complete tree can be stored in an array.

· 2009/03/19 17:12 · Melvin Zhang

Tutorial 9 - Heaps

Q1c

Notice that in the first method (insert one-by-one), in the worst case the nodes that are initially inserted at the last level might have to bubble up all the way to the top. There are `O(n/2)` nodes which may need to bubble up `O(\lg n)` levels, hence the complexity is at least `O(n \lg n)`.

On the other hand for the heapify method, the nodes bubble down. The majority of the nodes are near the bottom and the number of level they can bubble down is small, while nodes near the top have to move further there is much less of them. All in all, the maximum number of comparisons is `O(n)`

Q2

Cecilia R. Aragon and Raimund Seidel developed the Treap data structure in 1989. It is known that for a random insert order, the resulting binary tree is expected to have height `O(\lg n)`. By assigning each new node with a random priority value and rotating the nodes to satisfy the heap property on the priority value it can be shown that the resulting tree is similar to one constructed by inserting the nodes in random order.

· 2009/04/02 16:58 · Melvin Zhang
 
cs1102/start.txt · Last modified: 2009/04/02 17:00 by melvin
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki