id
int64
0
25.6k
text
stringlengths
0
4.59k
21,200
good way to satisfy such queries for examplecalling successors(kon query key (ordpvd may : )could result in an iterator with the following entries((ordpvd may : )(aa : $ )((ordpvd may : )(aa : $ )((ordpvd may : )(aa : $ )((ordpvd may : )(aa : $ )maxima sets life is full of trade-offs we often have to trade off desired ...
21,201
and pair (abis called maximum pair if it is not dominated by any other pairs we are interested in maintaining the set of maxima of collection of price-performance pairs that iswe would like to add new pairs to this collection (for examplewhen new car is introduced)and we would like to query this collection for given do...
21,202
(lognexpected time and add( ,pupdates in (( )log nexpected timewhere is the number of points removed thuswe are able to achieve good running times for the methods that maintain our set of maxima exercises for source code and help with exercisesplease visit java datastructures net reinforcement -
21,203
initially empty map that is implemented with listr- describe how to use map to implement the dictionary adtassuming that the user does not attempt to insert entries with the same key - describe how an ordered list implemented as doubly linked list could be used to implement the map adt - what would be good hash code fo...
21,204
be included in the hash table implementation of code fragments - explain how to modify class hashtablemap given in code fragments so that it implements the dictionary adt instead of the map adt - show the result of rehashing the hash table shown in figure into table of size using the new hash function ( mod - argue why...
21,205
user may attempt to insert entries with the same key - suppose we are given two ordered search tables and teach with entries (with and being implemented with arraysdescribe an (log )-time algorithm for finding the kth smallest key in the union of the keys from and (assuming no duplicatesc- give an (logn)-time solution ...
21,206
given collection of cost-performance pairs ( , )describe an algorithm for finding the maxima pairs of in ( logntime - the quadratic probing strategy has clustering problem related to the way it looks for open slots namelywhen collision occurs at bucket ( )it checks buckets [( (kj mod ]for , show that mod will assume at...
21,207
counting the number of ' in - describe an efficient dictionary structure for storing entries that have an associated set of keys that comes from total order that isthe set of keys is smaller than the number of entries your structure should perform operation find all in (logr sexpected timewhere is the number of entries...
21,208
- implement the methods of the ordered dictionary adt using skip list - extend the previous project by providing graphical animation of the skip list operations visualize how entries move up the skip list during insertions and are linked out of the skip list during removals alsoin search operationvisualize the scan-for...
21,209
structures literature [ exercise - was contributed by james lee we use star to indicate sections containing material more advanced than the material in the rest of the this material can be considered optional in first reading search trees contents binary search trees searching
21,210
update operations java implementation avl trees update operations java implementation splay trees splaying when to splay
21,211
( trees multi-way search trees update operations for ( , trees red-black trees update operations java implementation exercises java datastructures net
21,212
binary search trees all of the structures we discuss in this are search treesthat istree data structures that can be used to implement dictionary let usthereforebegin by briefly reviewing the fundamental methods of the dictionary adtfind( )return an entry with key kif it exists findall( )return an iterable collection o...
21,213
searching to perform operation find(kin dictionary that is represented with binary search tree twe view the tree as decision tree (recall figure in this casethe question asked at each internal node of is whether the search key is less thanequal toor greater than the key stored at node vdenoted with key(vif the answer i...
21,214
the analysis of the worst-case running time of searching in binary search tree is simple algorithm treesearch is recursive and executes constant number of primitive operations for each recursive call each recursive call of treesearch is made on child of the previous node that istreesearch is called on the nodes of path...
21,215
findall(kin time ( )where is the number of entries returned howeverthis method is slightly more complicatedand the details are left as an exercise ( - admittedlythe height of can be as large as nbut we expect that it is usually much smaller indeedwe will show how to maintain an upper bound of (lognon the height of sear...
21,216
given this methodwe perform insert( ,xfor dictionary implemented with binary search tree by calling treeinsert( , , root())which is given in code fragment code fragment recursive algorithm for insertion in binary search tree this algorithm traces path from ' root to an external nodewhich is expanded into new internal n...
21,217
with binary search tree is bit more complexsince we do not wish to create any "holesin the tree we assumein this casethat proper binary tree supports the following additional update operationremoveexternal( )remove an external node and its parentreplacing ' parent with ' siblingan error occurs if is not external given ...
21,218
figure bwhere the entry to remove (with key is stored at node (wwith an external child(abefore the removal(bafter the removal figure removal from the binary search tree of figure bwhere the entry to remove (with key is stored at node (wwhose children are both internal(abefore the removal(bafter the removal performance ...
21,219
implementation of dictionary with entries only if the height of is small in the best caset has height log( which yields logarithmic-time performance for all the dictionary operations in the worst casehowevert has height nin which case it would look and feel like an ordered list implementation of dictionary such worst-c...
21,220
time size,isempty ( findinsertremove (hfindall ( snote that the running time of search and update operations in binary search tree varies dramatically depending on the tree' height we can nevertheless take comfort thaton averagea binary search tree with keys generated from random series of insertions and removals of ke...
21,221
its update methods inform any moved bstentry objects of their new positions we also use several simple auxiliary methods for accessing and testing datasuch as checkkeywhich checks if key is valid (albeit using fairly simple rule in this casewe also use an instance variableactionposwhich stores the position where the mo...
21,222
(continues in code fragment
21,223
class binarysearchtree (continued from code fragment
21,224
avl trees in the previous sectionwe discussed what should be an efficient dictionary data structurebut the worst-case performance it achieves for the various operations is linear timewhich is no better than the performance of listand array-based dictionary implementations (such as unordered lists and search tables disc...
21,225
avl tree is itself an avl tree the height-balance property has also the important consequence of keeping the height smallas shown in the following proposition proposition the height of an avl tree storing entries is (lognjustificationinstead of trying to find an upper bound on the height of an avl tree directlyit turns...
21,226
( that isn( ( )for any integer isuch that > since we already know the values of ( and ( )we pick so that is equal to either or that iswe pick by substituting the above value of in formula we obtainfor > ( by taking logarithms of both sides of formula we obtain log (hh/ from which we get logn( ( which implies that an av...
21,227
search treesbut with avl trees we must perform additional computations insertion an insertion in an avl tree begins as in an insert operation described in section for (simplebinary search tree recall that this operation always inserts the new entry at node in that was previously an external nodeand it makes become an i...
21,228
"search-and-repairstrategy in particularlet be the first node we encounter in going up from toward the root of such that is unbalanced (see figure alsolet denote the child of with higher height (and note that node must be an ancestor of wfinallylet be the child of with higher height (there cannot be tie and node must b...
21,229
called rotationbecause of the geometric way we can visualize the way it changes if ythe trinode restructuring method is called single rotationfor it can be visualized as "rotatingy over (see figure and otherwiseif xthe trinode restructuring operation is called double rotationfor it can be visualized as first "rotatingx...
21,230
restructuring operation (code fragment )(aand (ba single rotation(cand (da double rotation
21,231
as was the case for the insert dictionary operationwe begin the implementation of the remove dictionary operation on an avl tree by using the algorithm for performing this operation on regular binary search tree the added difficulty in using this approach with an avl tree is that it may violate the height-balance prope...
21,232
continue marching up looking for moreall the way to the root stillsince the height of is (logn)where is the number of entriesby proposition (logntrinode restructurings are sufficient to restore the height-balance property performance of avl trees we summarize the analysis of the performance of an avl tree as follows op...
21,233
performing local trinode restructurings (rotationsjava implementation let us now turn to the implementation details and analysis of using an avl tree with internal nodes to implement an ordered dictionary of entries the insertion and removal algorithms for require that we are able to perform trinode restructurings and ...
21,234
implementing dictionary using an avl tree (assuming the parent class includes an implementation of the restructure methodthis class extends binarysearchtree (code fragments and includes nested classavlnodewhich extends the btnode class used to represent the nodes of binary tree the avlnode class defines an additional i...
21,235
tallerchild and rebalance and dictionary methods insert and remove of class avltree
21,236
splay trees another way we can implement the fundamental dictionary operations is to use balanced search tree data structure known as splay tree this structure is conceptually quite different from the other balanced search trees we discuss in this for splay tree does not use any explicit rules to enforce its balance in...
21,237
in this casewe replace by and make have and as its childrenwhile maintaining the inorder relationships of the nodes in figure zig-zag(abefore(bafter there is another symmetric configuration where is right child and is left child zigx does not have grandparent (or we are not considering ' grandparent for some reason(see...
21,238
when has parent but not grandparent splaying step consists of repeating these restructurings at until becomes the root of note that this is not the same as sequence of simple rotations that brings to the root an example of the splaying of node is shown in figures and figure example of splaying node(asplaying the node s...
21,239
zig-zig(ethe next step is again zig-zig(fafter the zig-zig (continued from figure
21,240
when to splay the rules that dictate when splaying is performed are as followswhen searching for key kif is found at node xwe splay xelse we splay the parent of the external node at which the search terminates unsuccessfully for examplethe splaying in figures and would be performed after searching successfully for key ...
21,241
when deleting key kwe splay the parent of the node that gets removedthat isw is either the node storing or one of its descendents (recall the removal algorithm for binary search trees an example of splaying following deletion is shown in figure figure deletion from splay tree(athe deletion of from node is performed by ...
21,242
amortized analysis of splaying
21,243
of decreases by one thusif has depth dsplaying consists of sequence of / zig-zigs and/or zig-zagsplus one final zig if is odd since single zig-zigzig-zagor zig affects constant number of nodesit can be done in ( time thussplaying node in binary search tree takes time ( )where is the depth of in in other wordsthe time f...
21,244
value of the payment will be determined at the end of our analysiswe distinguish three casesif the payment is equal to the splaying workthen we use it all to pay for the splaying if the payment is greater than the splaying workwe deposit the excess in the accounts of several nodes if the payment is less than the splayi...
21,245
and bloga+logb < logc- ( let us consider the change in (tcaused by each type of splaying substep zig-zig(recall figure since the size of each node is one more than the size of its two childrennote that only the ranks of xyand change in zig-zig operationwhere is the parent of and is the parent of alsonote that ( )+ '( <...
21,246
< ( '( )- ( )proposition let be splay tree with root tand let be the total variation of (tcaused by splaying node at depth we have < ( (tr( ) + justificationsplaying node consists of / splaying substepseach of which is zig-zig or zig-zagexcept possibly the last onewhich is zig if is odd let (xr(xbe the initial rank of ...
21,247
in this ranger '( log( '( )log( ( <log( ( + ) ( + thusthe total variation of (tcaused by the insertion is '( )- ( <log( + thereforea payment of (logncyber-dollars is sufficient to maintain the invariant when new node is inserted when deleting node from splay tree with keysthe ranks of all the ancestors of are decreased...
21,248
each one searchinsertionor deletionstarting from splay tree with zero keys alsolet (idenote the number of times the entry is accessed in the splay treethat isits frequencyand let denote the total number of entries assuming that each entry is accessed at least oncethen the total running time for performing the sequence ...
21,249
keys and +then key stored in the subtree of rooted at child node must be "in betweentwo keys stored at this simple viewpoint gives rise to the rule that -node stores regular keysand it also forms the basis of the algorithm for searching in multi-way search tree by the above definitionthe external nodes of multi-way sea...
21,250
there is an interesting relationship between the number of entries and the number of external nodes proposition an -entry multi-way search tree has + external nodes we leave the justification of this proposition as an exercise ( - searching in multi-way tree given multi-way search tree twe note that searching for an en...
21,251
we are using it to support the biggerprimary data structure we denote the dictionary stored at node of as (vthe entries we store in (vwill allow us to find which child node to move to next during search operation specificallyfor each node of twith children , and entries ( , )( - , - )we store in the dictionary (vthe en...
21,252
node small and also keeps the primary multi-way tree balanced is the ( , treewhich is sometimes called - tree or tree this data structure achieves these goals by maintaining two simple properties (see figure )size propertyevery internal node has at most four children depth propertyall the external nodes have the same d...
21,253
( are true to justify these claims note first thatby the size propertywe can have at most nodes at depth at most nodes at depth and so on thusthe number of external nodes in is at most likewiseby the depth property and the definition of ( , treewe must have at least nodes at depth at least nodes at depth and so on thus...
21,254
entry into node and add new child (an external nodeto on the left of that iswe add entry (kxwto the dictionary (vour insertion method preserves the depth propertysince we add new external node at the same level as existing external nodes neverthelessit may violate the size property indeedif node was previously -nodethe...
21,255
after the split(kinsertion of (linsertion of
21,256
21,257
stored at such nodes thusit can be implemented to run in ( time as consequence of split operation on node va new overflow may occur at the parent of if such an overflow occursit triggers in turn split at node (see figure split operation either eliminates the overflow or propagates it into the parent of the current node...
21,258
begin such an operation by performing search in for an entry with key removing such an entry from ( , tree can always be reduced to the case where the entry to be removed is stored at node whose children are external nodes supposefor instancethat the entry with key that we wish to remove is stored in the ith entry ( , ...
21,259
(hafter removing
21,260
propagating sequence of fusions in ( , tree(aremoval of which causes an underflow(bfusionwhich causes another underflow(csecond fusion operationwhich causes the root to be removed(dfinal tree performance of ( , trees table summarizes the running times of the main operations of dictionary realized with ( , tree the time...
21,261
(noperation time sizeisempty ( findinsertremove (lognfindall (logn sthus( , trees provide for fast dictionary search and update operations ( , trees also have an interesting relationship to the data structure we discuss next red-black trees although avl trees and ( , trees have number of nice propertiesthere are some d...
21,262
tree of figure each external node of this red-black tree has black ancestors (including itself)henceit has black depth we use the color blue instead of red alsowe use the convention of giving an edge of the tree the same color as the child node as for previous types of search treeswe assume that entries are stored at t...
21,263
and red-black tree( -node( -node( -node the correspondence between ( , trees and red-black trees provides important intuition that we will use in our discussion of how to perform updates in red-black trees in factthe update algorithms for red-black trees are mysteriously complex without this intuition proposition the h...
21,264
the fact that has internal nodes we assume that red-black tree is realized with linked structure for binary trees (section )in which we store dictionary entry and color indicator at each node thus the space requirement for storing keys is (nthe algorithm for searching in red-black tree is the same as that for standard ...
21,265
take node zits parent vand grandparent uand temporarily relabel them as aband cin left-to-right orderso that aband will be visited in this order by an inorder tree traversal replace the grandparent with the node labeled band make nodes and the children of bkeeping inorder relationships unchanged after performing the re...
21,266
and black and their parent red (unless is the rootin which caseit is colored blackit is possible thatafter such recoloringthe double red problem reappearsalbeit higher up in the tree tsince may have red parent if the double red problem reappears at uthen we repeat the consideration of the two cases at thusa recoloring ...
21,267
tree figure sequence of insertions in redblack tree(ainitial tree(binsertion of (cinsertion of which causes double red(dafter restructuring(einsertion of which causes double red(fafter recoloring (the root remains black)(ginsertion of (hinsertion of (iinsertion of which causes double red(jafter restructuring(kinsertion...
21,268
sequence of insertions in redblack tree(minsertion of which causes double red(nafter restructuring(oinsertion of which causes double red(pafter recoloring there is again double redto be handled by restructuring(qafter restructuring (continued from figure
21,269
since the case action eliminates the double-red problem with single trinode restructuring and the case action performs no restructuring operationsat most one restructuring is needed in red-black tree insertion by the above analysis and the fact that restructuring or recoloring takes ( timewe have the followingpropositi...
21,270
suppose now that we are asked to remove an entry with key from red-black tree removing such an entry initially proceeds as for binary search tree (section firstwe search for node storing such an entry if node does not have an external childwe find the internal node following in the inorder traversal of tmove the entry ...
21,271
this node may be colored either red or black
21,272
figures and resolving this case corresponds to fusion operation in the corresponding ( , tree we do recoloringwe color blackwe color redandif is redwe color it black (figure )otherwisewe color double black (figure henceafter this recoloringthe double black problem may reappear at the parent of (see figure that isthis r...
21,273
recoloring of red-black tree that propagates the double black problem(aconfiguration before the recoloring and corresponding nodes in the associated ( , tree before the fusion (other similar configurations are possible)(bconfiguration after the recoloring and corresponding nodes in the associated ( , tree after the fus...
21,274
adjustment operationas follows if is the right child of xlet be the right child of yotherwiselet be the left child of execute the trinode restructuring operation restructure( )which makes the parent of color black and red an adjustment corresponds to choosing different representation of -node in the ( , tree after the ...
21,275
corresponding nodes in the associated ( , tree ( symmetric configuration is possible)(bconfiguration after the adjustment with the same corresponding nodes in the associated ( , tree from the above algorithm descriptionwe see that the tree updating needed after removal involves an upward march in the tree twhile perfor...
21,276
thussince any changes we make at node in during this upward march takes ( time (because it affects constant number of nodes)we have the followingproposition the algorithm for removing an entry from red-black tree with entries takes (logntime and performs (lognrecolorings and at most one adjustment plus one additional t...
21,277
after recoloring(hremoval of (iremoval of causing double black (handled by an adjustment)(jafter the adjustment the double black needs to be handled by recoloring(kafter the recoloring (continued from figure
21,278
table summarizes the running times of the main operations of dictionary realized by means of red-black tree we illustrate the justification for these bounds in figure
21,279
realized by red-black treewhere denotes the size of the collection returned by findall the space usage is (noperation time sizeisempty ( findinsertremove (lognfindall (logn +sfigure illustrating the running time of searches and updates in red-black tree the time performance is ( per levelbroken into down phasewhich typ...
21,280
searching and updating in dictionary the red-black tree data structure is slightly more complicated than its corresponding ( , tree even soa red-black tree has conceptual advantage that only constant number of trinode restructurings are ever needed to restore the balance in red-black tree after an update java implement...
21,281
binarysearchtree (code fragments through we assume the parent class supports the method restructure for performing trinode restructurings (rotations)its implementation is left as an exercise ( - class rbtree inherits methods sizeisemptyfindand findall from binarysearchtree but overrides methods insert and remove it imp...
21,282
corresponding methods of the superclass first and then rebalance the tree by calling
21,283
(given by the actionpos variable inherited from the superclassto the root code fragment method remove and auxiliary method remedydoubleblack of class rbtree
21,284
exercises for source code and help with exercisesplease visit java datastructures net reinforcement - we defined binary search tree so that keys equal to node' key can be in either the left or right subtree of that node suppose we change the definition so that we restrict equal keys to the right subtree what must subtr...
21,285
- draw the avl tree resulting from the insertion of an entry with key into the avl tree of figure - draw the avl tree resulting from the removal of the entry with key from the avl tree of figure - explain why performing rotation in an -node binary tree represented using an array list takes ohm(ntime - is the search tre...
21,286
consider the sequence of keys ( , , , , , , , , , , draw the result of inserting entries with these keys (in the given orderinto an initially empty ( , tree an initially empty red-black tree - for the following statements about red-black treesprovide justification for each true statement and counterexample for each fal...
21,287
is red-black tree is splay tree is binary search tree - perform the following sequence of operations in an initially empty splay tree and draw the tree after each set of operations insert keys in this order search for keys in this order delete keys in this order - what does splay tree look like if its entries are acces...
21,288
design variation of algorithm treesearch for performing the operation findal(kin an ordered dictionary implemented with binary search tree tand show that it runs in time ( )where is the height of and is the size of the collection returned - describe how to perform an operation removeall( )which removes all the entries ...
21,289
let be an ordered dictionary with entries show how to modify the avl tree to implement the following method for in time (logn)countallinrange( , )compute and return the number of entries in with key such that < < - show that the nodes that become unbalanced in an avl tree after operation insertatexternal is performedwi...
21,290
let be red-black tree storing entriesand let be the key of an entry in show how to construct from tin (logntimetwo red-black trees and ''such that contains all the keys of less than kand 'contains all the keys of greater than this operation destroys - show that the nodes of any avl tree can be colored "redand "blackso ...
21,291
- -body simulations are an important modeling tool in physicsastronomyand chemistry in this projectyou are to write program that performs simple nbody simulation called "jumping leprechauns this simulation involves leprechaunsnumbered to it maintains gold value for each leprechaun iwhich begins with each leprechaun sta...
21,292
write java class that implements all the methods of the ordered dictionary adt (see section using red-black tree - form three-programmer team and have each member implement different one of the previous three projects perform extensive experimental studies to compare the speed of these three implementations design thre...
21,293
[ (see also [ ] sortingsetsand selection contents merge-sort divide-and-conquer merging arrays and lists the running time of merge-sort java implementations of merge-sort
21,294
merge-sort and recurrence equations quick-sort randomized quick-sort in-place quick-sort lower bound on sorting bucket-sort and radix-sort bucket-sort radix-sort
21,295
comparison of sorting algorithms the set adt and union/find structures simple set implementation partitions with union-find operations tree-based partition implementation selection prune-and-search randomized quick-select
21,296
exercises java datastructures net merge-sort in this sectionwe present sorting techniquecalled merge-sortwhich can be described in simple and compact way using recursion divide-and-conquer merge-sort is based on an algorithmic design pattern called divide-and-conquer the divide-and-conquer pattern consists of the follo...
21,297
the remaining / elements / elements of sand contains recurrecursively sort sequences and conquerput back the elements into by merging the sorted sequences and into sorted sequence in reference to the divide stepwe recall that the notation indicates the ceiling of xthat isthe smallest integer msuch that < similarlythe n...
21,298
of merge-sort the nodes drawn with dashed lines represent calls that have not been made yet the node drawn with thick lines represents the current call the empty nodes drawn with thin lines represent completed calls the remaining nodes (drawn with thin lines and not emptyrepresent calls that are waiting
21,299
figure visualization of an execution of mergesort (continues in figure