general remarks
play

General remarks Algorithms Algorithms Week 5+ Oliver Oliver - PowerPoint PPT Presentation

CS 270 CS 270 General remarks Algorithms Algorithms Week 5+ Oliver Oliver Kullmann Kullmann Dynamic Dynamic We consider binary search trees , another important data sets sets BinaryTrees structure. Trees Trees We learn how to use


  1. CS 270 CS 270 General remarks Algorithms Algorithms Week 5+ Oliver Oliver Kullmann Kullmann Dynamic Dynamic We consider binary search trees , another important data sets sets BinaryTrees structure. Trees Trees We learn how to use them, by making efficient queries. Implementing Implementing rooted trees rooted trees And we learn how to build them. Dynamic sets 1 The notion The notion of “binary of “binary search tree” search tree” Trees Reading from CLRS for week 5 2 Queries in Queries in binary binary This week we consider Part III “Data Structures” from CLRS : search trees search trees Implementing rooted trees 3 1 the introduction to Part III on using sets on a computer Insertion Insertion and deletion and deletion The notion of “binary search tree” 4 2 Section 10.4 on implementing rooted trees Final Final remarks remarks 3 Sections 12.1, 12.2, 12.3 on binary search trees. Queries in binary search trees 5 The following week than we conclude the consideration of data Insertion and deletion 6 structures by investigating Part V. Final remarks 7 CS 270 CS 270 Sets Dynamic sets: elements and keys Algorithms Algorithms Oliver Oliver Kullmann Kullmann A “dynamic set” might contain The most fundamental mathematical notion is that of a set : Dynamic Dynamic sets sets pointers (or iterators) to objects, We have the possibility to determine the elements of a set. Trees Trees Implementing Implementing And we can form sets, either by some set-defining property, or the objects themselves (in Java this can be only integers rooted trees rooted trees and other primitive types, in C++ this is possible for every or by using already given sets (e.g., unions, intersections, The notion The notion differences). of “binary type of object). of “binary search tree” search tree” Queries in Queries in Whatever the objects in a set are, access to them (especially for Now to bring the eternal and infinite world of mathematics to a binary binary search trees search trees computer, we need to take care of changing them) is only possible via a pointer (or iterator). Insertion Insertion and deletion and deletion CLRS assumes that we always use some key to identify an construction of “objects” Final Final object: remarks remarks destruction of “objects” naming (basically of functions). 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. For this CLRS uses the (generic) ADT of “dynamic sets”.

  2. CS 270 CS 270 Dynamic sets: elementship and search Dynamic sets: modifying operations Algorithms Algorithms Oliver Oliver With sets S we can “ask” whether “ x ∈ S ” is true. This is the Kullmann Kullmann most basic set operation, and the equivalent for dynamic sets is Dynamic Dynamic With the following operations we can build and change dynamic sets sets the operation sets: Trees Trees SEARCH ( S , k ) for key k and dynamic set S , returning Implementing Implementing INSERT ( S , x ) inserts an object into dynamic set S , where rooted trees rooted trees either a pointer (iterator) to an object in S with key k , or x is either a pointer or the object itself. The notion The notion NIL if there is no such object. of “binary of “binary search tree” search tree” DELETE ( S , x ) deletes an object from dynamic set S , Queries in Queries in where here x is a pointer (iterator) into S . We require the ability to extract the key from an object in S , binary binary search trees search trees and to compare keys for equality. Note that the “ x ” in DELETE is of a different nature than the Insertion Insertion and deletion and deletion 1 Storing S via an array (or a list), SEARCH can be “ x ” in INSERT : It is a pointer (iterator!) into the (dynamic) Final Final set (and thus these two x are of different type). remarks remarks performed in O ( | S | ) time (that is, linear time ) by simple sequential search. The most important application is for creating a dynamic set: 2 To do faster than this, typically in time O (log( | S | )) To create a set S of size n , call INSERT ( S , − ) n -times. ( logarithmic time ), under various circumstances, is a major aim of data structures for dynamic sets. CS 270 CS 270 Dynamic sets: using order Dynamic sets: four further operations Algorithms Algorithms Oliver Oliver Kullmann Kullmann Often it is assumed that a linear order is given on the keys: Given a linear order on the objects (to be put into the set), we Dynamic Dynamic So besides “ k == k ′ ?” have the following additional operations: sets sets we now can ask “ k ≤ k ′ ?”. Trees Trees MINIMUM ( S ) returns a pointer (iterator) to the element Implementing Implementing rooted trees rooted trees with the smallest key In practice using strict orders “ < ” is more common, however The notion The notion MAXIMUM ( S ) returns a pointer (iterator) to the element of “binary of “binary this creates some (necessary) technical complications, which in search tree” search tree” with the largest key this module we won’t be much concerned about (we discuss Queries in Queries in binary binary SUCCESSOR ( S , x ), where x is a pointer (iterator) into issues when the need arises). search trees search trees S , returns the next element in S w.r.t. the order on the keys Insertion Insertion (These complications have to do with the notion of “equality”, and deletion and deletion PREDECESSOR ( S , x ), where x is a pointer (iterator) since “ < ” includes “not equal”. Considering Java, recall that Final Final into S , returns the previous element in S . remarks remarks there (lacking operator overloading and lacking the ability to distinguish between “object” and “pointer”) you have two Operations for computing successors and predecessors can fail operations for checking “equality”: “ == ” for object-identity and (there is no successor resp. predecessor iff we are already at the “ .equals() ” for object-equality, and now we needed end resp. beginning), and in such cases we return NIL . appropriate object-equality ( consistent with the algorithms ).)

  3. CS 270 CS 270 Using sorting algorithms: static case Using sorting algorithms: dynamic case Algorithms Algorithms Oliver Oliver Dynamic sets can be realised using sorting, where we have to Now INSERTION and DELETION take time Kullmann Kullmann assume a linear order on the keys. O (log( n ) + n ) = O ( n ), searching first for the right Dynamic Dynamic sets insertion/deletion place, and then shifting the other sets If the set S with n elements is to be built only once, at the Trees Trees elements appropriately, beginning, from a sequence of elements, then storing the Implementing Implementing while the five other (non-modifying) operations still take rooted trees rooted trees elements in an array and sorting them, using for example logarithmic resp. constant time. The notion The notion MERGE-SORT with time complexity O ( n · log n ), is a good of “binary of “binary search tree” search tree” option: For practical applications, the linear complexity of insertion and Queries in Queries in binary binary deletion is not acceptable. And we also see that most of the SEARCH then takes time O (log n ) (using binary search) search trees search trees intelligence of sophisticated searching algorithms is blown out of Insertion Insertion while each of MINIMUM , MAXIMUM , and deletion and deletion the window, and only some form of INSERTION-SORT (in SUCCESSOR , PREDECESSOR takes constant time. Final Final combination with binary search) survived. remarks remarks However we are concerned here with the dynamic case, where It could be said that data structures for dynamic sets try to insertions and deletions are used in unpredictable ways. If we introduce some of the intelligent methods into the dynamic not assume that building the set is done (once and for all) at the framework, making insertion and deletion more efficient beginning, but we want to have insertion and deletion, then the (necessarily at the cost of making the other operations case is much less favourable. (somewhat) less efficient). CS 270 CS 270 Dictionaries and priority queues The notion of a “tree”: origins Algorithms Algorithms Oliver Oliver Kullmann Kullmann Recall last week (and see Sections B.5.1, B.5.2 and B.5.3 in Dynamic Dynamic sets CLRS for more information on this topic). sets Often not all of the operations for dynamic sets are needed, Trees Trees The original notion of a tree (in our context) comes from opening up the possibilities of specialised and more efficient Implementing Implementing rooted trees rooted trees mathematics, where a tree is a “connected undirected graph implementations. Two important cases are as follows: The notion The notion without cycles”, e.g. of “binary of “binary search tree” search tree” dictionary only INSERTION , DELETION and SEARCH Queries in Queries in are needed; T = 1 2 6 7 binary binary ❃ search trees search trees ❃ � priority queue only INSERTION , MINIMUM resp. ❃ � ❃ � ❃ � Insertion Insertion ❃ � ❃ � MAXIMUM (for min- resp. max-priority queues) and deletion ❃ � and deletion � 4 3 5 and DELETE-MIN resp. DELETE-MAX are Final Final remarks remarks required. We have vertices 1 , . . . , 7 and edges between them. From any vertices we can reach every other vertex, and this essentially in a unique way (this is the special property of trees).

Recommend


More recommend