158
SNU IDB Lab. Ch 16. Balanced Search Trees © copyright 2006 SNU IDB Lab.

Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

  • Upload
    others

  • View
    14

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

SNU

IDB Lab.

Ch 16. Balanced Search Trees

© copyright 2006 SNU IDB Lab.

Page 2: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

2SNU

IDB Lab.Data Structures

Bird’s-Eye View (0)� Chapter 15: Binary Search Tree

� BST and Indexed BST

� Chapter 16: Balanced Search Tree

� AVL tree: BST + Balance

� B-tree: generalized AVL tree

� Chapter 17: Graph

Page 3: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

3SNU

IDB Lab.Data Structures

Bird’s-Eye View� Balanced tree structures

- Height is O(log n)

� AVL� Binary Search Tree with Balance

� Red-black trees

� Splay trees� Individual dictionary operation � 0(n)

� Take less time to perform a sequence of u operations � 0(u log u)

� B-trees (Balanced Tree)� Suitable for external memory

Page 4: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

4SNU

IDB Lab.Data Structures

Table of Contents

� AVL TREES

� Definition

� Searching an AVL Search Tree� Inserting into an AVL Search Tree� Deletion from an AVL Search Tree

� RED-BLACK TREES

� SPLAY TREES

� B-TREES

Page 5: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

5SNU

IDB Lab.Data Structures

The History of Balanced Trees

� Adel'son-Vel'skiĭ and Landis introduced AVL tree in 1962

� Ensures balance by restricting every node's depth to differ at most by 1

� Bayer and McCreight introduced B-tree in 1972

� Kept balanced by requiring that all leaf nodes are at the same depth

� Join or split is needed instead of re-balancing

� Bayer, Guibas and Sedgewick introduced Red-black tree in 1978

� Ensures balance by restricting the occurrence of red nodes in the tree

� Sleator and Tarjan introduced Splay tree in 1983

� Maintains balance without any explicit balance condition such as color

� Splay operations are performed within the tree every time an access is made

Page 6: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

6SNU

IDB Lab.Data Structures

AVL TREES� Balanced tree

� Trees with a worst-case height of O(log n)

� AVL search tree� Balanced binary search trees� Can be generalized to a B-tree

� A height-balanced k tree (HB(k) tree)

� Allowable height difference of any two sub-trees is k

� AVL Tree : HB(1) Tree� G.M. Adel’son, Vel’skii, E.M. Landis

� Performance � Given N keys, worst-case search � 1.44 log2(N+2)

cf. Completely balanced AVL tree : worst-case search � log2(N+1)

Page 7: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

7SNU

IDB Lab.Data Structures

Height of an AVL Tree

� n : nodes in AVL tree� Nh : min number of nodes in an AVL tree of height h� Nh = Nh-1 + Nh-2 + 1, N0 = 0, and N1 = 1

� Similar in definition to Fibonacci numbersFh = Fn-1 + Fn-2., F0 = 0 and F1 = 1

It can be shown that Nh = Fh+2 - 1 for h > 0 � Fibonacci theory: Fh ≒ Øh/√5 where Ø = (1 + √5)/2therefore Nh ≒ Øh+2/√5-1

� If there are n nodes then its height h = logØ(√5(n+1)) - 2 ≒1.44log2(n+2) h = O(log n)

Page 8: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

8SNU

IDB Lab.Data Structures

AVL Tree Definition

� An empty binary tree is an AVL Tree

� If T is a nonempty binary tree with TL and TR as its left and right subtrees, then T is an AVL tree iff

(1) TL and TR are AVL Trees and

(2) | hL - hR| ≤ 1 where hL and hR are the heights of TL and TR, respectively

� For any node in tree T in AVL tree, BF(T) should be one of “ -1, 0, 1”

� If BF(T) is -2 or 2, then proper rotation is performed in order to get balance

� Conceptually AVL search tree = AVL tree + Binary Search Tree

Page 9: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

9SNU

IDB Lab.Data Structures

AVL Tree Examples

(a) AVL Trees

X X

X X

(b) Non - AVL Trees

Page 10: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

10SNU

IDB Lab.Data Structures

Intuition: AVL Search Tree� AVL Search Tree = Binary Search Tree + AVL Tree

= Balanced Binary Search Tree

20

12 18

15 25

22

30

405

2

60

70

8065

XOXAVL ST

XOOAVL

OOXBST

( c )( b )( a )

Page 11: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

11SNU

IDB Lab.Data Structures

Indexed AVL Search Tree

� Indexed AVL search Tree

= AVL Tree + LeftSize variable

= (Balanced + Binary Search Tree) + LeftSize variable

MAY

AUG

APR

NOV

MAR

3

1

1

0

1

Page 12: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

12SNU

IDB Lab.Data Structures

Representation of an AVL Tree� Balance factor bf(x) of a node x = height of left subtree – height of right subtree

� Permissible balance factors: (-1, 0, 1)

30

35

5 40

20

12 18

15 25

30

-1

0 1

0

0

0

0 0 0

-1

Page 13: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

13SNU

IDB Lab.Data Structures

AVL Search Tree Example (1)

New Identifier

MARCH

After Insertion No Rebalancing needed

0MAR

New Identifier

MAY

After Insertion No Rebalancing needed

New Identifier

NOVEMBER

After Insertion After Rebalancing

-1MAR

0MAY

-2MAR

-1MAY

0NOV

0MAY

0MAR

0NOV

RR

Page 14: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

14SNU

IDB Lab.Data Structures

AVL Search Tree Example (2)

New Identifier

AUGUST

After Insertion No Rebalancing needed

+1MAY

+1MAR

0AUG

0NOV

Page 15: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

15SNU

IDB Lab.Data Structures

AVL Search Tree Example (3)

New Identifier

APRIL

After Insertion After Rebalancing

+2MAY

+2MAR

+1AUG

0NOV

0APR

+1MAY

0AUG

0APR

0NOV

0MAR

LL

Page 16: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

16SNU

IDB Lab.Data Structures

AVL Search Tree Example (4)

+2MAY

-1AUG

0APR

0NOV

+1MAR

New Identifier

JANUARY

After Insertion After Rebalancing

0JAN

0MAR

0AUG

-1MAY

0JAN

0NOV

0APR

LR

Page 17: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

17SNU

IDB Lab.Data Structures

AVL Search Tree Example (5)

New Identifier

DECEMBER

After Insertion No Rebalancing needed

+1MAR

-1AUG

-1MAY

+1JAN

0NOV

0APR

0DEC

Page 18: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

18SNU

IDB Lab.Data Structures

AVL Search Tree Example (6)

New Identifier

JULY

After Insertion No Rebalancing needed

+1MAR

-1AUG

-1MAY

0JAN

0NOV

0APR

0DEC

0JUL

Page 19: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

19SNU

IDB Lab.Data Structures

AVL Search Tree Example (7)

New Identifier

FEBRUARY

After Insertion After Rebalancing

+2MAR

-2AUG

-1MAY

+1JAN

0NOV

0APR

-1DEC

0JUL

0FEB

+1MAR

0DEC

-1MAY

0JAN

+1AUG

0NOV

0APR

0FEB

0JUL

RL

Page 20: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

20SNU

IDB Lab.Data Structures

AVL Search Tree Example (8)

New Identifier

JUNE

After Insertion After Rebalancing

+2MAR

-1DEC

-1MAY

-1JAN

+1AUG

0NOV

0APR

0FEB

-1JUL

0JUN

0JAN

+1DEC

0MAR

0FEB

+1AUG

0APR

-1MAY

-1JUL

0JUN

-1NOV

LR

Page 21: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

21SNU

IDB Lab.Data Structures

AVL Search Tree Example (9)

-1JAN

+1DEC

-1MAR

0FEB

+1AUG

0APR

-2MAY

-1JUL

0JUN

-1NOV

New Identifier

OCTOBER

After Insertion

0OCT

After Rebalancing

RR

0JAN

+1DEC

0MAR

0FEB

+1AUG

0APR

0NOV

-1JUL

0JUN

0OCT

0MAY

Page 22: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

22SNU

IDB Lab.Data Structures

AVL Search Tree Example (11)

New Identifier

SEPTEMBER

After Insertion No Rebalancing needed

-1JAN

+1DEC

-1MAR

0FEB

+1AUG

0APR

-1NOV

-1JUL

0JUN

-1OCT0

MAY

0SEP

Page 23: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

23SNU

IDB Lab.Data Structures

Table of Contents

� AVL TREES� Definition

� Searching an AVL Search Tree

� Inserting into an AVL Search Tree

� Deletion from an AVL Search Tree

� RED-BLACK TREES

� SPLAY TREES

� B-TREES

Page 24: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

24SNU

IDB Lab.Data Structures

Searching in an AVL Search Tree

� search in binary search tree : Wish to Search for thekey from root to leaf

If (root == null) search is unsuccessful;elseif (thekey < key in root) only left subtree is to be searched;else if (thekey > key in root) only right subtree is to be searched;

else (thekey == key in root) search terminates successfully;

� Subtrees may be searched similarly in a recursive manner

� TimeComplexity = O(height)

� Height of an AVL tree with n element � O(log n): search time is O(log n)

Page 25: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

25SNU

IDB Lab.Data Structures

Table of Contents

� AVL TREES� Definition� Searching an AVL Search Tree� Inserting into an AVL Search Tree� Deletion from an AVL Search Tree

� RED-BLACK TREES

� SPLAY TREES

� B-TREES

Page 26: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

26SNU

IDB Lab.Data Structures

Unbalance due to Inserting

� When an insertion into an AVL Tree using the strategy of Program15.5 (insert in BST), the resulting tree is unbalanced

New element

30

35

5 40

-1

0 1

0

Page 27: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

27SNU

IDB Lab.Data Structures

Observations on Imbalance due to Insertion

O1: In the unbalanced tree the BFs are limited to –2, -1, 0, 1, 2

O2: A node with BF “2” had a BF “1” before the insertion

O3: The BF of only those nodes on the path from the root to the newly inserted node can change as a result of the insertion

O4: Let A denote the nearest ancestor of the newly inserted node whose BF is either –2 or 2. The BF of all nodes on the path from A to the newly inserted node was 0 prior to the insertion

O5: Imbalance can happen in the last node encountered that has a balance factor 1 or –1 prior to the insertion

Page 28: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

28SNU

IDB Lab.Data Structures

Node X with Potential Imbalance (1)

� Let X denote the last node encountered that has a balance factor 1 or –1prior to the insertion

� If the tree is unbalanced following the insertion, X exists

� If bf(x) = 0 after the insertion, then the height of the subtree with root X is the same before and after the insertion

0

30

35

5 40

-1

1

0

20

12 18

15 25

30

0

0

0 0 0

-1

20

12 18

15 25

30

0

0

0 0 0

-1

32

22

28 50 10 14 16 19

XX

No node X

Page 29: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

29SNU

IDB Lab.Data Structures

imbalancedbalancedbalancedbalanced

201bf(x)

h + 1hhheight

( c )( b )( a )

� The only way the tree can become unbalanced is when the insertion causes bf(x) to change from –1 to –2 or from 1 to 2.

Node X with Potential Imbalance (2)

Page 30: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

30SNU

IDB Lab.Data Structures

Imbalance Patterns due to Insertion

� The imbalance at A is one of the types

� LL (when new node is in the left subtree of the left subtree of A)

� LR (when new node is in the right subtree of the left subtree of A)

� RR (when new node is in the right subtree of the right subtree of A)

� RL (when new node is in the left subtree of the right subtree of A)

� LL and RR imbalances require single rotation

� LR and RL imbalances require double rotations

A

Insert YLL LR RL RR

Page 31: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

31SNU

IDB Lab.Data Structures

LL Rebalancing after Insertion

+1A

0B

BL BR

AR

h

h+2

+2A

0B

BL BR

AR

0B

0A

BR AR

BL

rotation typerotation typeLLLL

h+2

Balanced SubtreeUnbalanced following

insertion

Height of BL increase to h+1(BL < B < BR < A < AR)

Balanced Subtree

Page 32: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

32SNU

IDB Lab.Data Structures

RR RR Rebalancing Rebalancing after Insertion

-1A

0B

BL BR

AL

0B

0A

Al BL

BR

rotation typerotation typeRRRR

h+2

Balanced SubtreeUnbalanced following

insertion

Height of BR increase to h+1(AL < A < BL < B < BR)

h+2

-2A

0B

BL BR

AL

Balanced Subtree

Page 33: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

33SNU

IDB Lab.Data Structures

LR-a Rebalancing after Insertion

+1A

0B

Balanced Subtree Unbalanced followinginsertion

+1A

-1B

0C

Balanced Subtree

0C

0B

0A

rotation typerotation typeLR(a)LR(a)

(B < C < A)

Page 34: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

34SNU

IDB Lab.Data Structures

LRLR--b b Rebalancing Rebalancing after Insertion

Balanced SubtreeUnbalanced following

insertionBalanced Subtree

+1A

BL

0B

0C

CL CR

h

h-1

AR h+2

+2A

BL

-1B

+1C

CL CR

AR

0C

0B

-1A

BL CL CR AR

rotation typerotation typeLR(b)LR(b)

h

h+2

h

(BL < B < CL < C < CR < A < AR)

Page 35: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

35SNU

IDB Lab.Data Structures

LRLR--c c Rebalancing Rebalancing after Insertion

Balanced SubtreeUnbalanced following

insertionBalanced Subtree

+1A

BL

0B

0C

CL CR

h

h-1

AR h+2

+2A

BL

-1B

-1C

CL CR

AR

0C

+1B

0A

BL CL CR AR

rotation typerotation typeLR(c)LR(c)

h+2

RL a, b and c are symmetric to LR a, b and c

h

Page 36: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

36SNU

IDB Lab.Data Structures

Table of Contents

� AVL TREES� Definition� Searching an AVL Search Tree� Inserting into an AVL Search Tree� Deletion from an AVL Search Tree

� RED-BLACK TREES

� SPLAY TREES

� B-TREES

Page 37: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

37SNU

IDB Lab.Data Structures

Deletion from an AVL Tree

� Let q be the parent of the node that was physically deleted

� If the deletion took place from � the left subtree of q � bf(q) decreases by 1� the right subtree of q � bf(q) increases by 1

� Observations

D1 : If the new BF of q is 0, its height has decreased by 1.we need to change the BF of its parent (if any) and possibly those of its other ancestors

D2 : If the new BF of q is either –1 or 1, its height is the same as before the deletion and the BFs of tis ancestors and unchanged

D3 : If the new BF of q is either –2 or 2, the tree is unbalanced at q

Page 38: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

38SNU

IDB Lab.Data Structures

Imbalance Patterns due to Deletion

� Type L� If the deletion took place from A’s left subtree with root B

� Subclassified : L-1, L0 and L1 depending on bf(B)

� Type R� If the deletion took place from A’s right subtree with root B

� Subclassified : R-1, R0 and R1 depending on bf(B)

Page 39: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

39SNU

IDB Lab.Data Structures

R0 rotation after Deletion� Height of tree is h+2 (h+2) before (after) deletion

� Single rotation is sufficient

� BL < B < BR < A < AR

Page 40: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

40SNU

IDB Lab.Data Structures

R1 rotation after Deletion� Height of tree is h+2 (h+1) before (after) deletion

� Single rotation is sufficient

� BL < B < BR < A < AR

Page 41: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

41SNU

IDB Lab.Data Structures

R-1 rotation after Deletion� Height of tree is h+2 (h + 1) before (after) deletion

� Double rotations

� BL < B < CL < C < CR < A < AR

Page 42: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

42SNU

IDB Lab.Data Structures

Rotation Taxonomy in AVL

� Rotation types due to Insertion� LL type� RR type� LR type: LR-a, LR-b, LR-c� RL type: RL-a, RL-b, LR-c

� Rotation types due to Deletion� R type: R-1, R0, R1� L type: L-1, L0, L1

� LL rotation in insertion and R1 rotation in deletion are identical� LR rotation in insertion and R-1 rotation in deletion are identical� LL rotation in insertion and R0 rotation in deletion differ only in the final

BF of A and B

Page 43: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

43SNU

IDB Lab.Data Structures

Table of Contents

� AVL TREES

� RED-BLACK TREES� Definition� Searching a Red-Black Tree� Inserting into a Red-Black Tree� Deletion from a Red-Black Tree� Implementation Considerations and Complexity

� SPLAY TREES

� B-TREES

Page 44: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

44SNU

IDB Lab.Data Structures

Red-Black Tree vs. AVL Tree (1)

less balanced more balanced

AVL treeRed-Black tree

O(logn)

O(logn)

O(logn)

O(logn)Deletion

O(logn)Insertion

O(logn)Lookup

Page 45: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

45SNU

IDB Lab.Data Structures

Red-Black Tree vs. AVL Tree (2)

insert a node x

x x

Red-black tree doesn't need rebalancing AVL tree needs rebalancing

Page 46: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

46SNU

IDB Lab.Data Structures

Red-Black Tree: Definition

� Red-black tree� Binary Search tree

� Every node is colored red or black

RB1. Root and all external nodes are black.

RB2. No root-to-external-node path has two consecutive red nodes.

RB3. All root-to-external-node paths have the same number of black nodes

RB1’. Pointers from an internal node to an external node are black

RB2’. No root-to-external-node path has two consecutive red pointers

RB3’. All root-to-external-node paths have the same number of black pointers

equivalent

Page 47: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

47SNU

IDB Lab.Data Structures

Red-Black Tree: Example

� Every path from the root to an external node has exactly 2 black pointers and 3 black nodes

� No such path has two consecutive red nodes or pointers� Small black box nodes are for ensuring every node has two children� The color of newly inserted node is red

65

10 60

50 80

70

5 62

Page 48: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

48SNU

IDB Lab.Data Structures

RBT: Glossary

� Rank: number of black pointers on any path from the node to any external node in red-black tree

� Length (of a root-to-external-node path): number of pointers on the path.

• rank = 1• height = length = 2

Page 49: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

49SNU

IDB Lab.Data Structures

RBT: Lemma 1

� Lemma 1

� If P and Q are two root-to-external-node paths in a red-black tree,

Then length(P) ≤ 2 * length(Q)

� Proof

� Suppose that the rank of the root is r

� From RB1’ and RB2’, each root-to-external-node path has between r and 2r pointers

� So length(P) ≤ 2length(Q)

length(P)=4

length(Q)=2

Page 50: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

50SNU

IDB Lab.Data Structures

RBT: Lemma 2� Lemma 2

� h : height of a red-black tree

� n : number of internal nodes

� r : rank of the root

h=4n=5r=2

� (a) h ≤ 2r

� From Lemma 16.1, no root-to-external-node path has length > 2r

� (b) n ≥ 2r – 1

� No external nodes at levels 1 through r so 2r – 1 internal nodes at these levels

� (c) h ≤ 2log2(n+1)

� 2r ≤ n + 1 from (b)

� r ≤ log2(n+1)

� f ≤ 2r ≤ 2log2(n+1)

Page 51: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

51SNU

IDB Lab.Data Structures

RBT: Representation

� Null pointers represent external nodes

� Pointer and node colors are closely related

� Each node we need to store only

its color ( one additional bit per node ) or

the color of the two pointers to its children

(two additional bit per node)

→ null pointer

→ R / B

or

→ {R / B, R / B}

Page 52: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

52SNU

IDB Lab.Data Structures

Table of Contents

� AVL Tree

� RED-BLACK TREES

� Definition

� Searching a Red-Black Tree

� Inserting into a Red-Black Tree

� Deletion from a Red-Black Tree

� Implementation

� Splay Tree

� B Tree

Page 53: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

53SNU

IDB Lab.Data Structures

Searching a Red-Black Tree� Use the same code to search ordinary binary search tree (Program 15.4),

AVL tree, red-black trees

if(root == null) {search is unsuccessful

} else {if ( thekey < key in root)

only left subtree is to be searched} else {

if(thekey > key in root)only right subtree is to be searched

else (thekey == key in root)search terminates successfully

}}

Page 54: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

54SNU

IDB Lab.Data Structures

Table of Contents

� AVL Tree

� RED-BLACK TREES

� Definition

� Searching a Red-Black Tree

� Inserting into a Red-Black Tree

� Deletion from a Red-Black Tree

� Implementation

� Splay Tree

� B Tree

Page 55: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

55SNU

IDB Lab.Data Structures

Violations due to Insertion (1)� The RBT should have the same number of black nodes in all paths� If new node is colored as black

� The updated tree will always violate RB3 (same number of black nodes)

3

2 4

3

2 4

1

r=3

r=2

insert 1

Page 56: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

56SNU

IDB Lab.Data Structures

Violations due to Insertion (2)� If new node is colored as red

� If the parent of inserted node is black, it's OK (no violation).� But if the parent of inserted node is also red, violation occurs!

� Violate RB2 (no two consecutive reds)

3

2

3

2

1RB2 violation!

insert 1

Page 57: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

57SNU

IDB Lab.Data Structures

L Type Imbalances due to Insertion (1)

� u be the inserted node (red)� uL & uR

� pu be the parent of u (red)� puL & puR

� gu be the granparent of u� guL & guR

� LLr & LRr� The color of guR is red

Page 58: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

58SNU

IDB Lab.Data Structures

L Type Imbalances due to Insertion (2)

� u be the inserted node (red)� uL & uR

� pu be the parent of u (red)� puL & puR

� gu be the granparent of u� guL & guR

� LLb & LRb� The color of guR is black

U�

U�

Page 59: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

59SNU

IDB Lab.Data Structures

Fixing LLr and LRr Imbalance

Beginchange the color of pu & guR : red � blackif (gu != root) { change the color of gu : black � red } else { the color change not done.

the number of black nodes increases by 1. (on all root-to-external-node paths) }

if (the color change of gu causes imbalance) gu became the new u node

if (gu != root && the color change causes imbalance) continue to rebalance

End

Page 60: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

60SNU

IDB Lab.Data Structures

Fixing LLr Imbalance

A

C

B

LLr imbalance After LLr color change

D E

F

G

u

If a node (which is red) u is left child of its parent (also red)and its parent is left child of its grandparent & its uncle is red,

then change its grandparent's color to red & change its parent's and uncle's color to black

B

C

A

D E

F

G

u

Page 61: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

61SNU

IDB Lab.Data Structures

Fixing LRr Imbalance

A

B

LRr imbalanceAfter LRr color change

C

If a node (which is red) u is right child of its parent (also red)and its parent is left child of its grandparent & its uncle is red,

then change its grandparent's color to red& change its parent's and uncle's color to black

D

E F

u

G

A

C D

E F

u

B G

Page 62: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

62SNU

IDB Lab.Data Structures

Fixing LLb and LRb Imbalance

� Rotation first & then Change the color

� The root of the involved subtree is black following the rotation

� Number of black nodes on all root-to-external-node paths is unchanged

� LLb rotation in RB tree is similar to LL rotation in AVL tree

� LRb rotation in RB tree is similar to LR rotation in AVL tree

Page 63: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

63SNU

IDB Lab.Data Structures

Fixing LLb Imbalance

LLb imbalance

A

C

B

Du

E

After LLb rotation

B

C

E

u A

D

If a node (which is red) u is left child of its parent(also red)and its parent is left child of its grandparent & its uncle is black,

then do rotation and color change like the following

Page 64: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

64SNU

IDB Lab.Data Structures

Fixing LRb Imbalance

LRb imbalance After LRb rotation

D

G

u

A

F

If a node (which is red) u is right child of its parent (also red)and its parent is left child of its grandparent & its uncle is black,

then do rotation and color change like the following

A

B

C D

E F

u

G

E

B

C

Page 65: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

65SNU

IDB Lab.Data Structures

Insertion Example in RBT (1)

50

10 80

90

(a) Initial state:all root-to-external-node paths have 3 black nodes & 2 black pointers

50

10 80

70 90

(b) insert 70 as a red node:No violations of RBT ����No remedial action is necessary

Page 66: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

66SNU

IDB Lab.Data Structures

Insertion Example in RBT (2)50

10 80

70 90

60

pu

u

gu

(c) insert 60 as a red node ���� LLr imbalance

50

10 80

70 90

60

pu

u

(d) LLr color change on nodes 70, 80 & 90;gu is null, so not RB2 imbalance

u

Page 67: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

67SNU

IDB Lab.Data Structures

Insertion Example in RBT (3)50

10 80

70 90

60

65

gu

pu

u

(e) Insertion 65 as a red node ���� LRb imbalance

50

10 80

65 90

60 70(f) Perform LRb rotation

Page 68: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

68SNU

IDB Lab.Data Structures

Insertion Example in RBT (4)50

10 80

65 90

60 70

62

gu

pu

u

(g) Insertion 62 as a red node ���� LRr imbalance

50

10 80

65 90

60 70

62

gu

pu

u

(h) LRr color change on nodes 65, 60 & 70 ����RLb imbalance

Page 69: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

69SNU

IDB Lab.Data Structures

Insertion Example in RBT (5)

65

10 60

50 80

70

62

90

(i) Perform RLb rotation

50

10 80

65 90

60 70

62

gu

pu

u

RLb imbalance

Page 70: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

70SNU

IDB Lab.Data Structures

Table of Contents

� AVL Tree

� RED-BLACK TREES

� Definition

� Searching a Red-Black Tree

� Inserting into a Red-Black Tree

� Deletion from a Red-Black Tree

� Implementation

� Splay Tree

� B Tree

Page 71: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

71SNU

IDB Lab.Data Structures

Violations due to Deletion (1)

� If the parent of deleted node is red, RB2 violation occurs!

4

3

2

1

delete 2

...

4

3

1

...

RB2 violation!

Page 72: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

72SNU

IDB Lab.Data Structures

Violations due to Deletion (2)

� If the deleted node is black, RB3 violation occurs!

delete 23

2 4

3

4

r=1

r=2

Page 73: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

73SNU

IDB Lab.Data Structures

Deletion & Imbalance in RBT (1)

(b) Delete 70� Deleted node was red

� Same number of black nodes before and after the rotation

� This is OK

65

10 60

50

70

62

90(a) A Red-Black tree

65

10 60

50

62

90

Page 74: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

74SNU

IDB Lab.Data Structures

Deletion & Imbalance in RBT (2)

(c) Delete 90

� The red node 70 takes the place of the deleted node which was black

� Then, the number of black nodes on path from root-to-external node in y is 1 less than before ���� RB3 violation occurs = imbalance

� Change the color of y to Black

65

10 60

50

70

62

90

65

10 60

50 70

62

y

(a) A Red-Black tree

Page 75: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

75SNU

IDB Lab.Data Structures

Deletion & Imbalance in RBT (3)

(d) Delete 65

� Deleted node was black and the node 62 was red, so change to black

** An RB3 violation occurs

only when the deleted node was black

and y is not the root of the resulting tree.

65

10 60

50

70

62

90

10 60

50

70

6290

(a) A Red-Black tree

Page 76: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

76SNU

IDB Lab.Data Structures

Rb Imbalance due to Deletion

� Rb0 => color change

� Rb1 => handled by rotation

� Rb2 => handled by rotation

(y is the node that takes the place of removed node)

number of y’s nephewy's sibling is blacky is the right child of its parent

Page 77: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

77SNU

IDB Lab.Data Structures

Deletion Imbalances:

Rb family

y: the node that takes the place of removed node

py: parent of y

v: sibling of y

vL & vR: children of v

Page 78: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

78SNU

IDB Lab.Data Structures

Fixing Rb0 Imbalance

Rb0 imbalance

A

D

yE

After Rb0 color change

B

C

A

D

yEB

C

If a node (which is black) y is right child of its parentand its sibling is black & its sibling has 0 red child,

then change its sibling's color to red

Page 79: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

79SNU

IDB Lab.Data Structures

Fixing Rb1 Imbalance

Rb1 imbalance

A

D

yG

After Rb1 rotation

B

C

If a node(which is black) y is right child of its parentand its sibling is black & its sibling has 1 red child,

then do rotation and color change like the following

C is red

D is redFE

B

D y

AC

G

FE

D

F y

AB

GC E

red / black

Page 80: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

80SNU

IDB Lab.Data Structures

Fixing Rb2 Imbalance

Rb2 imbalance

A

D

yG

After Rb2 rotation

B

C

FE

D

F y

AB

GC E

If a node(which is black) y is right child of its parentand its sibling is black & its sibling has 2 red children,

then do rotation and color change like the following

Page 81: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

81SNU

IDB Lab.Data Structures

Rr Imbalance due to Deletion

� Rr0

� Rr1 handled by rotation

� Rr2

number of red child that v’s right child has

(v is sibling of y)

(y is the node that takes the place of removed node)

y's sibling is red

y is the right child of its parent

Page 82: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

82SNU

IDB Lab.Data Structures

Deletion Imbalances:

Rr family

y: the node that takes the place of removed node

py: parent of y

v: sibling of y

vL & vR: children of v

Page 83: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

83SNU

IDB Lab.Data Structures

Fixing Rr0 Imbalance

Rr0 imbalance

A

D

yE

After Rr0 rotation

B

C

B

E y

A

D

C

If a node(which is black) y is right child of its parentand its sibling is red & its nephew has 0 red child,

then do rotation and color change like the following

Page 84: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

84SNU

IDB Lab.Data Structures

Fixing Rr1 Imbalance

Rr1 imbalance

A

D

yI

After Rr1 rotation

B

C

If a node(which is black) y is right child of its parentand its sibling is red & its nephew has 1 red child,

then do rotation and color change like the following

E is red

F is redFE

F

H y

AB

IC Dred / black HG

D

F y

AB

IC E

HG

GE

Page 85: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

85SNU

IDB Lab.Data Structures

Fixing Rr2 Imbalance

Rr2 imbalance After Rr2 rotation

If a node(which is black) y is right child of its parentand its sibling is red & its nephew has 2 red children,

then do rotation and color change like the following

F

H y

AB

IC D

GE

A

D

yIB

C

FE

HG

Page 86: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

86SNU

IDB Lab.Data Structures

Deletion Example (1)

� (a) 90 deleted

� Not root & black

� Imbalance Rb0

65

10 60

50 80

70

62

90

65

10 60

50 80

70

62

py

v

vR

y

Page 87: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

87SNU

IDB Lab.Data Structures

Deletion Example (2)

� ( C) delete 80

� Black node “80” was deleted

� So tree remains balanced

� (b) Rb0 color change

� py was red before delete

� Rb0 color change of 70 & 80

� we are done

65

10 60

50 80

70

62

py

v

vR65

10 60

50 70

62

Page 88: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

88SNU

IDB Lab.Data Structures

Deletion Example (3)

� (d) delete 70

� Nonroot black node was deleted

� Tree is imbalance Rr1(ii)

� (e) after Rr1(ii) Rotation

� This tree is now balanced!

65

10 60

50 70

62

65

10 60

50

62

py

v

v w

x

62

10 60

50 65

v

Page 89: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

89SNU

IDB Lab.Data Structures

Rotation Taxonomy in RBT� Rotation types due to Insertion

� L family� LLr type LRr type LLb type LRb type

� R family� RRr type RLr type RRb type RLb type

� Rotation types due to Deletion� Rb family

� Rb0 Rb1(i) Rb1(ii) Rb2

� Rr family� Rr0 Rr1(i) Rr1(ii) Rr2

� Lb family� Lb0 Lb1(i) Lb1(ii) Lb2

� Lr family� Lr0 Lr1(i) Lr1(ii) Lr2

Page 90: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

90SNU

IDB Lab.Data Structures

Table of Contents

� AVL Tree

� RED-BLACK TREES

� Definition

� Searching a Red-Black Tree

� Inserting into a Red-Black Tree

� Deletion from a Red-Black Tree

� Implementation

� Splay Tree

� B Tree

Page 91: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

91SNU

IDB Lab.Data Structures

Implementation

� Considerations� Insertion / Deletion require backward movement� If use red-black-tree nodes �Backward movement is easy

else Backward movement is complex //use stack instance of color fields..etc

� Complexity� For an n-element red-black tree

� parent-pointer scheme runs slightly faster than tack scheme

� Color change : O(log n) // propagate back toward the root� Rotation : O(1)� Each color change or ratation : Θ(1)� Total insert/delete O(log n)

Page 92: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

92SNU

IDB Lab.Data Structures

Table of Contents

� AVL TREES

� RED-BLACK TREES

� SPLAY TREES

� B-TREES

Page 93: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

93SNU

IDB Lab.Data Structures

Splay Tree� Splay tree is a binary search tree whose nodes are rearranged by splay

operation whenever search, insertion, or deletion occurs

� The recently accessed node is moved to the top

Self-Balancingby Splay operation

� Properties of splay tree

� Recently accessed elements are quick to access again

� Basic operations run in O(log n) amortized time

� It is simpler to implement splay trees than red-black trees or AVL trees

� Splay trees don't need to store any extra data in nodes

Page 94: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

94SNU

IDB Lab.Data Structures

The Splay Operation� We call recently accessed(searched, inserted, or deleted) node as splay node

� Splay operation is performed on splay node to move it to the root

� We can perform successive accesses faster because recently accessed node is moved to the top of the tree

g

Dp

Ax

B C

x

p

A B C D

g

splay node

� Splay operation comprises sequence of the following splay steps.

If (Splay node = root) then sequence of steps is empty

Else splay step moves the splay node either 1 level or 2 levels up the tree

Page 95: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

95SNU

IDB Lab.Data Structures

Splay Node� Search(x) makes the node x as a splay node

� Insert(x) makes the node x as a splay node

� Delete(x) makes the parent node of x as a splay node

5

62

1 4

5

62

1 4

5

62

1 4

5

62

1

3Search(4)

Insert(3)

Delete(4)

splay node

splay node

splay node

Page 96: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

96SNU

IDB Lab.Data Structures

One Level Splay Step� When the level of splay node = 2 (Only)

� L splay step : splay node is Left child of its parent� R splay step : splay node is Right child of its parent

� L splay step� If splay node q is the left child of its parent, then do rotation like the

following� Notice that following the splay step the splay node becomes the root of

binary search tree

root root

Page 97: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

97SNU

IDB Lab.Data Structures

Two Level Splay Step

� When the level of splay node > 2

� Types

� LL : p is Left child of gp, q is Left child of p

� LR : p is Left child of gp, q is Right child of p

� RR : p is Right child of gp, q is Right child of p

� RL : p is Right child of gp, q is Left child of p

LL LR RL RR

Page 98: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

98SNU

IDB Lab.Data Structures

LL Splay Step� If splay node q is the left child of its parent

& its parent is the left child of its grandparent,

then do rotation like the following

� The splay node is moved 2 level up

Page 99: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

99SNU

IDB Lab.Data Structures

LR Splay Step

� If splay node q is the right child of its parent

& its parent is the left child of its grandparent,

then do rotation like the following

� The splay node is moved 2 level up

Page 100: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

100SNU

IDB Lab.Data Structures

Sample Splay Operation

Search “2”

Page 101: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

101SNU

IDB Lab.Data Structures

Rotation Taxonomy of Splay Tree

� 1 level splay step� L type

� R type

� 2 level splay step� LL type

� LR type

� RL type

� RR type

Page 102: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

102SNU

IDB Lab.Data Structures

Concept of Amortized� Rule: Spend less than 100$ per month

� Normal spending – Spend less than 100$ per month

� Amortized spending – Spend less than (100 * 12)$ per year

� Remember array expansion� Regular complexity

� Double the size (initialize) -- O(n)

� Copy the old array to the new array – O(n)

� Amortized complexity

� Doubling will happen after n insertions!

� One insertion is responsible for one slot expansion � O(1)

Page 103: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

103SNU

IDB Lab.Data Structures

Amortized Complexity (1)� In an amortized analysis, the time required to perform a sequence of

data-structure operations is averaged over all the operations

performed

� Amortized analysis differs from average-case analysis

� Amortized analysis guarantees the average performance of each

operation in the worst case

� Theorem 16.1� The amortized complexity of a get, put or remove operation performed

on a splay tree with n element is O(log n)

� Actual Complexity of any sequence of g get, p put and r remove operations� O((g+p+r)log n)

∑∑==

≥n

i

n

i

iactualiamortized11

)()(

Page 104: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

104SNU

IDB Lab.Data Structures

Amortized Complexity (2)

� Example (1)

splay

7

6

5

4

1

2

3

7

6

5

2

1

3

4

splay

7

2

5

6

1

3

4splay

7

2

5

6

1

3

4LR LL L

7

6

5

4

1

2

3

search(2)

T1 = (search time)+(splay time)= 6 comparisons + 5 rotations

Page 105: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

105SNU

IDB Lab.Data Structures

Amortized Complexity (3)

� Example (2)

splay

L

7

2

5

6

1

3

4

7

2

5

6

1

3

4

search(1)

7

2

5

6

3

4

1

T2 = (search time) + (splay time) = 2 comparisons + 1 rotation

Page 106: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

106SNU

IDB Lab.Data Structures

Amortized Complexity (4)

� Example (3)

splay

R

7

2

5

6

1

3

4

search(2)

T3 = (search time) + (splay time) = 2 comparisons + 1 rotations

7

2

5

6

3

4

1

7

2

5

6

3

4

1

Page 107: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

107SNU

IDB Lab.Data Structures

Amortized Complexity (5)

� Example (4)� In the previous example, total time taken is 10 comparisons + 7 rotations

� If there were no splay operation, total time taken would be 18 comparisons

� Generally, it is known that (t1+t2+…+tk) / k ≤ 3*log2n, where n is the number of nodes if k is large enough

7

6

5

4

1

2

3

7

6

5

4

1

2

3

search(2)

7

6

5

4

1

2

3

search(1)

7

6

5

4

1

2

3

search(2)

Page 108: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

108SNU

IDB Lab.Data Structures

Table of Contents� AVL TREES� RED-BLACK TREES� SPLAY TREES

� B-TREES

� Indexed Sequential Files (ISAM)

� m-WAY Search Trees

� B-Trees of Order m

� Height of a B-Tree

� Searching a B-Tree

� Inserting into a B-Tree

� Deletion from a B-Tree

Page 109: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

109SNU

IDB Lab.Data Structures

Indexed Sequential Access Method (ISAM)

� Small dictionary may reside in internal memory

� Large dictionary must reside on a disk� A disk consists of many blocks

� Elements (records) are packed into a block in ascending order

� ISAM file (= Indexed Sequential file)� disk-based file structure for large dictionary

� Provide good sequential and random access

� Primary Concern: reducing the number of disk IO s during search

Page 110: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

File Structures 110SNU

IDB Lab.Data Structures

Overview : ISAM File R

61 ∞

10 20 50 61 101 ∞

30 40 45D C A

1 3 10A B A

11 20C D

51 55 57A D B

65 70 101

E B C120150

A D

50D

60B

61A

a

b c

ihgfed

part description recordsPART No PART-Type

primary key

Example : Indexed sequential structure (when using overflow chain)

Page 111: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

File Structures 111SNU

IDB Lab.Data Structures

File Structure Evolution

� Sequential file: records can be accessed sequentially

� not good for access, insert, delete records in random order

� Indexed-sequential file = Indexed Sequential Access Method (ISAM)

� Sequential file + Index

� B+ tree file

� Indexed-sequential file + Balance

But here we study “B tree” data structure --- m-Way search tree is similar to ISAM file

Page 112: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

112SNU

IDB Lab.Data Structures

Table of Contents� AVL TREES� RED-BLACK TREES� SPLAY TREES

� B-TREES

� Indexed Sequential Files (ISAM)

� m-WAY Search Trees

� B-Trees of Order m

� Height of a B-Tree

� Searching a B-Tree

� Inserting into a B-Tree

� Deletion from a B-Tree

Page 113: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

113SNU

IDB Lab.Data Structures

m-Way Search Tree� Binary Search Tree can be generalized to m-Way search tree

� White box is an internal node while solid square is external node

� Each internal node can have up to six keys and seven pointers

� A certain input sequence would build the following example

Page 114: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

114SNU

IDB Lab.Data Structures

Properties of m-WAY Search Tree

� m-Way search tree has the following properties� In the corresponding extended search tree, each internal node has up to p+1 children

and between 1 and p elements.� Every node with p elementshas exactly p + 1 children

� Let k1, ...,kp be the keys of these ordered elements (k1< k2<…< kp) � Let c0, c1…, cp be the p+1 children of the node.

� Key ranges� The elements in the subtree with root co have keys smaller thank1

� The elements in the subtree with root cp have keys larger thankp

� The elements in the subtree with root ci have keys larger thanki but smaller thanki+1, 1≤ i ≤ p

Page 115: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

115SNU

IDB Lab.Data Structures

Searching an m-Way Search Tree� Search the element with key 31

� 10< 31 <80 : Move to the middle subtree� k2< 31 <k3 : Move to the third subtree� 31< k1 : Move to the first subtree, Fall off the tree, No element

Page 116: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

116SNU

IDB Lab.Data Structures

Inserting into an m-Way Search Tree

� Insert the new key 31(a) Search for 31 & Fall off the tree at the node[32,26](b) Insert at the first element in the node

Page 117: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

117SNU

IDB Lab.Data Structures

Inserting into an m-Way Search Tree� Insert the new key 65:

(a)Search for 65 & Fall off the tree at six subtree of node [20,30,40,50,60,70](b) New node obtained & New node becomes the sixth child of [20,30,40,50,60,70]

65

Page 118: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

118SNU

IDB Lab.Data Structures

Deleting from an m-Way Search Tree

� Delete the key 20� Search for 20, k1=20 & C0=C1=0, and Simply Delete 20

Page 119: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

119SNU

IDB Lab.Data Structures

Deleting from an m-Way Search Tree� Delete the key 84

� Search for 84, k2=84 & C1=C2=0, and Simply Delete 84

Page 120: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

120SNU

IDB Lab.Data Structures

Deleting from an m-Way Search Tree� Delete the key 5 :

(a) Only one key in the node � Need to replace(b) From C0, move up the element with largest key � move the key 4 to the key 5’s position

Page 121: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

121SNU

IDB Lab.Data Structures

Deleting from an m-Way Search Tree

� Delete the key 10Replace this element with either the largest element in C0 or smallest element in C1So, element with key 5 is moved to top & element with key 4 is moved up to the key 5’s position

Page 122: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

122SNU

IDB Lab.Data Structures

Height of an m-Way Search Tree

� h : Height, n : number of elements, m : m-way

� The number of elements: h ≤ n ≤ mh – 1

� The number of nodes : ∑ mi = (mh-1)/(m-1) nodes

� The range of height: logm(n+1) ≤ h ≤ n

� The number of disk accesses : O(h)

� We want to ensure that the height h is close to logm(n+1) � this is accomplished by B-tree!

i = 0

h - 1

Page 123: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

123SNU

IDB Lab.Data Structures

Table of Contents

� B-TREES� Indexed Sequential Access Method(ISAM)

� m-WAY Search Trees

� B-Trees of Order m

� Height of a B-Tree

� Searching a B-Tree

� Inserting into a B-Tree

� Deletion from a B-Tree

Page 124: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

124SNU

IDB Lab.Data Structures

Definition: B tree of Order m� B-tree is a m-way search tree satisfying the following properties

1. The root has at least two children

2. All internal nodes other than the root at least m/2 children (pointers to the children nodes)

3. All external nodes are at the same level

� Internal node has several pairs of a key and a pointer to a disk block

Page 125: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

125SNU

IDB Lab.Data Structures

B-Trees of Order m� B-tree of order 2: Fully binary tree

� B-tree of order 3 (= 2- 3 tree): 2 or 3 children

� B-tree of order 4 (= 2- 3- 4 tree): 2 or 3 or 4 children

Page 126: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

126SNU

IDB Lab.Data Structures

Table of Contents

� B-TREES� Indexed Sequential Access Method(ISAM)

� m-WAY Search Trees

� B-Trees of Order m

� Height of a B-Tree

� Searching a B-Tree

� Inserting into a B-Tree

� Deletion from a B-Tree

Page 127: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

127SNU

IDB Lab.Data Structures

Height of a B-Tree of Order m� Remember: All internal nodes other than the root at least m/2 children

(pointers to the children nodes)

� Lemma 16.3Let T be a B-tree of order mLet h be the height of T

Let d= m/2 be the degree of TLet n be the number of elements in T

� (a) 2dh-1 ≤ n ≤ mh – 1

� (b) logm(n + 1) ≤ h ≤ logd((n+1)/2) + 1

Page 128: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

128SNU

IDB Lab.Data Structures

Table of Contents

� B-TREES� m-WAY Search Trees

� B-Trees of Order m

� Height of a B-Tree

� Searching a B-Tree

� Inserting into a B-Tree

� Deletion from a B-Tree

Page 129: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

129SNU

IDB Lab.Data Structures

Searching a B-Tree

� Using the same algorithm as is an m-way search tree� First visit the root with the given key K

� Compare K and the keys in the root

� Follow the corresponding pointer

� Search the child node recursively until the leaf node

� If arrived at the leaf node, Search the external node

Page 130: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

130SNU

IDB Lab.Data Structures

Table of Contents

� B-TREES� Indexed Sequential Access Method(ISAM)

� m-WAY Search Trees

� B-Trees of Order m

� Height of a B-Tree

� Searching a B-Tree

� Inserting into a B-Tree

� Deletion from a B-Tree

Page 131: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

131SNU

IDB Lab.Data Structures

Inserting into a B-Tree

� First search with the key of the new element

� Found � Insertion fails (if duplicates are not permitted)

� Not Found

Insert the new element into the last encountered internal node

If (no overflow) return ok

Else (overflow) { split the last internal node into 2 new nodes;

go to the 1-level up for updating the parent node (recursively)}

Page 132: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

132SNU

IDB Lab.Data Structures

Notations in B-tree� e : element c : children p : parent node

� Full node has m elements & m+1 children

� d : degree of a node � at least m/2

� ei : element pointers ci : children pointers

� Overfull node

� m, c0, (e1, c1), …, (em, cm)

� P : Left remainder

� d-1, c0, (e1, c1), …, (ed1-1, cd-1)

� Q : Right remainder

� m-d, cd, (ed+1, cd+1), …, (em, cm)

� Pair(ed, Q) is inserted into the parent of P

Page 133: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

133SNU

IDB Lab.Data Structures

Insert the key 3 in B-tree

Page 134: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

134SNU

IDB Lab.Data Structures

Insert the key 25 in B-tree� d = 4 & the target node (“6”, 20,30,40,50,60,70)

� P : 3, 0, (20,0), (25,0), (30,0)

� Q : 3, 0, (50,0), (60,0), (70,0)

� (40, Q) is inserted into parent of P

P Q

Page 135: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

135SNU

IDB Lab.Data Structures

Growing B tree by Insertion (1)

20

30 80

9050 60

10 25 55 9570 82 8535 40

Fig 16.25 B-tree of order 3 (at least 2 pointers)node format: M, C0, (e1, c1), (e2, c2)… (em, cm) where m= no of elements, ei = elements, ci = children

Page 136: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

136SNU

IDB Lab.Data Structures

Growing B tree by Insertion (2)

354044

� d = 2 & the target node was (2, c5, (35,c6),(40, c7))� Overfull node

� 3, c5, (35,c6), (40,c7), (44,cn)

20

3080

905060

10 25 55 9570 8285

Insert 44

Page 137: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

137SNU

IDB Lab.Data Structures

Growing B tree by Insertion (3)

35 44

� d= 3/2 � 2, split the overfull node into P & Q� P : 1, 0, (35,0)

� Q : 1, 0, (44,0)

� (40,Q) into the parent A of P� Again the parent A is overfull node

20

3080

90

10 25 55 9570 8285

405060P Q C D

S T

Page 138: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

138SNU

IDB Lab.Data Structures

Growing B tree by Insertion (4)

35 44

� Node A is again the overfull node

� A : 3, P, (40,Q), (50,C), (60,D)

20

3080

90

10 25 55 9570 8285

405060P Q C D

S TA

Page 139: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

139SNU

IDB Lab.Data Structures

Growing B tree by Insertion (5)

35 44

� d= 3/2 = 2, split the node A into A & B� A : 1, P, (40,Q)

� B : 1, C, (60,D)

� Move (50,B) into the parent of A

� Again the parent of A is overfull node

20 90

10 25 55 9570 8285

40 60P Q C D

S TA305080

B

R

Page 140: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

140SNU

IDB Lab.Data Structures

Growing B-tree by Insertion (6)

35 44

� The root node R is now the overfull node

� R : 3, S, (30,A), (50,B), 80,T)

20 90

10 25 55 9570 8285

40 60P Q C D

S TA305080

B

R

Page 141: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

141SNU

IDB Lab.Data Structures

Growing B tree by Insertion (7)

35 44

� d= 3/2 � 2, split the root node R into R & U� R : 1, S, (30,A)

� U : 1, B, (80,T)

� Move the new index (50, U) into the parent of R

� R has no parent, we create a new root for the new index

20 90

10 25 55 9570 8285

40 60P Q C D

S TA30

50

80B

R U

Page 142: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

142SNU

IDB Lab.Data Structures

Disk accesses in B tree� Worst case: Insertion may cause s nodesto split upto root

� Number of disk accesses in the worst caseh (to read in the nodes on the search path)+2s (to write out the two split parts of each node)+1 (to write the new root or the node into which an insertion that does not result in a

split is made)

� � h + 2s + 1

� � at most 3h + 1because s is at most h

� The worst scenario is to have 3h+1 disk IOs by splitting

Page 143: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

143SNU

IDB Lab.Data Structures

Table of Contents

� B-TREES� Indexed Sequential Access Method (ISAM)

� m-WAY Search Trees

� B-Trees of Order m

� Height of a B-Tree

� Searching a B-Tree

� Inserting into a B-Tree

� Deletion from a B-Tree

Page 144: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

144SNU

IDB Lab.Data Structures

Deletion from a B-Tree

� Deletion cases� Case 1: Key k is in the leaf node

� Case 2: Key k is in the internal node

� Case 2 � by replacing the deleted element with

� The largest element in its left-neighboring subtree

� The smallest element in its right-neighboring subtree

� Replacing element is supposed to be in a leaf, so we can apply case 1

Page 145: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

145SNU

IDB Lab.Data Structures

Case 1: Leaf Node Deletion

� If key k is in leaf node, then remove k from leaf node

X

� If underfull node happens, care must be exercised (will address shortly)

Page 146: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

146SNU

IDB Lab.Data Structures

Case 2: Internal Node Deletion

� If the key k is in the internal node x

One of 3 subcases:

a. If the left child y preceding k in x has ≥ t keys

b. If the right child z following k in x has ≥ t keys

c. If both the left and right subchild y and z have t-1 keys

� t : m/2 - 1 (half of the keys)

Page 147: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

147SNU

IDB Lab.Data Structures

Case 2a: Internal Node Deletion (1)

If the left child y preceding k in x has ≥ t keys

� Find predecessor k' of k in subtree rooted at y

� Replace k by k' in x

x

Page 148: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

148SNU

IDB Lab.Data Structures

Case 2a: Internal Node Deletion (2)

� If underfull node happens, care must be exercised (will address shortly)

Page 149: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

149SNU

IDB Lab.Data Structures

Case 2b: Internal Node Deletion� If the right child z following k in x has ≥ t keys:

(a) Find successor k' of k in subtree y, (b) Replace k by k' in x

� If underfull node happens, care must be exercised (will address shortly)

Page 150: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

150SNU

IDB Lab.Data Structures

Case 2c: Internal Node Deletion� If both the left and right subchild y and z have t-1 keys

� Select the replacement as shown in case 2a or case 2b

� If underfull node happens, care must be exercised as shown in the below

Page 151: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

151SNU

IDB Lab.Data Structures

Shrinking B-Tree by Deletion (1)

35 44

20 90

10 25 55 9570 82 85

40 60P Q C D

S TA30

50

80B

R U * Try to delete “44”

35 44

20 90

10 25 55 9570 82 85

40 60P Q C D

S TA30

50

80B

R U

� After deleting “44”, “35” & “40” are merged

Page 152: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

152SNU

IDB Lab.Data Structures

Shrinking B-Tree by Deletion (2)

� “20” & “40” also needs to merged

� “50” and “80” also needs to merged

Page 153: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

153SNU

IDB Lab.Data Structures

� “50” & “80” are merged and now the old root becomes empty

Shrinking B-Tree by Deletion (3)

� Free the old root and make the new root

Page 154: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

154SNU

IDB Lab.Data Structures

Technique for Reducing Node Merging: B tree Deletion with Redistribution (1)

� Underflow happens & Redistribute some neighbor nodes

� Move down 10 & move up 6

Try to delete “25”

Save node merging

Page 155: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

155SNU

IDB Lab.Data Structures

Try to delete “10”

Technique for Reducing Node Merging: B tree Deletion with Redistribution (2)

Merging is unavoidable

Page 156: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

156SNU

IDB Lab.Data Structures

� Consider redistributing some nodes: move down “30” & move up “50”

Technique for Reducing Node Merging: B tree Deletion with Redistribution (3)

Save propagation of node merging

Page 157: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

157SNU

IDB Lab.Data Structures

Summary (0)

� Chapter 15: Binary Search Tree

� BST and Indexed BST

� Chapter 16: Balanced Search Tree

� AVL tree: BST + Balance

� B-tree: generalized AVL tree

� Chapter 17: Graph

Page 158: Ch 16. Balanced Search Treesocw.snu.ac.kr/sites/default/files/NOTE/492.pdf · Chapter 15: Binary Search Tree BST and Indexed BST Chapter 16: Balanced Search Tree AVL tree: BST + Balance

158SNU

IDB Lab.Data Structures

Summary (1)

� Balanced tree structures- Height is O(log n)

� AVL and Red-black trees� Suitable for internal memory applications

� Splay trees� Individual dictionary operation � 0(n)

� Take less time to perform a sequence of u operations � 0(u log u)

� B-trees � Suitable for external memory