Docsity

Binary Search Trees: Static Dictionaries and Cost Analysis, Slides of Computer Science

Toggle sidebar
bg1
Static Dictionaries
•Collection of items.
•Each item is a pair.
(key, element)
Pairs have different keys.
•Operations are:
initialize/create
get (search)
•Each item/key/element has an estimated access
frequency (or probability).
•Consider binary search tree only.
Example
0.1c
0.1b
0.8a
ProbabilityKey
a
b
c
a < b < c
Cost = 0.8 * 2 + 0.1 * 1 + 0.1 * 2
= 1.9
a
b
c
Cost = 0.8 * 1 + 0.1 * 2 + 0.1 * 3
= 1.2
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Binary Search Trees: Static Dictionaries and Cost Analysis and more Slides Computer Science in PDF only on Docsity!

Static Dictionaries

  • Collection of items.
  • Each item is a pair. ƒ (key, element) ƒ Pairs have different keys.
  • Operations are: ƒ initialize/create ƒ get (search)
  • Each item/key/element has an estimated access frequency (or probability).
  • Consider binary search tree only.

Example

c 0.

b 0.

a 0.

Key Probability

a

b

c

a < b < c

Cost = 0.8 * 2 + 0.1 * 1 + 0.1 * 2 = 1.

a b

c

Cost = 0.8 * 1 + 0.1 * 2 + 0.1 * 3 = 1.

Search Types

  • Successful. ƒ Search for a key that is in the dictionary. ƒ Terminates at an internal node.
  • Unsuccessful. ƒ Search for a key that is not in the dictionary. ƒ Terminates at an external/failure node.

f 0

a

b

c f 1 f 2 f 3

Internal And External Nodes

  • A binary tree with n internal nodes has n + 1 external nodes.
  • Let s 1 , s 2 , …, sn be the internal nodes, in inorder.
  • key(s 1 ) < key(s 2 ) < … < key(sn).
  • Let key(s 0 ) = –infinity and key(sn+1) = infinity.
  • Let f 0 , f 1 , …, fn be the external nodes, in inorder.
  • fi is reached iff key(si) < search key < key(si+1).

f 0

a

b

c f 1 f 2 f 3

Dynamic Programming

  • Keys are a 1 < a 2 < …< an.
  • Let Ti j= least cost tree for ai+1, ai+2, …, aj.
  • T0n= least cost tree for a 1 , a 2 , …, an.

a 3

a 5

a 6

f^ a^4 a^7 2

f 3

f 5

f 4 f 6 f^7

  • Ti j includes pi+1, pi+2, …, pj and qi, qi+1, …, qj.

T2,

  • Ti j= least cost tree for ai+1, ai+2, …, aj.
  • ci j= cost of Ti j

= Σi <= u <= j qu (level(fu) – 1)

  • Σi < u <= j pu * level(su).
  • ri j= root of Ti j.
  • wi j= weight of Ti j

= sum of ps and qs in Ti j = pi+1+ pi+2+ …+ pj + qi + qi+1 + … + qj

Terminology

a 3

a 5

a 6

f^ a^4 a^7 2

f 3

f 5

f 4 f 6 f^7

T2,

i = j

  • Ti j includes pi+1, pi+2, …, pj and qi, qi+1, …, qj.
  • Ti i includes qi only.

fi Tii

  • ci i = cost of Ti i = 0.
  • ri i = root of Ti i = 0.
  • wi i = weight of Ti i

= sum of ps and qs in Ti i = qi

i < j

  • Ti j= least cost tree for ai+1, ai+2, …, aj.
  • Ti j includes pi+1, pi+2, …, pj and qi, qi+1, …, qj.
  • Let ak, i < k <= j, be in the root of Ti j. ak

L R

Ti j

  • L includes pi+1, pi+2, …, pk-1 and qi, qi+1, …, qk-1.
  • R includes pk+1, pk+2, …, pj and qk, qk+1, …, qj.

a 3

a 5 a 6

f 2 a 4 a^7 f 3

f 5

f 4 f^6 f^7

cij

  • Contribution of L to cij is cost(L) + wi k-1.
  • Contribution of R to cij is cost(R) + wkj.
  • cij = cost(L) + wi k-1 + cost(R) + wkj + pk = cost(L) + cost(R) + wij

ak

L R

Ti j

a 3

a 5

a 6

f^ a 4 a^7 2

f 3

f 5

f 4 f 6 f^7

cij

  • cij = cost(L) + cost(R) + wij
  • cost(L) = cik-
  • cost(R) = ckj
  • cij = cik-1 + ckj + wij
  • Don’t know k.
  • cij = mini < k <= j{cik-1 + ckj} + wij

ak

L R

Ti j

a 3

a 5

a 6

f^ a 4 a^7 2

f 3

f 5

f 4 f 6 f^7

cij

  • cij = mini < k <= j{cik-1 + ckj} + wij
  • rij = k that minimizes right side.

ak

L R

Ti j

Computation Of c0n And r0n

  • Start with ci i = 0, ri i = 0, wi i = qi, 0 <= i <= n (zero-key trees).
  • Use cij = mini < k <= j{cik-1 + ckj} + wij to compute cii+1, ri i+1, 0 <= i <= n – 1 (one-key trees).
  • Now use the equation to compute cii+2, ri i+2, 0 <= i <= n – 2 (two-key trees).
  • Now use the equation to compute cii+3, ri i+3, 0 <= i <= n – 3 (three-key trees).
  • Continue until c0n and r0n(n-key tree) have been computed.

Construct T0n

  • Root is r0n.
  • Suppose that r0n = 10.

a 10 T0n

T 09 T10,n

  • Construct T 09 and T10,n recursively.
  • Time is O(n).