2
Information Processing Letters 24 (1987) 95-96 30 January 1987 North-Holland A NOTE ON A NEW DATA STRUCTURE FOR IN-THE-PAST QUERIES Dan FIELD Computer Graphics Laboratory, Department of Computer Science, University of Waterloo, Waterloo, Ontario, Canada N2L 3G1 Communicated by W.L. Van der Poel Received 20 February 1986 This note describes a new data structure which records information and supports queries about elements that have been previously inserted and deleted. Both time and space parameters match those of Overmars (1981); the advantage to the structure described here is its simplicity. Keywords: Data structure, in-the-past query I. Introduction Dobkin and Munro [1] invested a data struc- ture that allows queries to be answered about previously held states. Subsequent papers by Overmars [2] and Aviad and Shamir [3] improved and generali7ed their data structure. This note, while not improving the asymptotic time or space bounds of Overmars, describes a data structure that is simpler to understand and implement. Let xx, x2,...,x n be a totally ordered set of elements. An update operation consists of the insertion or deletion of an element x i, and is assigned a time-stamp t indicating that t- 1 up- dates have preceded. The state of the data struc- ture at time t is the set of elements present after update t and before update t + 1. The data struc- ture must be able to answer two types of query: Q1 "What rank does x have in state t?" Q2 "What element has rank r in state t?" These so-called 'in-the-past' queries are useful for solving geometric searching and intersection prob- lems. Let N t denote the number of elements present in state t. Initially, N o = 0. Overmars demonstrates a data structure and algorithm that answers a query of state t in O(log Na) time. An update of state t- 1 takes O(log Nt) time and space for a total of 0020 ,~i ,~ t log N i) over t updates. 2. The data structure The data structure used here is a forest; ele- ments present at state t are stored in rank order at the leaves of balanced search trees To, T1,..., Tt, one tree for each state of the data structure. Each node v contains gv, the number of leaves descend- ing from the left child of v. It is not hard to see how queries Q1 and Q2 may be answered for a particular state t by performing the appropriate search of T t. Since there are N t leaves in Tt, the query can be answered in O0og Nt) time. As things now stand, space requirements are O~0 ~i*g tNi) • This is reduced by embedding sub- trees of T 0, Tx,..., Tt_ 1 in T t. Suppose update t removes element x. Let S be the set of nodes in T t_ 1 that would be modified by the deletion of x in the tree. Define S' = S t3 (w Iw is an ancestor of v e S }. T t is created by copying nodes in S' and linking them to subtrees in Tt_ 1 that would not be af- fected by the deletion of x. T t is now an exact copy of Tt_ 1 with total space O(ITt_xl+lS'l). 0020-0190/87/$3.50 © 1987, Elsevier Science Publishers B.V. (North-Holland) 95

A note on a new data structure for in-the-past queries

Embed Size (px)

Citation preview

Page 1: A note on a new data structure for in-the-past queries

Information Processing Letters 24 (1987) 95-96 30 January 1987 North-Holland

A N O T E O N A NEW DATA S T R U C T U R E FOR IN-THE-PAST Q U E R I E S

Dan F I E L D

Computer Graphics Laboratory, Department of Computer Science, University of Waterloo, Waterloo, Ontario, Canada N2L 3G1

Communicated by W.L. Van der Poel Received 20 February 1986

This note describes a new data structure which records information and supports queries about elements that have been previously inserted and deleted. Both time and space parameters match those of Overmars (1981); the advantage to the structure described here is its simplicity.

Keywords: Data structure, in-the-past query

I. Introduction

Dobkin and Munro [1] invested a data struc- ture that allows queries to be answered about previously held states. Subsequent papers by Overmars [2] and Aviad and Shamir [3] improved and generali7ed their data structure. This note, while not improving the asymptotic time or space bounds of Overmars, describes a data structure that is simpler to understand and implement .

Let xx, x 2 , . . . , x n be a totally ordered set of elements. An update operat ion consists of the insertion or deletion of an element x i, and is assigned a t ime-stamp t indicating that t - 1 up- dates have preceded. The s ta te of the data struc- ture at t ime t is the set of elements present after update t and before update t + 1. The data struc- ture must be able to answer two types of query:

Q1 " W h a t rank does x have in state t?" Q2 " W h a t element has rank r in state t?"

These so-called ' in-the-past ' queries are useful for solving geometric searching and intersection prob- lems.

Let N t denote the number of elements present in state t. Initially, N o = 0. Overmars demonstrates a data structure and algorithm that answers a query of state t in O(log Na) time. An update of

state t - 1 takes O(log Nt) time and space for a total of 0020 ,~ i ,~ t log N i) over t updates.

2. The data structure

The data structure used here is a forest; ele- ments present at state t are stored in rank order at the leaves of balanced search trees To, T1 , . . . , Tt, one tree for each state of the data structure. Each node v contains gv, the number of leaves descend- ing from the left child of v. It is not hard to see how queries Q1 and Q2 may be answered for a particular state t by performing the appropriate search of T t. Since there are N t leaves in Tt, the query can be answered in O0og N t ) time.

As things now stand, space requirements are O~0 ~i*g tNi) • This is reduced by embedding sub- trees of T 0, Tx , . . . , Tt_ 1 in T t.

Suppose update t removes element x. Let S be the set of nodes in T t_ 1 that would be modif ied by the deletion of x in the tree. Define

S' = S t3 (w I w is an ancestor of v e S }.

T t is created by copying nodes in S' and linking them to subtrees in Tt_ 1 that would not be af- fected by the deletion of x. T t is now an exact copy of Tt_ 1 with total space O(ITt_xl+lS'l).

0020-0190/87/$3.50 © 1987, Elsevier Science Publishers B.V. (North-Holland) 95

Page 2: A note on a new data structure for in-the-past queries

Volume 24, Number 2 INFORMATION PROCESSING LETTERS 30 January 1987

b c

/ \ d

i 2 7 i/

9

\ 11

la)

b' d'

/\, / \ C e

7 9 11 12

(b)

d'

C' e'

(c) 12

b /c d'

9 11 12

The delet ion of x is now pe r fo rmed on T t in the usual m a n n e r wi thout affecting shared por t ions of the tree (see Fig. 1). The same basic steps above are fol lowed when the upda te is an insertion.

For m a n y balanced trees including 2-3 trees [4], A V L trees [5], and BB[~] trees [6], [S'[ = O(log Nt). Therefore, by sharing c o m m o n sub- trees, the size of the data s tructure has been reduced to O(E 0 ¢ i ~ t log N i).

References

[1] G.M. Adelson-Velskii and Ye.M. Landis, An algorithm for the organiTation of information, Soviet Math. Dokl. 3 (1962) 1259-1263.

[2] A.V. Aho, J.E. Hopcroft and J.D. Ullman, Design and Analysis of Computer Algorithms (Addison-Wesley, Read- ing, MA, 1975).

[3] Z. Aviad and E. Shamir, A direct dynamic solution to range search and related problems for product regions, Manuscript.

[4] D.P. Dobkin and J.I. Munro, Efficient uses of the past, Proc. 21st I1~1~1~ Syrup. on Foundations of Computer Sci- ence (1980) 200-206.

[5] J. Nievergelt and E.M. Reingold, Binary search trees of bounded balance, SIAM J. Comput. 2 (1) (1973) 33-43.

[6] M.H. Overmars, Searching in the past I, Tech. Rept. RUU-CS-81-7, Univ. of Utrecht, April 1981.

(d)

Fig. 1. The sequence of operations to insert a leaf containing the value 12 into an AVL tree T. Part (a) depicts T before the insertion. Part (b) shows the tree T ' resulting from the inser- tion of a leaf containing 12. Note that a new interior node e ' was created and a rotation performed. Part (c) depicts the nodes S' of T ' that are different from those in T. Part (d) shows the result of embedding the subtrees of T into T'.

96