TDDB56 DALGOPT-D - TDDB57 DALG-C Lecture 7 Jan Maluszynski - HT 20067.1 TDDB56 – DALGOPT-D Algorithms and optimization Lecture 7 Splay Trees. Priority.

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Binary Search Trees
Advertisements

COL 106 Shweta Agrawal and Amit Kumar
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT TTIT33 – Algorithms and optimization Lecture 5 Algorithms ADT Map,
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 5 Jan Maluszynski - HT TDDB56 – TDDB57 Lecture 5 Trees Terminology, Implementation Binary Search Trees.
CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties and maintenance.
CSC 213 Lecture 7: Binary, AVL, and Splay Trees. Binary Search Trees (§ 9.1) Binary search tree (BST) is a binary tree storing key- value pairs (entries):
TDDB57 DALG-C, DALG Exam Requirements Jan Maluszynski - HT 2006DALG-C.1 TDDB57 – DALG-C Examination Requirements.
TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 6 Jan Maluszynski - HT TDDB56 TDDB57 Lecture 6 Search Trees: AVL-Trees, Multi-way Search Trees, B-Trees.
© 2004 Goodrich, Tamassia Heaps © 2004 Goodrich, Tamassia Heaps2 Priority Queue Sorting (§ 8.1.4) We can use a priority queue to sort a set.
Binary Search Trees   . 2 Ordered Dictionaries Keys are assumed to come from a total order. New operations: closestKeyBefore(k) closestElemBefore(k)
TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
© 2004 Goodrich, Tamassia (2,4) Trees
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
COMP20010: Algorithms and Imperative Programming Lecture 4 Ordered Dictionaries and Binary Search Trees AVL Trees.
CS 221 Analysis of Algorithms Ordered Dictionaries and Search Trees.
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
© 2004 Goodrich, Tamassia Binary Search Trees1 CSC 212 Lecture 18: Binary and AVL Trees.
Chapter 10: Search Trees Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
1 Searching the dictionary ADT binary search binary search trees.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
CS 253: Algorithms Chapter 13 Balanced Binary Search Trees (Balanced BST) AVL Trees.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
Oct 26, 2001CSE 373, Autumn A Forest of Trees Binary search trees: simple. –good on average: O(log n) –bad in the worst case: O(n) AVL trees: more.
CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search.
3.1. Binary Search Trees   . Ordered Dictionaries Keys are assumed to come from a total order. Old operations: insert, delete, find, …
Review for Exam 2 Topics covered (since exam 1): –Splay Tree –K-D Trees –RB Tree –Priority Queue and Binary Heap –B-Tree For each of these data structures.
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.
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.
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.
Binary Search Trees1 Chapter 3, Sections 1 and 2: Binary Search Trees AVL Trees   
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
1 Binary Search Trees   . 2 Ordered Dictionaries Keys are assumed to come from a total order. New operations: closestKeyBefore(k) closestElemBefore(k)
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
© 2004 Goodrich, Tamassia BINARY SEARCH TREES Binary Search Trees   
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Search Trees.
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
Binary Search Trees.
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Binary Search Trees.
Chapter 10.1 Binary Search Trees
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Bohyung Han CSE, POSTECH
Heaps 9/13/2018 3:17 PM Heaps Heaps.
CS200: Algorithms Analysis
Chapter 2: Basic Data Structures
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Binary Search Trees < > =
Tree Representation Heap.
© 2013 Goodrich, Tamassia, Goldwasser
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Ch. 8 Priority Queues And Heaps
Dictionaries < > = /9/2018 3:06 AM Dictionaries
Dictionaries < > = /17/2019 4:20 PM Dictionaries
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Binary Search Trees < > = Dictionaries
Splay Trees Binary search trees.
Heaps 9/29/2019 5:43 PM Heaps Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

TDDB56 DALGOPT-D - TDDB57 DALG-C Lecture 7 Jan Maluszynski - HT TDDB56 – DALGOPT-D Algorithms and optimization Lecture 7 Splay Trees. Priority Queues, Heap

TDDB56 DALGOPT-D - TDDB57 DALG-C Lecture 7 Jan Maluszynski - HT Content: Wrap-up of Trees: –Splay trees [Goodrich & Tamassia 10.3] Priority Queue ADT : [Goodrich & Tamassia ] –extension of ADT Dictionary –used e.g. in shortest paths algorithm for graphs –implementations by balanced trees, skip list, heaps

TDDB56 DALGOPT-D - TDDB57 DALG-C Lecture 7 Jan Maluszynski - HT Splay Tree – basic idea... Recall the basic BST: Simple insert and reasonable delete when balanced, but... The ”balance” is determined by order of inserts and deletes... Combind with the ”keep recent objects first” heuristics for lists? Often-used elements should be near the root!

TDDB56 DALGOPT-D - TDDB57 DALG-C Lecture 7 Jan Maluszynski - HT Splay Tree properties A new operation Splay( r, T ) modifies tree T : –Element r becomes the new root of T if it exists –Otherwise, the new root will be the inorder predecessor (or successor) of the ”non-existent” r All operations are implemented using Splay: –LookUp( k, T ): Splay( k, T ); if root( T ) = k, return –Insert( k, i, T ): Splay( k, T ); if root( T ) = k, update, else insert new root in T. –Delete(k, T ):....special, but it also involves ”Splay”

TDDB56 DALGOPT-D - TDDB57 DALG-C Lecture 7 Jan Maluszynski - HT The Splay( k, T ) operation: 1.Perform a normal search for k, remember all nodes we pass... 2.Label the last node we inspect P If k is in T, then k is in node P Otherwise P is an empty (external) child 3.Return back to root, at each node do a rotation to move P up the tree... (3 cases)

TDDB56 DALGOPT-D - TDDB57 DALG-C Lecture 7 Jan Maluszynski - HT The Splay( k, T ) operation... Case 1: Parent( P ) is the root: rotate around P Case 2: P and Parent( P ) are both left children (or both right children): perform two rotations to shift up P : P QP Q P Q R P Q R P Q R

TDDB56 DALGOPT-D - TDDB57 DALG-C Lecture 7 Jan Maluszynski - HT The Splay( k, T ) operation Case 3: One of P and Parent( P ) is a left child and the other is a right child: Perform two rotations in different directions: Note: These rotations may increase the height of the tree... P Q cb d R a... P Q cbd R a

TDDB56 DALGOPT-D - TDDB57 DALG-C Lecture 7 Jan Maluszynski - HT Delete ( k, T ) We need help procedure Concat ( T 1, T 2 ) where T1 and T2 are trees such that: Concat ( T 1, T 2 ): Splay(+ , T 1 )...will re-structure T 1 to have the largest element as root, and the root has no right child. setRightChild (T 1, T 2 )...reinstall T 2 as right child. Delete( k, T): Splay ( k, T)...if root does not contain k, ok Concat(leftChild( k ), rightChild( k )) +  is a dummy key larger than all existing, valid keys...

TDDB56 DALGOPT-D - TDDB57 DALG-C Lecture 7 Jan Maluszynski - HT Splay Trees - performance Each operation may face a totally unbalanced tree – thus not guaranteed to operate in O(log n) in worst case Amortized time is logarithmic: –Any sequence of length m of these operations, starting with an empty tree, will take a total amount of O(m log n) time... [Goodrich & Tamassia p. 442] –Thus, the amortized cost/time is O(log n) although individual op’s may be significantly worse...

TDDB56 DALGOPT-D - TDDB57 DALG-C Lecture 7 Jan Maluszynski - HT Priority Queues Commonly encountered situation: Waiting list (tasks, passengers, vehicles entering a ferry, phone calls) If a resource is freed, choose an element from the waiting list The choice is based on some partial ordering: –some tasks are more essential to achieve the goal, –some passengers should be served before the others (children, sick people) –fire dept. and first aid vehicles have priority How to organize prioritized service?

TDDB56 DALGOPT-D - TDDB57 DALG-C Lecture 7 Jan Maluszynski - HT ADT Priority Queue –Linearly ordered set K of keys –We store pairs (as in Dictionary), multiple pairs with same key are allowed. –A frequent operation is retrieving pairs with minimal key.

TDDB56 DALGOPT-D - TDDB57 DALG-C Lecture 7 Jan Maluszynski - HT Implementing Priority Queues ”last” leaf This is a complete binary tree! This is also called a HEAP

TDDB56 DALGOPT-D - TDDB57 DALG-C Lecture 7 Jan Maluszynski - HT Updates on a Heap Structure DeleteMin = deletion of the rot –Replace root by last leaf –Restore partial order by swaping nodes downwards ”down-heap bubbling” Insert –Insert new node after last leaf –Restore partial ordering by ”up-heap bubbling”

TDDB56 DALGOPT-D - TDDB57 DALG-C Lecture 7 Jan Maluszynski - HT HEAP Properties: size(), FindMin(): O (1) insertItem(), DeleteMin(): O (log n ) Recall vector representation of BST! A complete binary tree... Compact vector representation Bubble-up and bubble-down have fast implementations

TDDB56 DALGOPT-D - TDDB57 DALG-C Lecture 7 Jan Maluszynski - HT HEAP example – bubble-up after insert(4) We store pairs of in the tree. Recall tree representation in a table: getLeftChild(i) = 2*i+1 getRightChild(i) = 2*i+2...where i is the table index, not the key! In [G/T] the root starts at index 1 and representation is: -getLeft(i) = 2*i -getRight(i) = 2*i+1...in [L/D] the root starts at 0...

TDDB56 DALGOPT-D - TDDB57 DALG-C Lecture 7 Jan Maluszynski - HT Variants of Heaps: Kind of partial ordering: minKey in the root maxKey in the root Kind of vector representation: Forward level-order numbering (starting with 0 or 1) Backward level-order numbering (starting with 0 or 1)