{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lec11_12_Sorting_Heap_Linear

# Lec11_12_Sorting_Heap_Linear - Heap Sort The Heap Data...

This preview shows pages 1–13. Sign up to view the full content.

Heap Sort The Heap Data Structure

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Basic Heap Property Basic Heap Property Complete binary tree All internal nodes have two children All leaves have depth d Essentially complete binary tree It is a complete binary tree down to a depth of d-1 The nodes with depth d are as far to the left as possible
Basic Heap Property Basic Heap Property Depth of a tree The depth of a node in a tree is the number of edges in the unique path from the root to that node The depth of a tree is the maximum depth of all nodes in the tree A leaf in a tree is any node with no children Internal node is any node that has at least one child

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Basic Heap Property Basic Heap Property Heap A heap is an essentially complete binary tree such that The values stored at the nodes come from an ordered set The value stored at each node is greater than or equal to the values stored at its children.
Procedure Procedure Siftdown Siftdown Siftdown Procedure to sift the key at the root down the heap until the heap property is satisfied. Comparing the parent key with the larger key at the children. If smaller exchange. Repeat until no smaller key than the larger of its children.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Heap data structure Heap data structure 3 2 8 7 18 14 9 29 6 1 2 3 4 5 6 7 8 9 10 1 1 3 2 8 7 29 6 1 2 3 4 5 6 7 8 9 18 14 9 10 last array root = 1 Parent ( i ) = i /2 Left ( i )=2 i Right ( i )=2 i +1
Heap Sort Heap Sort Makeheap From nodes in the depth level of d-1 to level 0 Each depth level, from right node to left node to make heap Removekeys Move root node value into the array S Move leaf node (far right) into the root and make a heap, delete that leaf node Repeat the process until all the node values are moved into the array S, which is formed the sorted sequence.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Overview Overview Usage of a heap HeapSort Priority queue Definitions: height, depth, full binary tree, complete binary tree Definition of a heap Methods of a heap
Priority Queue A priority queue is a collection of zero or more items, associated with each item is a priority Operations: insert a new item delete item with the highest priority find item with the highest priority

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Worst case time complexity for heaps Build heap with n items - Θ (n) insert () into a heap with n items - Θ (lg n) deleteMin () from a heap with n items- Θ (lg n) findMin () - Θ (1)
Depth of tree nodes Depth of tree nodes Depth of a node is: If node is the root --- 0 Otherwise - ( depth of its parent + 1 ) Depth of a tree is maximum depth of its leaves. 0 1 1 2 2 A tree of depth 2

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Height of tree nodes Height of tree nodes Height of a node is: If node is a leaf --- 0 Otherwise - ( maximum height of its children +1) Height of a tree is the height of the root. 0 0 0
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 65

Lec11_12_Sorting_Heap_Linear - Heap Sort The Heap Data...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online