TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT 20066.1 TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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 AVL Trees (10.2) CSE 2011 Winter April 2015.
Chapter 4: Trees Part II - AVL Tree
TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT TTIT33 – Algorithms and optimization Lecture 5 Algorithms ADT Map,
CSE332: Data Abstractions Lecture 9: B Trees Dan Grossman Spring 2010.
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.
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.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
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.
Course Review COMP171 Spring Hashing / Slide 2 Elementary Data Structures * Linked lists n Types: singular, doubly, circular n Operations: insert,
1 COSC 2P03 Lecture #5 – Trees Part III, Heaps. 2 Today Take up the quiz Assignment Questions Red-Black Trees Binary Heaps Heap sort D-Heaps, Leftist.
1 TCSS 342, Winter 2005 Lecture Notes Priority Queues and Heaps Weiss Ch. 21, pp
Binary Search Trees1 ADT for Map: Map stores elements (entries) so that they can be located quickly using keys. Each element (entry) is a key-value pair.
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 7 Jan Maluszynski - HT TDDB56 – DALGOPT-D Algorithms and optimization Lecture 7 Splay Trees. Priority.
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.
Priority Queues1 Part-D1 Priority Queues. Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is.
Advanced Trees Part III Briana B. Morrison Adapted from Alan Eugenio & William J. Collins.
Heaps and Priority Queues Priority Queue ADT (§ 2.4.1) A priority queue stores a collection of items An item is a pair (key, element) Main.
CSE 373 Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)
Splay Trees and B-Trees
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
CSC 213 – Large Scale Programming. Today’s Goals  Review a new search tree algorithm is needed  What real-world problems occur with old tree?  Why.
CSCE 3110 Data Structures & Algorithm Analysis
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
CS 3610 Midterm Review.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
AVL Trees Amanuel Lemma CS252 Algoithms Dec. 14, 2000.
Starting at Binary 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.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
CS 253: Algorithms Chapter 13 Balanced Binary Search Trees (Balanced BST) AVL Trees.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
Lecture 11COMPSCI.220.FS.T Balancing an AVLTree Two mirror-symmetric pairs of cases to rebalance the tree if after the insertion of a new key to.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
© 2004 Goodrich, Tamassia Trees
3.1. Binary Search Trees   . Ordered Dictionaries Keys are assumed to come from a total order. Old operations: insert, delete, find, …
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
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.
Internal and External Sorting External Searching
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.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
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)
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.
COMP261 Lecture 23 B Trees.
Heaps (8.3) CSE 2011 Winter May 2018.
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,
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Chapter 2: Basic Data Structures
Part-D1 Priority Queues
Tree Representation Heap.
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
1 Lecture 13 CS2013.
Binary Search Trees < > = Dictionaries
Heaps 9/29/2019 5:43 PM Heaps Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search Trees, Heaps

TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT Content: Balanced Search Trees –AVL Trees [GT 10.2, LD 7.1] –Multi-way search trees [ GT 10.4, LD 7.2, CL ] –Information on B-trees [GT 14.3, LD 7.2, CL ] Piority Queues – Heaps [GT 8.1,8.3, LD 9.1, CL 6.1-5]

TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT AVL Trees A BST LeftHeight(v)-RightHeight(v)

TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT Worst AVL Tree A worst AVL tree is a maximally unbalanced AVL tree… …or a tree of a given height with a minimum number of internal nodes (Example: insert 44, 17, 78, 32, 50, 88, 62)

TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT AVL-Tree Insertion – Rebalance…

TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT AVL-Tree Insertion… (Goodrich/Tamassia) How to insert an item with key 50? Perform a LookUp(50) While searching for 50, keep track of last passed node with balance  0  critical node If not found, insert at the leaf where search ended Recompute balance on the way back Check critical node! If balance  [-1..1], rebalance! 30 b: 0 20 b: 1 60 b: 0 10 b: 0 40 b: 0 70 b: 0 50 b: 0 40 b: b: 1 30 b: -1

TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT AVL-Tree Insertion & Re-balance… Now try an item with key Perform a LookUp(15) While searching for 15, keep track of last passed node with balance  0  critical node If not found, insert at the leaf where search ended Recompute balance on the way back Check critical node! If balance  [-1..1], rebalance! 30 b: b: 1 60 b: 1 10 b: 0 40 b: b: 0 50 b: 0 10 b: b: 1 30 b: ? 15 b: 0 20 b: 2

TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT Time to re-balance… 60 b: 1 70 b: 0 50 b: 0 k 15 b: 0 b lm 10 b: -1 a 20 b: 2 c n 30 b: ? 40 b: -1 Label the critical node and its 2 descendants on the path to ”15” as a, b, c, such that a < b < c, in an in-order traversal Re-structure the nodes a, b and c so that b has a and c as children!

TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT Time to re-balance… 60 b: 1 70 b: 0 50 b: 0 15 b: 0 b k 10 b: -1 a 20 b: 2 c n 15 b: 0 b k 10 b: -1 a 20 b: 2 c n 15 b: 0 b k 10 b: -1 a 20 b: 2 c n 15 b: 0 b k 10 b: 0 a 20 b: 0 c n 30 b: ? 40 b: -1 lm Label the critical node and its 2 descendants on the path to ”15” as a, b, c, such that a < b < c, in an in-order traversal Re-structure the nodes a, b and c so that b has a and c as children! Update balance! 30 b: -1

TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT Removal of a node... Same thing, but in reverse! 1.Perform a LookUp and Remove as in an ordinary binary tree 2.Update the balance on the way back to the root 3.If too unbalanced: Re-structure!...but: Label the critical node, the child on the deepest side, and its descendants on the deepest side as a, b, c, such that a < b < c, in an in-order traversal Re-structure as previous Go to #2 and continue update and check towards the root (we may have to re-balance more than once!)

TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT Tri-node restructuring = rotations.... Other authors use left and right rotations: Single left rotation: left part of the sub- tree (a and j) is lowered We have ”rotated (up) b over a”... c b a j k l m c b a jkl m

TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT Double rotations... Two rotations are needed when the nodes to re-balance are placed in a zig-zag pattern... 1.Rotate up b over a 2.Rotate up b over c c b a j k l m c b a j k l m c b a j k lm Note: Labeling of a, b and c same as before!

TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT New approach: relax some condition... AVL-Tree: binary tree, accepts a small unbalance Recall: Full binary tree: nonempty; degree is either 0 or 2 for each node Perfect binary tree: full, all leaves have the same depth Can we build and maintain a perfect tree (if we skip ”binary”) ??  we would always know the worst search time exactly!

TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT (2,3) trees [or (2,4) trees, or ( a,b) -trees…] Previously: A single ”pivot element” If larger we search to the right If smaller we search to the left Now: Multiple pivot elements No. of children = no. of pivot elements

TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT (2,3) trees, (2,4) trees, or ( a,b) trees... Each node is either a leaf, or has c children where a  c  b LookUp works aproximately as before Insert must check that a node does not overflow (then we split the node) Delete must check that a node does not become empty (then we transfer or merge nodes)

TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT Insert in (a,b)-tree where a=2 and b=3 As long as there is room in the child we find, add element to that child... If full, split and push the selected pivot element upwards......may happen repeatedly Insert(10) Insert(15) Insert(18) Insert(17)

TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT Delete in a (2,3)-tree Three cases: 1.No constraints are violated by removal 2.A leaf is removed (becomes empty)  transfer some other key to that leaf, …ok if we have a sibling with 2+ elements Delete(25) 30 ?35 40 ? Transfer of 30 and 35

TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT Delete in a (2,3)-tree 2.A leaf is removed (becomes empty)  transfer some other key to that leaf, or  merge (fuse) it with a neighbor Delete(18) ? No pivot ?? Too many children!

TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT Delete in a (2,3)-tree 3.An internal node becomes empty Root: replace with in-order pred. or succ.  repair inconsistencies with suitable merge and transfer operations… Delete(20) 10 5? ? Replace......merge leafs 17 ? Internal underflow......merge nodes

TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT Properties of a (2,3) tree Always a perfect tree A minimal tree of height h will have nodes (it’s a full binary tree with full set of nodes at all levels) A maximal (2,3)-tree will have a branching factor of 3, thus …2 keys in every node  Thus the height No of keys in a tree

TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT B-Tree Used to keep an index over external data (e.g. content of a disc) It’s only an (a,b)-tree where a =  b/2  We may now choose b so that b-1 references to children (other disc blocks) fit into a single disc block By defining a =  b/2  we will always fill up a disc block when two blocks are merged!

TTIT33 Algorithms and Optimization – DALG Lecture 6 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. The key denotes priority –Items retrieved by priority: i.e. by the minimal key.

TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT Implementing Priority Queues ”last” leaf This is a complete binary tree! This is also called a HEAP

TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT Updates on a Heap Structure DeleteMin = deletion of the root –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”

TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT HEAP Properties: size(), minElemenent(): O (1) minElement(), minKey(): O (1) insertItem(), removeMin(): O (log n ) >> HeapSort O (n log n ) sorting algorithm Recall vector representation of BST! A complete binary tree... Compact vector representation Bubble-up and bubble-down have fast implementations

TTIT33 Algorithms and Optimization – DALG Lecture 6 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...

TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT Heap Sort Kinds of heaps : minKey in the root => (Min)Heap maxKey in the root => MaxHeap Heap Sort: input D[0..n] Heapify D => Max Heap worst case O(n log n) For i from 0 to n-1 DeleteMax => put in D[n-i] (worst case O(log n) for restoring heap D[0..i]