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

Similar documents
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.

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

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

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

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

CS 241 Week 4 Tutorial Solutions

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

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

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

COMP108 Algorithmic Foundations

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

COMP 423 lecture 11 Jan. 28, 2008

Lecture 13: Graphs I: Breadth First Search

CMPUT101 Introduction to Computing - Summer 2002

Problem Final Exam Set 2 Solutions

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

Parallelization Optimization of System-Level Specification

Presentation Martin Randers

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

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

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

Lecture 8: Graph-theoretic problems (again)

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

CS553 Lecture Introduction to Data-flow Analysis 1

Error Numbers of the Standard Function Block

Distance vector protocol

Orthogonal line segment intersection

Midterm Exam CSC October 2001

Algorithm Design (5) Text Search

COSC 6374 Parallel Computation. Dense Matrix Operations

Greedy Algorithm. Algorithm Fall Semester

Computational geometry

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

INTEGRATED WORKFLOW ART DIRECTOR

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

EXPONENTIAL & POWER GRAPHS

Calculus Differentiation

CS453 INTRODUCTION TO DATAFLOW ANALYSIS

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

ITEC2620 Introduction to Data Structures

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

Minimal Memory Abstractions

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

Lesson 4.4. Euler Circuits and Paths. Explore This

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

Can Pythagoras Swim?

Algorithms. Algorithms 3.2 BINARY SEARCH TREES. BSTs ordered operations iteration deletion (see book or videos) ROBERT SEDGEWICK KEVIN WAYNE

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

3.2 BINARY SEARCH TREES. BSTs ordered operations iteration deletion. Algorithms ROBERT SEDGEWICK KEVIN WAYNE.

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

Deletion The Two Child Case 10 Delete(5) Deletion The Two Child Case. Balanced BST. Finally

UTMC APPLICATION NOTE UT1553B BCRT TO INTERFACE PSEUDO-DUAL-PORT RAM ARCHITECTURE INTRODUCTION ARBITRATION DETAILS DESIGN SELECTIONS

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

FASTEST METHOD TO FIND ALTERNATIVE RE-ROUTE

Containers: Queue and List

McAfee Web Gateway

Troubleshooting. Verify the Cisco Prime Collaboration Provisioning Installation (for Advanced or Standard Mode), page

Introduction to Algebra

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

The Greedy Method. The Greedy Method

Duality in linear interval equations

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

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

Distributed Systems Principles and Paradigms

Width and Bounding Box of Imprecise Points

Algorithms. Algorithms 3.2 BINARY SEARCH TREES. BSTs ordered operations deletion ROBERT SEDGEWICK KEVIN WAYNE.

Hyperbolas. Definition of Hyperbola

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

LINX MATRIX SWITCHERS FIRMWARE UPDATE INSTRUCTIONS FIRMWARE VERSION

10.2 Graph Terminology and Special Types of Graphs

Final Exam Review F 06 M 236 Be sure to look over all of your tests, as well as over the activities you did in the activity book

PARALLEL AND DISTRIBUTED COMPUTING

COMPUTER EDUCATION TECHNIQUES, INC. (WEBLOGIC_SVR_ADM ) SA:

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

EECS 281: Homework #4 Due: Thursday, October 7, 2004

10.5 Graphing Quadratic Functions

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

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

Enterprise Digital Signage Create a New Sign

Convex Hull Algorithms. Convex hull: basic facts

Chapter 4 Fuzzy Graph and Relation

Tiling Triangular Meshes

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

Ma/CS 6b Class 1: Graph Recap

From Dependencies to Evaluation Strategies

Inter-domain Routing

Grade 7/8 Math Circles Geometric Arithmetic October 31, 2012

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

Towards Unifying Advances in Twig Join Algorithms

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

Outline. CS38 Introduction to Algorithms. Graphs. Graphs. Graphs. Graph traversals

GENG2140 Modelling and Computer Analysis for Engineers

What are suffix trees?

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

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

Suffix trees, suffix arrays, BWT

Introduction. Example

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

Lecture 14: Binary Search Trees (2)

Graphing Conic Sections

Transcription:

OBT DGWIK KVIN WYN lgorithms OBT DGWIK KVIN WYN ymol tle review lgorithms F O U T D I T I O N http://lgs4.s.prineton.edu 3.3 BND T 2 3 serh trees red lk BTs B-trees implementtion sequentil serh (unordered list) inry serh (ordered rry) gurntee serh insert delete hllenge. Gurntee performne. serh verge se This leture. 2 3 trees, left-lening red lk BTs, B-trees. hit insert delete ordered ops? key interfe N N N N N N equls() log N N N log N N N ompreto() BT N N N log N log N N ompreto() gol log N log N log N log N log N log N ompreto() st updted on 2/28/16 9:33 2 2 3 tree lgorithms 3.3 BND T 2 3 serh trees red lk BTs B-trees ymmetri order. Inorder trversl yields keys in sending order. erfet lne. very pth from root to null link hs sme length. llow 1 or 2 keys per node. 2-node: one key, two hildren. 3-node: two keys, three hildren. smller thn 3-node J lrger thn J 2-node OBT DGWIK KVIN WYN http://lgs4.s.prineton.edu etween nd J null link 4

2 3 tree demo 2 3 tree demo: insertion erh. ompre serh key ginst keys in node. Find intervl ontining serh key. Follow ssoited link (reursively). Insert into 2-node t ottom. erh for key, s usul. eple 2-node with 3-node. serh for insert K K is less thn (go left) K J J 5 2 3 tree demo: insertion Insert into 2-node t ottom. erh for key, s usul. eple 2-node with 3-node. 2 3 tree demo: insertion Insert into 2-node t ottom. erh for key, s usul. eple 2-node with 3-node. insert K insert K K is greter thn J (go right) K J J K serh ends here

2 3 tree demo: insertion 2 3 tree demo: insertion Insert into 2-node t ottom. erh for key, s usul. eple 2-node with 3-node. Insert into 2-node t ottom. erh for key, s usul. eple 2-node with 3-node. insert K insert K J J K K reple 2-node with 3-node ontining K 2 3 tree demo: insertion 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. 2 3 tree demo: insertion 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. insert Z Z is greter thn (go right) insert Z Z Z is greter thn (go right) J J Z K K

2 3 tree demo: insertion 2 3 tree demo: insertion 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. 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. insert Z insert Z J J K Z K Z serh ends here reple 3-node with temporry 4-node ontining Z 2 3 tree demo: insertion 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. 2 3 tree demo: insertion 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. insert Z insert Z J J K Z K Z split 4-node into two 2-nodes (pss middle key to prent)

2 3 tree demo: insertion 2 3 tree demo: insertion 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. 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. insert Z insert Z J J K Z K Z 2 3 tree demo: insertion 2 3 tree demo: insertion 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. 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. insert insert onvert 3-node into 4-node

2 3 tree demo: insertion 2 3 tree demo: insertion 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. 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. insert insert split 4-node (move to prent) 2 3 tree demo: insertion 2 3 tree demo: insertion 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. 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. insert insert height of tree inreses y 1 split 4-node (move to prent)

2 3 tree demo: insertion 2 3 tree: insertion 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. Insertion into 2-node t ottom. dd new key to 2-node to rete 3-node. Insertion 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. root insert rtie: drw the 2-3 tree onstrution for 26 insert 2 3 tree

insert insert onvert 2-node into 3-node 2 3 tree insert onvert 3-node into 4-node

insert insert split 4-node (move to prent) insert 2 3 tree

insert insert onvert 2-node into 3-node 2 3 tree insert onvert 2-node into 3-node

insert 2 3 tree insert insert onvert 3-node into 4-node

insert insert split 4-node (move to prent) 2 3 tree insert onvert 2-node into 3-node

insert 2 3 tree insert insert onvert 2-node into 3-node

2 3 tree insert onvert 3-node into 4-node insert insert split 4-node (move to prent)

insert insert split 4-node (move to prent) insert 2 3 tree

insert insert onvert 2-node into 3-node onvert 2-node into 3-node 2 3 tree: glol properties Invrints. intins symmetri order nd perfet lne. f. h trnsformtion mintins symmetri order nd perfet lne. 2-3 tree root prent is 3-node left d e omework: verify this d e prent is 2-node left d d middle e d e d right right d d d d e e 64

2 3 tree: performne Blned serh trees: quiz 1 plitting 4-node is lol trnsformtion: onstnt numer of opertions. Wht is the height of 2 3 tree with N keys in the worst se? e d. ~ log3 N B. ~ log2 N. ~ 2 log2 N less thn etween nd etween nd etween nd d etween d nd e greter thn e D. ~ N. I don't know. e d less thn etween nd etween nd etween nd d etween d nd e greter thn e 65 66 2 3 tree: performne T implementtions: summry erfet lne. very pth from root to null link hs sme length. Tree height. Worst se: lg N. Best se: [ll 2-nodes] log3 N.631 lg N. [ll 3-nodes] Between 12 nd 20 for million nodes. Between 18 nd 30 for illion nodes. Bottom line. Gurnteed logrithmi performne for serh nd insert. implementtion sequentil serh (unordered list) inry serh (ordered rry) gurntee serh insert delete serh hit verge se insert delete ordered ops? key interfe N N N N N N equls() log N N N log N N N ompreto() BT N N N log N log N N ompreto() 2 3 tree log N log N log N log N log N log N ompreto() ut hidden onstnt is lrge (depends upon implementtion) 67 68

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. 3.3 B ND T 2 3 serh trees red lk BTs Beutiful lgorithms re not lwys the most useful. B-trees lgorithms Donld Knuth left-lening version optimized for tehing nd oding; developed y Bo edgewik in reting this ourse! OBT DGWIK K VIN W YN http://lgs4.s.prineton.edu Bottom line. ould do it, ut there's etter wy. 69 ow to implement 2 3 trees with inry trees? eft-lening red lk BTs (Guis-edgewik 1979 nd edgewik 2007) 3-node hllenge. ow to represent 3 node? red lk tree nnot mp from BT k to 2 3 tree. 2. Use "internl"left-lening links s "glue" 3 nodes. greter less for etween thn thn nd J pproh 1. egulr BT. No wy to tell 3-node from 2-node. 3-node etween nd J less thn less thn red lk tree J 2 3 tree lk links onnet 2-nodes nd 3-nodes red links "glue" nodes within 3-node noding 3-node with two 2-nodes onneted y left-lening red link greter thn etween nd noding 3-node with two 2-nodes onneted y left-lening red link thn J Widely used in prtie. ritrry restrition: red links len left. greter thn greter etween nd lrger key is root 2-3 tree pproh 3. egulr BT with red "glue" links. less thn pproh 2. egulr BT with red "glue" nodes. horizontl red links Wstes spe, wsted link. ode proly messy. 1. epresent 2 3 tree s BT. orresponding red lk BT 1 1 orrespondene etween red-lk nd 2-3 trees horizontl red links 71 72 J 2-3 tree J

eft-lening red lk BTs: 1 1 orrespondene with 2 3 trees Definition of left-lening red-lk tree Key property. 1 1 orrespondene etween 2 3 nd B. BT suh tht: red lk red lk tree tree 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" J red lk tree J horizontl horizontl red red links links J 2-3 2-3tree tree horizontl red links J J 1 1 1 1 orrespondene orrespondene etween etween red-lk nd 2-3 trees 73 74 2-3 tree J erh implementtion for red lk BTs ed lk BT representtion Oservtion. erh is the sme s for elementry BT (ignore olor). Q. ow to represent olor of links in Jv dt struture? 1 1 orrespondene etween red-lk nd 2-3 trees 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 Vlue 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 privte stti finl oolen D = true; privte stti finl oolen BK = flse; J privte lss Node { Key key; Vlue vl; Node left, right; oolen olor; // olor of prent link } horizontl red links } J privte oolen ised(node x) { 2-3 tree if (x == null) return flse; return x.olor == D; } emrk. ost other ops (e.g., floor, itertion, seletion) Jre lso identil. null links re lk h h.left.olor is D J D h.right.olor is BK G privte stti finl oolen D = true; privte stti finl oolen BK = flse; privte lss Node { Key key; Vlue vl; Node left, right; int N; oolen olor; 75 1 1 orrespondene etween red-lk nd 2-3 trees } Node(Key key, { this.key this.vl this.n this.olor } // // // // // // key ssoited dt sutrees # nodes in this sutree olor of link from prent to this node Vlue vl) = = = = key; vl; 1; D; privte oolen ised(node x) { if (x == null) return flse; 76

lementry red lk BT opertions lementry red lk BT opertions eft rottion. Orient (temporrily) right-lening red link to len left. eft rottion. Orient (temporrily) right-lening red link to len left. rotte left (efore) less thn h etween nd x greter thn rotte left (fter) less thn h etween nd x greter thn privte Node rotteeft(node h) { ssert ised(h.right); Node x = h.right; h.right = x.left; x.left = h; x.olor = h.olor; h.olor = D; return x; } kipped in lss Invrints. intins symmetri order nd perfet lk lne. Invrints. intins symmetri order nd perfet lk lne. 77 78 lementry red lk BT opertions ight rottion. Orient left-lening red link to (temporrily) len right. lementry red lk BT opertions ight rottion. Orient left-lening red link to (temporrily) len right. rotte right (efore) less thn x etween nd h greter thn rotte right (fter) less thn x etween nd h greter thn privte Node rotteight(node h) { ssert ised(h.left); Node x = h.left; h.left = x.right; x.right = h; x.olor = h.olor; h.olor = D; return x; } kipped in lss Invrints. intins symmetri order nd perfet lk lne. Invrints. intins symmetri order nd perfet lk lne. 79 80

lementry red lk BT opertions lementry red lk BT opertions olor flip. eolor to split (temporry) 4-node. olor flip. eolor to split (temporry) 4-node. flip olors flip olors (efore) (fter) h h less thn etween nd etween nd greter thn less thn Invrints. intins symmetri order nd perfet lk lne. privte void flipolors(node h) { ssert!ised(h); ssert ised(h.left); ssert ised(h.right); h.olor = D; h.left.olor = BK; h.right.olor = BK; } etween nd pe d k i p l s s in etween nd greter thn Invrints. intins symmetri order nd perfet lk lne. 81 left 82 root serh ends t this null link Insertion into B tree Insertion into B tree root Wrmup 1. Insert into tree with extly 1 node. left root right root right root serh ends t this null link red link to new node ontining onverts 2-node to 3-node lrger serh ends t this null link tthed new node with red link root rotted left to mke legl 3-node Insert into single 2-node (two ses) lrger rotted left to mke legl 3-node Insert into single 2-node (two ses) lrger smller etween smller etween etween serh ends serh ends serh ends t this null link t this t thisends null linknull link serh serh endsserh ends t this null link t this null link smller serhends t this null link serh ends serh ends t this t this null link link null t this null link tthed new tthed new tthed new tthed new node with node with tthed new node with tthed red link new node with red link node with red link node with red link tthed new tthed new red link node red withlink tthed newnode with red link node with red link red link rotted rotted left rotted right rotted left flipped right olors rotted to lk rotted rotted left olors flipped right right rotted olors flipped to lk right lk to rotted olors flipped to lk right olors flipped olors flipped to lk to lk olors flipped olors flipped to lk to lk tthed new node with red link root Wrmup 2. Insert into tree with extly 2 nodes. root serh ends t this null link red link to new node ontining onverts 2-node to 3-node olors flipped to lk Insert into single 3-node (three ses) Insert into single 3-node (three ses) 83 84 Insert into single 3-node (three ses)

Insertion into B tree Generl se. Do stndrd BT insert; olor new link red. epet until needed: Two left red links in row: rotte right. inserting dd new node here two lefts right link red oth hildren red so flip olors node here two lefts in row so rotte right right link red so rotte left ed lk BT onstrution prtie: insert to mintin symmetri order nd perfet lk lne to fix olor invrints Insert into 3-node 85 Insertion into B tree: pssing red links up the tree Generl se. right link red oth hildren so rotte left red so to mintin symmetri order flip olors Do stndrd BT insert; olor new link red. inserting nd perfet lk lne right link red epet until needed: so rotte left two lefts in row inserting so rotte right Two left red links in row: rotte right. dd new to fix olor invrints node here inserting Both hildren red: flip olors. two lefts in row so rotte right dd new inserting node here oth hildren oth hildren red red so so flip olors flip olors dd new node here right link red oth hildren red so oth rotte hildren left so flip olors red so dd new flip olors node here oth hildren right link red red so so rotte flip olors left two lefts in row right link red so rotte right so rotte left two lefts in row ssing red link up the tree so rotte right two lefts in row oth hildren red so rotte right so flip olors ssing red link up the tree two lefts in row so rotte right oth hildren red so flip olors oth hildren red so flip olors oth hildren red so flip olors ssing red link up the tree ssing red link up the tree insert ssing red link up the tree ed-lk BT onstrution demo inserting inserting dd new node here dd new node here oth hildren red so flip olors 86 ssing red link up the tree Do stndrd BT insert; olor new link red. epet until needed: 87 Do stndrd BT insert; olor new link red. epet until needed:

ed-lk BT onstrution demo ed-lk BT onstrution demo insert insert Do stndrd BT insert; olor new link red. epet until needed: Do stndrd BT insert; olor new link red. epet until needed: ed-lk BT onstrution demo ed-lk BT onstrution demo insert two left reds in row (rotte right) oth hildren red (flip olors) Do stndrd BT insert; olor new link red. epet until needed: Do stndrd BT insert; olor new link red. epet until needed:

ed-lk BT onstrution demo ed-lk BT onstrution demo oth hildren red (flip olors) red-lk BT Do stndrd BT insert; olor new link red. epet until needed: Do stndrd BT insert; olor new link red. epet until needed: ed-lk BT onstrution demo ed-lk BT onstrution demo red-lk BT insert Do stndrd BT insert; olor new link red. epet until needed: Do stndrd BT insert; olor new link red. epet until needed:

ed-lk BT onstrution demo ed-lk BT onstrution demo red-lk BT red-lk BT Do stndrd BT insert; olor new link red. epet until needed: Do stndrd BT insert; olor new link red. epet until needed: ed-lk BT onstrution demo ed-lk BT onstrution demo insert right link red (rotte left) Do stndrd BT insert; olor new link red. epet until needed: Do stndrd BT insert; olor new link red. epet until needed:

ed-lk BT onstrution demo ed-lk BT onstrution demo red-lk BT red-lk BT Do stndrd BT insert; olor new link red. epet until needed: Do stndrd BT insert; olor new link red. epet until needed: ed-lk BT onstrution demo ed-lk BT onstrution demo red-lk BT insert Do stndrd BT insert; olor new link red. epet until needed: Do stndrd BT insert; olor new link red. epet until needed:

ed-lk BT onstrution demo ed-lk BT onstrution demo two left reds in row (rotte right) oth hildren red (flip olors) Do stndrd BT insert; olor new link red. epet until needed: Do stndrd BT insert; olor new link red. epet until needed: ed-lk BT onstrution demo ed-lk BT onstrution demo right link red (rotte left) red-lk BT Do stndrd BT insert; olor new link red. epet until needed: Do stndrd BT insert; olor new link red. epet until needed:

ed-lk BT onstrution demo ed-lk BT onstrution demo red-lk BT red-lk BT Do stndrd BT insert; olor new link red. epet until needed: Do stndrd BT insert; olor new link red. epet until needed: ed-lk BT onstrution demo ed-lk BT onstrution demo insert insert right link red (rotte left) Do stndrd BT insert; olor new link red. epet until needed: Do stndrd BT insert; olor new link red. epet until needed:

ed-lk BT onstrution demo ed-lk BT onstrution demo red-lk BT red-lk BT Do stndrd BT insert; olor new link red. epet until needed: Do stndrd BT insert; olor new link red. epet until needed: ed-lk BT onstrution demo ed-lk BT onstrution demo red-lk BT insert Do stndrd BT insert; olor new link red. epet until needed: Do stndrd BT insert; olor new link red. epet until needed:

ed-lk BT onstrution demo ed-lk BT onstrution demo insert red-lk BT right link red (rotte left) Do stndrd BT insert; olor new link red. epet until needed: Do stndrd BT insert; olor new link red. epet until needed: ed-lk BT onstrution demo ed-lk BT onstrution demo insert right link red (rotte left) two red hildren (flip olors) Do stndrd BT insert; olor new link red. epet until needed: Do stndrd BT insert; olor new link red. epet until needed:

ed-lk BT onstrution demo ed-lk BT onstrution demo two left reds in row (rotte right) two red hildren (flip olors) Do stndrd BT insert; olor new link red. epet until needed: Do stndrd BT insert; olor new link red. epet until needed: ed-lk BT onstrution demo ed-lk BT onstrution demo two red hildren (flip olors) red-lk BT Do stndrd BT insert; olor new link red. epet until needed: Do stndrd BT insert; olor new link red. epet until needed:

ed-lk BT onstrution demo ed-lk BT onstrution demo red-lk BT red-lk BT Do stndrd BT insert; olor new link red. epet until needed: Do stndrd BT insert; olor new link red. epet until needed: Insertion into B tree: Jv implementtion Insertion into B tree: visuliztion me ode for ll ses. ight hild red, left hild lk: rotte left. eft hild, left-left grndhild red: rotte right. Both hildren red: flip olors. kipped in lss h right rotte h left rotte h 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 if (mp == 0) h.vl = vl; ssing red link up red-lk tree insert t ottom (nd olor it red) 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)) flipolors(h); len left lne 4-node split 4-node 255 insertions in sending order } return h; only few extr lines of ode provides ner-perfet lne 127 128

Insertion into B tree: visuliztion Insertion into B tree: visuliztion 255 rndom insertions 255 insertions in desending order 129 130 Blned serh trees: quiz 2 Wht is the height of B tree with N keys in the worst se?. ~ log3 N B. ~ log2 N. ~ 2 log2 N D. ~ N. I don't know. n out of time out here Blne in B trees roposition. eight of tree is 2 lg N in the worst se. f. Blk height = height of orresponding 2 3 tree lg N. Never two red links in--row. roperty. eight of tree is ~ 1.0 lg N in typil pplitions. 131 132

T implementtions: summry Wr story: why red lk? implementtion sequentil serh (unordered list) gurntee serh insert delete serh hit verge se insert delete ordered ops? key interfe N N N N N N equls() inry serh (ordered rry) log N N N log N N N ompreto() BT N N N log N log N N ompreto() 2 3 tree log N log N log N log N log N log N ompreto() erox innovtions. [1970s] lto. GUI. thernet. mlltlk. Interress. ser printing. Bitmpped disply. WYIWYG text editor.... DIlIOlV1TI FUl\l\V()K Fon BND T erox lto red lk BT log N log N log N log N log N log N ompreto() eo J. Guis.erox lo lto eserh enter, lo lto, liforni, nd rnegie-fellon University nd oert edgewik* rogrm in omputer iene Brown University rovidene,. I. hidden onstnt is smll (t most 2 lg N ompres) 133 BTUT I() this pper we present uniform frmework for the implementtion nd study of hlned tree lgorithms. \Ve show how to imhd in this the wy down towrds lef. s we will see, this hs numer of signifint dvntges ovr the older methods. We shll xmine numhr of vritions on ommon theme nd exhiit full implementtions whih re notle for their rvity. One imp1n1enttion is extnined refully, nd some properties out its 134 Wr story: red lk BTs Telephone ompny ontrted with dtse provider to uild rel-time dtse to store ustomer informtion. Dtse implementtion. ed Blk BT. xeeding height limit of 80 triggered error-reovery proess. xtended telephone servie outge. in use = height ound exeeded! Telephone ompny sues dtse provider. egl testimony: show llow for for up to 2 40 keys If implemented properly, the height of red lk BT with N keys is t most 2 lg N. expert witness did not relne BT during delete lgorithms OBT DGWIK KVIN WYN http://lgs4.s.prineton.edu 3.3 BND T 2 3 serh trees red lk BTs B-trees type of Blned tree (o-)invented y udolf Byer while working t Boeing 135

File system model B-trees (Byer-reight, 1972) ge. ontiguous lok of dt (e.g., 4,096-yte hunk). roe. First ess to pge (e.g., from disk to memory). slow fst roperty. Time required for proe is muh lrger thn time to ess dt within pge. ost model. Numer of proes. Gol. ess dt using minimum numer of proes. B-tree. Generlize 2 3 trees y llowing up to keys per node. t lest / 2 keys in ll nodes (exept root). very pth from root to lef hs sme numer of links. G U B-tree ( = 6) hoose s lrge s possile so tht keys fit in pge ( = 1,024 is typil) D F I J K O Q T V W Y Z 137 138 erh in B-tree trt t root. hek if node ontins key. Otherwise, find intervl for serh key nd tke orresponding link. ould use inry serh (ut ll ops re onsidered free) Insertion in B-tree erh for new key. Insert t ottom. plit nodes with + 1 keys on the wy k up the B-tree (moving middle key to prent). G U G U D F I J K O Q T V W Y Z D F I J K O Q T V W Y Z B-tree ( = 6) B-tree ( = 6) 139 140

Blne in B-tree Blned serh trees: quiz 3 roposition. serh or n insertion in B-tree of order with N keys requires etween ~ log N nd ~ log /2 N proes. f. ll nodes (exept possily root) hve etween / 2 nd keys. In prtie. Numer of proes is t most 4. = 1024; N = 62 illion log /2 N 4 Wht of the following does the B in B-tree not men?. Byer B. Blned. Binry D. Boeing. I don't know. the more you think out wht the B in B-trees ould men, the more you lern out B-trees nd tht is good. udolph Byer 141 142 Blned trees in the wild ed Blk 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. ms: onservtive stk snning. B-tree ousins. B+ tree, B*tree, B# tree, B-trees (nd ousins) re widely used for file systems nd dtses. Windows: NTF. : F, F+. inux: eiserf, F, xt3f, JF, BTF. Dtses: O, DB2, ING, Q, ostgreq. 143