Você está na página 1de 25

Data Structures and Algorithms

(CS210/ESO207/ESO211)
Lecture 28

Miscellaneous application of binary trees - II

1
Outline of the lecture
Part 1:

A closer look at the binary tree data structure for Multi-Increment problem

Solution for Dynamic Range-minima Problem

We shall go slowly on this topic so that all of you fully internalize the concept of this
data structure. This topic manifests the true power of binary trees beyond dictionary
problems and I would like all of you to realize and appreciate it

Part 2: (a sketch only)

Data structure for maintaining sets under two special operations

2
Part 1
3
A closer look at the binary tree data structure for
Multi-Increment
An insight into the hierarchy of intervals







4

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

0,3 [4,7] 8,11 [12,15]
0,1 2,3 4,5 6,7 8,9 10,11 12,13 [14,15]
[8,15]
[0,7]
[0,15]
An insight into the hierarchy of intervals









5
0,1 2,3 4,5 6,7 8,9 10,11 12,13 [14,15]
0,3 [4,7] 8,11 [12,15]
[0,7]
[8,15]
[0,15]

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

An example:
Locate the nodes defining interval [2,10]









6
0,1 2,3 4,5 6,7 8,9 10,11 12,13 [14,15]
0,3 [4,7] 8,11 [12,15]
[0,7]
[8,15]
[0,15]

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

An example:
Locate the nodes defining interval [6,8]









7
0,1 2,3 4,5 6,7 8,9 10,11 12,13 [14,15]
0,3 [4,7] 8,11 [12,15]
[0,7]
[8,15]
[0,15]

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

Data structure for Multi-Increment
8
Multi-Increment(, , ) efficiently








How to do Multi-Increment(2,10,12) ?


9
0,3 [4,7] 8,11 [12,15]
[0,7]
[8,15]
[0,15]

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

4 14 9 17 23 21 29 91 37 25 8 33 2 67 11 44
Initial
sequence
0,1 2,3 4,5 6,7 8,9 10,11 12,13 [14,15]
0 0 0 0 0 0 0 0
0 0 0 0
0 0
0
Multi-Increment(, , ) efficiently








How to do Multi-Increment(2,10,12) ?


10
0,3 [4,7] 8,11 [12,15]
[0,7]
[8,15]
[0,15]

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

4 14 19 29 23 21 29 91 37 25 18 33 2 67 11 44
Initial
sequence
0,1 2,3 4,5 6,7 8,9 10,11 12,13 [14,15]
0 0 0 0 12 0 0 0
0 12 0 0
0 0
0
Proof of correctness

Let T be the tree data structure for Multi-Increment problem.
Let u be the leaf node corresponding to

.

Question: What can we say about T in terms of the sequence after a series of operations ?

After every operation the following assertion holds:
Value of

is the sum of values stored at each node on the path from u to root(T).

To prove the correctness of our datastructure/algorithm, you need to prove that the above
mentioned assertion holds after each Multi-Increment() operation. (Do it as a small
exercise (4-5 sentences only)).
11
The solution of Multi-Increment Problem




Theorem: There exists a data structure of size O() for maintaining a
sequence S =
0
, ,
1
such that each Multi-Increment() and Report()
operation takes O(log ) time.

12
Dynamic Range-minima

13
Dynamic Range Minima Problem
Given an initial sequence S =
0
, ,
1
of numbers, maintain a compact data
structure to perform the following operations efficiently for any 0 < < .
ReportMin(, ):
Report the minimum element from {

| for each }
Update(, a):
a becomes the new value of

.

Example:
Let the initial sequence be S = 14, 12, 3, 49, 4, 21, 322, -40
ReportMin(, ) returns
ReportMin(, ) returns
Update(, 19) update S to

ReportMin(, ) returns
ReportMin(, ) returns






14
14, 12, 19, 49, 4, 21, 322, -40
3
3
4
12
Dynamic Range Minima Problem
Given an initial sequence S =
0
, ,
1
of numbers, maintain a compact data
structure to perform the following operations efficiently for any 0 < < .
ReportMin(, ):
Report the minimum element from {

| for each }
Update(, a):
a becomes the new value of

.


AIM:
O() size data structure.
ReportMin(, ) in O(log ) time.
Update(, a) in O(log ) time.






15
Data structure for dynamic range minima







Question: What should be stored in the leaf nodes ?
Answer: The initial sequence.
Question: What should be stored in an internal node v ?
Answer: minimum value stored in subtree(v).
16

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

4 14 9 17 23 21 29 91 37 25 8 33 2 67 11 44
Initial
sequence
4 9 21 29 25 8 2 11
2 8 21 4
2
4 2
Data structure for dynamic range minima







How to do Report-Min(2,10) ?






17

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

4 14 9 17 23 21 29 91 37 25 8 33 2 67 11 44
Initial
sequence
2
4 2
2 8 21 4
4 9 21 29 25 8 2 11
Data structure for dynamic range minima







How to do Report-Min(2,10) ?






18

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

4 14 9 17 23 21 29 91 37 25 8 33 2 67 11 44
Initial
sequence
2
4 2
2 8 21 4
4 9 21 29 25 8 2 11
Data structure for dynamic range minima







Data structure: An array A of size 2n-1.
Copy the sequence S =
0
, ,
1
into ??
Leaf node corresponding to

= ??
How to check if a node is left child or right child of its parent ?
(if index of the node is odd, then the node is left child, else the node is right child)
19
0
1 2
3 4 5 6
7 8 9 10 11 12 13 14
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
A[( 1) + ]
A[ 1]A[2 2]
4 14 9 17 23 21 29 91 37 25 8 33 2 67 11 44
Initial
sequence
4 9 21 29 25 8 2 11
2 8 21 4
4 2
2
Update(, )

Update(, )
( ) + ;
A[] ;
( )/ ;
While( ?? )
{
If(< A[]) A[] ;
( )/ ;
}


20
0
There is an error in the above
pseudocode. Try spotting it.
(thanks to Karan Singh for pointing it out in the class)
Update(, )

Update(, )
( ) + ;
A[] ;
( )/ ;
While( ?? )
{
If( A[ +]< A[ +])
A[] A[ +]
else
A[] A[ +] ;
( )/ ;
}


21
0
This is the correct pseudocode.
Report-Min(,)
Report-Min(,)
( ) + ;
+ ;
min A();
If ( > )
{ If (A()< min) min A();
While( ?? )
{
If(%2=1 and A( +)< min) min A( +);
If(%2=0 and A( )< min) min A( );
( 1)/2 ;
( 1)/2 ;
}
}
return min;

22
( 1)/2 <> ( 1)/2
Proof of correctness
Let T be the tree data structure for Dynamic Range-minima problem.
Let u be any node in T.

Question:
What can we say about value(u) after a series of operations ?

After every operation:
value(u) is minimum among all values stores in the leaf nodes of subtree(u).

To prove the correctness of our datastructure/algorithm, you need to prove that the above
mentioned assertion holds after each Update() operation. (Do it as a small exercise (4-5
sentences only)).



23
Part 2
24
A data structure for set for two special operations
Sets under special operations
Given: a collection of singleton sets {}, {}, {}, { }
Aim: a compact data structure to perform the following operations efficiently.
Union(, ):
Unite the two sets containing and .
Same_sets(, ):
Determine if and belong to the same set.


Trivial Solution: Keep an array Label[] such that Label[]=Label[] if and only if and belong
to the same set.
O() time for Union(, ) and O(1) time for Same_sets(, ).

In the next class, we shall discuss a rooted tree data structure for the above problem which
achieves much faster time for each operation. There is a famous historical background of
this problem and its solution.







25

Você também pode gostar