Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
Heapsort.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
Heaps and heapsort COMP171 Fall Sorting III / Slide 2 Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. Sizes: Job.
Priority queues CS310 – Data Structures Professor Roch Weiss, Chapter 6.9, 21 All figures marked with a chapter and section number are copyrighted © 2006.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
BST Data Structure A BST node contains: A BST contains
Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
© 2006 Pearson Addison-Wesley. All rights reserved12 A-1 Chapter 12 Heaps.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Chapter 4: Trees Binary Search Trees
Fundamentals of Python: From First Programs Through Data Structures
PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
Binary Trees Chapter 6.
1 Priority Queues (Heaps)  Sections 6.1 to The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant.
Heapsort Based off slides by: David Matuszek
Priority Queues and Heaps Bryce Boe 2013/11/20 CS24, Fall 2013.
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Chapter 21 Binary Heap.
Computer Sciences Department1. Sorting algorithm 3 Chapter 6 3Computer Sciences Department Sorting algorithm 1  insertion sort Sorting algorithm 2.
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
CSC 213 – Large Scale Programming Lecture 15: Heap-based Priority Queue.
Sorting Dr. Yingwu Zhu. Heaps A heap is a binary tree with properties: 1. It is complete Each level of tree completely filled Except possibly bottom level.
Chapter 20 Binary Search Trees
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
The ADT Table The ADT table, or dictionary Uses a search key to identify its items Its items are records that contain several pieces of data 2 Figure.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly.
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010.
Heapsort. What is a “heap”? Definitions of heap: 1.A large area of memory from which the programmer can allocate blocks as needed, and deallocate them.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Heaps & Priority Queues
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
Sorting Dr. Yingwu Zhu. Heaps A heap is a binary tree with properties: 1. It is complete Each level of tree completely filled Except possibly bottom level.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
CS 367 Introduction to Data Structures Lecture 8.
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
1 Binary Search Trees What are the disadvantages of using a linked list? What are the disadvantages of using an array- based list? Binary search trees.
Heaps, Heap Sort, and Priority Queues. Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two children * A node that.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
AA Trees.
Heaps (8.3) CSE 2011 Winter May 2018.
Source: Muangsin / Weiss
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Tree Representation Heap.
Computer Science 2 Heaps.
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Chapter 12 Heap ADT © 2011 Pearson Addison-Wesley. All rights reserved.
Priority Queues Binary Heaps
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Priority Queues (Heaps)
Presentation transcript:

Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

Part I: Binary Search Tree Part II: Priority Queue & Heaps Part III: Heapsort Chapter Contents 2

Part I: Binary Search Tree 3

Binary Search Trees (BSTs)  A search tree is a tree whose elements are organized to facilitate finding a particular element when needed  A binary search tree is a binary tree that, for each node n  the left subtree of n contains elements less than the element stored in n  the right subtree of n contains elements greater than or equal to the element stored in n 4

Binary Search Trees (BSTs) 5

 To determine if a particular value exists in a tree  start at the root  compare target to element at current node  move left from current node if target is less than element in the current node  move right from current node if target is greater than element in the current node  We eventually find the target or encounter the end of a path (target is not found) 6

Binary Search Trees (BSTs)  The particular shape of a binary search tree depends on the order in which the elements are added to the tree  The shape may also be dependant on any additional processing performed on the tree to reshape it  Binary search trees can hold any type of data, so long as we have a way to determine relative ordering 7

Adding an Element to a BST  Process of adding an element is similar to finding an element  New elements are added as leaf nodes  Start at the root, follow path dictated by existing elements until you find no child in the desired direction  Add the new element 8

Adding an Element to a BST Next to add:

Removing an Element from a BST  Removing a target in a BST is not as simple as that for linear data structures  After removing the element, the resulting tree must still be valid  Three distinct situations must be considered when removing an element  The node to remove is a leaf  The node to remove has one child  The node to remove has two children 10

Removing an Element from a BST 11

Removing an Element from a BST  Dealing with the situations  Node is a leaf: it can simply be deleted  Node has one child: the deleted node is replaced by the child  Node has two children: an appropriate node is found lower in the tree and used to replace the node: Either selecting the largest element in the left subtree. Or selecting the smallest element in the right subtree. 12

After the Root Node is Removed 13

Complexity 14 Logarithmic, depends on the shape of the tree O(log 2 N) In the worst case – O(N) comparisons

Advantages of BST 15  Simple  Efficient  Dynamic  One of the most fundamental algorithms in CS  The method of choice in manyapplications

Disadvantages of BST 16  The shape of the tree depends on the order of insertions, and it can be degenerated. (Becomes a Linked List)  When inserting or searching for an element, the key of each visited node has to be compared with the key of the element to be inserted/found.

Improvements of BST 17 Keeping the tree balanced:  AVL trees (Adelson - Velskii and Landis)  Balance condition: left and right subtrees of each node can differ by at most one level.  It can be proved that if this condition is observed the depth of the tree is O(log 2 N).

Part II: Priority Queue & Heaps 18

Priority Queue ADT 19  The data in a priority queue is (conceptually) a queue of elements  The “queue” can be thought of as sorted with the largest in front, and the smallest at the end  Its physical form, however, may differ from this conceptual view considerably

Priority Queue ADT Operations 20  enqueue, an operation to add an element to the queue  dequeue, an operation to take the largest element from the queue  an operation to determine whether or not the queue is empty  an operation to empty out the queue

Priority Queue Implementation  Priority Queue could be implemented in different ways.  One way is to use vectors.  Another way is to use Binary Heap.  What’s a Heap? 21

Heaps 22  A heap is a complete binary tree in which the value of each node is greater than or equal to the values of its children (if any)  Technically, this is called a maxheap  In a minheap, the value of each node is less than or equal to the values of its children

Heaps (cont.)  The element stored in each node of a heap can be an object  When we talk about the value of a node, we are really talking about some data member of the object that we want to prioritize  For example, in employee objects, we may want to prioritize age or salary 23

Implementations 24 Binary heap Better than BST because it does not support links. Insert: O(log 2 N) Find minimum O(log 2 N) Deleting the minimal element takes a constant time, however after that the heap structure has to be adjusted, and this requires O(log 2 N) time.

Binary Heap 25 Heap-Structure Property: Complete Binary Tree - Each node has two children, except for the last two levels. The nodes at the last level do not have children. New nodes are inserted at the last level from left to right. Heap-Order Property: Each node has a higher priority than its children

Binary Heap Next node to be inserted - right child of the yellow node

Basic Operations 27  Build the heap  Insert a node – Percolate Up  Delete a node – Percolate Down

Build Heap - O(N) 28 Given an array of elements to be inserted in the heap, treat the array as a heap with order property violated, and then do operations to fix the order property.

Example

Example (cont) After processing height 1

Example (cont) After processing height 2

Example (cont) After processing height 3

Percolate Up – Insert a Node 33 A hole is created at the bottom of the tree, in the next available position

Percolate Up 34 Insert

35 Insert Percolate Up

Complexity of insertion: O(log 2 N) Percolate Up

Percolate Down – Delete a Node

Percolate Down – the wrong way

Percolate Down – the wrong way The empty hole violates the heap-structure property

Percolate Down 40 Last element The hole at the root We try to insert 20 in the hole by percolating the hole down

Percolate Down

Percolate Down

Percolate Down Complexity of deletion: O(logN)

Example of a Heap root Only the data member is shown that we want to prioritize

Example of a Heap (cont.) root Where is the greatest value in a heap always found?

Example of a Heap (cont.) root Where is the greatest value in a heap always found?

Dequeue 47  Dequeuing the object with the greatest value appears to be a  ( 1 ) operation  However, after removing the object, we must turn the resultant structure into a heap again, for the next dequeue  Fortunately, it only takes O( log 2 n ) time to turn the structure back into a heap again (which is why dequeue in a heap is a O( log 2 n ) operation

48  The process of swapping downwards to form a new heap is called heapifying  When, we heapify, it is important that the rest of the structure is a heap, except for the root node that we are starting off with; otherwise, a new heap won’t be formed  A loop is used for heapifying; the number of times through the loop is always lg n or less, which gives the O( lg n ) complexity  Each time we swap downwards, the number of nodes we can travel to is reduced by approximately half Heapify

Part III: Heapsort 49

Heapsort  Consider a priority queue with n items implemented by means of a heap The space used is O(n) Methods enqueue and removeMax take O(log n) time  Using a heap-based priority queue, we can sort a sequence of n elements in O(n log n) time  The resulting algorithm is called heap-sort 50

Heapsort Build a binary heap of N elements –O(N) time Then perform N deleteMax operations –log(N) time per deleteMax Total complexity O(N log N) 51

Questions 52