Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
1 AVL-Trees (Adelson-Velskii & Landis, 1962) In normal search trees, the complexity of find, insert and delete operations in search trees is in the worst.
Advertisements

AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
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.
Balanced Binary Search Trees
Advanced Tree Data Structures Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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 Data Structures and Algorithms COSC-600 Lecture presentation-6.
Balanced Search Trees Chapter 27 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Data Structures and Algorithms TREE-TRAVERSAL. Searching - Re-visited Binary tree O(log n) if it stays balanced Simple binary tree good for static collections.
2-3 Tree. Slide 2 Outline  Balanced Search Trees 2-3 Trees 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.
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.
CSE332: Data Abstractions Lecture 7: AVL Trees
Lecture Trees Professor Sin-Min Lee.
Lecture 23 Red Black Tree Chapter 10 of textbook
COMP261 Lecture 23 B Trees.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
AA Trees.
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
Data Structures – LECTURE Balanced trees
AVL 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 M.
B/B+ Trees 4.7.
Multiway Search Trees Data may not fit into main memory
UNIT III TREES.
Red Black Trees
CSIT 402 Data Structures II
CS202 - Fundamental Structures of Computer Science II
Lecture 17 Red-Black Trees
B+ Trees What are B+ Trees used for What is a B Tree What is a B+ Tree
CS202 - Fundamental Structures of Computer Science II
Balanced Binary Search Trees
CS 201 Data Structures and Algorithms
CSE373: Data Structures & Algorithms
B+-Trees.
B+-Trees.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
AVL Tree Mohammad Asad Abbasi Lecture 12
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
B-Trees Disk Storage What is a multiway tree? What is a B-tree?
AVL Tree 27th Mar 2007.
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
(edited by Nadia Al-Ghreimil)
AVL Trees 11/10/2018 AVL Trees v z AVL Trees.
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
Chapter 6 Transform and Conquer.
Wednesday, April 18, 2018 Announcements… For Today…
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
B-Tree.
Data Structures and Algorithms
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
(2,4) Trees (2,4) Trees (2,4) Trees.
Algorithms and Data Structures Lecture VIII
B-Trees Disk Storage What is a multiway tree? What is a B-tree?
CSE 332: Data Abstractions AVL Trees
B-Trees Disk Storage What is a multiway tree? What is a B-tree?
(2,4) Trees 2/15/2019 (2,4) Trees (2,4) Trees.
(edited by Nadia Al-Ghreimil)
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
CE 221 Data Structures and Algorithms
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
(2,4) Trees (2,4) Trees (2,4) Trees.
AVL-Trees (Part 1).
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
1 Lecture 13 CS2013.
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

Data Structures and Algorithms Searching Red-Black and Other Dynamically BalancedTrees PLSD210

Lecture 11 - Key Points Red-Black Trees Software engineers A complex algorithm Gives O(log n) addition, deletion and search Software engineers Know about algorithms Know when to use them Know performance Know where to find an implementation Are too clever to re-invent wheels ... They re-use code So they have more time for Sailing, eating, drinking, ... Anything else I should put here

AVL and other balanced trees AVL Trees First balanced tree algorithm Discoverers: Adelson-Velskii and Landis Properties Binary tree Height of left and right-subtrees differ by at most 1 Subtrees are AVL trees AVL Tree AVL Tree

AVL trees - Height Theorem Proof Sh = Fh+3+1 An AVL tree of height h has at least Fh+3+1 nodes Proof Let Sh be the size of the smallest AVL tree of height h Clearly, S0 = 1 and S1 = 2 Also, Sh = Sh-1 + Sh-2 + 1 A minimum height tree must be composed of min height trees differing in height by at most 1 By induction .. Sh = Fh+3+1

AVL trees - Height n ³ Sh , so n is W( bh ) Now, for large i, Fi+1 / Fi = f, where f = ½(1 + Ö5) or Fi » c (½(1 + Ö5))i Sh = Fh+3 + 1 = O( bh ) n ³ Sh , so n is W( bh ) and h £ logb n or h is O(log n)

AVL trees - Height n ³ Sh , so n is W( bh ) Now, for large i, Fi+1 / Fi = f, where f = ½(1 + Ö5) or Fi » c (½(1 + Ö5))i Sh = Fh+3 + 1 = O( bh ) n ³ Sh , so n is W( bh ) and h £ logb n or h is O(log n) In this case, we can show h £ 1.44 logb (n+2) - 1.328 h is no worse than 44% higher than the optimum

AVL Trees - Rebalancing Insertion leads to non-AVL tree 4 cases 1 and 4 are mirror images 2 and 3 are mirror images 1 2 3 4

AVL Trees - Rebalancing Case 1 solved by rotation Case 4 is the mirror image rotation

AVL Trees - Rebalancing Case 2 needs a double rotation Case 3 is the mirror image rotation

AVL Trees - Data Structures AVL trees can be implemented with a flag to indicate the balance state Insertion Insert a new node (as any binary tree) Work up the tree re-balancing as necessary to restore the AVL property typedef enum { LeftHeavy, Balanced, RightHeavy } BalanceFactor; struct AVL_node { BalanceFactor bf; void *item; struct AVL_node *left, *right; }

Dynamic Trees - Red-Black or AVL Insertion AVL : two passes through the tree Down to insert the node Up to re-balance Red-Black : two passes through the tree but Red-Black is more popular??

Dynamic Trees - A cautionary tale Insertion If you read Cormen et al, There’s no reason to prefer a red-black tree However, in Weiss’ text M A Weiss, Algorithms, Data Structures and Problem Solving with C++, Addison-Wesley, 1996 you find that you can balance a red-black tree in one pass! Making red-black more efficient than AVL if coded properly!!!

Dynamic Trees - A cautionary tale Insertion If you read Cormen et al, There’s no reason to prefer a red-black tree However, in Weiss’ text M A Weiss, Algorithms, Data Structures and Problem Solving with C++, Addison-Wesley, 1996 you find that you can balance a red-black tree in one pass! Making red-black more efficient than AVL if coded properly!!! Moral: You need to read the literature!

Dynamic Trees - A cautionary tale Insertion in one pass As you proceed down the tree, if you find a node with two red children, make it red and the children black This doesn’t alter the number of black nodes in any path If the parent of this node was red, a rotation is needed ... May need to be a single or a double rotation

Trees - Insertion Adding 4 ... Discover two red children here Swap colours around

Trees - Insertion Adding 4 ... Red sequence, violates red-black property Rotate

Trees - Insertion Adding 4 ... Rotate Add the 4

Balanced Trees - Yet more variants Basically the same ideas 2-3 Trees 2-3-4 Trees Special cases of m-way trees ... coming! Variable number of children per node A more complex implementation 2-3-4 trees Map to red-black trees Possibly useful to understand red-black trees

Lecture 12 - Key Points AVL Trees 2-3, 2-3-4 trees First dynamically balanced tree Height within 44% of optimum Rebalanced with rotations O(log n) Less efficient than properly coded red-black trees 2-3, 2-3-4 trees m-way trees - Yet more variations 2-3-4 trees map to red-black trees

m-way trees Only two children per node? Reduce the depth of the tree to O(logmn) with m-way trees m children, m-1 keys per node m = 10 : 106 keys in 6 levels vs 20 for a binary tree but ........

m-way trees But you have to search through the m keys in each node! Reduces your gain from having fewer levels! A curiosity only?

B-trees B+ trees All leaves are on the same level All nodes except for the root and the leaves have at least m/2 children at most m children B+ trees All the keys in the nodes are dummies Only the keys in the leaves point to “real” data Linking the leaves Ability to scan the collection in order without passing through the higher nodes Each node is at least half full of keys

B+-trees B+ trees All the keys in the nodes are dummies Only the keys in the leaves point to “real” data Data records kept in a separate area

B+-trees - Scanning in order Linking the leaves Ability to scan the collection in order without passing through the higher nodes

B+-trees - Use Use - Large Databases Reading a disc block is much slower than reading memory ( ~ms vs ~ns ) Put each block of keys in one disc block Physical disc blocks

B-trees - Insertion Insertion B-tree property : block is at least half-full of keys Insertion into block with m keys block overflows split block promote one key split parent if necessary if root is split, tree becomes one level deeper

B-trees - Insertion Insertion Insert 9 Leaf node overflows, split it Promote middle (8) Root overflows, split it Promote middle (6) New root node formed Height increased by 1

B-trees on disc Disc blocks Overall O( log n ) Deletion similar 512 - 8k bytes 100s of keys Use binary search within the block Overall O( log n ) Matched to hardware! Deletion similar But merge blocks to maintain B-tree property (at least half full)