original list {67, 33,49, 21, 25, 94} pass { } {67 94}

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
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.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
Balanced Trees. Binary Search tree with a balance condition Why? For every node in the tree, the height of its left and right subtrees must differ by.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Heapsort Based off slides by: David Matuszek
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
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.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
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 21 Binary Heap.
Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.
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 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) 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.
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
Chapter 13 Priority Queues. 2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-in-first-out The “smallest”
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.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
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.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Linda Shapiro Spring 2016.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
CSE373: Data Structures & Algorithms Priority Queues
Partially Ordered Data ,Heap,Binary Heap
AA Trees.
Heaps (8.3) CSE 2011 Winter May 2018.
Priority Queues and Heaps
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
Topics covered (since exam 1):
October 30th – Priority QUeues
MA/CSSE 473 Day 21 AVL Tree Maximum height 2-3 Trees
Week 11 - Friday CS221.
Hashing Exercises.
Source: Muangsin / Weiss
Heap Sort Example Qamar Abbas.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
(edited by Nadia Al-Ghreimil)
ADT Heap data structure
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
7/23/2009 Many thanks to David Sun for some of the included slides!
CMSC 341: Data Structures Priority Queues – Binary Heaps
Wednesday, April 18, 2018 Announcements… For Today…
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Topics covered (since exam 1):
B-Tree Insertions, Intro to Heaps
ITEC 2620M Introduction to Data Structures
Heaps and the Heapsort Heaps and priority queues
CS Data Structure: Heaps.
Priority Queues (Chapter 6.6):
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
(edited by Nadia Al-Ghreimil)
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Sorting Dr. Yingwu Zhu.
Priority Queues.
Priority Queues (Chapter 6):
Topics covered (since exam 1):
CO4301 – Advanced Games Development Week 4 Binary Search Trees
Hash Maps: The point of a hash map is to FIND DATA QUICKLY.
Chapter 12 Heap ADT © 2011 Pearson Addison-Wesley. All rights reserved.
การวิเคราะห์และออกแบบขั้นตอนวิธี
Data Structures and Algorithm Analysis Priority Queues (Heaps)
Heaps & Multi-way Search Trees
Presentation transcript:

original list {67, 33,49, 21, 25, 94} pass 1 {25 33 21} 49 {67 94} pass 2 {25 21} 33 {} {} 67 {94} pass 3 {21} 25 {} ~~ ~~ ~~ ~~ ~~ sorted list {21, 25, 33, 49, 67 94}

Assignment 8 questions???

Zybook assignment before class on Thursday chapter 24 (section 1)

Remaining topics Heap Balanced binary search trees Data structure for implementing a priority queue Basis of heapsort algorithm Balanced binary search trees

a binary tree A binary tree is either Empty Consists of an item called the root and 2 binary trees called the left subtree and the right subtree

Some special binary trees A binary search tree is a binary tree that has the bst property Each item holds a value Its left child is either empty or holds a smaller value Its right child is either empty or holds a larger value A heap Is A complete binary tree that has the heap order property 45 36 56 42 25 52 60 30 50

A complete binary tree All levels except the bottom most have all possible nodes Nodes on the bottom most level fill the left most positions

Heap order property Max heap Min heap Element stored in each node is >= element stored in each of its children Min heap Element stored in each node is <= element stored in each of its children 34 25 32 15 12 27 5 13 10

A complete binary tree can be efficiently stored in an array size 12 0 1 2 3 4 5 6 7 8 9 10 11 12 -----

Allows computation of location of an element’s parent, left child and right child left child of i is at 2 * i + 1 right child of i is at 2 * i + 2 parent of i is at (i – 1) / 2 How can we tell if there is no left or right child? How can we tell if there is no parent? 0 1 2 3 4 5 6 7 8 9 10 11 12 ----- size 12

Adding an item to a heap Put the new item at the end Increment size Do comparisons with parents to let the item percolate up to where it belongs

size 12 0 1 2 3 4 5 6 7 8 9 10 11 12 ----- Add 7

size 13 7 7 0 1 2 3 4 5 6 7 8 9 10 11 12 ----- Add 7

7 8 size 13 50 7 8 50 0 1 2 3 4 5 6 7 8 9 10 11 12 ----- Add 7

What will a min heap built from the following items look like size 0 1 2 3 4 5 6 7

2 / \ 5 3 / \ / 8 7 4 size 6 2 5 3 8 7 4 0 1 2 3 4 5 6 7

Big O of adding an item to a heap?

Big O depends on height of the binary tree A complete binary tree holding n items has a height of log2 n

Removing smallest/largest item from a heap Item being removed is in position 0 Move last item in the heap to position 0 (temporarily) decrement size Do comparisons with children to let the item trickle down to where it belongs Maximum number of comparisons is the height of the tree Height of a complete tree is (log2 n)

size 12 0 1 2 3 4 5 6 7 8 9 10 11 12 ----- Remove smallest

55 size 12 11 55 0 1 2 3 4 5 6 7 8 9 10 11 12 ----- step 1

8 16 55 size 11 8 16 55 0 1 2 3 4 5 6 7 8 9 10 11 12 ----- Trickle down

Do 3 removes 2 / \ 5 3 / \ / 8 7 4 size 6 2 5 3 8 7 4 0 1 2 3 4 5 6 7

After first remove 3 / \ 5 4 / \ 8 7 size 5 3 5 4 8 7 4 / \ 5 4 / \ 8 7 size 5 3 5 4 8 7 4 0 1 2 3 4 5 6 7

After second remove 4 / \ 5 7 / 8 size 4 4 5 7 8 7 4 0 1 2 3 4 5 6 7

after 3 removes 5 / \ 8 7 size 3 5 8 7 8 7 4 0 1 2 3 4 5 6 7

Uses of a heap Data structure for implementing a priority queue Enqueue and deque both O(log2 N) Front O(1) How are equal priorities treated? Basis of the heapsort algorithm

Given a randomly ordered array how can we use a heap to put the array in sorted order?

Heapsort strategy Step 1: form_heap (heapify) Step 2: sort_heap Reorganize the array so it forms a heap Step 2: sort_heap Put the elements in the heap into sorted order

heapify Heap size is 0 Heap size is 1 Heap size is 2 Heap size is 3 0 1 2 3 4 5 6 7 2 7 4 8 5 3 4 7 2 8 5 3 Heap size is 0 Heap size is 3 Heap size is 1 Heap size is 2 0 1 2 3 4 5 6 7 2 5 3 8 7 4 Heap size is 6

Sort_heap Heap size is 6 Heap size is 5 Heap size is 4 Heap size is 3 0 1 2 3 4 5 6 7 2 5 3 8 7 4 Heap size is 6 0 1 2 3 4 5 6 7 3 5 4 8 7 2 Heap size is 5 0 1 2 3 4 5 6 7 4 5 7 8 3 2 Heap size is 4 0 1 2 3 4 5 6 7 5 8 7 4 3 2 Heap size is 3 Sorted array 0 1 2 3 4 5 6 7 8 7 5 4 3 2

Heapsort performance Step 1: heapify Step 2: sort_heap Reorganize the array so it forms a heap Adding an element to a heap is O(log2 n) Adding n elements is O(N log2 n) Step 2: sort_heap Put the elements in the heap into sorted order Removing an element from a heap is O(log2 n) Removing n elements is O(N log2 n) Heapsort performance is O(N log2 n)

heapsort Is it Stable? Is it Adaptive? How much extra space does it require?

heapsort Is it Stable? Is it Adaptive? no Is it Adaptive? How much extra space does it require? O(1)

balanced binary search trees How can the worst case performance of a BST be prevented? When adding and removing elements, make sure the tree remains “balanced” Requires more work when adding and removing but ensures that find and retrieve will be O(log2 n) Avl trees and red-black trees are balanced bst

Avl trees Will never have a height greater than 1.44 log2 n An avl tree is a binary tree with the bst property Additions and removals are done in such a way that the tree is kept “balanced” All other operations the same as for a bst A tree is balanced if the height of its two subtrees differ by at most 1

Is this tree balanced?

Each node of an avl tree has a balance factor data member The balance factor of a node is the height of its left subtree minus the height of its right subtree 0 – tree with that node as its root is balanced 1 – tree with that node as its root is left heavy -1 – tree with that node as its root is right heavy +2 or -2 tree with that node as its root is out of balance Needs to be rebalanced using rotations

What is the balance factor of each node?

What is the balance factor of each node? -1 -1 -1 -1

Adding an element Search exactly the same as for bst After adding a node more work to be done Balance factors of nodes on the search path need to be updated What is the balance factor of the node that was added? Back up the search path At each node update balance factor by Adding 1 if element added to its left subtree Subtracting 1 if element added to its right subtree If updating changes a balance factor to 0 quit – no rebalancing needed If updating changes a balance factor to 2 or -2 tree is no longer balanced Get it back into balance by performing a rotation

What happens after adding this element? -1 -1 -1 -1

What happens after adding this element? -1 -1 -1+1=0 -1 0+1=1

What happens after adding this element? -1 -1 -1 -1

What happens after adding this element? -1 -1+-1=-2 -1 -1 0+-1=-1

Need to rotate subtree that is out of balance -1 40 -1 -1 50 60

After rotation -1 50 -1 -1 40 60

rotations Rotations can be Left rotation Right rotation Right-left rotation Left-right rotation Rotations require extra time but big 0 of insert is still log2 n involves only the subtree whose root is the node that went out of balance Removing an element also requires rotations to keep tree balanced

Final exam Topics since exam2 (about 2/3) Hash tables Stl Graphs Sorting Balance BST Topics covered on exam 1 and exam 2 (about 1/3) Selecting the best data structure for a specific application

example A campus club is planning a fund raiser. Each member is asked to give her best idea. The club secretary enters each member’s name and idea into a dictionary. Later a report listing all the ideas will be printed. What dictionary implementation would you choose and why would you choose it?

Questions to ask What operations are needed? Add, remove, retrieve/find, traverse Are operations based on position? Or on value? How frequently are each of operations performed? Is it necessary to traverse the items in sorted order? If so, how often? Is there a maximum size for the collection? Will the size grow and shrink over time?