DOC PREVIEW
MSU CSE 830 - Lecture 8

This preview shows page 1-2-3-27-28-29 out of 29 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Binary search treesSlide 2BSTs and Dynamic SetsDynamic Set OperationsSimple deletionSimple binary search treesTree-Balancing AlgorithmsTree RotationsRed-Black TreesExample Red-Black TreeInsertion(T,z)RB-Insert-FixupRB-Insert-Fixup (parent(z) is right child of parent(parent(z)))Slide 14RB-Insert-Fixup Analysis (parent(z) is right child of parent(parent(z)))Delete(T,z)Move To Front (MTF) TechniqueSplay Tree ExampleSlide 19Effectiveness in listsNotation for computing costsPerformance of MTFDirect Cost ComparisonPotential Function ΦAmortized CostAmortized Cost ComparisonAmortized Cost Comparison Cont’dSplay Tree PerformanceDynamic Optimality ConjectureBinary search trees•Definition•Binary search trees and dynamic set operations•Balanced binary search trees–Tree rotations–Red-black trees•Move to front “balancing” technique–Unsorted linked lists–Splay treesBinary search trees•Basic tree property–For any node x•left subtree has nodes ≤ x•right subtree has nodes ≥ xBSTs and Dynamic Sets•Dynamic set operations and binary search trees–Search(S,k)–Insert(S,x)–Delete(S,x)–Minimum or Maximum(S)–Successor or Predecessor (S,x)–List All(S)–Merge(S1,S2)Dynamic Set Operations•Listall(T)?–time to list?•Search(T,k)?–search time?•Minimum(T)? Maximum(T)?–search time?•Successor(T,x)? Predecessor(T,x)?–Search time•Simple Insertion(T,x)Simple deletion•Delete(T,x): Three possible cases:–a) x is a leaf :–b) x has one child : –c) x has two children : Replace x with successor(x). •Successor(x) has at most one child (why?); •Use step a or b on successor(x)Simple binary search trees•What is the expected height of a binary search tree?•Difficult to compute if we allow both insertions and deletions•With insertions, analysis of section 12.4 shows that expected height is O(log n)•Implications about BSTs as dynamic sets?Tree-Balancing Algorithms•Tree rotations•Red-Black Trees•Splay Trees•Others–AVL Trees–2-3 Trees and 2-3-4 TreesTree RotationsABT1T2 T3BAT3T1 T2Right Rotate(T,A)Left Rotate(T,B)Red-Black Trees•All nodes in the tree are either red or black.•Every null-child is included and colored black.•All red nodes must have two black children.•Every path from any node x (including the root) to a leaf must have the same number of black nodes.•How balanced of a tree will this produce? •How hard will it be to maintain?Example Red-Black TreeInsertion(T,z)•Find place to insert using simple insertion•Color node z as red•Fix up tree so that it is still a red-black tree•What needs to be fixed?–Problem 1: z is root (first node inserted)•Minor detail–Problem 2: parent(z) is redRB-Insert-Fixup•Situation: parent(z) is red and z is red–Case 1: uncle(z) is red•Then make both uncle(z) and parent(z) black and p(p(z)) red and recurse up treezzRB-Insert-Fixup(parent(z) is right child of parent(parent(z)))•Situation: parent(z) is red and z is red–Case 2: uncle(z) is black and z is a left child•Right rotate to make into case 3ABT1T2 T3zBAT3T1 T2zRB-Insert-Fixup(parent(z) is right child of parent(parent(z)))•Situation: parent(z) is red and z is red–Case 3: uncle(z) is black and z is a left child•Left rotate to make B root of treeABCT1T2 T3zABCT1T2 T3RB-Insert-Fixup Analysis(parent(z) is right child of parent(parent(z)))•Situation: parent(z) is red and z is red–Case 1: no rotations, always moving up tree–Cases 2 and 3: At most 2 rotations total and tree ends up balanced•No more need to fix up once these cases are met–Total cost: at most 2 rotations and log n operationsDelete(T,z)•Find node y to delete using simple deletion•Let x be a child of y if such a child exists (otherwise x is a null child)•If y is black, fix up tree so that it is still a red-black tree•What needs to be fixed?–Problem 1: y was root, so now we might have red root–Problem 2: x and parent(y) are both red–Problem 3: Removal of y violates black height properties of paths that used to go through yMove To Front (MTF) Technique•A powerful “balancing” mechanism is the “move to front” idea•This technique is effective in managing both unsorted lists and binary search trees•The idea: Whenever an item is accessed (by search or by insertion), it is always moved to the front–In a list, the front is well-defined–In a binary search tree, the front of the tree is the root–A tree that implements this idea is called a splay tree•Rotations are not simple single rotations but occur in pairs•We give some intuition about the power of MTFSplay Tree ExampleSplay Tree ExampleEffectiveness in lists•Reference: Amortized efficiency of list update and paging rules, Sleator and Tarjan, CACM 1985•Problem statement:–Suppose you are maintaining an unsorted list where every search must progress from the front of the list to the item (or end of list if search is unsuccessful)–Operations: search, insert, delete•Costs: finding or deleting the ith item costs i•Inserting a new item costs n+1•Immediately after an insertion or search of an item i, item i may be moved anywhere closer to the front of the list at no extra cost•Goal: Find a way to manage list that minimizes total cost of a sequence of operationsNotation for computing costs•S: sequence of requests –(insertions, deletions, searches)•A: any algorithm for maintaining list–including those those that know S in advance•cA(S): cost incurred by algorithm A on sequence S not including paid exchanges•xA(S): # of paid exchanges for A on S•fA(S): # of free exchanges for A on S•Example:–List: 5, 9, 2, 7, 3, 6 and we search for 7–MTF then has list with 7, 5, 9, 2, 3, 6–cMTF(S) increases by 4–xMTF(S) increases by 0 since moving 7 to the front is a free–fMTF(S) increases by 3 since we made 3 free exchanges to move 7Performance of MTF•Thm: For any algorithm A and any sequence SxMTF(S) + cMTF(S) ≤ 2cA(S) + xA(S) – FA(S) – m•Observation: xMTF(S) = 0•Interpretation–MTF incurs at most twice the cost of any other algorithm, even those that know the request sequence in advanceDirect Cost Comparison•The ideal approach to proving this result is that for each operation, MTF incurs a cost that is at most twice that of algorithm A•However, this is clearly not always true•Example just before tth operation search(1):–A’s list: 1, 20, 7, 9, 3, 5, 24, 4, 8•A’s cost is just 1–MTF’s list: 5, 24, 8, 3, 9,


View Full Document

MSU CSE 830 - Lecture 8

Download Lecture 8
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 8 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 8 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?