• Non ci sono risultati.

Representing Trees

N/A
N/A
Protected

Academic year: 2021

Condividi "Representing Trees"

Copied!
9
0
0

Testo completo

(1)

Paolo Ferragina, Università di Pisa

Representing Trees

Paolo Ferragina

Dipartimento di Informatica, Università di Pisa

(2)

Standard representation

Binary tree: each node has two

pointers to its left and right children An n-node tree takes

2n pointers or 2n lg n bits.

Supports finding left child or right child of a node (in constant time).

For each extra operation (eg. parent, subtree size) we have to pay additional n lg n bits each.

x

x x x

x

x x x x

(3)

Can we improve the space bound?

 There are less than 2

2n

distinct binary trees on n nodes.

 2n bits are enough to distinguish between any two different binary trees.

 Can we represent an n node binary tree using 2n bits,

and still be able to navigate it in constant time ?

(4)

Binary tree representation

 A binary tree on n nodes can be represented using 2n+o(n) bits to support:

parent

left child

right child

in constant time.

(5)

Heap-like notation for a binary tree

1 1

1 1

1 1

1

1

0

0 0 0

0 0

0 0

0 Add external nodes

Label internal nodes with a 1 and external nodes with a 0

Write the labels in level order

1 1 1 1 0 1 1 0 1 0 0 1 0 0 0 0 0

One can reconstruct the tree from this sequence

An n node binary tree can be represented in 2n+1 bits.

What about the operations?

(6)

Heap-like notation for a binary tree

1 1 1 1 0 1 1 0 1 0 0 1 0 0 0 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

8

5 6 7

4

3 2

1

9

17 16

15 14

12 13 11

10 1

8 7

6 5

4

2 3

1 2 3 4 5 6 7 8 Let x be the green code of an internal node or a leaf:

- parent(x) = ⌊x/2⌋ is the code on red Let x be the code of an internal node:

left child(x) = 2x is the code on green

right child(x) = 2x+1 is the code on green

If bit is 0 then pointer is NULL

x  x: # 1’s up to x (Rank1(x)) x  x: position of x-th 1 (Select1(x))

(7)

Arbitrary fan-out

A rooted ordered tree (on n nodes, arbitrary fan-out):

Navigational operations:

- parent(x) = a - first child(x) = b - next sibling(x) = c

Other useful operations:

- degree(x) = 2

- subtree size(x) = 4

x

a

b

c

(8)

Level-order degree sequence (LOUDS)

3 2 0 3 0 1 0 2 0 0 0 0

But, this still requires n lg n bits

Solution: write degree k in unary 1

k

0

3 2 0 3 0 1 0 2 0 0 0 0 1 1 1 0 1 1 0 0 1 1 1 0 0 1 0 0 1 1 0 0 0 0 0

Takes 2n-1 bits

(every node is represented twice, as 0 and as 1, except the root represented only as 0)

Write the degree sequence in level order

3

2 0 3

0 0

0 0 0

0

1 2

A tree is uniquely determined by its degree sequence

(9)

Supporting operations

1 0 1 1 1 0 1 1 0 0 1 1 1 0 0 1 0 0 1 1 0 0 0 0 0 1 2 3 4 5 6 7 8 9 10 11 12

Add a dummy root so that each node has a corresponding 1

1

2 3 4

5 6 7 8 9

10 11 12

Next sibling(k) = k+1 if B[Select_1(k)+1] ≠ 0 Parent(k) = # 0’s up to the k-th 1

First_child(k): y=Select_0(k)+1 if B[y] = 0 then leaf

else return y-k [i.e. #1 up to y]

Node k corresponds to the k-th 1 in the sequence

Children of k correspond to the 1s following the k-th 0

In 2n+o(n) bits and constant time per ops.

No support for subtree size.

degree(k)

= Select_0(k+1) – ( Select_0(k) + 1 )

Riferimenti

Documenti correlati

Chapter 4 develops a model selection to multivariate time series of large dimension through graphical vector autoregressive models and introducing sparsity on the structure of

Summary: Singular generalized function groups are studied.After recalling the fundamental notions and definitions, we prove that for a singular group some properties of ordinary

78 Department of Physics and Astronomy, University College London, London, United Kingdom 79 Louisiana Tech University, Ruston LA, United States of America. 80 Laboratoire de

In the first case, we simply update pointers of the parent node, and thus of all the ancestors, which are at most O(h) In the second case, we replace the erased node with the

In this paper, we present randomized algorithms over binary search trees such that: (a) the insertion of a set of keys, in any fixed order, into an initially empty tree always

Supports finding left child or right child of a node (in constant time). For each extra

Oxygen and carbon isotope data of carbonate minerals have already been published for several nonsulfide deposits in the Middle East, as Hakkari in Turkey [8,11], Angouran [4,6],

While our discussion focussed on the Ewens–Pitman sampling model, the unilateral stable distribution also arises in the study of the asymptotic behavior of a more general class