Balanced Trees. 2-3 trees red-black trees B-trees. 2-3 trees red-black trees B-trees smaller than. 2-node. 3-node E J S X A C.

Similar documents
2-3 search trees red-black BSTs B-trees

implementation sequential search (unordered list) binary search (ordered array) BST N N N log N log N N compareto()

4.3 Balanced Trees. let us assume that we can manipulate them conveniently and see how they can be put together to form trees.

CS 241 Week 4 Tutorial Solutions

Algorithms. Algorithms 3.3 BALANCED SEARCH TREES. 2 3 search trees red black BSTs B-trees (see book or videos) ROBERT SEDGEWICK KEVIN WAYNE

Paradigm 5. Data Structure. Suffix trees. What is a suffix tree? Suffix tree. Simple applications. Simple applications. Algorithms

CSCI 104. Rafael Ferreira da Silva. Slides adapted from: Mark Redekopp and David Kempe

Algorithms. Algorithms 3.3 BALANCED SEARCH TREES. 2 3 search trees red black BSTs ROBERT SEDGEWICK KEVIN WAYNE.

CMPUT101 Introduction to Computing - Summer 2002

COMP 423 lecture 11 Jan. 28, 2008

Advanced Programming Handout 5. Enter Okasaki. Persistent vs. Ephemeral. Functional Queues. Simple Example. Persistent vs.

COMP108 Algorithmic Foundations

2 Computing all Intersections of a Set of Segments Line Segment Intersection

Pattern Matching. Pattern Matching. Pattern Matching. Review of Regular Expressions

Distance vector protocol

Presentation Martin Randers

Distributed Systems Principles and Paradigms. Chapter 11: Distributed File Systems

Greedy Algorithm. Algorithm Fall Semester

Containers: Queue and List

COMMON FRACTIONS. or a / b = a b. , a is called the numerator, and b is called the denominator.

Lecture 13: Graphs I: Breadth First Search

CS 340, Fall 2016 Sep 29th Exam 1 Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string.

Shared Memory Architectures. Programming and Synchronization. Today s Outline. Page 1. Message passing review Cosmic Cube discussion

Outline. Motivation Background ARCH. Experiment Additional usages for Input-Depth. Regular Expression Matching DPI over Compressed HTTP

Midterm Exam CSC October 2001

Introduction to Algebra

CS 551 Computer Graphics. Hidden Surface Elimination. Z-Buffering. Basic idea: Hidden Surface Removal

COSC 6374 Parallel Computation. Dense Matrix Operations

Internet Routing. IP Packet Format. IP Fragmentation & Reassembly. Principles of Internet Routing. Computer Networks 9/29/2014.

Can Pythagoras Swim?

ITEC2620 Introduction to Data Structures

Union-Find Problem. Using Arrays And Chains. A Set As A Tree. Result Of A Find Operation

Lesson 4.4. Euler Circuits and Paths. Explore This

Orthogonal line segment intersection

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Minimal Memory Abstractions

The Greedy Method. The Greedy Method

Parallelization Optimization of System-Level Specification

Chapter 4 Fuzzy Graph and Relation

Distributed Systems Principles and Paradigms

Error Numbers of the Standard Function Block

Lecture 12 : Topological Spaces

CS453 INTRODUCTION TO DATAFLOW ANALYSIS

Calculus Differentiation

10.5 Graphing Quadratic Functions

CS553 Lecture Introduction to Data-flow Analysis 1

GENG2140 Modelling and Computer Analysis for Engineers

1.5 Extrema and the Mean Value Theorem

Lecture 8: Graph-theoretic problems (again)

Solving Problems by Searching. CS 486/686: Introduction to Artificial Intelligence Winter 2016

Tries. Yufei Tao KAIST. April 9, Y. Tao, April 9, 2013 Tries

Duality in linear interval equations

Problem Final Exam Set 2 Solutions

6.3 Volumes. Just as area is always positive, so is volume and our attitudes towards finding it.

Systems I. Logic Design I. Topics Digital logic Logic gates Simple combinational logic circuits

In the last lecture, we discussed how valid tokens may be specified by regular expressions.

What do all those bits mean now? Number Systems and Arithmetic. Introduction to Binary Numbers. Questions About Numbers

COMPUTER EDUCATION TECHNIQUES, INC. (WEBLOGIC_SVR_ADM ) SA:

UNIT 11. Query Optimization

Suffix trees, suffix arrays, BWT

Questions About Numbers. Number Systems and Arithmetic. Introduction to Binary Numbers. Negative Numbers?

COMBINATORIAL PATTERN MATCHING

Algorithm Design (5) Text Search

Type Checking. Roadmap (Where are we?) Last lecture Context-sensitive analysis. This lecture Type checking. Symbol tables

A Tautology Checker loosely related to Stålmarck s Algorithm by Martin Richards

CS201 Discussion 10 DRAWTREE + TRIES

cisc1110 fall 2010 lecture VI.2 call by value function parameters another call by value example:

FEEDBACK: The standard error of a regression is not an unbiased estimator for the standard deviation of the error in a multiple regression model.

AI Adjacent Fields. This slide deck courtesy of Dan Klein at UC Berkeley

Class Overview. Database Design. Database Design Process. Database Design. Introduction to Data Management CSE 414

P(r)dr = probability of generating a random number in the interval dr near r. For this probability idea to make sense we must have

Lexical Analysis. Amitabha Sanyal. ( as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

What are suffix trees?

Computational geometry

String comparison by transposition networks

Allocator Basics. Dynamic Memory Allocation in the Heap (malloc and free) Allocator Goals: malloc/free. Internal Fragmentation

Width and Bounding Box of Imprecise Points

1.1. Interval Notation and Set Notation Essential Question When is it convenient to use set-builder notation to represent a set of numbers?

COSC 6374 Parallel Computation. Non-blocking Collective Operations. Edgar Gabriel Fall Overview

From Dependencies to Evaluation Strategies

10.2 Graph Terminology and Special Types of Graphs

Enterprise Digital Signage Create a New Sign

Suffix Tries. Slides adapted from the course by Ben Langmead

Solving Problems by Searching. CS 486/686: Introduction to Artificial Intelligence

V = set of vertices (vertex / node) E = set of edges (v, w) (v, w in V)

Inter-domain Routing

Today. CS 188: Artificial Intelligence Fall Recap: Search. Example: Pancake Problem. Example: Pancake Problem. General Tree Search.

box Boxes and Arrows 3 true 7.59 'X' An object is drawn as a box that contains its data members, for example:

Introduction to Computer Engineering EECS 203 dickrp/eecs203/ CMOS transmission gate (TG) TG example

Lecture 10 Evolutionary Computation: Evolution strategies and genetic programming

Before We Begin. Introduction to Spatial Domain Filtering. Introduction to Digital Image Processing. Overview (1): Administrative Details (1):

PARALLEL AND DISTRIBUTED COMPUTING

Asurveyofpractical algorithms for suffix tree construction in external memory

McAfee Web Gateway

Functor (1A) Young Won Lim 8/2/17

SMALL SIZE EDGE-FED SIERPINSKI CARPET MICROSTRIP PATCH ANTENNAS

Network Layer: Routing Classifications; Shortest Path Routing

c s ha2 c s Half Adder Figure 2: Full Adder Block Diagram

EXPONENTIAL & POWER GRAPHS

Introduction. Example

Transcription:

ymol tle review Blned Trees implementtion gurntee verge se serh insert delete serh hit insert delete ordered itertion? opertions on keys sequentil serh (linked list) N N N N/2 N N/2 no equls() 2-3 trees red-lk trees B-trees inry serh (ordered rry) lg N N N lg N N/2 N/2 yes ompreto() BT N N N 1.39 lg N 1.39 lg N? yes ompreto() Gol log N log N log N log N log N log N yes ompreto() eferenes: ndout on red-lk trees http://www.s.prineton.edu/lgs4/43lned lgorithms in Jv, 4 th dition oert edgewik nd Kevin Wyne opyright 2008 Otoer 13, 2008 10:17:08 hllenge. Gurntee performne. This leture. 2-3 trees, left-lening red-lk trees, B-trees. introdued to the world in O 226, Fll 2007 (see hndout) 2 2-3 tree llow 1 or 2 keys per node. 2-node: one key, two hildren. 3-node: two keys, three hildren. ymmetri order. Inorder trversl yields keys in sending order. erfet lne. very pth from root to null link hs sme length. 2-3 trees red-lk trees B-trees smller thn 3-node J lrger thn J 2-node nd J null link 3 4

erh in 2-3 tree Insertion in 2-3 tree ompre serh key ginst keys in node. Find intervl ontining serh key. Follow ssoited link (reursively). se 1. Insert into 2-node t ottom. erh for key, s usul. eple 2-node with 3-node. suessful serh for unsuessful serh for B is thn so look to the left B is thn so look to the left inserting K J J J found so return vlue (serh hit) is nd so look in the middle J B is thn so look to the left B is nd so look in the middle link is null so B is not in the tree (serh miss) J J J J K serh for K ends here reple 2-node with new 3-node ontining K Insert into 2-node uessful nd unsuessful serh in 2-3 tree 5 6 Insertion in 2-3 tree Insertion in 2-3 tree se 2. Insert into 3-node t ottom. dd new key to 3-node to rete temporry 4-node. ove middle key in 4-node into prent. why middle key? inserting Z se 2. Insert into 3-node t ottom. dd new key to 3-node to rete temporry 4-node. ove middle key in 4-node into prent. epet up the tree, s neessry. J J J Z serh for Z ends t this 3-node reple 3-node with temporry 4-node ontining Z reple 2-node with new 3-node ontining middle key Z inserting D serh for D ends t this 3-node dd new key D to 3-node to mke temporry 4-node J D J dd middle key to 3-node to mke temporry 4-node J D split 4-node into two 2-nodes pss middle key to prent dd middle key to 2-node to mke new 3-node D J split 4-node into two 2-nodes pss middle key to prent split 4-node into two 2-nodes pss middle key to prent Insert into 3-node whose prent is 3-node 7 8

Insertion in 2-3 tree 2-3 tree onstrution tre se 2. Insert into 3-node t ottom. dd new key to 3-node to rete temporry 4-node. ove middle key in 4-node into prent. epet up the tree, s neessry. If you reh the root nd it's 4-node, split it into three 2-nodes. tndrd indexing lient. insert inserting D serh for D ends t this 3-node D J dd new key D to 3-node to mke temporry 4-node J dd middle key to 3-node to mke temporry 4-node split 4-node into two 2-nodes pss middle key to prent split 4-node into three 2-nodes inresing tree height y 1 J D J D insert insert emrk. plitting the root inreses height y 1. 9 10 2-3 tree onstrution tre The sme keys inserted in sending order. ol trnsformtions in 2-3 tree plitting 4-node is lol trnsformtion: onstnt numer of steps. insert e d thn thn nd nd nd nd e nd d nd d d nd e plitting 4-node is lol trnsformtion d d nd e greter thn e greter thn e stndrd indexing lient sme keys in inresing order 2-3 tree onstrution tres 11 12

Glol properties in 2-3 tree 2-3 tree: performne Invrint. ymmetri order. Invrint. erfet lne. erfet lne. very pth from root to null link hs sme length. f. h trnsformtion mintins order nd lne. left right d d prent is 2-node d d prent is 3-node left d e d e middle e d e d Tree height. Worst se: Best se: splitting the root right d e d e 13 14 2-3 tree: performne T implementtions: summry erfet lne. very pth from root to null link hs sme length. implementtion gurntee verge se serh insert delete serh hit insert delete ordered itertion? opertions on keys sequentil serh (linked list) N N N N/2 N N/2 no equls() Tree height. Worst se: lg N. [ll 2-nodes] Best se: log 3 N.631 lg N. [ll 3-nodes] Between 12 nd 20 for million nodes. Between 18 nd 30 for illion nodes. Gurnteed logrithmi performne for serh nd insert. inry serh (ordered rry) lg N N N lg N N/2 N/2 yes ompreto() BT N N N 1.39 lg N 1.39 lg N? yes ompreto() 2-3 tree lg N lg N lg N lg N lg N lg N yes ompreto() onstnts depend upon implementtion 15 16

2-3 tree: implementtion? Diret implementtion is omplited, euse: intining multiple node types is umersome. Need multiple ompres to move down tree. Need to move k up the tree to split 4-nodes. rge numer of ses for splitting. Bottom line. ould do it, ut there's etter wy. 2-3-4 trees red-lk trees B-trees 17 18 eft-lening red-lk trees (Guis-edgewik 1979 nd edgewik 2007) n equivlent definition 1. epresent 2 3 tree s BT. 2. Use "internl" left-lening links s "glue" for 3 nodes. 3-node thn nd greter thn thn nd noding 3-node with two 2-nodes greter thn lrger key is root BT suh tht: No node hs two red links onneted to it. very pth from root to null link hs the sme numer of lk links. ed links len left. "perfet lk lne" Key property. 1 1 orrespondene 2 3 nd B. 2-3 tree red lk tree J J lk links onnet 2-nodes nd 3-nodes red lk tree J red links "glue" nodes within 3-node 19 20

erh implementtion for red-lk trees ed-lk tree representtion Oservtion. erh is the sme s for elementry BT (ignore olor). ut runs fster euse of etter lne h node is pointed to y preisely one link (from its prent) n enode olor of links in nodes. puli Vl get(key key) Node x = root; while (x!= null) int mp = key.ompreto(x.key); if (mp < 0) x = x.left; else if (mp > 0) x = x.right; else if (mp == 0) return x.vl; return null; red lk tree J privte stti finl oolen D = true; privte stti finl oolen BK = flse; privte lss Node Key key; Vlue vl; Node left, right; oolen olor; privte oolen ised(node x) if (x == null) return flse; return x.olor == D; h.left.olor is D D h G J h.right.olor is BK null links re lk emrk. ny other ops (e.g., eiling, seletion, itertion) re lso identil. 21 22 lementry red-lk tree opertions lementry red-lk tree opertions eft rottion. Orient (temporrily) right-lening red link to len left. ight rottion. Orient left-lening red link to (temporrily) len right. h thn ould e right or left, red or lk nd Node rotteeft(node h) x greter thn return x; h thn nd eft rotte (right link of h) x greter thn x thn nd h greter thn x thn nd h greter thn privte Node rotteeft(node h) x = h.right; h.right = x.left; x.left = h; x.olor = h.olor; h.olor = D; return x; privte Node rotteight(node h) Node x = h.left; h.left = x.right; x.right = h; x.olor = h.olor; h.olor = D; return x; Invrints. intins symmetri order nd perfet lk lne. Invrints. intins symmetri order nd perfet lk lne. 23 24

lementry red-lk tree opertions lementry red-lk tree opertions: exmples olor flip. eolor to split (temporry) 4-node. h ould e left or right link lk links split to 2-nodes red link tthes middle node to prent n red ors flip olors ed ht thn nd nd greter thn thn nd nd greter thn privte void flipolors(node h) h.olor = D; h.left.olor = BK; h.right.olor = BK; rotte left rotte right Invrints. intins symmetri order nd perfet lk lne. 25 26 Insertion in B tree: overview Insertion in B tree Bsi strtegy. intin 1-1 orrespondene with 2-3 trees y pplying elementry red-lk tree opertions. Wrmup 1. Insert into tree with extly 1 node. insert dd new node here rotte left left serh ends t this null link root root red link to new node ontining onverts 2-node to 3-node right root serh ends t this null link tthed new node with red link root rotted left to mke legl 3-node B tree 2-3 tree 27 28

Insertion in B tree Insertion in B tree Wrmup 2. Insert into tree with extly 2 nodes. ed lrger serh ends t this null link tthed new node with red link olors flipped to lk smller serh ends t this null link tthed new node with red link rotted right olors flipped to lk d serh ends t this null link tthed new node with red link rotted left rotted right l olors flipped to lk se 1. Insert into 2-node t the ottom. Do stndrd BT insert; olor new link red. If new red link is right link, rotte left. insert dd new node here right link red so rotte left 29 30 Insertion in B tree Insertion in B tree se 2. Insert into 3-node t the ottom. Do stndrd BT insert; olor new link red. otte to lne the 4-node (if needed). Flip olors to pss red link up one level. otte to mke len left (if needed). insert dd new node here two lefts in row so rotte right oth hildren red right link red so rotte left oth hildren red so flip olors se 2. Insert into 3-node t the ottom. Do stndrd BT insert; olor new link red. otte to lne the 4-node (if needed). Flip olors to pss red link up one level. otte to mke len left (if needed). epet se 1 or se 2 up the tree (if needed). insert oth hildren red so flip olors right link red so rotte right dd new node here two lefts in row so rotte right oth hildren red so flip olors 31 two lefts in row so rotte right 32

B tree onstrution tre B tree onstrution tre tndrd indexing lient. tndrd indexing lient. insert insert red lk tree 2-3 tree stndrd indexing lient sme keys in inresing order red-lk tree onstrution tres red lk tree 2-3 tree 33 34 Insertion in B tree: Jv implementtion Insertion in B tree: visuliztion me ode for oth ses. If the right hild is red nd the left hild is not red, rotte left. If oth the left hild nd its left hild re red, rotte right. If oth hildren re red, flip olors. privte Node put(node h, Key key, Vlue vl) if (h == null) return new Node(key, vl, D); int mp = key.ompreto(h.key); if (mp < 0) h.left = put(h.left, key, vl); else if (mp > 0) h.right = put(h.right, key, vl); else h.vl = vl; insert t ottom if (ised(h.right) &&!ised(h.left)) h = rotteeft(h); if (ised(h.left) && ised(h.left.left)) h = rotteight(h); if (ised(h.left) && ised(h.right)) h = flipolors(h); len left lne 4-node split 4-node 255 insertions in sending order return h; only few extr lines of ode provide ner-perfet lne 35 36

Insertion in B tree: visuliztion Insertion in B tree: visuliztion 255 insertions in desending order 50 rndom insertions 37 38 Insertion in B tree: visuliztion Blne in B trees roposition. eight of tree is 2 lg N in the worst se. f. very pth from root to null link hs sme numer of lk links. Never two red links in--row. 255 rndom insertions roperty. eight of tree is ~ 1.00 lg N in typil pplitions. 39 40

T implementtions: summry Why left-lening trees? implementtion sequentil serh (linked list) inry serh (ordered rry) gurntee verge se serh insert delete serh hit insert delete ordered itertion? opertions on keys N N N N/2 N N/2 no equls() lg N N N lg N N/2 N/2 yes ompreto() BT N N N 1.39 lg N 1.39 lg N? yes ompreto() 2-3 tree lg N lg N lg N lg N lg N lg N yes ompreto() red-lk tree 2 lg N 2 lg N 2 lg N 1.00 lg N 1.00 lg N 1.00 lg N yes ompreto() ext vlue of oeffiient unknown ut extremely lose to 1 old ode (tht students hd to lern in the pst) privte Node put(node x, Key key, Vlue vl, oolen sw) if (x == null) return new Node(key, vlue, D); int mp = key.ompreto(x.key); if (ised(x.left) && ised(x.right)) x.olor = D; x.left.olor = BK; x.right.olor = BK; if (mp < 0) x.left = put(x.left, key, vl, flse); if (ised(x) && ised(x.left) && sw) x = rotteight(x); if (ised(x.left) && ised(x.left.left)) x = rotteight(x); x.olor = BK; x.right.olor = D; else if (mp > 0) x.right = put(x.right, key, vl, true); if (ised(h) && ised(x.right) &&!sw) x = rotteeft(x); if (ised(h.right) && ised(h.right.right)) x = rotteeft(x); x.olor = BK; x.left.olor = D; else x.vl = vl; return x; new ode (tht you hve to lern) puli Node put(node h, Key key, Vlue vl) if (h == null) return new Node(key, vl, D); int mp = kery.ompreto(h.key); if (mp < 0) h.left = put(h.left, key, vl); else if (mp > 0) h.right = put(h.right, key, vl); else h.vl = vl; if (ised(h.right) &&!ised(h.left)) h = rotteeft(h); if (ised(h.left) && ised(h.left.left)) h = rotteight(h); if (ised(h.left) && ised(h.right)) h = flipolors(h); return h; extremely triky strightforwrd (if you ve pid ttention) 41 42 Why left-lening trees? implified ode. eft-lening restrition redues numer of ses. hort inner loop. me ides simplify implementtion of other opertions. Delete min/mx. ritrry delete. Improves widely-used lgorithms. V trees, 2-3 trees, 2-3-4 trees. ed-lk trees. new 2008 1978 1972 2-3-4 trees red-lk trees B-trees Bottom line. eft-lening red-lk trees re the simplest lned BT to implement nd the fstest in prtie. 43 44

File system model B-trees (Byer-reight, 1972) ge. ontiguous lok of dt (e.g., file or 4096-yte hunk). roe. First ess to pge (e.g., from disk to memory). B-tree. Generlize 2-3 trees y llowing up to links per node. t lest 1 entry t root. t lest /2 links in other nodes. xternl nodes ontin lient keys. Internl nodes ontin opies of keys to guide serh. hoose s lrge s possile so tht links fit in pge, e.g., = 1000 slow fst * K 2-node odel. Time required for proe is muh lrger thn time to essdt within pge. Gol. ess dt using minimum numer of proes. * B sentinel key * D 5-node (full) D F I J K N O Q T lient keys (lk) re in externl nodes eh red key is opy of min key in sutree ntomy of B-tree set ( = 5) K Q U internl node U W externl node ll nodes exept the root re 3-, 4- or 5-nodes 45 46 erhing in B-tree Insertion in B-tree trt t root. Find intervl for serh key nd tke orresponding link. erh termintes in externl node. erh for new key. Insert t ottom. plit (+1)-nodes on the wy up the tree. inserting * K Q U * B F I J K N O Q T U W serhing for * B follow this link euse is * nd K serh for in this externl node * D * K follow this link euse is D nd K Q U D F I J K N O Q T U W * B F I J K N O Q T U W new key () uses overflow nd split * * K Q U * K Q U * B F I J K N O Q T U W * K root split uses new root to e reted new key () uses overflow nd split K Q U * B F I J K N O Q T U W 47 48

Blne in B-tree Blned trees in the wild roes. serh or insert in B-tree of order with N items requires log N nd log /2N proes. f. ll internl nodes (esides root) hve /2 nd links. ed-lk trees re widely used s system symol tles. Jv: jv.util.treep, jv.util.treeet. ++ T: mp, multimp, multiset. inux kernel: ompletely fir sheduler, linux/rtree.h. In prtie. Numer of proes is t most 4! Optimiztion. lwys keep root pge in memory. = 1000; N = 62 illion log /2 N 4 B-tree vrints. B+ tree, B*tree, B# tree, B-trees (nd vrints) re widely used for file systems nd dtses. Windows: F. : F, F+. inux: eiserf, F, xt3f, JF. Dtses: O, DB2, ING, Q, ostgreq. 49 50 ed-lk trees in the wild ed-lk trees in the wild ommon sense. ixth sense. Together they're the FBI's newest tem. 51 52