27
Trees Trees By Charl du Plessis By Charl du Plessis

Trees By Charl du Plessis. Contents Basic Terminology Basic Terminology Binary Search Trees Binary Search Trees Interval Trees Interval Trees Binary Indexed

Embed Size (px)

Citation preview

TreesTrees

By Charl du PlessisBy Charl du Plessis

ContentsContents

• Basic TerminologyBasic Terminology

• Binary Search TreesBinary Search Trees

• Interval TreesInterval Trees

• Binary Indexed TreesBinary Indexed Trees

Basic TerminologyBasic Terminology

• Connected graph with no cyclesConnected graph with no cycles• Unique path from every vertex to every Unique path from every vertex to every

other vertexother vertex• E = V-1 in any tree, where E and VE = V-1 in any tree, where E and V

are the number of edges and vertices are the number of edges and vertices respectively.respectively.

Root

Leaves

1

2 3

4 7

9 10

5

11

Binary Search TreesBinary Search Trees

10

5 12

3 16

6 9

8

15

• Structure?Structure?• Operations: O(h)Operations: O(h)

- query- query- delete - delete - insert - insert

Cons:Cons:

Degenerate trees make all operations Worst case O(n)Degenerate trees make all operations Worst case O(n)

But can be “self-balanced” such as in But can be “self-balanced” such as in

Red-Black Trees.Red-Black Trees.

C++ stl <set> uses self-balancing binary treesC++ stl <set> uses self-balancing binary trees

Interval Trees: ProblemInterval Trees: Problem

Consider the problem:Consider the problem:• You have a set of n items; 1,…, n You have a set of n items; 1,…, n • There is some number of each itemThere is some number of each item• You want to increment the number of You want to increment the number of

each item in range [x,y] by one. (for each item in range [x,y] by one. (for each i, x <= i <= y)each i, x <= i <= y)

• And query how many of item i there And query how many of item i there areare

Interval Trees: StructureInterval Trees: Structure

• Use a binary tree, where each node Use a binary tree, where each node holds a count over range [A,B]holds a count over range [A,B]

• Root holds count over [1,n]Root holds count over [1,n]• Node ranging over [A,B] will have Node ranging over [A,B] will have

children [A, (A+B)/2] and children [A, (A+B)/2] and [(A+B)/2+1, B][(A+B)/2+1, B]

• The leaves of the tree will store The leaves of the tree will store count over a single item rangecount over a single item range

Interval Trees: RepresentationInterval Trees: Representation

Representation of tree:Representation of tree:• Use an arrayUse an array• Index nodes from 1Index nodes from 1• Children of node i will be 2*i and 2*i +1Children of node i will be 2*i and 2*i +1

1

2 3

4 65 7

Interval Trees: Updating a rangeInterval Trees: Updating a range

1[1,4](1)

2[1,2](3)

3[3,4](0)

4[1,1](1)

6[3,3](2)

5[2,2](3)

7[4,4](0)

Interval Trees: Updating a rangeInterval Trees: Updating a range

1[1,4](1)

2[1,2](3)

3[3,4](0)

4[1,1](1)

6[3,3](2)

5[2,2](3)

7[4,4](0)

[x,y] = [3,4][x,y] = [3,4]

Interval Trees: Updating a rangeInterval Trees: Updating a range

1[1,4](1)

2[1,2](3)

3[3,4](1)

4[1,1](1)

6[3,3](2)

5[2,2](3)

7[4,4](0)

[x,y] = [3,4][x,y] = [3,4]

Interval Trees: Updating a rangeInterval Trees: Updating a range

1[1,4](1)

2[1,2](3)

3[3,4](1)

4[1,1](1)

6[3,3](2)

5[2,2](3)

7[4,4](0)

[x,y] = [2,4][x,y] = [2,4]

Interval Trees: Updating a rangeInterval Trees: Updating a range

1[1,4](1)

2[1,2](3)

3[3,4](1)

4[1,1](1)

6[3,3](2)

5[2,2](3)

7[4,4](0)

[x,y] = [2,4][x,y] = [2,4]

Interval Trees: Updating a rangeInterval Trees: Updating a range

1[1,4](1)

2[1,2](3)

3[3,4](1)

4[1,1](1)

6[3,3](2)

5[2,2](4)

7[4,4](0)

[x,y] = [2,4][x,y] = [2,4]

Interval Trees: Updating a rangeInterval Trees: Updating a range

1[1,4](1)

2[1,2](3)

3[3,4](2)

4[1,1](1)

6[3,3](2)

5[2,2](4)

7[4,4](0)

[x,y] = [2,4][x,y] = [2,4]

Interval Trees: Updating CodeInterval Trees: Updating Code

Updating a range [x,y]:Updating a range [x,y]:• Recurse into the tree, starting at rootRecurse into the tree, starting at root• Suppose [A,B] is the current interval being Suppose [A,B] is the current interval being

considered:considered:If [x,y] overlaps [A,B]:If [x,y] overlaps [A,B]:

Increment count of nodeIncrement count of nodeElse if [A,B] contains none of [x,y]:Else if [A,B] contains none of [x,y]:

StopStopElse:Else:

Recurse into Left Child.Recurse into Left Child.Recurse into Right Child.Recurse into Right Child.

Interval Trees: QueryInterval Trees: Query

1[1,4](1)

2[1,2](3)

3[3,4](2)

4[1,1](1)

6[3,3](2)

5[2,2](4)

7[4,4](0)

Retrieve value Retrieve value of item 3of item 3

1 1

Interval Trees: QueryInterval Trees: Query

1[1,4](1)

2[1,2](3)

3[3,4](2)

4[1,1](1)

6[3,3](2)

5[2,2](4)

7[4,4](0)

Retrieve value Retrieve value of item 3of item 3

1+21+2

Interval Trees: QueryInterval Trees: Query

1[1,4](1)

2[1,2](3)

3[3,4](2)

4[1,1](1)

6[3,3](2)

5[2,2](4)

7[4,4](0)

Retrieve value Retrieve value of item 3of item 3

1+2+2 = 51+2+2 = 5

Interval Trees: Query CodeInterval Trees: Query CodeQuerying the number of item i:Querying the number of item i:

- Let sum = 0- Let sum = 0- Recurse into tree.- Recurse into tree.- Considering range [A,B]:- Considering range [A,B]:

sum += count of [A,B]sum += count of [A,B]If (A != B):If (A != B):

Recurse into child containing iRecurse into child containing iin its range.in its range.

Binary Indexed Trees: ProblemBinary Indexed Trees: ProblemProblem: Suppose we have a row of n numbers.Problem: Suppose we have a row of n numbers.

We want to:We want to:

1.) Increment value of the ith number1.) Increment value of the ith number

2.) Query the sum of the values in range2.) Query the sum of the values in range

[k,j][k,j]

BIT implementation:BIT implementation:

- Query can be done in O(log n)- Query can be done in O(log n) for worst case for worst case

- Short code- Short code

Radix trees are equivalent in efficiencyRadix trees are equivalent in efficiency

Binary Indexed Trees: IntroductionBinary Indexed Trees: Introduction• Interested in counts over ranges of the form Interested in counts over ranges of the form

[1,i][1,i]• Uses the fact every number can be written as Uses the fact every number can be written as

the sum of powers of 2.the sum of powers of 2.• We will use this to represent ranges [1,i]We will use this to represent ranges [1,i]• 13 = 8 + 4 + 113 = 8 + 4 + 1• [1, 13] = [1, 8] + [9, 12] + [13, 13][1, 13] = [1, 8] + [9, 12] + [13, 13]

Binary Indexed Trees: IntervalsBinary Indexed Trees: Intervals• We store interval counts of the form [i -2^r+1, We store interval counts of the form [i -2^r+1,

i] where r is the position of the last non-zero i] where r is the position of the last non-zero digit of i in binary formdigit of i in binary form

• Example: Example:

13 = 1011 in binary, position of last non-zero 13 = 1011 in binary, position of last non-zero digit is 0.digit is 0.

4 = 100 in binary, position of last non-zero 4 = 100 in binary, position of last non-zero digit is 2.digit is 2.

• How it works will become clear when we explain How it works will become clear when we explain the structure of the treethe structure of the tree

Binary Indexed Trees: StructureBinary Indexed Trees: Structure

f[i] = value at index if[i] = value at index i

c[i] = f[1] + f[2] + … + f[i]c[i] = f[1] + f[2] + … + f[i]

tree[i] = count of [i-2^r +1,i]tree[i] = count of [i-2^r +1,i]

ii (i-2^r+1)…i(i-2^r+1)…i

11 11

22 1…21…2

33 33

44 1…41…4

55 55

66 5…65…6

77 77

88 1…81…8

99 99

1010 9…109…10

1111 1111

1212 9…129…12

1313 1313

1414 13…1413…14

1515 1515

1616 1…161…16

Binary Indexed Trees: StructureBinary Indexed Trees: Structure

Binary Indexed Trees: QueryBinary Indexed Trees: Query

int read(int idx){

int sum = 0;while (idx > 0){

sum += tree[idx];idx -= (idx & -idx);

}return sum;

}

Binary Indexed Trees: UpdateBinary Indexed Trees: Update

void update(int idx ,int val){

while (idx <= MaxVal){

tree[idx] += val;idx += (idx & -idx);

}}

QuestionsQuestions

??