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é,

Slides:



Advertisements
Similar presentations
1 AVL Trees (10.2) CSE 2011 Winter April 2015.
Advertisements

CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.
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.
A balanced life is a prefect life.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
CSC401 – Analysis of Algorithms Lecture Notes 7 Multi-way Search Trees and Skip Lists Objectives: Introduce multi-way search trees especially (2,4) 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)
© 2004 Goodrich, Tamassia Binary Search Trees   
Goodrich, Tamassia Search Trees1 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):
Binary Search Trees1 Part-F1 Binary Search Trees   
CSC 213 Lecture 9: Red-Black Trees. Announcements Reminder: Daily Quizzes should take 15 minutes Goal is to provide chance to see if you really understand.
© 2004 Goodrich, Tamassia (2,4) Trees
CSE 326: Data Structures B-Trees Ben Lerner Summer 2007.
CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees.
Advanced Trees Part III Briana B. Morrison Adapted from Alan Eugenio & William J. Collins.
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.
1 Multiway trees & B trees & 2_4 trees Go&Ta Chap 10.
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.
CSC Analysis of Algorithms 3-1 CSC401 – Analysis of Algorithms Chapter 3 Search Trees and Skip Lists Objectives: Review binary search trees and present.
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.
2-3 Tree. Slide 2 Outline  Balanced Search Trees 2-3 Trees Trees.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
© 2004 Goodrich, Tamassia Binary Search Trees1 CSC 212 Lecture 18: Binary and AVL Trees.
Fall 2006 CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties.
CSC 213 – Large Scale Programming Lecture 21: Red-Black Trees.
© 2004 Goodrich, Tamassia Trees
CS 61B Data Structures and Programming Methodology Aug 7, 2008 David Sun.
3.1. Binary Search Trees   . Ordered Dictionaries Keys are assumed to come from a total order. Old operations: insert, delete, find, …
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.
Binary Search Trees (BST)
(2,4) Trees1 What are they? –They are search Trees (but not binary search trees) –They are also known as 2-4, trees.
1 Binary Search Trees   . 2 Ordered Dictionaries Keys are assumed to come from a total order. New operations: closestKeyBefore(k) closestElemBefore(k)
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
1 COMP9024: Data Structures and Algorithms Week Six: Search Trees Hui Wu Session 1, 2014
AA Trees.
COMP9024: Data Structures and Algorithms
Red-Black Trees v z Red-Black Trees Red-Black 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.
Red-Black Trees 5/22/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Binary Search Trees < > =
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Binary Search Tree Chapter 10.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Binary Search Trees.
(edited by Nadia Al-Ghreimil)
(2,4) Trees (2,4) Trees 1 (2,4) Trees (2,4) Trees
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Red-Black Trees v z /20/2018 7:59 AM Red-Black Trees
Red-Black Trees v z Red-Black Trees Red-Black 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.
Binary Search Trees < > =
Algorithms and Data Structures Lecture VIII
Red-Black Trees v z /17/2019 4:20 PM Red-Black 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
(2,4) Trees (2,4) Trees (2,4) Trees.
(2,4) Trees /6/ :26 AM (2,4) Trees (2,4) Trees
Binary Search Trees < > = Dictionaries
Red-Black Trees v z /6/ :10 PM Red-Black Trees
CS210- Lecture 20 July 19, 2005 Agenda Multiway Search Trees 2-4 Trees
Presentation transcript:

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é, fraternité Traverse up the tree & check for balance When a node’s children’s heights differ by 2 or more Each restructure uses node, taller child, tallest grandchild

Review of Last Lecture Splay Tree – Greed is Good Splay a node until it becomes the root Each splay operation involves the node, its parent, and its grandparent  Only exception is if a node’s parent is the root

Multi-Way Search Tree (§ 9.4.1) A multi-way search tree is an ordered tree Each internal node in a multi-way search tree: Has at least 2 children (can have more!) Has 1 fewer entries than it has children Keeps its entries and children in sorted order

Multi-Way Search Tree (§ 9.4.1) For a node with children v 1 v 2 … v d storing entries with keys k 1 k 2 … k d  1 Keys in the subtree of v 1 are smaller than k 1 Keys in the subtree of v i are between k i  1 and k i Keys in the subtree of v d are greater than k d 

Multi-Way Inorder Traversal Can traverse multi-way search trees in-order Alternate visiting child i then entry e i Like with a BST, inorder traversal visits keys in increasing order

Multi-Way Searching Similar to search in a BST At each with children v 1 v 2 … v d and entries e 1 e 2 … e d  1 for i = 1 to d – 1  if k  k i : return result of search in child v i  if k = k i : return e i Return result of search in child v d Throw exception if we reach an external node Example: find(30)

Multi-Way Searching Similar to search in a BST At each with children v 1 v 2 … v d and entries e 1 e 2 … e d  1 for i = 1 to d – 1  if k  k i : return result of search in child v i  if k = k i : return e i Return result of search in child v d Throw exception if we reach an external node Example: find(1)

(2,4) Trees (§ 9.4.2) (2,4) tree is multi-way search tree with 2 properties: Node-Size Property: internal nodes have at most 4 children Depth Property: all the external nodes have the same depth Nodes can be a 2-node, 3-node or 4-node

Height of a (2,4) Tree (2,4) tree of size n has height O(log n) In the largest tree, each node has 2 children But external nodes must be at same depth  This property automatically balances the tree! This worst case scenario: a balanced binary tree! 1 2 2h12h1 0 entries 0 1 h1h1 h depth

Insertion Begin with a search for the key k If k does not exist within the tree, add entry to last internal node we searched in… …thereby preserving the depth property Example: insert(30) v v

Insertion This insertion can cause an overflow! Node v became a 5-node This violates Node-Size property v v

Overflow and Split Handle overflow using split: Split node v into 2 nodes ( v' and v" )  A 3-node ( v’ ) with keys e 1 e 2 and children v 1 v 2 v 3  A 2-node ( v’’ ) with key e 4 and children v 4 v 5 Promote e 3 to parent node u (this may create new root) This may cause parent node ( u ) to overflow v u v1v1 v2v2 v3v3 v4v4 v5v v'v' u v1v1 v2v2 v3v3 v4v4 v5v5 35 v"v"

Overflow and Split Splitting a node when the parent also overflows is similar, but takes a little more work Example: insert(29)

Overflow and Split Example: insert(29)

Overflow and Split Example: insert(29)

Overflow and Split Example: insert(29)

Overflow and Split Example: insert(29)

Overflow and Split Example: insert(29) But this promotion could also cause an overflow…

Analysis of Insertion Algorithm insert(k, o) 1.Search for key k to find insert node v 2.Add the new entry (k, o) at node v 3. while overflow(v) if isRoot(v) create a new empty root above v split(v) v  parent(v) What is big-Oh time for: Step 1? Step 2? Checking overflow(v)? Calling isRoot(v)? Calling split(v)? Step 3 ? insert(k,o) big-Oh time?

Deletion Deletion from a leaf node is simple: Just remove the entry and the null child Example: delete(27)

Deletion If entry is not at a leaf node, replace it with inorder successor Go to right child and then go as far left as possible Example: delete(24)

Underflow and Fusion Deleting an entry may cause underflow Cause a node to be a 1-node (has one child, but no entries) This has two solutions, the solution depends on the situation Example: remove(15)

Fusion Case 1: v has adjacent siblings that is a 2-node Make new node ( v’ ) by merging node ( v ) with adjacent sibling ( w ) Steal entry from parent ( u ) that was between v & w  This may propagate underflow to the parent! Example: remove(15) u v u v'v'w

6 8 Transfer Case 2: v has adjacent sibling w that is 3- or 4-node Move entry in u that is between w & v into v Promote entry in w that is closest to v into u Make child of w that is next to v be a child of v No more underflows can exist Example: remove(10) u vw 2 u vw

Analysis of Deletion What is big-Oh time needed for deletion? Height of tree = O(log n) Time to find entry to delete Time needed for each fusion Maximum possible number of fusions Time needed for transfer Maximum possible number of transfers Total time needed for deletion

Your Turn Insert 1, 2, 3, 4, 5, 6, 7, 8 into a (2,4) tree Delete 3, 6, 5, 2, 8, 4, 1, 7 from your tree

Daily Quiz Write the Node class for a (2,4)-tree. What public methods should you define (does it make sense to include all getters and setters)? You will want to consider using other data structures in your Node.