Presentation is loading. Please wait.

Presentation is loading. Please wait.

CST 230 Razdan et alhttp://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Tree Traversals Pre-order traversal –process root.

Similar presentations


Presentation on theme: "CST 230 Razdan et alhttp://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Tree Traversals Pre-order traversal –process root."— Presentation transcript:

1 CST 230 Razdan et alhttp://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Tree Traversals Pre-order traversal –process root –pre-order traverse left subtree –pre-order traverse right subtree

2 CST 230 Razdan et alhttp://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 2 Traversals cont... In-order Traversal –in-order traverse left subtree –process root –in-order traverse right subtree

3 CST 230 Razdan et alhttp://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 3 Traversals cont... Post-order Traversal –post-order traverse left subtree –post-order traverse right subtree –process root

4 Chapter 10 Heaps Anshuman Razdan Div of Computing Studies razdan@asu.edu http://dcst2.east.asu.edu/~razdan/cst230/

5 CST 230 Razdan et al5 Heap Storage Rules The elements of a heap must come from a totally ordered set. A heap is a binary tree in which the following 2 rules apply: –The element contained by each node is >= the elements of that nodes’ children –The tree is a complete binary tree so that every level is full except the deepest. At the deepest level, all the nodes are as far left as possible

6 CST 230 Razdan et al6 Heap or not? 91 7746 69 3 11 52 7746 69 3 11 91 7746 69 3 11

7 CST 230 Razdan et al7 Heap Implementation A heap could be implemented just using a Binary Tree. Since a heap is a COMPLETE binary tree, the array implementation is more efficient. Example: i’s children are: i’s parent is:

8 CST 230 Razdan et al8 Heap = Priority Queue Recall that a priority queue is a queue in which items can be inserted/removed in priority order. A heap is an efficient implementation of a priority queue (highest priority item is at the root of the tree)

9 CST 230 Razdan et al9 Add method Suppose we want to add to the following 45 3523 27 21 22 19 5 4 4535232721224195

10 CST 230 Razdan et al10 Pseudocode for Add 1.Place the new element at the 1 st available location 2.while( the element has priority > parent ) swap element with parent 4535232721224195 add 30 add 40 add 50

11 CST 230 Razdan et al11 Remove Method (Priority Q) In a priority queue, we want to remove the item with highest priority  root of heap Similar to add except we need to “heapify” downward instead of upward.

12 CST 230 Razdan et al12 Pseudocode for Remove 1.Copy root to return variable 2.Move last element of array to index 0 (root) 3.while( element priority < 1 of its children ) swap element with highest-priority child 4.return value from step 1 454223273522419521

13 CST 230 Razdan et al13 Complexity Analysis Heapifiy O(log n) Add to heap O(log n), avg is constant remove root from heap (priority q) remove any element from heap find specific element

14 CST 230 Razdan et al14 Heapsort arrange array elements to be sorted so that the elements are a heap swap first element with last element of array (now largest value is at end of array) pretend the array is 1 element smaller and “heapify” downward from root. swap root to end and repeat previous

15 CST 230 Razdan et al15 Pseudocode 1.Convert array of n elements into a heap 2.unsorted = n 3.while( unsorted > 1 ) unsorted-- swap data[0] with data[unsorted] reheapify downward

16 CST 230 Razdan et al16 convert array to heap heapsize = 1 for( i = 1; i < n; i++ ) –add data[i] to heap in data[0..heapsize-1] –heapsize++ 213522272345421945

17 CST 230 Razdan et al17 Time complexity of Heapsort Time complexity to makeHeap Time complexity for heapify downward Number times we must heapify downward Complexity of Heapsort =


Download ppt "CST 230 Razdan et alhttp://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Tree Traversals Pre-order traversal –process root."

Similar presentations


Ads by Google