Heaps and Heapsort (PDF)
Document Details
Uploaded by ProfuseLight3397
MMSU
Tags
Related
- Lecture 1 to 4 - Data Structures and Algorithm.pdf
- Data Structures & Algorithm Past Paper (July 2024) PDF
- Data Structures and Algorithm Analysis in C++ 4th Edition PDF
- MAULANA ABUL KALAM AZAD UNIVERSITY OF TECHNOLOGY, WEST BENGAL BCA 3rd Semester Data Structures and Algorithms Exam 2023-2024 PDF
- CSC 1061 Week 04 Data Structures Static Array PDF
- Data Structures and Algorithms MCQ's PDF
Summary
These are lecture notes focusing on Heaps and Heapsort, an algorithm for sorting. The document includes examples of sorting algorithms and how they work with binary trees. Useful for computer science and data structures students.
Full Transcript
Heaps and Sift-Up Sift-Up - bottom-up, right-to-left process of converting a complete binary tree into a heap Heaps and Sift-Up Example 1 2 3 4 5 6 7 8 9 10 a a l g o r i t h m s l...
Heaps and Sift-Up Sift-Up - bottom-up, right-to-left process of converting a complete binary tree into a heap Heaps and Sift-Up Example 1 2 3 4 5 6 7 8 9 10 a a l g o r i t h m s l g o r i t h m s Heaps and Sift-Up Example 1 2 3 4 5 6 7 8 9 10 a a l g o r i t h m s l g o r i t h m s Heaps and Sift-Up Example 1 2 3 4 5 6 7 8 9 10 a a l g o s i t h m r l g o s i t h m r Heaps and Sift-Up Example 1 2 3 4 5 6 7 8 9 10 a a l g o s i t h m r l g o s i t h m r Heaps and Sift-Up Example 1 2 3 4 5 6 7 8 9 10 a a l g o s i t h m r l g OK o s i t h m r Heaps and Sift-Up Example 1 2 3 4 5 6 7 8 9 10 a a l g o s i t h m r l g o s i t h m r Heaps and Sift-Up Example 1 2 3 4 5 6 7 8 9 10 a a l g o s i t h m r l g o s i t h m r Heaps and Sift-Up Example 1 2 3 4 5 6 7 8 9 10 a a l t o s i g h m r l t o s i g h m r Heaps and Sift-Up Example 1 2 3 4 5 6 7 8 9 10 a a l t o s i g h m r l t o s i g h m r Heaps and Sift-Up Example 1 2 3 4 5 6 7 8 9 10 a a l t o s i g h m r l t o s i g h m r Heaps and Sift-Up Example 1 2 3 4 5 6 7 8 9 10 a a s t o l i g h m r s t o l i g h m r Heaps and Sift-Up Example 1 2 3 4 5 6 7 8 9 10 a a s t o l i g h m r s t o l i g h m r Heaps and Sift-Up Example 1 2 3 4 5 6 7 8 9 10 a a s t o r i g h m l s t o r i g h m l Heaps and Sift-Up Example 1 2 3 4 5 6 7 8 9 10 a a s t o r i g h m l s t o r i g h m l Heaps and Sift-Up Example 1 2 3 4 5 6 7 8 9 10 a a s t o r i g h m l s t o r i g h m l Heaps and Sift-Up Example 1 2 3 4 5 6 7 8 9 10 t t s a o r i g h m l s a o r i g h m l Heaps and Sift-Up Example 1 2 3 4 5 6 7 8 9 10 t t s a o r i g h m l s a o r i g h m l Heaps and Sift-Up Example 1 2 3 4 5 6 7 8 9 10 t t s i o r a g h m l s i o r a g h m l Seatwork tree1 tree2 tree3 Heapsort Heapsort algorithm put forth in 1964 by R. W. Floyd and J. W. J. Williams 1. Assign the keys to be sorted to the nodes of a complete binary tree. 2. Convert this binary tree into a heap by applying sift-up to its nodes in reverse level order. 3. Repeatedly do the following until the heap is empty: a) Remove the key at the root of the heap (the smallest in the heap) and place it in the output. b) Detach from the heap the rightmost leaf node at the bottommost level, extract its key, and store this key at the root of the heap. c) Apply sift-up to the root to convert the binary tree into a heap once again. Heapsort Example 1 2 3 4 5 6 7 8 9 10 t t s i o r a g h m l s i o r a g h m l Heapsort Example 1 2 3 4 5 6 7 8 9 10 t t s i o r a g h m l s i o r a g h m l Heapsort Example 1 2 3 4 5 6 7 8 9 10 l l s i o r a g h m t s i o r a g h m t Heapsort Example 1 2 3 4 5 6 7 8 9 10 l l s i o r a g h m t heap sorted s i o r a g h m t Heapsort Example 1 2 3 4 5 6 7 8 9 10 l l s i o r a g h m t heap sorted s i o r a g h m t Seatwork Heapsort. Arrange the following elements in ascending & descending order. Show the state of the tree at every step. CGAHFEDJBI 1 6 3 4 9 7 5 8 2 12 10 14 Quiz Heaps & Heapsort. Arrange the following elements in ascending & descending order. Draw the tree, the heap order tree and the elements.Show the state of the tree at every step of the sort. CLARENCE Treap The treap was first described by Cecilia R. Aragon and Raimund Seidel in 1989; its name is a portmanteau of tree and heap. It is a Cartesian tree in which each key is given a (randomly chosen) numeric priority. As with any binary search tree, the inorder traversal order of the nodes is the same as the sorted order of the keys. The structure of the tree is determined by the requirement that it be heap-ordered: that is, the priority number for any non-leaf node must be greater than or equal to the priority of its children. Thus, as with Cartesian trees more generally, the root node is the maximum-priority node, and its left and right subtrees are formed in the same manner from the subsequences of the sorted order to the left and right of that node. A treap with alphabetic key and numeric max heap order Treap To insert a new key x into the treap, generate a random priority y for x. Binary search for x in the tree, and create a new node at the leaf position where the binary search determines a node for x should exist. Then, as long as x is not the root of the tree and has a larger priority number than its parent z, perform a tree rotation that reverses the parent-child relation between x and z. Treap Seatwork Create the treap for the given keys below. Show the state of the treap for every insertion & rotation. A(50), C(30), B(57), F(88), G(47), D(95), H(90) Seatwork Create the treap for the given keys below. Show the state of the treap for every insertion & rotation. A(50) , B(17), C(30), D (52), E (93), F(17), G (69), H(22)