Вы находитесь на странице: 1из 13

1.

(5 marks) Giving the following AVL tree:

18

13

22

15

20

When we add 9 to this tree, rotation will take place. Draw what the tree looks like after the 1st and the 2nd rotation.

2. (5 marks) Giving the following tree that we want to make into an AVL tree: r 5

11

13 4 8 15

17

Method for rebalancing a tree is given as follows (every code is the same as in the slides):
private AVLNode rebalance(AVLNode r){ if (r == null) return r; int balance = balanceValue(r); if (balance == 2){//left hand side has more nodes than it should have if (balanceValue(r.left) ==-1) r.left = rotateWithRightChild (r.left); r = rotateWithLeftChild(r); } else if (balance == -2){//right hand side has more nodes than it should have if (balanceValue(r.right) ==1) r.right = rotateWithLeftChild (r.right); r = rotateWithRightChild(r); } r.setHeight(); return r;

} public int balanceValue(AvlNode t){ return height(t.left) height(t.right); } private void setHeight(){// this is a method of AVLNode height = 1 + Math.max(height(left), height(right)); } private static int height(AVLNode n){ // this is a method of AVLNode return (n == null ? -1 : n.height); }

It can be seen that rebalance(r) does not work on our tree. In fact, rebalance only works when r is the node that loses AVL property. Show how you can modify rebalance so that it can correctly make any tree (rooted by r) into AVL tree.

3.

(10 marks) for an AVL tree (codes are the same as in the slides) that stores non duplicated integers, write method

public AVLNode removesubtree(AVLNode root, AVLNode sub) The tree under consideration has root as its root. This method removes node sub and all nodes below it from the tree. The remaining tree has to be AVL.

4. (5 marks) Given a hash table for integer data of size 11. Let hash(x) = x%TableSize and hash2(x) = 5-(x%5). The hash table already has 22 inside. Show and explain (step by step) what happens when 5, 27, 33, 34, 2 are inserted into the table in order, using double hashing. The table for each step is drawn for you. Insert 5

22

Insert 27

22

Insert 33 22

Insert 34 22

Insert 2

22 5. (5 marks) do the same as in the previous question, but use quadratic probing with hash(x) = x%TableSize. Insert 5

22

Insert 27 22

Insert 33

22

Insert 34

22

Insert 2

22

6. (10 marks) A heap is used to build a Huffman tree for English alphabets. Each data element stored in the heap is stored as an object of class HuffmanNode.

public class HuffmanNode{ public String element; // an English alphabet. At start, each HuffmanNode in the heap represents an alphabet and //its frequency. But as the Huffman tree is built, a HuffmanNode does not need an alphabet //in its element if it is not a leaf node. public int frequency; //this can be added up during the Huffman tree construction. public HuffmanNode left; public HuffmanNode right; } Let the class Heap have the following methods: public boolean isEmpty(): returns true if the heap is empty, false otherwise. public Object removeMin(): removes a HuffmanNode object with the lowest frequency from the heap. It returns the removed object. y public void add(Object x): adds a HuffmanNode object to the heap. The object is automatically put into its correct position within the heap according to its frequency value. Write the following method of class Heap: y y public HuffmanNode buildHuffmanTree(Heap h) that builds a Huffman tree from any given heap. The method returns the root of the finished Huffman tree as its result.

7. (5 marks) Draw a Huffman tree from the following data: a has frequency = 100 b has frequency = 600 c has frequency = 400 d has frequency = 300 e has frequency = 600

8. (10 marks) class Student is given as follows: public class Student { String name; //student s name int mark; // student s score

public Student(String n, int m){ name = n; mark = m; } .. // assume all get, set methods are available. .. }

A heap of Students has the following usable methods:

y y y

compare(Student first, Student second): return negative number when first has less score than second. Return positive number when first has more score than second (assume that no one has the same score). add(Object item): the code of this method is given below. percolateUp(int startIndex): the code of this method is given below. percolateDown(int startIndex): the code of this method is given below.

Write void changemark(String name, int newmark) This method can be used to change a score of any student. After the score is changed, the student s heap must still be a heap.
1:
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:

// Postcondition: element is added as a member of heap


// worst case time = O(n) // average time is constant public void add(Object element){ if (++size == heap.length){ //if the array is full, resize it. Object[ ] newHeap = new Object [2 * heap.length]; System.arraycopy (heap, 0, newHeap, 0, size); heap = newHeap; } heap [size - 1] = element; percolateUp(size-1); }

16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31:

// Postcondition: move last element up the tree. Worst time is O(log n). // Average time is constant. protected void percolateUp(int startIndex) { int parent; int child = startIndex; Object temp; while (child>0) { parent = (child-1)/2; if(compare(heap[parent],heap[child]) <=0) break; temp = heap[parent]; heap[parent] = heap[child]; heap[child] = temp; child = parent; } }

10

32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48:

// Postcondition: move the top value down the tree. // worst case and average time = O(log n). protected void percolateDown(int startIndex) { int parent = startIndex; int child = 2*parent+1; Object temp; while (child<size) { if(child<size-1&&compare(heap[child],heap[child+1]) >0) child++; if(compare(heap[parent],heap[child]) <=0) break; temp = heap[child]; heap[child] = heap[parent]; heap[parent] = temp; parent = child; child= 2*parent+1; } }

11

9. (5 marks) Write the code of class Heap (codes are in page 10) that combines this with another heap.

BinaryHeap mergeHeap(BinaryHeap thesecondHeap){

} What is the worst case running time of your code? Is sorting faster? Discuss.

10. (10 marks) A min-max heap looks like the following:

Level 0 80 Level 1 8 Level 2

12
10 4 9 5

Even levels are like min heap (small value is more important). Odd levels are like max heap (greater value is more important). A member in an even level must have smaller value than the value in its direct parent. For example, 8 and 5 are less than 80. A member in an odd level must have larger value than the value in its direct parent. For example, 30 and 8 are greater than 4. Write void add(int newnumber) This method adds a new number to the heap. When the adding finishes, the heap must still be a min-max heap.

13

Вам также может понравиться