TCSS 342, Winter 2006 Lecture Notes

Slides:



Advertisements
Similar presentations
B+-Trees (PART 1) What is a B+ tree? Why B+ trees? Searching a B+ tree
Advertisements

Balanced Search Trees. 2-3 Trees Trees Red-Black Trees AVL Trees.
A balanced life is a prefect life.
Data Structures and Algorithms1 B-Trees with Minimum=1 2-3 Trees.
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,
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.
Splay Trees and B-Trees
CPSC 335 BTrees Dr. Marina Gavrilova Computer Science University of Calgary Canada.
IntroductionIntroduction  Definition of B-trees  Properties  Specialization  Examples  2-3 trees  Insertion of B-tree  Remove items from B-tree.
Balanced Trees Ellen Walker CPSC 201 Data Structures Hiram College.
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,
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
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.
2-3 Tree. Slide 2 Outline  Balanced Search Trees 2-3 Trees Trees.
Starting at Binary Trees
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 12: Multi-way Search Trees Java Software Structures: Designing.
Fall 2006 CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties.
B+ Trees  What if you have A LOT of data that needs to be stored and accessed quickly  Won’t all fit in memory.  Means we have to access your hard.
© 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, …
Balanced Search Trees Chapter 19 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
(2,4) Trees1 What are they? –They are search Trees (but not binary search trees) –They are also known as 2-4, trees.
Lecture Trees Professor Sin-Min Lee.
COMP261 Lecture 23 B Trees.
Chapter 47 Red Black Trees
Data Structures Balanced Trees CSCI 2720 Spring 2007.
Chapter 48 Red Black Trees
Multiway Search Trees Data may not fit into main memory
B-Trees B-Trees.
Extra: B+ Trees CS1: Java Programming Colorado State University
Chapter 11: Multiway Search Trees
B+-Trees.
B+ Trees What are B+ Trees used for What is a B Tree What is a B+ Tree
B+-Trees.
B+-Trees.
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
B-Trees Disk Storage What is a multiway tree? What is a B-tree?
B-Trees © Dave Bockus Acknowledgements to:
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
(edited by Nadia Al-Ghreimil)
Data Structures and Algorithms
B Tree Adhiraj Goel 1RV07IS004.
Data Structures Balanced Trees CSCI
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
TCSS 342, Winter 2006 Lecture Notes
(2,4) Trees (2,4) Trees 1 (2,4) Trees (2,4) Trees
Wednesday, April 18, 2018 Announcements… For Today…
Lecture 26 Multiway Search Trees Chapter 11 of textbook
CS202 - Fundamental Structures of Computer Science II
B+ Trees What are B+ Trees used for What is a B Tree What is a B+ Tree
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
(2,4) Trees /26/2018 3:48 PM (2,4) Trees (2,4) Trees
B-Trees.
Chapter 43 Red Black Trees
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
(2,4) Trees (2,4) Trees (2,4) Trees.
Multiway Trees Searching and B-Trees Advanced Tree Structures
B-Trees Disk Storage What is a multiway tree? What is a B-tree?
2-3-4 Trees Red-Black 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.
(2,4) Trees /24/2019 7:30 PM (2,4) Trees (2,4) Trees
(2,4) Trees (2,4) Trees (2,4) Trees.
(2,4) Trees /6/ :26 AM (2,4) Trees (2,4) Trees
B-Trees.
Heaps & Multi-way Search Trees
Binary Search Trees < > = Dictionaries
CS210- Lecture 20 July 19, 2005 Agenda Multiway Search Trees 2-4 Trees
Presentation transcript:

TCSS 342, Winter 2006 Lecture Notes Multiway Search Trees version 1.0

Objectives Define Multiway Search trees Show through how 2-3-4 trees are kept balanced Show the connection between 2-3-4 trees and red-black trees Explain the motivation behind B-Trees.

Multi-Way search Trees Like Binary Search trees except: nodes can have more than one element perfectly balanced (all leaf nodes at same level) Still obeys ordering property (2,3) tree contains only 2-nodes and 3-nodes 2-node: one item node with 0 or 2 children same ordering property as binary search tree nodes 3-node: two item node with 0 or 3 children With two items k1 and k2 and 3 children c0, c1, c2, ordering property is: c0 < k1 < c1 < k2 < c2. all leaves at same level.

Example A (2,3) tree storing 18 items. 20 80 5 30 70 90 100 25 40 50 20 80 5 30 70 90 100 25 40 50 75 85 110 120 2 4 10 95

Multiway search trees (2,4) tree Alternative 2-4 tree definition: contains only 2-nodes, 3-nodes, and 4-nodes 4-node: three item node with 0 or 4 children; satisfies similar ordering property all leaves at same level Alternative 2-4 tree definition: All nodes must contain between 1 and 3 items All leaf nodes must be at the same level Every internal (non-leaf) node satisfies: If it contains i items k1, k2, .. ki, then It must have i+1 children c0, c1, .. ci Items are sorted so that for each item kb, All items in subtree cb-1 are less than kb. All items in subtree cb are greater than kb.

(2,4) Tree Example 10 45 60 3 8 70 90 100 50 55 25

B-Trees (2,3) Tree = B-tree of order 3 (2,4) Tree = B-tree of order 4 In general, B-tree of order m allows k items per node, where m/2 -1  k  m-1 Internal nodes with k items have k+1 children Satisfies similar ordering property All leaves at same level Design ideas behind B-Trees: In array implementation, each node at most ½ empty (# items per node allowed not strictly followed when there are not enough items) Always balanced

B-Trees motivation Suppose data stored on disk To read data, speed depends on Seek time (moving head) Transfer time (after location is found on disk) Accessing a contiguous chunk of data relatively fast Accessing data scattered in different parts of disk slow Storing sorted data on disk: Use B-tree to store long contiguous chunks Design order of B-tree so items in one node fits in exactly one fast-loading chunk of data.

Implementing multiway search trees Always maintain the search tree properties after every insertion and deletion Maintain ordering property Maintain all leaves at same level Make sure #items in each node is acceptable Do some combination of the following to maintain tree properties: Rotations (Redistributions) Splitting full nodes Fusing neighboring nodes together Moving item from child to parent, or vice versa Replacing an item with its in-order successor. Do case-by-case analysis to figure out exact steps

(2,4) Trees, some details While adjusting trees Insertion: Temporarily allow 4 items in a node. Temporarily allow leaves to not be at same level. Insertion: Find and insert item into proper leaf node (by sorted order) If node is too full (has 4 items), then split the node Split full node into one node with two subtrees Leaves of new subtrees are now one level off Merge root of split node upward with parent Fixes level problem Repeatedly apply split to parent node, if necessary. If process goes all the way top, may get new root. Alternative insertion strategy: split full nodes on the way down when looking for proper leaf node.

(2,4) Tree Next: Insert 38 10 45 60 3 8 70 90 100 50 55 25

After insert(38); next: insert(105) (2,4) Tree After insert(38); next: insert(105) 45 10 60 70 90 100 3 8 25 38 50 55

(2,4) Tree After Insert(105); 45 10 60 90 3 8 50 55 70 100 105 25 38

Removal Swap node to removed with inorder successor Inorder successor will always be in a leaf. Remove item from the leaf node. If node is now empty, try to fill it with an extra item from neighbor (redistribution/rotation). If all neighbors have only one item, we have an UNDERFLOW. Fix this by move parent item down into child node so it contains two items move a grandparent item down into parent node merge parent node with sibling (possible node split) If necessary, fix UNDERFLOW at grandparent level Underflows propagating to root shrink tree.

Delete(15) 35 20 60 6 15 40 50 70 80 90

Delete(15) 35 20 60 6 40 50 70 80 90

Continued Drop item from parent 35 60 6 20 40 50 70 80 90

Continued Drop item from grandparent 35 60 6 20 40 50 70 80 90

Fuse 35 60 6 20 40 50 70 80 90

Drop item from root Remove root, return the child. 35 60 6 20 40 50 35 60 6 20 40 50 70 80 90

Summary (2,4) trees make it very easy to maintain balance. Insertion and deletion easier for (2,4) tree. Cost is waste of space in each node. Also extra comparison inside each node. Does not “extend” binary trees.

Red-Black and (2,4) trees. Every Red-Black tree has a corresponding (2,4) tree. Move every red node upwards into parent black node. Does this always work? Why? max # items in a node? all leaves at same depth? Is there only one unique way to convert red-black tree into (2,4) tree? (2,4) tree into red-black tree? Strategy for red-black deletion: Convert Red-Black to (2,4) tree. Delete from (2,4) tree. Convert back to red-black tree.

The corresponding (2,4) tree 10 30 20 40 50 55 5 Deleting 20 is a simple transfer! Remove the item 20. Drop an item (30) from parent. Move an item 40  parent.

Updated 2-3-4 tree 10 40 5 30 50 55 40 50 55

Now redraw the RBTree! 60 40 70 10 85 50 65 5 30 80 90 55 Drill: delete(65), delete(80), delete(90), delete(85)

References Lewis & Chase book, chapter 16.

Example A (2,3) tree storing 18 items. 20 80 5 30 70 90 100 25 40 50 20 80 5 30 70 90 100 25 40 50 75 85 110 120 2 4 10 95

Updating Insertion: Find the appropriate leaf. If there is only one item, just add to leaf. Insert(23); Insert(15) If no room, move middle item to parent and split remaining two items among two children. Insert(3);

Insertion Insert(3); 20 80 5 30 70 90 100 40 50 75 85 110 120 2 3 4 10 15 23 25 95

Insert(3); In mid air… 20 80 5 30 70 90 100 3 23 25 40 50 75 85 110 120 2 10 15 95 4

Done…. 20 80 3 5 30 70 90 100 4 23 25 40 50 75 85 110 120 2 10 15 95

Tree grows at the root… Insert(45); 20 80 3 5 30 70 90 100 4 25 20 80 3 5 30 70 90 100 4 25 40 45 50 75 85 110 120 2 10 95

New root: 45 20 80 3 5 30 70 90 100 4 25 40 50 75 85 110 120 2 10 95

Delete If item is not in a leaf exchange with in-order successor. If leaf has another item, remove item. Examples: Remove(110); (Insert(110); Remove(100); ) If leaf has only one item but sibling has two items: redistribute items. Remove(80);

Remove(80); Step 1: Exchange 80 with in-order successor. 45 20 85 3 5 3 5 30 70 90 100 110 120 4 25 40 50 75 80 2 10 95

Redistribute Remove(80); 45 20 85 3 5 30 70 95 110 120 4 25 40 50 75 3 5 30 70 95 110 120 4 25 40 50 75 90 2 10 100

Some more removals… Remove(70); Swap(70, 75); “Merge” Empty node with sibling; Join parent with node; Now every node has k+1 children except that one node has 0 items and one child. Sibling can spare an item: redistribute. 110

Delete(70) 45 20 85 3 5 30 75 95 110 120 4 25 40 50 90 2 10 100

New tree: Delete(85) will “shrink” the tree. 45 20 95 3 5 30 85 110 3 5 30 85 110 120 4 25 40 50 90 100 2 10

Details 1. Swap(85, 90) //inorder successor 2. Remove(85) //empty node created 3. Merge with sibling 4. Drop item from parent// (50,90) empty Parent 5. Merge empty node with sibling, drop item from parent (95) 6. Parent empty, merge with sibling drop item. Parent (root) empty, remove root.

“Shorter” 2-3 Tree 20 45 3 5 30 95 110 50 90 120 4 25 40 100 2 10

Deletion Summary If item k is present but not in a leaf, swap with inorder successor; Delete item k from leaf L. If L has no items: Fix(L); Fix(Node N); //All nodes have k items and k+1 children // A node with 0 items and 1 child is possible, it will have to be fixed.

Deletion (continued) If N is the root, delete it and return its child as the new root. Example: Delete(8); 5 5 1 2 3 3 3 5 3 8 Return 3 5

Deletion (Continued) If a sibling S of N has 2 items distribute items among N, S and the parent P; if N is internal, move the appropriate child from S to N. Else bring an item from P into S; If N is internal, make its (single) child the child of S; remove N. If P has no items Fix(P) (recursive call)