CSE Lecture 17 – Balanced trees

Slides:



Advertisements
Similar presentations
Numbers Treasure Hunt Following each question, click on the answer. If correct, the next page will load with a graphic first – these can be used to check.
Advertisements

Simplifications of Context-Free Grammars
Mathematical Preliminaries
COSC 2007 Data Structures II Chapter 12 Advanced Implementation of Tables II.
1
© 2008 Pearson Addison Wesley. All rights reserved Chapter Seven Costs.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
Properties Use, share, or modify this drill on mathematic properties. There is too much material for a single class, so you’ll have to select for your.
UNITED NATIONS Shipment Details Report – January 2006.
© 2001 by Charles E. Leiserson Introduction to AlgorithmsDay 18 L10.1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 10 Prof. Erik Demaine.
1 RA I Sub-Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Casablanca, Morocco, 20 – 22 December 2005 Status of observing programmes in RA I.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Properties of Real Numbers CommutativeAssociativeDistributive Identity + × Inverse + ×
Exit a Customer Chapter 8. Exit a Customer 8-2 Objectives Perform exit summary process consisting of the following steps: Review service records Close.
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt BlendsDigraphsShort.
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Year 6 mental test 10 second questions
REVIEW: Arthropod ID. 1. Name the subphylum. 2. Name the subphylum. 3. Name the order.
CS16: Introduction to Data Structures & Algorithms
Binary Tree Structure a b fe c a rightleft g g NIL c ef b left right pp p pp left key.
Break Time Remaining 10:00.
Turing Machines.
Table 12.1: Cash Flows to a Cash and Carry Trading Strategy.
PP Test Review Sections 6-1 to 6-6
Abstract Data Types and Algorithms
Data structure is concerned with the various ways that data files can be organized and assembled. The structures of data files will strongly influence.
1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.
EU market situation for eggs and poultry Management Committee 20 October 2011.
Bellwork Do the following problem on a ½ sheet of paper and turn in.
Exarte Bezoek aan de Mediacampus Bachelor in de grafische en digitale media April 2014.
CSE 373 Data Structures and Algorithms
David Luebke 1 8/25/2014 CS 332: Algorithms Red-Black Trees.
Definitions and Bottom-Up Insertion
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
1 RA III - Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Buenos Aires, Argentina, 25 – 27 October 2006 Status of observing programmes in RA.
Factor P 16 8(8-5ab) 4(d² + 4) 3rs(2r – s) 15cd(1 + 2cd) 8(4a² + 3b²)
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
CSE 4101/5101 Prof. Andy Mirzaian B-trees trees.
CONTROL VISION Set-up. Step 1 Step 2 Step 3 Step 5 Step 4.
© 2012 National Heart Foundation of Australia. Slide 2.
Adding Up In Chunks.
LO: Count up to 100 objects by grouping them and counting in 5s 10s and 2s. Mrs Criddle: Westfield Middle School.
Model and Relationships 6 M 1 M M M M M M M M M M M M M M M M
25 seconds left…...
Subtraction: Adding UP
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
10 -1 Chapter 10 Amortized Analysis A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)
Analyzing Genes and Genomes
Splay Trees Binary search trees.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Essential Cell Biology
Converting a Fraction to %
Rizwan Rehman Centre for Computer Studies Dibrugarh University
Clock will move after 1 minute
Intracellular Compartments and Transport
PSSA Preparation.
Essential Cell Biology
Foundations of Data Structures Practical Session #7 AVL Trees 2.
Immunobiology: The Immune System in Health & Disease Sixth Edition
Physics for Scientists & Engineers, 3rd Edition
Energy Generation in Mitochondria and Chlorplasts
Select a time to count down from the clock above
COL 106 Shweta Agrawal, Amit Kumar
Trees and Red-Black Trees Gordon College Prof. Brinton.
Advanced Associative Structures
Presentation transcript:

CSE 30331 Lecture 17 – Balanced trees Unbalanced binary search trees Balanced search trees 2-3-4 trees Red-black trees

Unbalanced Binary Search Tree and balanced alternatives

Two Binary Search Tree Examples Insertion sequence for tree (a): 5, 15, 20, 3, 9, 7, 12, 17, 6, 75, 100, 18, 25, 35, 40 Depth = 6 Depth = 4 Average comparisons per search = 4.0 Average comparisons per search = 3.47 5 9 3 15 5 20 9 20 3 7 15 75 7 12 17 75 6 12 17 35 100 6 18 25 100 18 25 40 (a) unbalanced 35 (b) Same data, but more balanced 40

What is a 2-3-4 Tree? Each node has 1, 2 or 3 values Each node has 2, 3 or 4 children The tree is ALWAYS perfectly balanced Depth of all subtrees of a node are equal

2-3-4 Tree Example

2-3-4 Tree (node types) A 2-node (value < A) (value > A) A B C (A < value < B) (value > C) (B < value < C) A B 3-node A < B (value < A) (A < value < B) (value > B)

2-3-4 Tree Insertion Splits 4-nodes top-down Inserting 7 requires splitting of the 4-node before adding 7 as a child (leaf or part of existing leaf) After splitting, 7 is added to a leaf following normal search tree order 9 3 6 2 10 7 8 4 3 6 9 2 4 10 8

2-3-4 Tree Insertion Details 4-nodes must be split on the way down Split involves “promotion” of middle value into parent node New value is always added to existing 2-node or 3-node LEAF 2-3-4 tree is always perfectly balanced Implementation is wasteful of space Each node must have space for 3 values, 1 parent pointer and 4 child pointers, even if not always used There is always an equivalent red-black binary tree

Building 2-3-4 Tree by Series of Insertions Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7 To insert 4, first split the root (a 4-node) and then add value to leaf

Building 2-3-4 Tree by Series of Insertions Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7 Insert 8 To insert 10, first split the root (a 4-node) and then add value to leaf

Building 2-3-4 Tree by Series of Insertions Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7 2 4 12 8 10 15 25 Insert 25 15 25 35 Insert 35 2 4 12 25 8 10 15 35 35 55 Insert 55 Split 4-node (15, 25, 35) To insert 55, first split the leaf (a 4-node) and then add value to leaf

Building 2-3-4 Tree by Series of Insertions Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7 To insert 11, again requires top-down split of each 4-node encountered, so root is split and new value is inserted in leaf 2 8 10 11 15 35 55 25 4 12 Insert 11 2 8 10 15 35 55 25 4 12 Split 4-node (4, 12, 25)

Building 2-3-4 Tree by Series of Insertions Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7 To insert 9, split the leaf (a 4-node) and insert new value 2 15 35 55 25 12 4 10 11 8 9 Insert 9 2 15 35 55 25 12 4 10 11 8 Split 4-node (8, 10, 11)

Building 2-3-4 Tree by Series of Insertions Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7 2 15 35 55 25 12 4 10 11 5 8 9 Insert 5 2 15 35 55 25 12 4 8 10 11 9 5 7 2 15 35 55 25 12 4 8 10 11 9 5 Split 4-node (5, 8, 9) And finally insert 7 in a leaf

Inefficiencies 2-3-4 trees require space for up to ... 3 values in every node Pointers to 4 children from every node Even if they are not used in all cases So, we can use a Red Black tree instead A (colored) form of binary search tree Always relatively balanced Nodes have no more storage required than normal binary search tree

Red Black Tree Properties The root is always BLACK A RED parent never has a RED child Every path from ROOT to an EMPTY subtree has the same black height (# of BLACK nodes) Black Height of tree is O(log2n)

Red Black Tree Node Types BLACK parent with two RED children is the equivalent of a 4-node A 4-node split is accomplished by flipping the colors to RED parent & two BLACK children, with possible rotation if colors conflict BLACK parent with single RED child is equivalent of a 3-node New nodes are always inserted a RED leaves

Red-Black subtree equivalents of 2-3-4 tree nodes

Converting a 2-3-4 Tree to Red-Black Tree (top-down) Original First, convert the root (note: shaded nodes are RED)

Converting a 2-3-4 Tree to Red-Black Tree (top-down) Next, convert left subtree Then convert the right subtree and we’re done

Building red black tree by repeated insertions Insert root and color it BLACK Each successive insertion point is found in normal search tree order Any 4-nodes (black parent with two red children) is split going down into tree Requires color changes and possible rotations of subtrees New value is added as RED leaf If result is RED child of RED parent, then rotations are required to correct

Situations in the Splitting of a 4-Node (A X B)

Required actions to split a 4-node (A X B) with parent P & grandparent G 4-node is either child of black parent Flip subtree colors ONLY (X red, A & B black) 4-node is child of red parent (left-left) Flip subtree colors, single right rotation about P & recolor 4-node is child of red parent (right-right) Flip subtree colors, single left rotation about P & recolor 4-node is child of red parent (left-right) Flip subtree colors, then double rotation (first left, then right) about X & then color X black, G & P red 4-node is child of red parent (right-left) Flip subtree colors, then double rotation (first right, then left) about X & then color X black, G & P red

Left child of a Black parent P: Split requires only a Color Flip

Example split prior to inserting 55: flip colors (40,50,60) & insert leaf

Required actions to split a 4-node (A X B) with parent P & grandparent G 4-node is either child of black parent Flip subtree colors ONLY (X red, A & B black) 4-node is child of red parent (left-left) Flip subtree colors, single right rotation about P & recolor 4-node is child of red parent (right-right) Flip subtree colors, single left rotation about P & recolor 4-node is child of red parent (left-right) Flip subtree colors, then double rotation (first left, then right) about X & then color X black, G & P red 4-node is child of red parent (right-left) Flip subtree colors, then double rotation (first right, then left) about X & then color X black, G & P red

RED parent, 4-node oriented left-left from G (grandparent)

Required actions to split a 4-node (A X B) with parent P & grandparent G 4-node is either child of black parent Flip subtree colors ONLY (X red, A & B black) 4-node is child of red parent (left-left) Flip subtree colors, single right rotation about P & recolor 4-node is child of red parent (right-right) Flip subtree colors, single left rotation about P & recolor 4-node is child of red parent (left-right) Flip subtree colors, then double rotation (first left, then right) about X & then color X black, G & P red 4-node is child of red parent (right-left) Flip subtree colors, then double rotation (first right, then left) about X & then color X black, G & P red

Red Parent : Oriented Left-Right From G

Red Parent : Oriented Left-Right From G B A X G P C D Red-black tree after color flip B A X G P C D Red-black tree after first (left) rotation (about X)

Red Parent : Oriented Left-Right From G B A X G P C D Red-black tree after first (left) rotation (about X) After second (right) rotation about X and coloring X black, G & P red B A G X P C D

Building A Red-Black Tree Insertions: 2, 15, 12, 4, 8, 10, 25, 35

Building A Red-Black Tree Insertions: 2, 15, 12, 4, 8, 10, 25, 35

Example on blackboard Insert: 2, 12, 15, 7, 5, 8, 10

Deleting nodes from a Red-Black Tree If node has 2 children Swap value with that of inorder successor and Delete the successor node (it has at most 1 child) If node has 1 or 0 children If it is RED Delete it and replace with its BLACK child If it is BLACK and its child is RED Delete it, replace with its RED child and recolor it BLACK If it is BLACK and its child is BLACK Yikes!!!!

Deleting nodes from a Red-Black Tree If node has 1 or 0 children If it is BLACK and its child is BLACK Delete the node and replace with its child BUT ... Now we have a situation. We have removed a BLACK node from some paths and thus the BLACK height may differ in this part of the tree from others If the node deleted was the root, we are done All paths are shorter by 1 black node If not the root, then there are 5 other cases These involve bottom-up rebalancing of the tree using recoloring and various rotations See http://en.wikipedia.org/wiki/Red-black_tree

Representing a Red-Black Tree The rbNode class from Ford & Topp

Summary 2-3-4 tree Each node has either … 1 value and 2 children 2 values and 3 children 3 values and 4 children Construction of 2-3-4 trees is complex, so we build an equivalent binary tree known as a red-black tree