Docsity

Multi-way Search Trees and B-Trees: (a,b) Trees and Applications, Slides of Java Programming

Toggle sidebar
bg1
(a,b) Trees, (2,4) Trees, B-Trees
CalinC urescu, some material from Goodrich & Tamas sia
(a,b) Trees, (2,4) Trees, B-Trees
Calin Curescu, some material from Goodrich & Tamassia
15 pages
TDDC32 lecture 4, 2006
(a,b) Trees, (2,4) Trees, B-Trees
CalinC urescu, some material from Goodrich & Tamas sia
2of 15
TDDC32 lecture 4, 2006
Multi-way search trees
•Ordered tree
– A Node is a d-node if it has dchildren (d– order of node)
– Each d-node stores d-1 keys
• An entry is composed of a (key, value) pair
(In next examples we will show only the keys)
•x1
≤
xi
≤
xd-1 and xi-1
≤
yi
≤
xiwhere:
–xiare the keys of the a node v
–yiare the keys of the subtree i rooted at v
– we assume x0= 0 and xd=
∞
•Example
–0
≤
y1
≤
x1
•How do we search?
y1y2y3
x1x2
(a,b) Trees, (2,4) Trees, B-Trees
CalinC urescu, some material from Goodrich & Tamas sia
3of 15
TDDC32 lecture 4, 2006
(a,b) Trees
•Multi-way search trees where:
–2
≤
a
≤⎡
b /2
⎤
(e.g
⎡
1.3
⎤
= 2)
–Size Property: Every internal node has
• at least achildren if not root (at least 2 children for root)
– at least a-1keys per node
• every internal node has at most b children
– at most b-1keys per node
–Depth Property: All the leaves (external nodes) are on the
same level (i.e. have the same depth)
•Height of an (a,b) tree of n entries is O(log n)
– because at every level we have ainodes, and
– because O(logan) = O(log n/log a)
•Search is O(log n)
–because at every level we perform at most b comparisons
–thus: O( f(b) / log a * log n ) = O(log n)
(a,b) Trees, (2,4) Trees, B-Trees
CalinC urescu, some material from Goodrich & Tamas sia
4of 15
TDDC32 lecture 4, 2006
Use of B-trees: external searching
•Memory
– Primary – small and fast
– Secondary – large and slow (e.g. disk)
– One should do operations in primary memory
•A transfer – disk block ÅÆ memory
– One should minimize transfers
•Multi-way search trees:
– Ideally one node should fill one disk block
• For not wasting disk space and transfers times
– Faster search compared to binary trees
• Multi-way trees have smaller heights compared to binary
trees
•O(logBn) = O(log n / log B) vs. O(log n)
–Bnr of entries / block
(a,b) Trees, (2,4) Trees, B-Trees
CalinC urescu, some material from Goodrich & Tamas sia
5of 15
TDDC32 lecture 4, 2006
Insertion example
•Example: inserting key 30 causes an overflow
27 32 35
10 15 24
2 8 12 18
10 15 24
2 8 12 27 30 32 3518
v
v
(a,b) Trees, (2,4) Trees, B-Trees
CalinC urescu, some material from Goodrich & Tamas sia
6of 15
TDDC32 lecture 4, 2006
Insertion in (a,b) trees
•Add the new key to the suitable leaf
– Where the search of key brought us
•If overflow (i.e. leaf increased to bkeys)
–Split node in two
– Promote the median key
• i.e. move median key to parent
– If parent has an overflow then repeat
•If root has an overflow the height of the tree increases
•Insertion complexity O(log n)
– Similar to search
– Cause we manipulate a fixed number of keys per node
– Tree height is O(log n)
pf3

Partial preview of the text

Download Multi-way Search Trees and B-Trees: (a,b) Trees and Applications and more Slides Java Programming in PDF only on Docsity!

Calin Curescu, some material from Goodrich & Tamassia(a,b) Trees, (2,4) Trees, B-Trees

(a,b) Trees, (2,4) Trees, B-Trees

Calin Curescu, some material from Goodrich & Tamassia

TDDC32 lecture 4, 200615 pages (a,b) Trees, (2,4) Trees, B-TreesCalin Curescu, some material from Goodrich & Tamassia TDDC32 lecture 4, 20062 of 15

Multi-way search trees

  • Ordered tree
    • A Node is a d-node if it has d children ( d – order of node)
    • Each d-node stores d-1 keys
      • An entry is composed of a (key, value) pair (In next examples we will show only the keys)
  • x 1 ≤ xi ≤ xd-1 and xi-1 ≤ yi ≤ xi where:
    • x (^) i are the keys of the a node v
    • y (^) i are the keys of the subtree i rooted at v
    • we assume x 0 = 0 and x (^) d = ∞
  • Example
    • 0 ≤ y 1 ≤ x (^1)
  • How do we search?

y 1 y 2 y 3

x 1 x (^2)

Calin Curescu, some material from Goodrich & Tamassia^ (a,b) Trees, (2,4) Trees, B-Trees TDDC32 lecture 4, 20063 of 15

(a,b) Trees

  • Multi-way search trees where:
    • 2 ≤ a ≤ ⎡ b /2 ⎤ (e.g ⎡ 1.3 ⎤ = 2)
    • Size Property : Every internal node has
      • at least a children if not root (at least 2 children for root^ )
        • at least a-1 keys per node
  • every internal node has at most b children
  • at most b-1 keys per node
    • Depth Property : All the leaves (external nodes) are on the same level (i.e. have the same depth)
  • Height of an (a,b) tree of n entries is O(log n)
    • because at every level we have ai^ nodes, and
    • because O(loga n) = O(log n/log a)
  • Search is O(log n)
    • because at every level we perform at most b comparisons
    • thus: O( f(b) / log a * log n ) = O(log n)

(a,b) Trees, (2,4) Trees, B-TreesCalin Curescu, some material from Goodrich & Tamassia TDDC32 lecture 4, 20064 of 15

Use of B-trees: external searching

  • Memory
    • Primary – small and fast
    • Secondary – large and slow (e.g. disk)
    • One should do operations in primary memory
  • A transfer – disk block ÅÆ memory
    • One should minimize transfers
  • Multi-way search trees:
    • Ideally one node should fill one disk block
      • For not wasting disk space and transfers times
    • Faster search compared to binary trees
      • Multi-way trees have smaller heights compared to binary trees
      • O(logB n) = O(log n / log B) vs_. O(log n)_
        • B nr of entries / block

Insertion example

  • Example: inserting key 30 causes an overflow

27 32 35

10 15 24 2 8 12 18

10 15 24 2 8 12 18 27 30 32 35

v

v

(a,b) Trees, (2,4) Trees, B-TreesCalin Curescu, some material from Goodrich & Tamassia TDDC32 lecture 4, 20066 of 15

Insertion in (a,b) trees

  • Add the new key to the suitable leaf
    • Where the search of key brought us
  • If overflow (i.e. leaf increased to b keys)
    • Split node in two
    • Promote the median key
      • i.e. move median key to parent
    • If parent has an overflow then repeat
  • If root has an overflow the height of the tree increases
  • Insertion complexity O(log n)
    • Similar to search
    • Cause we manipulate a fixed number of keys per node
    • Tree height is O(log n)

Calin Curescu, some material from Goodrich & Tamassia(a,b) Trees, (2,4) Trees, B-Trees TDDC32 lecture 4, 20067 of 15

Alternative Insertion in (a,b) Trees

  • Alternative to node splitting (not presented in coursebook):
    • If one of the (at most 2) nearest siblings that has not b (maximum) children we can transfer keys: - Merge the two siblings - Using also the key of parent node situated between the siblings - Promote the new median key
    • Advantages
      • The tree is not increased and space for storing the extra nodes is saved
    • Disadvantages
      • We might have to do a split anyway at some upcoming insertion
      • We have to exchange keys between parent and children nodes

(a,b) Trees, (2,4) Trees, B-TreesCalin Curescu, some material from Goodrich & Tamassia TDDC32 lecture 4, 20068 of 15

Deleting example

  • May cause underflow

27 32 35

10 15 24 2 8 12 18

32 35

10 15 27 2 8 12 18

Calin Curescu, some material from Goodrich & Tamassia^ (a,b) Trees, (2,4) Trees, B-Trees TDDC32 lecture 4, 20069 of 15

Delete from (a,b) Trees

  • If key to be deleted not in a leaf
    • Find successor or predecessor
    • Swap entry with found entry
  • If leaf contains more than a-1 (minimum) keys
    • Delete entry in leaf
  • Else we have an underflow situation

(a,b) Trees, (2,4) Trees, B-TreesCalin Curescu, some material from Goodrich & Tamassia TDDC32 lecture 4, 200610 of 15

Underflow example

u

w v

u

w v

u

v

u

w v '

Underflow in (a,b) Tree

  • TRANSFER or FUSION
  • If one of the (at most 2 ) nearest siblings has more than a- (minimum) keys we can transfer keys: - Merge the leaf with the sibling - Using also the key of parent node situated between the siblings - Promote the new median key
  • Else do a fusion:
    • Merge the leaf with the sibling
      • Using also the key of parent node situated between the siblings
    • Parent looses the key
    • If parent has too few entries repeat the process upwards

(a,b) Trees, (2,4) Trees, B-TreesCalin Curescu, some material from Goodrich & Tamassia TDDC32 lecture 4, 200612 of 15

(2,4) Trees

  • Is an (a,b) tree with a=2 and b=
    • At least 2 and at most 4 children
    • At least 1 and at most 3 keys in a node
  • Insert
    • When splitting the median key is the 3 rd
  • Delete
    • If a nearest sibling has 2 or 3 keys then transfer
    • Else do a fusion
  • Note! - In coursebook they always use one more level (with empty trees) for implementation purposes - This applies also to binary search trees (including AVL, splay)