<strong>Algorithm</strong> 2: Basic Counting: When an element with timestamp t arrives// level 0if there is bucket 〈w(b), t, t〉 ∈ S 0 thenIncrement w(b);elseInsert bucket 〈1, t, t〉 into S 0;end// level i, i > 0<strong>for</strong> i = 1, . . . , M doif there is bucket b = 〈w(b), l(b), r(b)〉 ∈ S i with t ∈ [l(b), r(b)] thenIncrement w(b);if w(b) = 2 i+1 and l(b) ≠ r(b) then// bucket too heavy, split// note that a bucket is not split// if it is responsible <strong>for</strong> only a single time stampNew bucket b 1 = 〈2 i , l(b), l(b)+r(b)+1 − 1〉;2New bucket b 2 = 〈2 i , l(b)+r(b)+1 , r(b)〉;2Delete b from S i;Insert b 1 and b 2 into S i;endendend// handle overflow<strong>for</strong> i = 0, . . . , M doif |S i| > α then// overflowDiscard bucket b ∗ ∈ S i such that r(b ∗ ) = min b∈Si r(b);T i ← r(b ∗ );endend<strong>Algorithm</strong> 3: Basic Counting: Query(w)Input: w, the width of the query window, where w ≤ WOutput: An estimate of the number of elements with timestamps in [c − w, c]where c is the current timeLet l ∈ [0, . . . , M] be the smallest integer such that T l < c − w;return ∑ {b∈S l |l(b)≥c−w} w(b);
e the initial bucket which is inserted into S i during initialization (<strong>Algorithm</strong>1). The root of A is b 0 . For any bucket b ∈ A, if b is split into two buckets b land b r , then b l and b r will appear as the respective left and right children of bin A. Note that in A a node is either a leaf or has exactly two children. Tree Ahas depth at most log W (the root is at depth 0), since every time that a bucketsplits the time period divides in half, and the smallest time period is a discretetime step. For any node b ∈ A let A(b) denote the subtree with root b; we willalso refer to this as the subtree of b.Consider now the tree A at time c. The buckets in S i appear as the |S i |rightmost leaves of A. Let S i ′ denote the set of buckets in S i with l(b) ≥ t.clearly, e i t = ∑ b∈Sw(b). The buckets in S ′ i ′ i are the |S′ i | rightmost leaves of A.Suppose that S i ′ ≠ ∅ (the case S′ i = ∅ is discussed below). Let b′ be the leftmostleaf in A among the buckets in S i ′ . Let p denote the path in A from the root tob ′ . For the number of nodes |p| of p it holds |p| ≤ 1 + log W . Let H 1 (H 2 ) be theset that consists of the right (left) children of the nodes in p, such that thesechildren are not members of the path p. Note that b ′ /∈ H 1 ∪ H 2 . The union ofb ′ and the leaves in the subtrees of H 1 (∪ b∈H2 A(b)) constitute the nodes in S i ′.Further, each bucket b /∈ S i ′ is in a leaf in a subtree of H 2.Consider some element x with timestamp t ′ . Initially, when x arrives it isinitially assigned to the bucket b which t ′ belongs to. If b splits to two (children)buckets b 1 and b 2 , then we can assume that x is assigned arbitrarily to one ofthe two new buckets arbitrarily. Even through x’s timestamp may belong to b 1 ,x may be assigned to b 2 , and vice-versa. If again the new bucket splits, x isassigned to one of its children, and so on. Note that x is always assigned to aleaf of A.At time c, we can writee i t = s t + |X 1 | − |X 2 ∪ X 3 |, (1)such that: X 1 is the set of elements with timestamps in [0, t−1] which are assignedto buckets in S ′ i ; X 2 is the set of elements with timestamps in [l(b ′ ), W −1] whichare assigned to buckets outside of S ′ i ; and, <strong>for</strong> t < l(b′ ), X 3 is the set of elementswith timestamps in [t, l(b ′ )−1] which are assigned to buckets outside of S ′ i , while<strong>for</strong> t = l(b ′ ), X 3 = ∅. Note that the sets X 1 , X 2 , X 3 are disjoint.First, we bound |X 1 |. Consider some element x ∈ X 1 with timestamp in[0, t − 1] which at time c appears assigned to a leaf bucket b l ∈ S ′ i . Since b l ∈ S ′ i ,t cannot be a member of the time range of b l , that is, t /∈ [l(b l ), r(b l )]. Thus, xcould not have been initially assigned to b l . Suppose that b l ≠ b ′ . Then, there isa node ̂b ∈ H 1 such that b l is the leaf of the subtree A(̂b). None of the nodes inA(̂b) contain t in their time range, since all the leaves of A(̂b) are members of S ′ i .There<strong>for</strong>e, x could not have been initially assigned to A(̂b). Thus, x is initiallyassigned to a node b p ∈ p ′ = p − {b ′ }, since x could not have been assignedto any node in the subtrees of H 2 which would certainly bring x outside of S ′ i .Similarly, if b l ≠ b ′ , x is initially assigned to a node b p ∈ p ′ . Since at most 2 i+1elements are initially assigned to the root, and at most 2 i elements are initially