CSC401 – Analysis of Algorithms Lecture Notes 6 Dictionaries and Search Trees Objectives: Introduce dictionaries and its diverse implementations Introduce.

Slides:



Advertisements
Similar presentations
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Advertisements

AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Data Structures Lecture 11 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
© 2004 Goodrich, Tamassia Binary Search Trees   
Goodrich, Tamassia Search Trees1 Binary Search Trees   
CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties and maintenance.
Binary Search Trees1 Part-F1 Binary Search Trees   
Binary Search Trees   . 2 Ordered Dictionaries Keys are assumed to come from a total order. New operations: closestKeyBefore(k) closestElemBefore(k)
Binary Search Trees1 ADT for Map: Map stores elements (entries) so that they can be located quickly using keys. Each element (entry) is a key-value pair.
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.
AVL Trees v z. 2 AVL Tree Definition AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the.
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.
CS 221 Analysis of Algorithms Ordered Dictionaries and Search Trees.
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
© 2004 Goodrich, Tamassia Binary Search Trees1 CSC 212 Lecture 18: Binary and AVL Trees.
Binary Search Trees   . 2 Binary Search Tree Properties A binary search tree is a binary tree storing keys (or key-element pairs) at its.
Chapter 10: Search Trees Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data.
Fall 2006 CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties.
3.1. Binary Search Trees   . Ordered Dictionaries Keys are assumed to come from a total order. Old operations: insert, delete, find, …
Binary Search Trees1 Chapter 3, Sections 1 and 2: Binary Search Trees AVL Trees   
1 Binary Search Trees   . 2 Ordered Dictionaries Keys are assumed to come from a total order. New operations: closestKeyBefore(k) closestElemBefore(k)
1 COMP9024: Data Structures and Algorithms Week Six: Search Trees Hui Wu Session 1, 2014
© 2004 Goodrich, Tamassia BINARY SEARCH TREES Binary Search Trees   
Algorithms Design Fall 2016 Week 6 Hash Collusion Algorithms and Binary Search Trees.
Part-D1 Binary Search Trees
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Binary Search 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 < > =
Search Trees.
AVL Trees 6/25/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
Binary Search Trees < > = Binary Search Trees
Chapter 2: Basic Data Structures
AVL Trees 4/29/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
Priority Queues and Heaps
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Binary Search 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
Binary Search Trees < > = Binary Search Trees
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
Red-Black Trees 2018年11月26日3时46分 AVL Trees v z AVL Trees.
Locators 3 a 1 g 4 e 12/1/2018 4:40 AM Locators Locators
Dictionaries < > = /3/2018 8:58 AM Dictionaries
v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
Dictionaries < > = /9/2018 3:06 AM Dictionaries
Locators 3 a 1 g 4 e 12/29/2018 7:56 AM Locators Locators
Binary Search Trees < > =
Red-Black Trees v z /17/2019 4:20 PM Red-Black Trees
Dictionaries < > = /17/2019 4:20 PM Dictionaries
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
(2,4) Trees /6/ :26 AM (2,4) Trees (2,4) Trees
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
1 Lecture 13 CS2013.
Binary Search Trees < > = Dictionaries
Red-Black Trees v z /6/ :10 PM Red-Black Trees
Dictionaries 二○一九年九月二十四日 ADT for Map:
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
Presentation transcript:

CSC401 – Analysis of Algorithms Lecture Notes 6 Dictionaries and Search Trees Objectives: Introduce dictionaries and its diverse implementations Introduce binary search trees and present operations on binary search trees Analyze the performance of binary search tree operations Introduce balanced binary search trees: AVL tree and Red-Black tree Introduce a design pattern: Locator

2 Dictionary ADT The dictionary ADT models a searchable collection of key- element items The main operations of a dictionary are searching, inserting, and deleting items Multiple items with the same key are allowed Applications: –address book –credit card authorization –mapping host names (e.g., cs16.net) to internet addresses (e.g., ) Dictionary ADT methods: –findElement(k): if the dictionary has an item with key k, returns its element, else, returns the special element NO_SUCH_KEY –insertItem(k, o): inserts item (k, o) into the dictionary –removeElement(k): if the dictionary has an item with key k, removes it from the dictionary and returns its element, else returns the special element NO_SUCH_KEY –size(), isEmpty() –keys(), Elements()

3 Log File A log file is a dictionary implemented by means of an unsorted sequence –We store the items of the dictionary in a sequence (based on a doubly-linked lists or a circular array), in arbitrary order Performance: –insertItem takes O(1) time since we can insert the new item at the beginning or at the end of the sequence –findElement and removeElement take O(n) time since in the worst case (the item is not found) we traverse the entire sequence to look for an item with the given key The log file is effective only for dictionaries of small size or for dictionaries on which insertions are the most common operations, while searches and removals are rarely performed (e.g., historical record of logins to a workstation)

4 Binary Search Binary search performs operation findElement(k) on a dictionary implemented by means of an array-based sequence, sorted by key –similar to the high-low game –at each step, the number of candidate items is halved –terminates after a logarithmic number of steps Example: findElement(7) m l h m l h m l h l  m  h

5 Lookup Table A lookup table is a dictionary implemented by means of a sorted sequence –We store the items of the dictionary in an array-based sequence, sorted by key –We use an external comparator for the keys Performance: –findElement takes O(log n) time, using binary search –insertItem takes O(n) time since in the worst case we have to shift n  2 items to make room for the new item –removeElement take O(n) time since in the worst case we have to shift n  2 items to compact the items after the removal The lookup table is effective only for dictionaries of small size or for dictionaries on which searches are the most common operations, while insertions and removals are rarely performed (e.g., credit card authorizations)

6 Binary Search Tree A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying the following property: –Let u, v, and w be three nodes such that u is in the left subtree of v and w is in the right subtree of v. We have key(u)  key(v)  key(w) External nodes do not store items An inorder traversal of a binary search trees visits the keys in increasing order

7 Search To search for a key k, we trace a downward path starting at the root The next node visited depends on the outcome of the comparison of k with the key of the current node If we reach a leaf, the key is not found and we return NO_SUCH_KEY Example: findElement(4) Algorithm findElement(k, v) if T.isExternal (v) return NO_SUCH_KEY if k  key(v) return findElement(k, T.leftChild(v)) else if k  key(v) return element(v) else { k  key(v) } return findElement(k, T.rightChild(v))   

8 Insertion To perform operation insertItem(k, o), we search for key k Assume k is not already in the tree, and let let w be the leaf reached by the search We insert k at node w and expand w into an internal node Example: insert    w w

9 Deletion To perform operation removeElement( k ), we search for key k Assume key k is in the tree, and let let v be the node storing k If node v has a leaf child w, we remove v and w from the tree with operation removeAboveExternal( w ) Example: remove v w  

10 Deletion (cont.) We consider the case where the key k to be removed is stored at a node v whose children are both internal –we find the internal node w that follows v in an inorder traversal –we copy key(w) into node v –we remove node w and its left child z (which must be a leaf) by means of operation removeAboveExternal( z ) Example: remove v w z v 2

11 Performance Consider a dictionary with n items implemented by means of a binary search tree of height h –the space used is O(n) –methods findElement, insertItem and removeElement take O(h) time The height h is O(n) in the worst case and O(log n) in the best case

12 AVL Tree Definition AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the heights of the children of v can differ by at most 1. An example of an AVL tree where the heights are shown next to the nodes:

13 Height of an AVL Tree Fact: The height of an AVL tree storing n keys is O(log n). storing n keys is O(log n). Proof: Let us bound n(h): the minimum number of internal nodes of an AVL tree of height h. We easily see that n(1) = 1 and n(2) = 2 For n > 2, an AVL tree of height h contains the root node, one AVL subtree of height n-1 and another of height n-2. That is, n(h) = 1 + n(h-1) + n(h-2) Knowing n(h-1) > n(h-2), we get n(h) > 2n(h-2). So n(h) > 2n(h-2), n(h) > 4n(h-4), n(h) > 8n(n-6), … (by induction), n(h) > 2 i n(h-2i) Solving the base case we get: n(h) > 2 h/2-1 Taking logarithms: h < 2log n(h) +2 Thus the height of an AVL tree is O(log n) 3 4 n(1) n(2)

14 Insertion in an AVL Tree Insertion is as in a binary search tree Always done by expanding an external node. Example: w b=x a=y c=z before insertionafter insertion

15 Trinode Restructuring let (a,b,c) be an inorder listing of x, y, z perform the rotations needed to make b the topmost node of the three b=y a=z c=x T0T0 T1T1 T2T2 T3T3 b=y a=z c=x T0T0 T1T1 T2T2 T3T3 c=y b=x a=z T0T0 T1T1 T2T2 T3T3 b=x c=ya=z T0T0 T1T1 T2T2 T3T3 case 1: single rotation (a left rotation about a) case 2: double rotation (a right rotation about c, then a left rotation about a) (other two cases are symmetrical)

16 Insertion Example, continued

17 Restructuring (Single Rotations) Single Rotations:

18 Restructuring (Double Rotations) double rotations:

19 Removal in an AVL Tree Removal begins as in a binary search tree, which means the node removed will become an empty external node. Its parent, w, may cause an imbalance. Example: before deletion of 32after deletion

20 Rebalancing after a Removal Let z be the first unbalanced node encountered while travelling up the tree from w. Also, let y be the child of z with the larger height, and let x be the child of y with the larger height. We perform restructure(x) to restore balance at z. As this restructuring may upset the balance of another node higher in the tree, we must continue checking for balance until the root of T is reached w c=x b=y a=z

21 Running Times for AVL Trees a single restructure is O(1) –using a linked-structure binary tree find is O(log n) –height of tree is O(log n), no restructures needed insert is O(log n) –initial find is O(log n) –Restructuring up the tree, maintaining heights is O(log n) remove is O(log n) –initial find is O(log n) –Restructuring up the tree, maintaining heights is O(log n)

22 Red-Black Tree A red-black tree can also be defined as a binary search tree that satisfies the following properties: –Root Property: the root is black –External Property: every leaf is black –Internal Property: the children of a red node are black –Depth Property: all the leaves have the same black depth Theorem: A red-black tree storing n items has height O(log n) –The height of a red- black tree is at most twice the height of its associated (2,4) tree, which is O(log n) The search algorithm for a binary search tree is the same as that for a binary search tree By the above theorem, searching in a red-black tree takes O(log n) time

23 Insertion To perform operation insertItem (k, o), we execute the insertion algorithm for binary search trees and color red the newly inserted node z unless it is the root –We preserve the root, external, and depth properties –If the parent v of z is black, we also preserve the internal property and we are done –Else ( v is red ) we have a double red (i.e., a violation of the internal property), which requires a reorganization of the tree Example where the insertion of 4 causes a double red: z vv z

24 Remedying a Double Red Consider a double red with child z and parent v, and let w be the sibling of v z vw Case 1: w is black –The double red is an incorrect replacement of a 4-node –Restructuring: we change the 4-node replacement Case 2: w is red –The double red corresponds to an overflow –Recoloring: we perform the equivalent of a split z v w

25 Restructuring A restructuring remedies a child-parent double red when the parent red node has a black sibling It is equivalent to restoring the correct replacement of a 4-node The internal property is restored and the other properties are preserved z v w z v w

26 Restructuring (cont.) There are four restructuring configurations depending on whether the double red nodes are left or right children

27 Recoloring A recoloring remedies a child-parent double red when the parent red node has a red sibling The parent v and its sibling w become black and the grandparent u becomes red, unless it is the root It is equivalent to performing a split on a 5-node The double red violation may propagate to the grandparent u z v w z v w … 4 … 2

28 Analysis of Insertion Recall that a red-black tree has O(log n) height Step 1 takes O(log n) time because we visit O(log n) nodes Step 2 takes O(1) time Step 3 takes O(log n) time because we perform –O(log n) recolorings, each taking O(1) time, and –at most one restructuring taking O(1) time Thus, an insertion in a red-black tree takes O(log n) time Algorithm insertItem(k, o) 1.We search for key k to locate the insertion node z 2.We add the new item (k, o) at node z and color z red 3. while doubleRed(z) if isBlack(sibling(parent(z))) z  restructure(z) z  restructure(z)return else { sibling(parent(z) is red } z  recolor(z) z  recolor(z)

29 Deletion To perform operation remove (k), we first execute the deletion algorithm for binary search trees Let v be the internal node removed, w the external node removed, and r the sibling of w –If either v of r was red, we color r black and we are done –Else ( v and r were both black) we color r double black, which is a violation of the internal property requiring a reorganization of the tree Example where the deletion of 8 causes a double black: v rw r

30 Remedying a Double Black The algorithm for remedying a double black node w with sibling y considers three cases Case 1: y is black and has a red child –We perform a restructuring, equivalent to a transfer, and we are done Case 2: y is black and its children are both black –We perform a recoloring, equivalent to a fusion, which may propagate up the double black violation Case 3: y is red –We perform an adjustment, equivalent to choosing a different representation of a 3-node, after which either Case 1 or Case 2 applies Deletion in a red-black tree takes O(log n) time

31 Red-Black Tree Reorganization remedy double red Insertion double red removed or propagated up splitrecoloring double red removed change of 4-node representation restructuring result (2,4) tree action Red-black tree action remedy double black Deletion restructuring or recoloring follows change of 3-node representation adjustment double black removed or propagated up fusionrecoloring double black removed transferrestructuring result (2,4) tree action Red-black tree action

32 Locators A locators identifies and tracks a (key, element) item within a data structure A locator sticks with a specific item, even if that element changes its position in the data structure Intuitive notion: –claim check –reservation number Methods of the locator ADT: –key(): returns the key of the item associated with the locator –element(): returns the element of the item associated with the locator Application example: –Orders to purchase and sell a given stock are stored in two priority queues (sell orders and buy orders) the key of an order is the price the element is the number of shares –When an order is placed, a locator to it is returned –Given a locator, an order can be canceled or modified

33 Locator-based Methods Locator-based priority queue methods: –insert(k, o): inserts the item (k, o) and returns a locator for it –min(): returns the locator of an item with smallest key –remove(l): remove the item with locator l –replaceKey(l, k): replaces the key of the item with locator l –replaceElement(l, o): replaces with o the element of the item with locator l –locators(): returns an iterator over the locators of the items in the priority queue Locator-based dictionary methods: –insert(k, o): inserts the item (k, o) and returns its locator –find(k): if the dictionary contains an item with key k, returns its locator, else return the special locator NO_SUCH_KEY –remove(l): removes the item with locator l and returns its element –locators(), replaceKey(l, k), replaceElement(l, o)

34 Implementation The locator is an object storing –key –element –position (or rank) of the item in the underlying structure In turn, the position (or array cell) stores the locator Example: –binary search tree with locators 3 a 6 d 9 b 1 g 4 e 8 c

35 Positions vs. Locators Position –represents a “place” in a data structure –related to other positions in the data structure (e.g., previous/next or parent/child) –implemented as a node or an array cell Position-based ADTs (e.g., sequence and tree) are fundamental data storage schemes Locator –identifies and tracks a (key, element) item –unrelated to other locators in the data structure –implemented as an object storing the item and its position in the underlying structure Key-based ADTs (e.g., priority queue and dictionary) can be augmented with locator-based methods