TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 6 Jan Maluszynski - HT 20066.1 TDDB56 TDDB57 Lecture 6 Search Trees: AVL-Trees, Multi-way Search Trees, B-Trees.

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

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
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Trees Types and Operations
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture27.
CSE332: Data Abstractions Lecture 9: B Trees Dan Grossman Spring 2010.
B-Trees. Motivation for B-Trees Index structures for large datasets cannot be stored in main memory Storing it on disk requires different approach to.
AVL Trees / Slide 1 Delete Single rotation Deletion.
TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 5 Jan Maluszynski - HT TDDB56 – TDDB57 Lecture 5 Trees Terminology, Implementation Binary Search Trees.
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):
B-Trees Disk Storage What is a multiway tree? What is a B-tree?
Multi-Way search Trees Trees: a. Nodes may contain 1 or 2 items. b. A node with k items has k + 1 children c. All leaves are on same level.
TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search.
1 Database indices Database Systems manage very large amounts of data. –Examples: student database for NWU Social Security database To facilitate queries,
B + -Trees (Part 1). Motivation AVL tree with N nodes is an excellent data structure for searching, indexing, etc. –The Big-Oh analysis shows most operations.
© 2004 Goodrich, Tamassia (2,4) Trees
CSE 326: Data Structures B-Trees Ben Lerner Summer 2007.
Multi-Way search Trees Trees: a. Nodes may contain 1 or 2 items. b. A node with k items has k + 1 children c. All leaves are on same level.
TDDB56 DALGOPT-D - TDDB57 DALG-C Lecture 7 Jan Maluszynski - HT TDDB56 – DALGOPT-D Algorithms and optimization Lecture 7 Splay Trees. Priority.
CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees.
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.
Advanced Trees Part III Briana B. Morrison Adapted from Alan Eugenio & William J. Collins.
AVL Trees v z. 2 AVL Tree Definition AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the.
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
1 Multiway trees & B trees & 2_4 trees Go&Ta Chap 10.
B-Tree. B-Trees a specialized multi-way tree designed especially for use on disk In a B-tree each node may contain a large number of keys. The number.
Balanced Trees Ellen Walker CPSC 201 Data Structures Hiram College.
More Trees Multiway Trees and 2-4 Trees. Motivation of Multi-way Trees Main memory vs. disk ◦ Assumptions so far: ◦ We have assumed that we can store.
ALGORITHMS FOR ISNE DR. KENNETH COSH WEEK 6.
1 B Trees - Motivation Recall our discussion on AVL-trees –The maximum height of an AVL-tree with n-nodes is log 2 (n) since the branching factor (degree,
1 AVL-Trees: Motivation Recall our discussion on BSTs –The height of a BST depends on the order of insertion E.g., Insert keys 1, 2, 3, 4, 5, 6, 7 into.
Balanced Search Trees Chapter 27 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
AVL Trees Amanuel Lemma CS252 Algoithms Dec. 14, 2000.
Balanced Trees AVL Trees Red-Black Trees 2-3 Trees Trees.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
© 2004 Goodrich, Tamassia Red-Black Trees v z.
COSC 2007 Data Structures II Chapter 15 External Methods.
B + -Trees. Motivation An AVL tree with N nodes is an excellent data structure for searching, indexing, etc. The Big-Oh analysis shows that most operations.
Starting at Binary Trees
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
© 2004 Goodrich, Tamassia Binary Search Trees1 CSC 212 Lecture 18: Binary and AVL Trees.
CS 253: Algorithms Chapter 13 Balanced Binary Search Trees (Balanced BST) AVL Trees.
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.
CSC 213 Lecture 8: (2,4) Trees. Review of Last Lecture Binary Search Tree – plain and tall No balancing, no splaying, no speed AVL Tree – liberté, égalité,
© 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, …
D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.
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.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
Binary Search Trees1 Chapter 3, Sections 1 and 2: Binary Search Trees AVL Trees   
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
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
AVL Trees AVL (Adel`son-Vel`skii and Landis) tree = – A BST – With the property: For every node, the heights of the left and right subtrees differ at most.
More Trees. Outline Tree B-Tree 2-3 Tree Tree Red-Black Tree.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
COMP261 Lecture 23 B Trees.
Red-Black Trees 5/17/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Multiway Search Trees Data may not fit into main memory
AVL Trees 6/25/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
AVL Trees 4/29/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
TCSS 342, Winter 2006 Lecture Notes
Lecture No.20 Data Structures Dr. Sohail Aslam
1 Lecture 13 CS2013.
Presentation transcript:

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

TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 6 Jan Maluszynski - HT Content: Motivation: keeping search trees balanced AVL Tree [Goodrich & Tamassia 10.2] Multi-way search trees [Goodrich & Tamassia 10.4] Information on B-trees [Goodrich & Tamassia ]

TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 6 Jan Maluszynski - HT AVL Trees A BST LeftHeight(v)-RightHeight(v) right child

TDDB56 DALGOPT-D TDDB57 DALG-C 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)

TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 6 Jan Maluszynski - HT AVL-Tree Insertion – Rebalance…

TDDB56 DALGOPT-D TDDB57 DALG-C 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

TDDB56 DALGOPT-D TDDB57 DALG-C 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

TDDB56 DALGOPT-D TDDB57 DALG-C 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!

TDDB56 DALGOPT-D TDDB57 DALG-C 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

TDDB56 DALGOPT-D TDDB57 DALG-C 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!)

TDDB56 DALGOPT-D TDDB57 DALG-C 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

TDDB56 DALGOPT-D TDDB57 DALG-C 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!

TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 6 Jan Maluszynski - HT New approach: relax some condition... AVL-Tree: strict 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!

TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 6 Jan Maluszynski - HT (2,3) trees Previously: A single ”pivot element” If larger we search to the right If smaller we search to the left Now: Multipple pivot elements No. of children = no. of pivot elements

TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 6 Jan Maluszynski - HT (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)

TDDB56 DALGOPT-D TDDB57 DALG-C 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)

TDDB56 DALGOPT-D TDDB57 DALG-C 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

TDDB56 DALGOPT-D TDDB57 DALG-C 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!

TDDB56 DALGOPT-D TDDB57 DALG-C 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

TDDB56 DALGOPT-D TDDB57 DALG-C 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

TDDB56 DALGOPT-D TDDB57 DALG-C 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!