Presentation is loading. Please wait.

Presentation is loading. Please wait.

I hope that you get along well with your teammate. If not… You can always change. A quick answer about teams.

Similar presentations


Presentation on theme: "I hope that you get along well with your teammate. If not… You can always change. A quick answer about teams."— Presentation transcript:

1 I hope that you get along well with your teammate. If not… You can always change. A quick answer about teams

2 Implementation of Trees using arrays

3 Implementation of trees using arrays The structure of a tree in an array Consequences for heap

4 How to see a binary tree as an array Implementation of trees using arrays 7 58 39 14 7583ØØ9147583ØØ914 012345678012345678 0 12 3 45 6 78 For a node i, its children are 2i + 1 and 2i + 2.

5 How to see a binary tree as an array Implementation of trees using arrays 6 38 57 638Ø57Ø638Ø57Ø 01234560123456 For a node i, its children are 2i + 1 and 2i + 2.

6 Algorithms for a tree in an array Implementation of trees using arrays Let say that a binary search tree is implemented as an array. How would you write the algorithm contains that returns true when a key is in the tree? 1 2 3 4 1 Ø 2 Ø Ø Ø 3 Ø Ø Ø Ø Ø Ø Ø 4 0 2*0 + 2 = 2 2*2 + 2 = 6 2*6 + 2 = 14 The thing NOT to do would be to just scan the array. Indeed, the array can be very sparse. Worst case: 14 comparisons versus 4.

7 Algorithms for a tree in an array Implementation of trees using arrays Let say that a binary search tree is implemented as an array. How would you write the algorithm contains that returns true when a key is in the tree? « My momma always said that an array is like a box of chocolates: you don’t have to eat all of them, just pick the ones you like. »

8 Algorithms for a tree in an array Implementation of trees using arrays Let say that a binary search tree is implemented as an array. How would you write the algorithm contains that returns true when a key is in the tree? For a node i, its children are 2i + 1 and 2i + 2. public boolean contains(int key){ int i = 0; while(i<T.length){ if(T[i]==key) return true; if(key>T[i]) i = 2*i+2; // go right else i = 2*i + 1; // or go left } return false;// did not find it } Time Complexity? Exactly the same that doing it in a pointer-based structure: O(h). ( O(log n) if it’s balanced, O(n) in the worst case… )

9 Algorithms for a tree in an array Implementation of trees using arrays What about adding in a binary search tree implemented as an array? {1, « I »} {5, « ♥ »} {7, « Mr. French’s »} {8, « labs »} Remember that you always have a key and data bound to it. An array is made of couples {key, data}. public class Couple{ private int key; private Object data; // constructor // setData, getData, getKey When the algorithm does not care about the data, we can neglect it. Otherwise, let’s have a class Couple and our array will be Couple[] T = new Couple[10]; and our array will be:

10 Algorithms for a tree in an array Implementation of trees using arrays What about adding in a binary search tree implemented as an array? public boolean add(int key, Object o){ if(shortOnSpace()) resize(); int i = 0; while(true){ if(T[i]==null){ T[i] = new Couple(key,o); break; } if(key>T[i].getKey) i = 2*i+2; // go right else i = 2*i + 1; // or go left } if(i>sizeUsed) sizeUsed = i; } We have to resize: when and how much is up to you. Just don’t crash. Assuming we have enough space, we go in the tree as long as we have not inserted the node. Once we have inserted the node, we stop. Since the array may be full of holes, maybe that we did not use more space than before. Otherwise, we have to know up to where we are using the space.

11 Algorithms for a tree in an array Implementation of trees using arrays Something you’ll like to do for practice: remove an element in a binary search tree implemented with an array.

12 To make a heap happy, give it an array Implementation of trees using arrays 10 8 9 4 35 6 8956432 2 If you use an array, inserting in a heap is like in a LinkedList: insert at the current position, and then go to the next one.

13 To make a heap happy, give it an array Implementation of trees using arrays If you use an array, inserting in a heap is like in a LinkedList: insert at the current position, and then go to the next one. public boolean add(int key, Object o){ if(index==T.length) resize(); T[index] = new Couple(key, o); index++; } This takes care of the shape: the new node is inserted at the last position. But what about the values? int move = index; while(move!=0 && T[move]>T[Math.floor((move-1)/2)]){ swap(move); // swap move and its father move = Math.floor((move-1)/2); } } After you insert it at the end, swap with the father until the ordering on the keys has been ensured.

14 To make a heap happy, give it an array Implementation of trees using arrays 30 20 24 18 19 7 30 20 24 18 19 7 35 0 1 2 3 4 5 35 6 I insert a task with key 35. The father is in floor[(6-1)/2] = 2. We have 35 > 24. 30 20 35 18 19 7 24 The father is in floor[(2-1)/2] = 0. We have 35 > 30. 30 20 35 18 19 7 24 35 20 30 18 19 7 24 We are at 0: stop (can’t go any higher)

15 Heapify Implementation of trees using arrays We have a root of which the left son is a heap, the right son is a heap, but the overall tree is not a heap. Heapify restores the heap property.

16 Heapify Implementation of trees using arrays We have a root of which the left son is a heap, the right son is a heap, but the overall tree is not a heap. Heapify restores the heap property. 13 3018 362014 12 First, look for the maximum between the root and its two children, and swap. And repeat until there aren’t any more swaps to perform.

17 Heapify Implementation of trees using arrays Heapify(T): Heapify(T, 0, |T|) Heapify(T, i, n): 2i+1 < n ^ max(T[i], T[2i+1], T[2i+2]) = T[i] → T 2i+1 < n ^ max(T[i], T[2i+1], T[2i+2]) = T[2i+1] → Heapify(T[i↔2i+1], 2i+1, n) 2i+1 < n ^ max(T[i], T[2i+1], T[2i+2]) = T[2i+2] → Heapify(T[i↔2i+2], 2i+2, n) 2i+1 = n ^ T[i] ≥ T[2i+1] → T 2i+1 = n ^ T[i] < T[2i+1] → T[i↔2i+1] 2i+1 > n → T We don’t have to swap: stop and return the array. Find with which son to swap, and continue. At the end: compare with a leaf. If we are a leaf, stop.

18 removeMax Implementation of trees using arrays 30 2214 121716 The max is always the root. Once you remove it, there will be 1 less element, and we are in an array. You can’t do a shift, because: the structure would be totally messed up it’s in O(n) but we are supposed to optimize removeMax Solution: Replace the root by the last element and call heapify to fix the heap.

19 removeMax Implementation of trees using arrays Object removeMax(){ Object max = T[0]; T[0] = T[index]; index--; Heapify(T); return max; } Worst case time complexity? Everything is in O(1) and we’re calling Heapify, so it’s the same than the worst case time complexity of Heapify. Heapify works only along one branch, and the tree being balanced a branch is of size O(log n), hence O(log n) time. Similarly, the insertion is in O(log n). GetMax is just looking at the root: O(1).

20 Happiness Enhancement Program © Implementation of trees using arrays Search for a key in a heap. Write it first if it’s implemented as pointers (Java and specification) and then in an array. Write heapify if the implementation is based on pointers (Java and specification).


Download ppt "I hope that you get along well with your teammate. If not… You can always change. A quick answer about teams."

Similar presentations


Ads by Google