Lecture 11COMPSCI.220.FS.T - 20041 Balancing an AVLTree Two mirror-symmetric pairs of cases to rebalance the tree if after the insertion of a new key to.

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

Data Structures Haim Kaplan and Uri Zwick November 2012 Lecture 5 B-Trees.
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
6/14/2015 6:48 AM(2,4) Trees /14/2015 6:48 AM(2,4) Trees2 Outline and Reading Multi-way search tree (§3.3.1) Definition Search (2,4)
Advanced Tree Data Structures Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
B-Trees Disk Storage What is a multiway tree? What is a B-tree?
B-Trees Disk Storage What is a multiway tree? What is a B-tree?
©Silberschatz, Korth and Sudarshan12.1Database System Concepts Chapter 12: Part B Part A:  Index Definition in SQL  Ordered Indices  Index Sequential.
CS 206 Introduction to Computer Science II 12 / 01 / 2008 Instructor: Michael Eckmann.
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
B + -Trees (Part 1) Lecture 20 COMP171 Fall 2006.
1 B-Trees Disk Storage What is a multiway tree? What is a B-tree? Why B-trees? Comparing B-trees and AVL-trees Searching a B-tree Insertion in a B-tree.
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.
B + -Trees (Part 1) COMP171. Slide 2 Main and secondary memories  Secondary storage device is much, much slower than the main RAM  Pages and blocks.
CSE 326: Data Structures B-Trees Ben Lerner Summer 2007.
CS 206 Introduction to Computer Science II 11 / 24 / 2008 Instructor: Michael Eckmann.
General Trees and Variants CPSC 335. General Trees and transformation to binary trees B-tree variants: B*, B+, prefix B+ 2-4, Horizontal-vertical, Red-black.
B-Trees and B+-Trees Disk Storage What is a multiway tree?
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.
AVL Trees ITCS6114 Algorithms and Data Structures.
Advanced Trees Part III Briana B. Morrison Adapted from Alan Eugenio & William J. Collins.
B + -Trees COMP171 Fall AVL Trees / Slide 2 Dictionary for Secondary storage * The AVL tree is an excellent dictionary structure when the entire.
Splay Trees and B-Trees
1 B-Trees Section AVL (Adelson-Velskii and Landis) Trees AVL tree is binary search tree with balance condition –To ensure depth of the tree is.
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
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.
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.
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,
Balanced Search Trees Chapter Chapter Contents AVL Trees Single Rotations Double Rotations Implementation Details 2-3 Trees Searching Adding Entries.
1 B-Trees & (a,b)-Trees CS 6310: Advanced Data Structures Western Michigan University Presented by: Lawrence Kalisz.
Trevor Brown – University of Toronto B-slack trees: Space efficient B-trees.
INTRODUCTION TO MULTIWAY TREES P INTRO - Binary Trees are useful for quick retrieval of items stored in the tree (using linked list) - often,
10/20/2015 2:03 PMRed-Black Trees v z. 10/20/2015 2:03 PMRed-Black Trees2 Outline and Reading From (2,4) trees to red-black trees (§9.5) Red-black.
© 2004 Goodrich, Tamassia Red-Black Trees v z.
© 2004 Goodrich, Tamassia Red-Black Trees v z.
Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.
Balanced Search Trees Fundamental Data Structures and Algorithms Margaret Reid-Miller 3 February 2005.
1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower.
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.
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.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, VUW B Trees and B+ Trees COMP 261.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture17.
Fall 2006 CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties.
3.1. Binary Search Trees   . Ordered Dictionaries Keys are assumed to come from a total order. Old operations: insert, delete, find, …
B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.
Internal and External Sorting External Searching
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
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   
1 Binary Search Trees   . 2 Ordered Dictionaries Keys are assumed to come from a total order. New operations: closestKeyBefore(k) closestElemBefore(k)
B-Trees Katherine Gurdziel 252a-ba. Outline What are b-trees? How does the algorithm work? –Insertion –Deletion Complexity What are b-trees used for?
ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: ec2620m.htm Office: TEL 3049.
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.
Red-Black Trees v z Red-Black Trees Red-Black Trees
B/B+ Trees 4.7.
Multiway Search Trees Data may not fit into main memory
B+-Trees.
B+-Trees.
Wednesday, April 18, 2018 Announcements… For Today…
Red-Black Trees v z /20/2018 7:59 AM Red-Black Trees
Red-Black Trees v z Red-Black Trees Red-Black Trees
Red-Black Trees v z /17/2019 4:20 PM Red-Black Trees
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees v z /6/ :10 PM Red-Black Trees
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

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 the bottom the AVL property is invalidated Only one single or double rotation is sufficient Deletions are more complicated: O(log n) rotations can be required to restore the balance AVL balancing is not computationally efficient. Better balanced search trees: red-black, AA-trees, B-trees

Lecture 11COMPSCI.220.FS.T Single Rotation The inserted new key invalidates the AVL property To restore the AVL tree, the root is moved to the node B and the rest of the tree is reorganised as the BST

Lecture 11COMPSCI.220.FS.T Double Rotation Balancing by more complex alternative representation of the tree (split of the ST2 and move of the root to D)

Lecture 11COMPSCI.220.FS.T Red-Black Tree A red - black tree is a binary search tree with the following ordering properties: –Every node is coloured either red or black –The root is black –In a node is red, its children must be black –Every path from a node to a leaf must contain the same number of black nodes Statement: because every path from the root to a leaf contains b black nodes, there are at least 2 b black nodes in the tree

Lecture 11COMPSCI.220.FS.T Red-Black Tree Proof of the statement by math induction: it is valid for b  1 (only the root or also 1-2 its children) Let it be valid for all red-black trees with b black nodes per path If a tree contains b  1 black nodes per path and it has 2 black children of the root, then it contains at least 2  (2 b  1)  1  2 b+1  1 black nodes

Lecture 11COMPSCI.220.FS.T Red-Black Tree If there is a red child of the root, it has necessarily only black children, and the total number of the black nodes is even larger There cannot be two consecutive red nodes on a path, so the height of a red-black tree is at most 2log 2 (n  1) Thus the search operation is logarithmic O(log n)

Lecture 11COMPSCI.220.FS.T Red-Black Tree Average case: a search in a red-black tree with n nodes built from random keys seems to require about log n comparisons and an insertion seems to require, on the average, less than one rotation Worst case: a search in a red-black tree with n nodes built from random keys requires less than 2log n + 2 comparisons, and an insertion requires fewer than one-quarter as many rotations as comparisons

Lecture 11COMPSCI.220.FS.T AA-Trees Software implementation of the operations insert and remove for red-black trees is a rather tricky process A balanced search AA-tree is a method of choice if deletions are needed The AA-tree adds one extra condition to the red - black tree: left children may not be red This condition greatly simplifies the red-black tree remove operation

Lecture 11COMPSCI.220.FS.T B-Trees: Efficient External Search For very big databases, even log 2 n search steps may be unacceptable To reduce the number of disk accesses: an optimal m - ary search tree of height about log m n n  log 2 n   log 10 n   log 100 n   log 1000 n  m -way branching lowers the optimal tree height by factor log 2 m (i.e., by 3.3 if m  10 )

Lecture 11COMPSCI.220.FS.T Multiway Search Tree of Order m  4 In an m -ary search tree, at most m  1 keys are used to decide which branch to take The data records are associated only with leaves, so the worst-case and average-case searches involve the tree height and the average leaf depth, respectively

Lecture 11COMPSCI.220.FS.T B-Tree Definition A B-tree of order m is defined as an m- ary balanced tree with the following properties: –The root is either a leaf or it has between 2 and m children inclusive –Every nonleaf node (except possibly the root) has between  m / 2  and m children inclusive

Lecture 11COMPSCI.220.FS.T B-Tree Definition A nonleaf node with  children has  keys ( key i : i  1, …,  ) to guide the search key i represents the smallest key in the subtree i  1 All leaves are at the same depth The data items are stored at leaves, and every leaf contains between  l / 2  and l data items, for some l that may be chosen independently of the tree order m Assuming that each node represents a disk block, the choice is based on the size of items that are being stored

Lecture 11COMPSCI.220.FS.T Example of choosing m, l Let one data block hold 8192 bytes and each key use 32 bytes Let there be 10 7 data records, 256 bytes per record B-tree of order m  m  keys per node plus m branches The branch is a number of another disk block, so let a branch be 4 bytes: 32(m  )  4m  36m  32  8192  m  228 Because the block size is 8192 bytes, l  32 records of size 256 bytes fit in a single block

Lecture 11COMPSCI.220.FS.T Example of choosing m, l Each leaf has between data records inclusive, and each internal node, except from the root, branches in 114 – 228 ways 10 7 records can be stored in  leaves (= 10 7  ( ) ) In the worst case the leaves would be on the level 4 (   625,000   )

Lecture 11COMPSCI.220.FS.T Naming the B-Trees B-trees are named after their branching factors, that is,  m / 2   m A 4  7-tree is the B-tree of order m  7 –2..4 children per root –4..7 children per each non-root node A 3  4-tree is the B-tree of order m  4 –2..3 children per root –3..4 children per each non-root node

Lecture 11COMPSCI.220.FS.T  4-Tree with 4  7 items per leaf

Lecture 11COMPSCI.220.FS.T Analysis of B-Trees A search or an insertion in a B-tree of order m with n data items requires fewer than  log m n  disk accesses In practice,  log m n  is almost constant as long as m is not small Data insertion is simple until the corresponding leaf is not already full; then it must be split into 2 leaves, and the parent(s) should be updated

Lecture 11COMPSCI.220.FS.T Analysis of B-Trees Additional disk writes for data insertion and deletion are extremely rare An algorithm analysis beyond the scope of this course shows that both insertions, deletions, and retrievals of data have only log m / 2 n disk accesses in the worst case (e.g.,  log   3 in the above example)