week 5
play

Week 5+ Oliver Kullmann Dynamic sets BinaryTrees Trees - PowerPoint PPT Presentation

CS 270 Algorithms Week 5+ Oliver Kullmann Dynamic sets BinaryTrees Trees Implementing rooted trees Dynamic sets 1 The notion of binary search tree Trees 2 Queries in binary search trees Implementing rooted trees 3


  1. CS 270 Algorithms Week 5+ Oliver Kullmann Dynamic sets BinaryTrees Trees Implementing rooted trees Dynamic sets 1 The notion of “binary search tree” Trees 2 Queries in binary search trees Implementing rooted trees 3 Insertion and deletion The notion of “binary search tree” 4 Final remarks Queries in binary search trees 5 Insertion and deletion 6 Final remarks 7

  2. CS 270 General remarks Algorithms Oliver Kullmann Dynamic We consider binary search trees , another important data sets structure. Trees We learn how to use them, by making efficient queries. Implementing rooted trees And we learn how to build them. The notion of “binary search tree” Reading from CLRS for week 5 Queries in binary This week we consider Part III “Data Structures” from CLRS : search trees 1 the introduction to Part III on using sets on a computer Insertion and deletion 2 Section 10.4 on implementing rooted trees Final remarks 3 Sections 12.1, 12.2, 12.3 on binary search trees. The following week than we conclude the consideration of data structures by investigating Part V.

  3. CS 270 Sets Algorithms Oliver Kullmann The most fundamental mathematical notion is that of a set : Dynamic sets We have the possibility to determine the elements of a set. Trees And we can form sets, either by some set-defining property, Implementing rooted trees or by using already given sets (e.g., unions, intersections, The notion of “binary differences). search tree” Queries in Now to bring the eternal and infinite world of mathematics to a binary search trees computer, we need to take care of Insertion and deletion construction of “objects” Final remarks destruction of “objects” naming (basically of functions). For this CLRS uses the (generic) ADT of “dynamic sets”.

  4. CS 270 Dynamic sets: elements and keys Algorithms Oliver Kullmann A “dynamic set” might contain Dynamic sets pointers (or iterators) to objects, Trees or the objects themselves (in Java this can be only integers Implementing rooted trees and other primitive types, in C++ this is possible for every The notion of “binary type of object). search tree” Queries in Whatever the objects in a set are, access to them (especially for binary search trees changing them) is only possible via a pointer (or iterator). Insertion and deletion CLRS assumes that we always use some key to identify an Final object: remarks If the object is for example a record of personal attributes, then the name or some form of ID can be used as a key.

  5. CS 270 Dynamic sets: elementship and search Algorithms Oliver With sets S we can “ask” whether “ x ∈ S ” is true. This is the Kullmann most basic set operation, and the equivalent for dynamic sets is Dynamic sets the operation Trees SEARCH ( S , k ) for key k and dynamic set S , returning Implementing rooted trees either a pointer (iterator) to an object in S with key k , or The notion of “binary NIL if there is no such object. search tree” Queries in We require the ability to extract the key from an object in S , binary search trees and to compare keys for equality. Insertion and deletion 1 Storing S via an array (or a list), SEARCH can be Final remarks performed in O ( | S | ) time (that is, linear time ) by simple sequential search. 2 To do faster than this, typically in time O (log( | S | )) ( logarithmic time ), under various circumstances, is a major aim of data structures for dynamic sets.

  6. CS 270 Dynamic sets: modifying operations Algorithms Oliver Kullmann Dynamic With the following operations we can build and change dynamic sets sets: Trees Implementing INSERT ( S , x ) inserts an object into dynamic set S , where rooted trees x is either a pointer or the object itself. The notion of “binary search tree” DELETE ( S , x ) deletes an object from dynamic set S , Queries in where here x is a pointer (iterator) into S . binary search trees Note that the “ x ” in DELETE is of a different nature than the Insertion and deletion “ x ” in INSERT : It is a pointer (iterator!) into the (dynamic) Final set (and thus these two x are of different type). remarks The most important application is for creating a dynamic set: To create a set S of size n , call INSERT ( S , − ) n -times.

  7. CS 270 Dynamic sets: using order Algorithms Oliver Kullmann Often it is assumed that a linear order is given on the keys: Dynamic So besides “ k == k ′ ?” sets we now can ask “ k ≤ k ′ ?”. Trees Implementing rooted trees In practice using strict orders “ < ” is more common, however The notion of “binary this creates some (necessary) technical complications, which in search tree” this module we won’t be much concerned about (we discuss Queries in binary issues when the need arises). search trees Insertion (These complications have to do with the notion of “equality”, and deletion since “ < ” includes “not equal”. Considering Java, recall that Final remarks there (lacking operator overloading and lacking the ability to distinguish between “object” and “pointer”) you have two operations for checking “equality”: “ == ” for object-identity and “ .equals() ” for object-equality, and now we needed appropriate object-equality ( consistent with the algorithms ).)

  8. CS 270 Dynamic sets: four further operations Algorithms Oliver Kullmann Given a linear order on the objects (to be put into the set), we Dynamic have the following additional operations: sets Trees MINIMUM ( S ) returns a pointer (iterator) to the element Implementing with the smallest key rooted trees The notion MAXIMUM ( S ) returns a pointer (iterator) to the element of “binary search tree” with the largest key Queries in binary SUCCESSOR ( S , x ), where x is a pointer (iterator) into search trees S , returns the next element in S w.r.t. the order on the keys Insertion and deletion PREDECESSOR ( S , x ), where x is a pointer (iterator) Final into S , returns the previous element in S . remarks Operations for computing successors and predecessors can fail (there is no successor resp. predecessor iff we are already at the end resp. beginning), and in such cases we return NIL .

  9. CS 270 Using sorting algorithms: static case Algorithms Oliver Dynamic sets can be realised using sorting, where we have to Kullmann assume a linear order on the keys. Dynamic sets If the set S with n elements is to be built only once, at the Trees beginning, from a sequence of elements, then storing the Implementing rooted trees elements in an array and sorting them, using for example The notion MERGE-SORT with time complexity O ( n · log n ), is a good of “binary search tree” option: Queries in binary SEARCH then takes time O (log n ) (using binary search) search trees Insertion while each of MINIMUM , MAXIMUM , and deletion SUCCESSOR , PREDECESSOR takes constant time. Final remarks However we are concerned here with the dynamic case, where insertions and deletions are used in unpredictable ways. If we not assume that building the set is done (once and for all) at the beginning, but we want to have insertion and deletion, then the case is much less favourable.

  10. CS 270 Using sorting algorithms: dynamic case Algorithms Oliver Now INSERTION and DELETION take time Kullmann O (log( n ) + n ) = O ( n ), searching first for the right Dynamic insertion/deletion place, and then shifting the other sets Trees elements appropriately, Implementing while the five other (non-modifying) operations still take rooted trees logarithmic resp. constant time. The notion of “binary search tree” For practical applications, the linear complexity of insertion and Queries in binary deletion is not acceptable. And we also see that most of the search trees intelligence of sophisticated searching algorithms is blown out of Insertion and deletion the window, and only some form of INSERTION-SORT (in Final combination with binary search) survived. remarks It could be said that data structures for dynamic sets try to introduce some of the intelligent methods into the dynamic framework, making insertion and deletion more efficient (necessarily at the cost of making the other operations (somewhat) less efficient).

  11. CS 270 Dictionaries and priority queues Algorithms Oliver Kullmann Dynamic sets Often not all of the operations for dynamic sets are needed, Trees opening up the possibilities of specialised and more efficient Implementing rooted trees implementations. Two important cases are as follows: The notion of “binary search tree” dictionary only INSERTION , DELETION and SEARCH Queries in are needed; binary search trees priority queue only INSERTION , MINIMUM resp. Insertion MAXIMUM (for min- resp. max-priority queues) and deletion and DELETE-MIN resp. DELETE-MAX are Final remarks required.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend