Maintain binary search tree property nodes to the left are less than the current node, nodes to the right are greater

Similar documents
CMSC 341 Priority Queues & Heaps. Based on slides from previous iterations of this course

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

CS61B Lecture #23. Today: Priority queues (Data Structures 6.4, 6.5) Range queries ( 6.2) Java utilities: SortedSet, Map, etc.

CS61B Lecture #23. Today: Priority queues (Data Structures 6.4, 6.5) Range queries ( 6.2) Java utilities: SortedSet, Map, etc.

Priority Queues. Lecture15: Heaps. Priority Queue ADT. Sequence based Priority Queue

Data Structures and Algorithms

CS S-08 Priority Queues Heaps 1

CMSC 341 Lecture 14: Priority Queues, Heaps

Heaps 2. Recall Priority Queue ADT. Heaps 3/19/14

Data Structures and Algorithms

Priority Queues and Binary Heaps

CS 361 Data Structures & Algs Lecture 9. Prof. Tom Hayes University of New Mexico

Heaps. Heaps. A heap is a complete binary tree.

CSE 241 Class 17. Jeremy Buhler. October 28, Ordered collections supported both, plus total ordering operations (pred and succ)

CS165: Priority Queues, Heaps

Programming Abstractions

CMSC 341 Leftist Heaps

SFU CMPT Lecture: Week 9

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Chapter 6 Heapsort 1

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap

Priority Queues Heaps Heapsort

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

Data Structure Lecture#12: Binary Trees 3 (Chapter 5) U Kang Seoul National University

CMSC 341. Binary Heaps. Priority Queues

403: Algorithms and Data Structures. Heaps. Fall 2016 UAlbany Computer Science. Some slides borrowed by David Luebke

binary tree empty root subtrees Node Children Edge Parent Ancestor Descendant Path Depth Height Level Leaf Node Internal Node Subtree

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

CSE2331/5331. Topic 6: Binary Search Tree. Data structure Operations CSE 2331/5331

INF2220: algorithms and data structures Series 4

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

ECE 242 Data Structures and Algorithms. Heaps I. Lecture 22. Prof. Eric Polizzi

Priority Queues. Outline. COMP9024: Data Structures and Algorithms. Priority Queue ADT. Total Order Relations. Entry ADT

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

CS 10: Problem solving via Object Oriented Programming. Priori9zing 2

Chapter 12 Advanced Data Structures

Cosc 241 Programming and Problem Solving Lecture 21 (15/5/2017) Heaps

Topic: Heaps and priority queues

CSE 373: Data Structures and Algorithms

Adding a Node to (Min) Heap. Lecture16: Heap Sort. Priority Queue Sort. Delete a Node from (Min) Heap. Step 1: Add node at the end

Data Structures Lecture 7

Before class. BSTs, Heaps, and PQs 2/26/13 1. Open TreeNodeExample.java Change main to:

Tables and Priority Queues

Dictionaries. Priority Queues

Stores a collection of elements each with an associated key value

CS 380 ALGORITHM DESIGN AND ANALYSIS

Programming II (CS300)

Heaps Goodrich, Tamassia. Heaps 1

Lecture 13: AVL Trees and Binary Heaps

Collection of priority-job pairs; priorities are comparable.

Describe how to implement deque ADT using two stacks as the only instance variables. What are the running times of the methods

CSCI-1200 Data Structures Fall 2018 Lecture 23 Priority Queues II

Priority Queues and Heaps. Heaps of fun, for everyone!

COMP 103 RECAP-TODAY. Priority Queues and Heaps. Queues and Priority Queues 3 Queues: Oldest out first

Data Structures and Algorithms for Engineers

Properties of a heap (represented by an array A)

Fibonacci Heaps Priority Queues. John Ross Wallrabenstein

Heaps. 2/13/2006 Heaps 1

Binary Trees and Binary Search Trees

The heap is essentially an array-based binary tree with either the biggest or smallest element at the root.

Comparisons. Θ(n 2 ) Θ(n) Sorting Revisited. So far we talked about two algorithms to sort an array of numbers. What is the advantage of merge sort?

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

CSCI 136 Data Structures & Advanced Programming. Lecture 22 Fall 2018 Instructor: Bills

CSE 214 Computer Science II Heaps and Priority Queues

Algorithms and Theory of Computation. Lecture 7: Priority Queue

Heaps, stacks, queues

CS350: Data Structures Heaps and Priority Queues

Heaps. Heapsort. [Reading: CLRS 6] Laura Toma, csci2000, Bowdoin College

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

PRIORITY QUEUES AND HEAPS

COS 226 Midterm Exam, Spring 2009

Comparisons. Heaps. Heaps. Heaps. Sorting Revisited. Heaps. So far we talked about two algorithms to sort an array of numbers

Computer Science 210 Data Structures Siena College Fall Topic Notes: Priority Queues and Heaps

Lecture 5: Sorting Part A

Sorting and Searching

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

CH 8. HEAPS AND PRIORITY QUEUES

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

Priority Queues and Heaps

Definition of a Heap. Heaps. Priority Queues. Example. Implementation using a heap. Heap ADT

Lecture 18 Heaps and Priority Queues

CSE 502 Class 16. Jeremy Buhler Steve Cole. March A while back, we introduced the idea of collections to put hash tables in context.

CH. 8 PRIORITY QUEUES AND HEAPS

HEAPS & PRIORITY QUEUES

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

The beautiful binary heap.

CSCI212 Computer Science. Binary Trees/Heaps/Binary Search Trees

Sorting and Searching

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

CS 270 Algorithms. Oliver Kullmann. Binary search. Lists. Background: Pointers. Trees. Implementing rooted trees. Tutorial

9. Heap : Priority Queue

1 Interlude: Is keeping the data sorted worth it? 2 Tree Heap and Priority queue

Section 4 SOLUTION: AVL Trees & B-Trees

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

CompSci 201 Tree Traversals & Heaps

Heaps and Priority Queues

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

Public-Service Announcement

CmpSci 187: Programming with Data Structures Spring 2015

Transcription:

CS61B, Summer 2002 Lecture #8 Barath Raghavan UC Berkeley Topics: Binary Search Trees, Priority queues 1 Binary search trees (BSTs) Represented as ordinary binary trees Maintain binary search tree property nodes to the left are less than the current node, nodes to the right are greater 2 Binary search tree property Given items x and y, x.key < y.key or y.key < x.key Transitive property holds if x.key < y.key and y.key < z.key then x.key < z.key. All nodes must have keys by which to compare For each node, nodes to the left have smaller keys, nodes to the right have greater keys When we say that one item is less than another, typically we mean that the key for that item is less than the other The key for a node can be its value 3 Finding elements in a BST Intuitive search to the left if the item we re looking for is less than the current, search to the right if it s greater Plan When searching for the item with the key k, if the current node T has T.key == k, return it. If k < T.key, search left recursively. If k > T.key, search right recursively. If we reach null through recursion, then the item is not in this BST. Search at most h+1 items, where h is the height of the tree 4 Inserting elements in a BST Intuitive search until the terminal location is found at which the item should be placed, and place it there Plan To insert an item n into the BST, search the BST as if the item were in the tree, checking to see if the child being recursed on is null. If it is, place n there We re not dealing with duplicate items in BSTs if a node with the same key is found, it is replaced 1

5 Removing elements from a BST There are several cases when removing a node n from a BST Leaf nodes simply find the leaf node and remove it by setting the pointer to it from its parent to null Nodes with one child if node n is being removed and has parent node p and child node c, remove node n by making p s child c, taking the place of n Nodes with two children we need the node that has the value closest to node n being removed (why?), so we select either n s left child s rightmost node or n s right child s leftmost node and replace n with it 6 Balance and completeness These operations do not guarantee balance insertion of items with keys 0, 1, 2, 3, 4, 5 in order will create a stringy tree that looks like a linked list We want trees to be bushy, meaning that they should be reasonably well balanced complete, or nearly complete A complete tree is one in which all leaf nodes occur last if read top-down, left to right such a tree is maximally balanced There are various notions of balance and ways of ensuring it (we ll see these in later weeks) 7 Array based tree representation Place the root node at index 0 For each node at index k, its left child is at 2k+1 and its right child is at 2k+2 Works well for complete trees or very well balanced trees (why?) Can be used for things like trees /** Gets the index of the parent of n in h */ private int parent(int n) { return (n-1)/2; /** Gets the index of the left child of n in h */ private int leftchild(int n) { return (n*2)+1; /** Gets the index of the right child of n in h */ private int rightchild(int n) { return (n*2)+2; 2

8 Announcements Lab 5 is about the project Exam tomorrow (Tuesday) in 155 Dwinelle at 6:40 PM Homework 3 is out (but much easier) Reading (for today, but not on the midterm): Data Structures (Into Java) 5.4, 6; Goodrich & Tamassia 7, 9.1 9 Priority queues Queues with ordering based on priority, which is some notion of the value of items (which may or may not be the item itself) Operations on a priority queue add, max or min, extractmax or extractmin. A priority queue usually either supports max and extractmax or min and extractmin, but not both pairs of operations. 10 Simple priority queue implementation Store items in a linked list on add Search for biggest item on extractmax or max Simple, but slow must search through the entire linked list to find the max element each time 11 Binary heap priority queue implementation Use a binary heap a tree-like structure that maintains the heap property In a max-heap (one that represents a maximum priority queue), the heap property requires that each node in the heap has priority greater than its children Node at the root of the tree is guaranteed by the heap property to be the item with the maximum priority A binary heap is like a complete binary tree, so we use an array-based tree representation Two internal operations maintain the heap property reheapifyup and reheapifydown Since the heap should be able to grow, can use a vector to be an internal array-based representation Constructing the binary heap public class BinaryHeap { private Vector h; // the heap public BinaryHeap() { h = new Vector();... 3

12 Binary heap insertion Insert into the first free index of the array; since the tree is complete, the first free index will be after all existing indicies Insertion will likely violate the heap property (because we may insert a value at a location at which it has a higher priority than its parent), so we need to restore the heap property We reheapifyup move the inserted item up the heap by swapping it with its parent until it is no longer greater than its parent. This operation may move the item all the way to the root Inserting into the binary heap public void insert(comparable c) { h.add(c); reheapifyup(h.size()-1); 13 Binary heap extractmax Remove the element at the top (root) of the heap this element is guaranteed to be the maximum Replace it with the last element in the heap this will likely violate the heap property Perform reheapifydown from the top of the heap move the item we just placed at the top downward by swapping it with its larger child until it is greater than both of its children public Comparable extractmax() { Comparable temp = (Comparable) h.get(0); h.setelementat(h.get(h.size()-1), 0); h.remove(h.size()-1); if (h.size() > 0) { reheapifydown(0); return temp; 14 Binary heap reheapify /** Re-heapifies upwards from n */ private void reheapifyup(int n) { if (n <= 0) return; int cur = n; while (cur > 0 && ((Comparable) h.get(cur)).compareto(h.get(parent(cur))) > 0) { swap(cur, parent(cur)); cur = parent(cur); 4

/** Re-heapifies downwards from n */ private void reheapifydown(int n) { int cur = n; int size = h.size()-1; int largechild = 0; for (;;) { /* If we re on leaf nodes, we re done */ if (leftchild(cur) > size) break; /* Figure out which of the children is bigger */ if (rightchild(cur) > size ((Comparable) h.get(leftchild(cur))).compareto(h.get(rightchild(cur))) > 0) largechild = leftchild(cur); else largechild = rightchild(cur); /* Is the largest child larger than cur? */ if (((Comparable) h.get(cur)).compareto(h.get(largechild)) > 0) break; /* It wasn t, so swap them */ swap(cur, largechild); cur = largechild; 15 Question Suppose we have an unsorted array of elements from which we want to build a heap. We could create a new heap and perform insert operations repeatedly for each item in the array, inserting them into the heap Is there a better (faster) way? We ll talk more about this later in the week 5