Binary Search Trees vs. Binary Heaps. Binary Search Tree Condition Given a node i –i.value is the stored object –i.left and i.right point to other nodes.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

Advanced Data Structures
CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
Binary Heap viewed as an array viewed as a binary tree Left(i) = 2*i Right(i) = 2*i + 1 Parent(i) = i.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
1 Advanced Data Structures. 2 Topics Data structures (old) stack, list, array, BST (new) Trees, heaps, union-find, hash tables, spatial, string Algorithm.
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.
1 TCSS 342, Winter 2005 Lecture Notes Priority Queues and Heaps Weiss Ch. 21, pp
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
1 CS211, Lecture 20 Priority queues and Heaps Readings: Weiss, sec. 6.9, secs When they've got two queues going, there's never any queue!
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Dr. Andrew Wallace PhD BEng(hons) EurIng
Week 10: Heap and Priority queue. Any feature here?
CSE 373 Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
Binary Search Trees Section Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Foundation of Computing Systems Lecture 6 Trees: Part III.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
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.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
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.
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
Heaps. What is a heap? Like a binary search tree, but less structure within each level. Guarantees: – Parent better than child – That’s it! What does.
CS 361 – Chapter 3 Sorted dictionary ADT Implementation –Sorted array –Binary search tree.
Data Structure & Algorithm II.  In a multiuser computer system, multiple users submit jobs to run on a single processor.  We assume that the time required.
COSC2007 Data Structures II Chapter 12 Tables & Priority Queues III.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
What is a heap? Always keep the thing we are most interested in close to the top (and fast to access). Like a binary search tree, but less structured.
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 1 Adam Smith L ECTURES Priority Queues and Binary Heaps Algorithms.
H EAPS. T WO KINDS OF HEAPS : MAX AND MIN Max: Every child is smaller than its parent Meaning the max is the root of the tree 10 / \ 9 7 / \ 6 8 / \ 2.
Heaps and basic data structures David Kauchak cs161 Summer 2009.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R3. Priority Queues.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
1 Heap Sort. A Heap is a Binary Tree Height of tree = longest path from root to leaf =  (lgn) A heap is a binary tree satisfying the heap condition:
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.
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
CSE 373: Data Structures and Algorithms Lecture 11: Priority Queues (Heaps) 1.
2 Binary Heaps What if we’re mostly concerned with finding the most relevant data?  A binary heap is a binary tree (2 or fewer subtrees for each node)
"Teachers open the door, but you must enter by yourself. "
CSE373: Data Structures & Algorithms Priority Queues
Partially Ordered Data ,Heap,Binary Heap
Priority Queue A Priority Queue Set S is made up of n elements: x0 x1 x2 x3 … xn-1 Functions: createEmptySet() returns a newly created empty priority.
Heaps (8.3) CSE 2011 Winter May 2018.
BST Trees
Binary Search Tree Chapter 10.
Source: Muangsin / Weiss
Heaps.
Priority Queues Linked-list Insert Æ Æ head head
Binary Search Trees.
CMSC 341: Data Structures Priority Queues – Binary Heaps
Chapter 8 – Binary Search Tree
Heapsort Heap & Priority Queue.
What is a heap? Always keep the thing we are most interested in close to the top (and fast to access). Like a binary search tree, but less structured.
Priority Queues.
Ch 6: Heapsort Ming-Te Chi
CSE 373: Data Structures and Algorithms
Priority Queues.
ITEC 2620M Introduction to Data Structures
"Teachers open the door, but you must enter by yourself. "
Priority Queues CSE 373 Data Structures.
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Heaps By JJ Shepherd.
Priority Queue and Heap
A Heap Is Efficiently Represented As An Array
Presentation transcript:

Binary Search Trees vs. Binary Heaps

Binary Search Tree Condition Given a node i –i.value is the stored object –i.left and i.right point to other nodes All of i’s left children and grand-children are less than i.value All of i’s right children and grand-children are greater than i.value

Binary Search Trees Binary search trees can be easily implemented using arrays X

Binary Search Trees Root is at index 1 (i = 1) Left(i) { return i*2; }Right(i) { return i*2 + 1; } X

Binary Search Trees bool Find_rec(int x, int i) { if (a[i] == -1 ) return false; else if (x == a[i]) return true; else if (x < a[i]) Find_rec(x, i*2); else Find_rec(x, i*2+1); } bool Find(int x) { return Find_rec(x, 1); } …

Binary Search Tree Features Find  O(log N) on average Insert in proper order  O(log N) on average O(log N) to find the correct location + O(1) to perform insertion Return Min  O(log N) on average –Keep moving left until you see a -1 Return Max  O(log N) on average –Keep moving left until you see a -1 Print in-order  O(N) all the time –See in-order traversal in book

Priority Queue (Min) Three functions –push(x) – pushes the value x into the queue –min() – return the minimum value in the queue –delete() – removes the minimum value in the queue Tons of applications: –OS process queues, Transaction Processing, Packet routing in advanced networks, used in various other algoriothms

Priority Queue (Min) Consider using an un-order Array –push(x) – O(1) just add it to the end of the array. –min() – O(N) sequential search for min value –delete() – O(N) might have to shift entire array

Priority Queue (Min) Consider using an Ordered Array –push(x) – O(N) to find correct location and shift array appropriately –min() – O(1) return the first value –delete() – O(N) might have to shift entire array Recall using an un-order Array –push(x) – O(1) just add it to the end of the array. –min() – O(N) sequential search for min value –delete() – O(N) might have to shift entire array

Priority Queue (Min) Why are simple array implementation bad? O(N) is not a problem, right? Consider this application: –A private network router has 10 million packets coming in every minute (mostly junk, spam, etc.) and I only want to let through the top 1 million (#1 is top priority – min)

Priority Queue (Min) N = 10 million in-coming packets M = 1 million out-going packets (top priority – priority #1) Consider using an Ordered Array –push(x) – O(N) Must do this N times  N*N –min() – O(1) –delete() – O(1) Must do this M times  M Recall using an un-order Array –push(x) – O(1) Must do this N time (not a problem) –min() – O(N) Must do this M times  N*M –delete() – O(N) Must do this M times  N*M

N*M N all the packets 10 million M – 1 million top priority packets Must be processed in one minute. Assume your computer can do 10 billion operation per second  600 billion operation in one minute. Unfortunately, N*M is 10 trillion operations.

Priority Queue (Min) Consider using an BST –push(x) – O(log N) add to the correct position –Log(n) * n, n = 10,000,000 –min() – O(log N) return the left-most node –delete() – O(log N) Recall using an un-order Array –push(x) – O(1) just add it to the end of the array. –min() – O(N) sequential search for min value –delete() – O(N) might have to shift entire array

Priority Queue (Min) Is this possible? –push(x) – O(1) to find correct location an shift array appropriately –min() – O(1) return the first value –delete() – O(log N)

Binary Heaps (Min Heap) Given a node i –i.value is the stored object –i.left and i.right point to other nodes All of i’s left children and grand-children are greater than i.value All of i’s right children and grand-children are greater than i.value

Binary Heaps (Min Heap) Binary heaps can be easily implemented using arrays

Binary Heap Features Find  O(N) requires sequential search Insert in proper order  O(1) on average Amazing Heap Property Return Min  O(log N) on average –O(1) to return min –O(log N) to restore the heap property Print in-order  O(N log N) requires progressively deleting the min.

Priority Queue (Min) N = 10 million in-coming packets M = 1 million out-going packets (top priority – priority #1) Consider using an Heap –push(x) – O(1) Must do this N times (not a problem) –min() – O(1) –delete() – O(log N) Must do this M times  log(N)*M Recall using an un-order Array –push(x) – O(1) Must do this N time (not a problem) –min() – O(N) Must do this M times  N*M –delete() – O(N) Must do this M times  N*M

log(N)*M N all the packets 10 million M – 1 million top priority packets Must be processed in one minute. Assume your computer can do 1 billion operation per second  60 billion operation in one minute. What is log(N)*M?

Summary: Priority Queue Implementations BST Implementation Push: O(log N) Find Min: O(log N) Remove Min: O(log N) Pushing N = 10,000, million operations Removing M minimums M = 1,000, million operations Binary Heap Implement. Push: O(1) Find Min: O(1) Remove Min: O(log N) Pushing N = 10,000, million operations Removing M minimums M = 1,000, million operations