COL 106 Shweta Agrawal, Amit Kumar

Slides:



Advertisements
Similar presentations
COSC 2007 Data Structures II Chapter 12 Advanced Implementation of Tables II.
Advertisements

Binary Tree Structure a b fe c a rightleft g g NIL c ef b left right pp p pp left key.
CSE 373 Data Structures and Algorithms
CSE 4101/5101 Prof. Andy Mirzaian B-trees trees.
Splay Trees Binary search trees.
CSE Lecture 17 – Balanced trees
Animated demo: B- Trees Slide Credit.
COL 106 Shweta Agrawal and Amit Kumar
Advanced Database Discussion B Trees. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if.
B+-Trees (PART 1) What is a B+ tree? Why B+ trees? Searching a B+ tree
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 Search Trees. 2-3 Trees Trees Red-Black Trees AVL Trees.
A balanced life is a prefect life.
CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
Data Structures and Algorithms1 B-Trees with Minimum=1 2-3 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)
Chapter 6: Transform and Conquer Trees, Red-Black Trees The Design and Analysis of Algorithms.
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.
1 Database indices Database Systems manage very large amounts of data. –Examples: student database for NWU Social Security database To facilitate queries,
© 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.
Trees Main and Savitch Chapter 10. Binary Trees A binary tree has nodes, similar to nodes in a linked list structure. Data of one sort or another may.
CS4432: Database Systems II
2-3 Trees Professor Sin-Min Lee. Contents n Introduction n The 2-3 Trees Rules n The Advantage of 2-3 Trees n Searching For an Item in a 2-3 Tree n Inserting.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
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.
CPSC 335 BTrees Dr. Marina Gavrilova Computer Science University of Calgary Canada.
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.
1 COP 3538 Data Structures with OOP Chapter 8 - Part 2 Binary Trees.
Multi-way Trees. M-way trees So far we have discussed binary trees only. In this lecture, we go over another type of tree called m- way trees or trees.
Data Structures CSCI 2720 Spring 2007 Balanced Trees.
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
2-3 Trees, Trees Red-Black Trees
2-3 Tree. Slide 2 Outline  Balanced Search Trees 2-3 Trees Trees.
Starting at Binary Trees
Binary Search Tree vs. Balanced Search Tree. Why care about advanced implementations? Same entries, different insertion sequence: 10,20,30,40,50,60,70,
Chapter 13 A Advanced Implementations of Tables. © 2004 Pearson Addison-Wesley. All rights reserved 13 A-2 Balanced Search Trees The efficiency of the.
File Organization and Processing Week Tree Tree.
Fall 2006 CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
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
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Balanced Search Trees Chapter 19 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
(2,4) Trees1 What are they? –They are search Trees (but not binary search trees) –They are also known as 2-4, trees.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 B+-Tree Index Chapter 10 Modified by Donghui Zhang Nov 9, 2005.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
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.
B+-Tree Deletion Underflow conditions B+ tree Deletion Algorithm
Balanced Search Trees 2-3 Trees AVL Trees Red-Black Trees
Lecture Trees Professor Sin-Min Lee.
TCSS 342, Winter 2006 Lecture Notes
Data Structures Balanced Trees CSCI 2720 Spring 2007.
AA Trees.
(edited by Nadia Al-Ghreimil)
Data Structures and Algorithms
Data Structures Balanced Trees CSCI
(2,4) Trees (2,4) Trees 1 (2,4) Trees (2,4) Trees
Multi-Way Search Trees
(2,4) Trees /26/2018 3:48 PM (2,4) Trees (2,4) Trees
(2,4) Trees (2,4) Trees (2,4) Trees.
(2,4) Trees 2/15/2019 (2,4) Trees (2,4) Trees.
(2,4) Trees /24/2019 7:30 PM (2,4) Trees (2,4) Trees
(edited by Nadia Al-Ghreimil)
(2,4) Trees (2,4) Trees (2,4) Trees.
(2,4) Trees /6/ :26 AM (2,4) Trees (2,4) Trees
CS210- Lecture 20 July 19, 2005 Agenda Multiway Search Trees 2-4 Trees
Presentation transcript:

COL 106 Shweta Agrawal, Amit Kumar 2-3 and 2-3-4 Trees COL 106 Shweta Agrawal, Amit Kumar

Multi-Way Trees A binary search tree: An M-way search tree: One value in each node At most 2 children An M-way search tree: Between 1 to (M-1) values in each node At most M children per node

M-way Search Tree Details Each internal node of an M-way search has: Between 1 and M children Up to M-1 keys k1 , k2 , ... , kM-1 kM-1 . . . ki-1 ki k1 Keys are ordered such that: k1 < k2 < ... < kM-1

Properties of M-way Search Tree k 1 . . . k k k k . . . k k i-1 i i M-1 T T . . . T T . . . T T 1 1 i i M For a subtree Ti that is the i-th child of a node: all keys in Ti must be between keys ki-1 and ki i.e. ki-1 < keys(Ti )< ki All keys in first subtree T1, keys(T1 )< k1 All keys in last subtree TM, keys(TM ) > kM-1

Example: 3-way search tree 68 Try: search 68

Search for X At a node consisting of values V1...Vk, there are four possible cases: If X < V1, recursively search for X in the subtree that is left of V1 If X > Vk, recursively search for X in the subtree that is right of Vk If X=Vi, for some i, then we are done (X has been found) Else, for some i, Vi < X < Vi+1. In this case recursively search for X in the subtree that is between Vi and Vi+1 Time Complexity: O((M-1)*h)=O(h) [M is a constant]

Insert X The algorithm for binary search tree can be generalized Follow the search path Add new key into the last leaf, or add a new leaf if the last leaf is fully occupied Example: Add 52,69 52 69

Delete X The algorithm for binary search tree can be generalized: A leaf node can be easily deleted An internal node is replaced by its successor and the successor is deleted Example: Delete 10, Delete 44, Time complexity: O(Mh)=O(h), but h can be O(n)

M-way Search Tree What we know so far: What is an M-way search tree How to implement Search, Insert, and Delete The time complexity of each of these operations is: O(Mh)=O(h) The problem (as usual): h can be O(n). B-tree: balanced M-way Search Tree

2-3 Tree

Why care about advanced implementations? Same entries, different insertion sequence:  Not good! Would like to keep tree balanced.

2-3 Trees Features each internal node has either 2 or 3 children all leaves are at the same level

2-3 Trees with Ordered Nodes leaf node can be either a 2-node or a 3-node

Example of 2-3 Tree

What did we gain? What is the time efficiency of searching for an item?

Gain: Ease of Keeping the Tree Balanced Binary Search Tree both trees after inserting items 39, 38, ... 32 2-3 Tree

Inserting Items Insert 39

Inserting Items Insert 38 divide leaf and move middle result value up to parent insert in leaf result

Inserting Items Insert 37

Inserting Items Insert 36 divide leaf and move middle value up to parent insert in leaf overcrowded node

Inserting Items ... still inserting 36 divide overcrowded node, move middle value up to parent, attach children to smallest and largest result

Inserting Items After Insertion of 35, 34, 33

Inserting so far

Inserting so far

Inserting Items How do we insert 32?

Inserting Items creating a new root if necessary tree grows at the root

Inserting Items Final Result

Deleting Items Delete 70 70 80

Deleting Items Deleting 70: swap 70 with inorder successor (80)

Deleting Items Deleting 70: ... get rid of 70

Deleting Items Result

Deleting Items Delete 100

Deleting Items Deleting 100

Deleting Items Result

Deleting Items Delete 80

Deleting Items Deleting 80 ...

Deleting Items Deleting 80 ...

Deleting Items Deleting 80 ...

Deleting Items Final Result comparison with binary search tree

Deletion Algorithm I Deleting item I: Locate node n, which contains item I If node n is not a leaf  swap I with inorder successor deletion always begins at a leaf If leaf node n contains another item, just delete item I else try to redistribute nodes from siblings (see next slide) if not possible, merge node (see next slide)

Deletion Algorithm II Redistribution A sibling has 2 items: redistribute item between siblings and parent Merging No sibling has 2 items: merge node move item from parent to sibling

Deletion Algorithm III Redistribution Internal node n has no item left redistribute Merging Redistribution not possible: merge node move item from parent to sibling adopt child of n If n's parent ends up without item, apply process recursively

Deletion Algorithm IV If merging process reaches the root and root is without item  delete root

Operations of 2-3 Trees all operations have time complexity of log n

2-3-4 Trees similar to 2-3 trees 4-nodes can have 3 items and 4 children 4-node

2-3-4 Tree Example

2-3-4 Tree: Insertion Insertion procedure: similar to insertion in 2-3 trees items are inserted at the leafs since a 4-node cannot take another item, 4-nodes are split up during insertion process Strategy on the way from the root down to the leaf: split up all 4-nodes "on the way"  insertion can be done in one pass (remember: in 2-3 trees, a reverse pass might be necessary)

2-3-4 Tree: Insertion Inserting 60, 30, 10, 20, 50, 40, 70, 80, 15, 90, 100

2-3-4 Tree: Insertion Inserting 60, 30, 10, 20 ... ... 50, 40 ...

2-3-4 Tree: Insertion Inserting 50, 40 ... ... 70, ...

2-3-4 Tree: Insertion Inserting 70 ... ... 80, 15 ...

2-3-4 Tree: Insertion Inserting 80, 15 ... ... 90 ...

2-3-4 Tree: Insertion Inserting 90 ... ... 100 ...

2-3-4 Tree: Insertion Inserting 100 ...

2-3-4 Tree: Insertion Procedure Splitting 4-nodes during Insertion

2-3-4 Tree: Insertion Procedure Splitting a 4-node whose parent is a 2-node during insertion

2-3-4 Tree: Insertion Procedure Splitting a 4-node whose parent is a 3-node during insertion

Insertion Algorithm Insert the new key at the lowest node reached in the search 2-node becomes 3-node 3-node becomes 4-node What about a 4-node? We can’t insert another key! (2,4) Trees

Top Down Insertion In our way down the tree, whenever we reach a 4-node, we break it up into two 2-nodes, and move the middle element up into the parent node Now we can perform the insertion using one of the previous two cases Since we follow this method from the root down to the leaf, it is called top down insertion (2,4) Trees

An Example (2,4) Trees

(2,4) Trees

Algorithm: Top Down Insertion If the current node is a 4-node: Remove and save the middle value to get a 3-node. Split the remaining 3-node up into a pair of 2-nodes (the now missing middle value is handled in the next step).

Algorithm: Top Down Insertion If the current node is a 4-node: Remove and save the middle value to get a 3-node. Split the remaining 3-node up into a pair of 2-nodes (the now missing middle value is handled in the next step). If current node is root node (which thus has no parent): the middle value becomes the new root 2-node and the tree height increases by 1. Ascend into the root. Otherwise, push the middle value up into the parent node. Ascend into the parent node.

Algorithm: Top Down Insertion If the current node is a 4-node: Remove and save the middle value to get a 3-node. Split the remaining 3-node up into a pair of 2-nodes (the now missing middle value is handled in the next step). If current node is root node (which thus has no parent): the middle value becomes the new root 2-node and the tree height increases by 1. Ascend into the root. Otherwise, push the middle value up into the parent node. Ascend into the parent node. Find the child whose interval contains the value to be inserted. If that child is a leaf, insert the value into the child node and finish. Otherwise, descend into the child and repeat from step 1

Time Complexity of Insertion A search visits O(log N) nodes An insertion requires O(log N) node splits Each node split takes constant time Hence, operations Search and Insert each take time O(log N) Notes: Instead of doing splits top-down, we can perform them bottom-up starting at the insertion node, and only when needed. This is called bottom-up insertion. A deletion can be performed by fusing nodes (inverse of splitting) (2,4) Trees

2-3-4 Tree: Deletion A little trickier First of all, find the key with a simple multi-way search If the item to delete has children, swap with inorder successor Remove the item ...but what about removing from 2-nodes? (2,4) Trees

2-3-4 Tree: Deletion Not enough items in the node - underflow Pull an item from the parent, replace it with an item from a sibling - called transfer Still not good enough! What happens if siblings are 2-nodes? Could we just pull one item from the parent? No. Too many children But maybe...

2-3-4 Tree: Deletion We know that the node’s sibling is just a 2-node So we fuse them into one (after stealing an item from the parent, of course) Last special case: what if the parent was a 2-node? (2,4) Trees

2-3-4 Tree: Deletion Underflow can cascade up the tree, too. (2,4) Trees

2-3-4 Tree: Deletion Deletion procedure: similar to deletion in 2-3 trees items are deleted at the leafs  swap item of internal node with inorder successor note: a 2-node leaf creates a problem Strategy (different strategies possible) on the way from the root down to the leaf: turn 2-nodes (except root) into 3-nodes  deletion can be done in one pass