bbalancedalanced binbinaryary seasearchrch tr treesees...
TRANSCRIPT
-
Data Structures and Programmingg g資料結構與程式設計
Topic 12 Balanced Search TreesTopic 12 Balanced Search Trees
課程編號:901 31900 EE 3011科目名稱 資料結構與程式設計科目名稱:資料結構與程式設計授課教師:黃鼎偉時間地點:一678 電機二館 229
Balanced Binary Search TreesBalanced Binary Search Trees
Height is O(log n) where n is the number ofHeight is O(log n), where n is the number of elements in the tree
AVL (Adelson-Velsky and Landis) treesRed Black treesRed-Black treesfind, insert, and erase take O(log n) time
22
Balanced Binary Search Trees Balanced Binary Search Trees
Indexed AVL treesIndexed AVL treesIndexed Red-Black treesIndexed operations also takeO(l ) tiO(log n) time
33
Other Balanced Search Trees Other Balanced Search Trees
B-trees 2-3 & 2-3-4 treesB-trees, 2-3 & 2-3-4 treesB+-treesB*-TreesW i ht b l d bi h tWeight balanced binary search treesetc.etc.
44
-
AVL TreeAVL Tree
Binary tree Binary tree For every node x, define its balance factor
balance factor of x = height of left subtree of x– height of right subtree of xheight of right subtree of x
Balance factor of every node x is -1, 0, or 1
AVL Search Tree AVL Search Tree AVL Tree ∩ Binary Search Tree
Indexed AVL Search Tree AVL Tree ∩ Indexed Binary Search Tree
55
AVL Tree ∩ Indexed Binary Search Tree
Balance FactorsBalance Factors
This is an AVL tree -1This is an AVL tree.
1 1
0 0 1 -1
0 0 -1 00
0
The number outside each node is its balanced factor.
66
Height of an AVL TreeHeight of an AVL Tree
N = N + N +1 N =0 N = 1 N 為 h 層 Nh Nh – 1 + Nh – 2 +1, N0 0, N1 1, N 為 h 層下最少所需的 node 數 Si il t Fib i b Similar to Fibonacci numbers Fh = Fh – 1 + Fh – 2 , F0 =0, F1 = 1
1 Nh = Fh + 2 – 1, where / 5hhF (1 5)/2
So, The height of an AVL tree that has n nodes is at
h2 / 5 1hhN
The height of an AVL tree that has n nodes is at
most 1.44 log2 (n+2). Th h i ht f d bi t i t
log ( 5( 1)) 2n The height of every n node binary tree is at
least log2 (n+1).
77
AVL Search TreeAVL Search Tree-1
1 1
10
1
1
17 40
0 0 1 -1
83 30 45
0 0 -1 00
1 5 2035 60
0
1 5 20
2525
88
-
insert(9)( )-1
1 10
10
1
1
107 40
0 0 1 -1-183 30 45
0 0 -1 00
90
1 5 2035 60
0
91 5 20
2525
99
insert(29)( )-1
1 1
10
1
1
17 40
0 0 1 -1
83 30 45
0 0 00
1 5 2035 60
-1-2
20
0
1 5 20
25-1RR imbalance new node is in
20
25
29
0right subtree of right subtree of blue node (node with bf = -2 )
1010
( )
insert(29)( )-1RR rotation
1 1
10RR rotation.1
1
17 40
0 0 1 -1
83 30 45
0 0 00
1 5 2535 60
0
0
1 5 25
200
2920 29
1111
InsertInsert
F ll i i t t th t d t d Following insert, retrace path towards root and adjust balance factors as needed.
Stop when you reach a node whose balance factor becomes 0, 2, or –2, or when you reach the root.becomes 0, 2, or 2, or when you reach the root.
The new tree is not an AVL tree only if you reach a node hose balance factor is either 2 or 2a node whose balance factor is either 2 or –2.
In this case, we say the tree has become unbalanced.
12
-
A-NodeA Node Let A be the nearest ancestor of the newly Let A be the nearest ancestor of the newly
inserted node whose balance factor becomes +22 f ll i h ior –2 following the insert.
Balance factor of nodes between new node and Balance factor of nodes between new node and A is 0 before insertion.
13
Imbalance TypesImbalance Types RR newly inserted node is in the right RR … newly inserted node is in the right
subtree of the right subtree of A. LL … left subtree of left subtree of A. RL left subtree of right subtree of A RL… left subtree of right subtree of A. LR… right subtree of left subtree of A.
14
LL RotationLL Rotation
1A A B 02
0B ARhB ARh
AB’Lh+1
01
BL BR
h
B’L BR
h
BR AR
h+1
Before h h
After insertion.h+1 h
After rotation.
Rh
Rh
Subtree height is unchangedinsertion. Subtree height is unchanged. No further adjustments to be done.
15
LR Rotation (case 1)LR Rotation (case 1)
1A A C2 0
0B B AB-1 0 0
C 0
Before After insertion.
C
After rotation.
Subtree height is unchangedinsertion. Subtree height is unchanged. No further adjustments to be done.
16
-
LR Rotation (case 2)LR Rotation (case 2)
C1A A 2 0
A0B ARhB ARh
B-1 0 -1
CR ARBL
h0 C BL
h
h
C BL C1
h-1 hCR
hCL CR
h
h 1C’h
h ’Lh
Subtree height is unchanged
h-1h-1 h-1’Lh
Subtree height is unchanged. No further adjustments to be done.
17
LR Rotation (case 3)LR Rotation (case 3)
1A A 2 C 0
0B ARhB ARh
-1 AB1 0
BL
h0 C BL
h
h
C-1 C ARBL CL
CRhCL C’
h
hCLh 1
’Rh hh h-1
Subtree height is unchanged
h-1h-1
Rhh-1
Subtree height is unchanged. No further adjustments to be done.
18
Single & Double RotationsSingle & Double Rotations Single Single LL and RR
Double LR and RL LR and RL LR is RR followed by LL RL is LL followed by RR
19
LR Is RR + LLLR Is RR + LL
A 2 A 2 C 0
B ARh-1 C ARh AB1 0
BLh
h
C-1 C’h
B C ARBL CL
C’h
hCLh 1
CLRh
BLh h 1
’Rh hh h-1
Rhh-1
After insertion.
h
After RR
h-1
After LL rotation. rotation.
20
-
AVL insertAVL insert1 template p2 void AVL::Insert(const K& k, const E& e)3 {4 if (!root) // 特例:空的樹5 {5 {6 root = new AvlNode (k, e);7 return;8 }9 // 第 回合:找出e的插入位置9 // 第一回合:找出e的插入位置。
10 AvlNode *a = 0, *pa = 0, *p = root, *pp = 0;14 while (p)15 {{16 if (p‐>bf) { a = p; pa = pp; }21 if (k key) { pp = p; p =p‐>leftChild; }26 else if (k > p‐>key) { pp = p; p = p‐>rightChild; }31 else(p‐>element = e; return;)31 else(p >element e; return;) 32 } // while的結尾34 // 第二回合:插入與重新平衡。K並不在樹裡並且35 // 可以插入成為pp的適當子樹36 AvlNode *y new AvlNode(k e);36 AvlNode *y = new AvlNode(k, e);37 if (k key) pp‐>leftChild = y; // 插入成左子樹38 else pp‐>rightChild = y; // 插入成右子樹
2121
AVL insert (cont )AVL insert (cont.)40 // 調整從路徑a到pp上節點的平衡因子。根據a的定義,所有在這條路徑上的節點目前的平衡因子都是0。它們42 // 的新平衡因子將變成 ±1。d = +1表示k是插入成為a的左子樹,而d = ‐1則表示k是插入成為a的右子樹。44 int d;44 int d;45 AvlNode *b, // a的子樹46 *c; // b的子樹47 if (k > a‐>key) { b = p = a‐>leftChild; d =‐1; }52 else (b = p = a‐>leftChild; d =1;)53 while (p !=y)53 while (p !=y)54 if (k >p‐>key) { p‐>bf = ‐1; p = p‐>rightChild; }59 else { p‐>bf = 1; p = p‐>leftChild; }66 if (!(a‐>bf) || !(a‐>bf + d)) { a‐>bf += d; return; } // 樹平衡72 if (d = =1) // // 樹不平衡,決定旋轉的型態 左邊不平衡73 { if (b‐>bf = =1) // LL旋轉型態73 { if (b >bf 1) // LL旋轉型態75 { a‐>leftChild = b‐>rightChild; b‐>rightChild = a;78 a‐>bf = 0; b‐>bf = 0; }81 else // LR旋轉型態82 { c = b‐>rightChild; b‐>rightChild = c‐>leftChild;85 a‐>leftChild = c‐>rightChild;g ;86 c‐>leftChild = b; c‐>rightChild = 1;88 switch (c‐>bf)89 { case 1: a‐>bf = ‐1; b‐>bf = 0; break;94 case ‐1: b‐>bf = 1; a‐>bf = 0; break;98 case 0: b‐>bf = 0; a‐>bf = 0; break;
102 }102 }103 c‐>bf = 0; b = c; // b是新的樹根105 }// LR的結尾106 } // 左邊不平衡的結尾107 else // 右邊不平衡:與左邊不平衡對稱108 { ; //RR & RL 旋轉 }108 { …; //RR & RL 旋轉 }112 // 包含樹根b的子樹已再度平衡113 if (!pa) root = b;114 else if (a == pa‐>leftChild) pa‐>leftChild = b;115 else pa‐>rightChild = b;116 return;
22
116 return;117 } // AVL::Insert的結尾
Delete An ElementDelete An Element-1
1 1
10
0
1
1 -1
17 4
00 0 1
0
-183 3
0
45
0 0 -1 00
1 5
0
2 3560
05
0
2
5 0
5Delete 8.
23
Delete An ElementDelete An Element-1
2 1
10q
0
2
1 -1
17 4
00 1
0
-13 3
0
45
0 0 -1 00
1 5
0
2 3560
05
0
2
5 0
• Let q be parent of deleted node.5• Retrace path from q towards root.
24
-
New Balance Factor of qNew Balance Factor of q
q
Deletion from left s btree of q bf Deletion from left subtree of q bf--. Deletion from right subtree of q bf++. New balance factor = 1 or –1 no change in height of
subtree rooted at qsubtree rooted at q. New balance factor = 0 height of subtree rooted at q
has decreased by 1. New balance factor = 2 or –2 tree is unbalanced at q. New balance factor 2 or 2 tree is unbalanced at q.
25
Imbalance Classificationb ce C ss c o Let A be the nearest ancestor of the deleted node
whose balance factor has become 2 or –2following a deletionfollowing a deletion.
Deletion from left subtree of A type L. Deletion from right subtree of A type R. Type R new bf(A) = 2 Type R new bf(A) = 2. So, old bf(A) = 1. So, A has a left child B. bf(B) = 0 R0 bf(B) 0 R0. bf(B) = 1 R1. bf(B) 1 R 1 bf(B) = –1 R-1.
26
R0 RotationR0 Rotation
1A B -1A 2
0B ARhABL
h1B A’
h 10
BL BR
h
BR A’R
hBL BR
Rh-1
Before h h
After rotation.
Rh
Rh-1
After deletion.h h
Subtree height is unchanged.deletion.
g g No further adjustments to be done. Similar to LL rotation Similar to LL rotation.
27
R1 RotationR1 Rotation
1A B 0A 2
1B ARhABL
h0B A’
h 11
BL BR
h
BR A’R
hBL BR
Rh-1
Before h h-1
After rotation.
Rh-1
Rh-1
After deletion.h h-1
Subtree height is reduced by 1.deletion.
g y Must continue on path to root. Similar to LL and R0 rotations Similar to LL and R0 rotations.
28
-
R-1 RotationR 1 Rotation
1A A 2 C 0
-1B ARhB A’R
h-1-1 AB
BLh-1
b C BLh-1
Cb CR A’Rh-1
BLh-1
CL
New balance factor of A and B depends on bCRCL CRCL
R
New balance factor of A and B depends on b. Subtree height is reduced by 1. Must continue on path to root. Similar to LR. Similar to LR.
29
AVL RotationsAVL Rotations
Insert Insert RR LL RLRL LR
l Delete R0, L0, R1, L1 R 1 L 1 R–1, L–1
3030
AVL RotationsAVL Rotations Number of Rebalancing Rotations Number of Rebalancing Rotations At most 1 for an insert. O(log n) for a delete.
Rotation Frequency for insert random numbers Rotation Frequency for insert random numbers. No rotation … 53.4% (approx). LL/RR … 23.3% (approx). LR/RL … 23.2% (approx).( pp )
31
Red-Black Trees with Colored NodesRed Black Trees with Colored Nodes
Colored Nodes DefinitionColored Nodes Definition Binary search tree. Each node is colored “red” or “black”. Root and all external nodes are “black”. Root and all external nodes are black . No root-to-external-node path has two
ti “ d” dconsecutive “red” nodes. All root-to-external-node paths have the same p
number of “black” nodes
3232
-
Example Red-Black TreeExample Red Black Tree
1010
7 40
8 30 453
1 5 2035 60
25
3333
Red-Black Trees with Colored EdgesRed Black Trees with Colored Edges
Colored Edges DefinitionColored Edges Definition Binary search tree. Child pointers are colored “red” or “black”. P i t t t l d i “bl k” Pointer to an external node is “black”. No root to external node path has two No oot to e te a ode pat as two
consecutive “red” pointers. E l d h h h Every root to external node path has the same
number of “black” pointers.p
3434
Example Red-Black TreeExample Red Black Tree
1010
7 40
8 30 453
1 5 2035 60
25
3535
Red-Black TreeRed Black Tree
The height of a red-black tree that has n The height of a red-black tree that has n(internal) nodes is between log2(n+1) and 2l ( 1)2log2(n+1).
C++ STL Class map and Class multimap C STL Class map and Class multimap Red-black tree
3636
-
Red-Black TreeRed Black Tree Start with a red black tree whose height is h; Start with a red black tree whose height is h;
collapse all red nodes into their parent black d h d dnodes to get a tree whose node-degrees are
between 2 and 4, height is ≥ h/2, and all gexternal nodes are at the same level.
37
Red-Black TreeRed Black Tree
1010
7 40
8 30 453
1 5 2035 60
25
38
Red-Black TreeRed Black Tree Let h’ ≥ h/2 be the height of the collapsed tree Let h ≥ h/2 be the height of the collapsed tree. In worst-case, all internal nodes of collapsed tree
h d 2have degree 2. Number of internal nodes in collapsed tree ≥ 2h’ – 1.p So, n ≥ 2h’ – 1 S h ≤ 2 l ( + 1) So, h ≤ 2 log2 (n + 1)
39
Red-Black TreeRed Black Tree
At most 1 rotation and O(log n) color flips per At most 1 rotation and O(log n) color flips per insert/delete.
Priority search trees. Two keys per element Two keys per element. Search tree on one key, priority queue on other. Color flip doesn’t disturb priority queue property. Rotation disturbs priority queue property.p y q p p y O(log n) fix time per rotation ≥ O(log2n) overall time.
O(1) i d l i O(1) amortized complexity to restructure following an insert/delete.g
40
-
InsertInsert
New pair is placed in a new node, which is inserted into the red-black tree.inserted into the red black tree.
New node color options. Black node one root-to-external-node path has an
extra black node (black pointer).( p ) Hard to remedy.
Red nodeone root-to-external-node path may have Red node one root-to-external-node path may have two consecutive red nodes (pointers). M b di d b l fli d/ t ti May be remedied by color flips and/or a rotation.
41
Classification of 2 Red Nodes/PointersClassification of 2 Red Nodes/Pointers
gppp
XY cd
pp
p XYz X relationship between gp and pp.
b
cp
pp left child of gp X = L. Y relationship between pp and p
a b Y relationship between pp and p. p right child of pp Y = R.
b (bl k) if d ll bl k d z = b (black) if d = null or a black node. z = r (red) if d is a red node z r (red) if d is a red node.
42
XYrXYr Color flip. Color flip.
gp gp
d
gppp
d
gppp
cd
p cd
p
a b a ba b
• Move p pp and gp up two levels• Move p, pp, and gp up two levels.• Continue rebalancing if necessary.
43
LLbLLb Rotate. Rotate.
ygp zx z
d
gppp y
z
a b c dcd
p x
a b
• Done!• Same as LL rotation of AVL tree.
44
-
LRbLRb Rotate. Rotate.
ygp yx z
d
gppp x
z
a b c dadpy
b c
p
• Done!
b c
• Same as LR rotation of AVL tree.• RRb and RLb are symmetric.RRb and RLb are symmetric.
45
DeleteDelete Delete as for unbalanced binary search tree Delete as for unbalanced binary search tree. If red node deleted, no rebalancing needed. If black node deleted, a subtree becomes one
black pointer (node) deficient.p ( )
46
Delete A Black LeafDelete A Black Leaf
110
7 40
8 30 453
1 5 2035 60
25
• Delete 8.
47
Delete A Black LeafDelete A Black Leaf
110
py7 4
0
py
y 30 453
1 5 2035 60
i t f d fi i t bt25
• y is root of deficient subtree.
• py is parent of y.py p y
48
-
Delete A Black Degree 1 NodeDelete A Black Degree 1 Node
110
7 40 py
8 30 453
1 5 2035 60 y
25• Delete 45.
• y is root of deficient subtree• y is root of deficient subtree.49
Delete A Black Degree 2 NodeDelete A Black Degree 2 Node
110
7 40
8 30 453
1 5 2035 60
25• Not possible, degree 2 nodes are never deleted.
50
Rebalancing StrategyRebalancing Strategy
If i d d k it bl k If y is a red node, make it black.
10
7 40 py
8 30 453
1 5 2035 60 y
5
25
51
Rebalancing StrategyRebalancing Strategy
N bt i d fi i t D ! Now, no subtree is deficient. Done!
10
7 40 py
8 30 453
601 5 20
35 y5
25
52
-
Rebalancing StrategyRebalancing Strategy y is a black root (there is no py) y is a black root (there is no py). Entire tree is deficient. Done!
10 y
7 40
8 30 453
601 20 351 5 20
252553
Rebalancing StrategyRebalancing Strategy y is black but not the root (there is a py) y is black but not the root (there is a py).
py
yv
• Xcn a b• Xcn y is right child of py X = R. Pointer to v is black c = b. v has 1 red child n = 1.
54
Rb0 (case 1)Rb0 (case 1)
py pypy
v yvyv y
a ba b a b
Color change. Now, py is root of deficient subtree. Continue! Continue!
55
Rb0 (case 2)Rb0 (case 2)
py pypy
v yvyv y
a b
a b a b
Color change. Deficiency eliminated Deficiency eliminated. Done!
56
-
Rb1 (case 1)Rb1 (case 1)
py v
y
py
v a pyyv a
b y
a b b y
LL rotation. Deficiency eliminated Deficiency eliminated. Done!
57
Rb1 (case 2)Rb1 (case 2)
py w
y
py
v pyvyv
c yba w c ya b
LR rotation. Deficiency eliminated
b c Deficiency eliminated. Done!
58
Rb2Rb2
py w
y
py
v pyvyv
c yb
i
a w c ya b
LR rotation. Deficiency eliminated.
b cy
Done!
59
Rr(n)Rr(n)
n = # of red children of v’s right child w n = # of red children of v s right child w.
pypy
v yv
a w
b c
60
-
Rr(0)Rr(0)
py vpy
v a pyyv a
b
py
a b b y
LL rotation. Done!
61
Rr(1) (case 1)Rr(1) (case 1)
py w
y
py
v pyvyv
ycba w yca b
LR rotation.b c Deficiency eliminated. Done! Done!
62
Rr(1) (case 2)Rr(1) (case 2)
py x
y
py
v pyvyv
yda w yda w
Rotation.bx b c
Deficiency eliminated. D !
c d Done!
63
Rr(2)Rr(2)
py x
y
py
v pyvyv
d ya w d ya w
Rotation.bx b c
Deficiency eliminated. Done!
c d Done!
64
-
B-TreesB Trees Large degree B-trees used to represent very large Large degree B trees used to represent very large
dictionaries that reside on disk. Smaller degree B trees used for internal memory Smaller degree B-trees used for internal-memory
dictionaries to overcome cache-miss penalties.
65
Disk access by AVL TreesDisk access by AVL Trees n = 230 = 109 (approx) n 2 10 (approx). 30 ≤ height ≤ 43. When the AVL tree resides on a disk, up to 43
disk access are made for a searchdisk access are made for a search. This takes up to (approx) 4 seconds. Not acceptable.
66
Disk Access by Red-Black TreesDisk Access by Red Black Trees n = 230 = 109 (approx) n 2 10 (approx). 30 ≤ height ≤ 60. When the red-black tree resides on a disk, up to
60 disk access are made for a search60 disk access are made for a search. This takes up to (approx) 6 seconds. Not acceptable.
67
m-way Search Treesm way Search Trees Each node has up to m – 1 pairs and m children Each node has up to m – 1 pairs and m children. m = 2 binary search tree.
68
-
4-Way Search Tree4 Way Search Tree
10 30 35
k < 10 10 < k < 30 30 < k < 35 k > 35
69
Maximum # of PairsMaximum # of Pairs Happens when all internal nodes are m-nodes Happens when all internal nodes are m-nodes. Full degree m tree. # of nodes = 1 + m + m2 + m3 + … + mh-1
( h 1)/( 1)= (mh – 1)/(m – 1). Each node has m – 1 pairs.ac ode as m pa s. So, # of pairs = mh – 1.
70
Capacity of m-Way Search TreeCapacity of m Way Search Tree
m = 2 m = 200 m 2 m 200 h = 3 7 8 * 106 - 1
h = 5 31 3.2 * 1011 - 1
h = 7 127 1.28 * 1016 - 1
71
Definition of B-TreeDefinition of B Tree
D fi iti t l d ( t d d Definition assumes external nodes (extended m-way search tree).
B-tree of order m. h t m-way search tree. Not empty root has at least 2 children. Remaining internal nodes (if any) have at least
m/2 children. External (or failure) nodes on same level.
72
-
2-3 and 2-3-4 Trees2 3 and 2 3 4 Trees
B t f d B-tree of order m. m-way search tree. Not empty root has at least 2 children. Remaining internal nodes (if any) have at least Remaining internal nodes (if any) have at least
m/2 children. E l ( f il ) d l l External (or failure) nodes on same level.
• B-tree of order 2 is full binary tree.y• 2-3 tree is a B-tree of order 3. • 2-3-4 tree is a B-tree of order 4• 2-3-4 tree is a B-tree of order 4.• B-tree of order 5 is 3-4-5 tree (root may be 2-
node though)node though).73
2-3 Tree2 3 Tree 2-3 Tree 2-3 Tree
74
2-3 Tree2 3 Tree
(a) Insert 70 (b) Insert 30
75
2-3 Tree2 3 Tree
(c) Insert 60
76
-
2-3-4 Tree2 3 4 Tree
2 3 4 Tree 2-3-4 Tree
77
Minimum # of PairsMinimum # of Pairs
n = # of pairs n = # of pairs. # of external nodes = n + 1. Height = h external nodes on level h + 1.
1 1
level # of nodes
1 1
2 ≥ 2 3 ≥ 2* m/2
h + 1 ≥ 2* m/2 h-1h + 1 ≥ 2 m/2
n + 1 ≥ 2*m/2h-1, h ≥ 1n + 1 ≥ 2 m/2 , h ≥ 178
Minimum # of PairsMinimum # of Pairs
n + 1 ≥ 2*m/2h-1 h ≥ 1
m = 200
n + 1 ≥ 2*m/2h 1, h ≥ 1
m = 200.
height # of pairsheight # of pairs
2 ≥ 199
3 ≥ 19,999
4 ≥ 2 * 106 – 14 ≥ 2 10 1
5 ≥ 2 * 108 – 1
h ≤ log m/2 (n+1)/2 + 1g m/2 ( )/
79
Choice of mChoice of m
W t h ti Worst-case search time. (time to fetch a node + time to search node) * height (a + b*m + c * log2m) * h
where a b and c are constantswhere a, b and c are constants.
search search time
m50 400m50 40080
-
InsertInsert
8
15 204
1 3 5 6 30 4091 3 5 6 30 409 16 17
Insertion into a full leaf triggers bottom-up node Insertion into a full leaf triggers bottom up node splitting pass.
81
Split An Overfull NodeSplit An Overfull Node
m a0 p1 a1 p2 a2 … pm am
ai is a pointer to a subtree. i di i ipi is a dictionary pair.m/2 1 a p a p a p am/2-1 a0 p1 a1 p2 a2 … pm/2-1 am/2-1
m - m/2 am/2 p m/2 +1 am/2 +1 … pm am
• pm/2 plus pointer to new node is inserted in parent. p
82
InsertInsert
8
15 204 15 204
1 3 5 6 30 409 16 17
• Insert a pair with key = 2.p y
• New pair goes into a 3-node.
83
Insert Into A Leaf 3-nodeInsert Into A Leaf 3 node
Insert new pair so that the 3 keys are in Insert new pair so that the 3 keys are in ascending order.
• Split overflowed node around middle key
1 2 3
• Split overflowed node around middle key.
2
1 3
2
• Insert middle key and pointer to new node
1 3
• Insert middle key and pointer to new node into parent.
84
-
InsertInsert
8
15 204
1 3 5 6 30 409 61 3 5 6 30 409 16 17
• Insert a pair with key = 2• Insert a pair with key = 2.
85
InsertInsert
8
15 204 2
5 6 30 4093
1 5 6 30 409 16 171
I i i h k 2 l i • Insert a pair with key = 2 plus a pointer into parent.
86
InsertInsert
8
15 202 4
1 5 6 30 40931 5 6 30 409 16 173
• Now insert a pair with key = 18• Now, insert a pair with key = 18.
87
Insert Into A Leaf 3-nodeInsert Into A Leaf 3 node Insert new pair so that the 3 keys are in Insert new pair so that the 3 keys are in
ascending order.
• Split the overflowed node
16 17 18
• Split the overflowed node.
17
1816
• Insert middle key and pointer to new node into parent.p
88
-
InsertInsert
8
15 202 4
1 5 6 30 40931 5 6 30 409 16 173
• Insert a pair with key = 18• Insert a pair with key = 18.
89
InsertInsert
817
15 202 4 18
17
1 5 6 30 4093
18
161 5 6 30 4093 16
• Insert a pair with key = 17 plus a pointer • Insert a pair with key = 17 plus a pointer into parent.
90
InsertInsert
8 17
2 4 15 20
1 5 6 30 4093 16 181 5 6 30 4093 16 18
• Insert a pair with key = 17 plus a pointer • Insert a pair with key = 17 plus a pointer into parent.
91
InsertInsert
8 178 17
2 4 15 20
1 5 6 30 4093 16 181 5 6 30 4093 16 18
• Now insert a pair with key = 7• Now, insert a pair with key = 7.
92
-
InsertInsert
8 178 17
62 4 15 20
7
6
30 401 93 16 185
7
30 401 93 16 185
• Insert a pair with key = 6 plus a pointer • Insert a pair with key = 6 plus a pointer into parent.
93
InsertInsert
8 17 4
15 206
2
5 730 401 93 16 18
• Insert a pair with key = 4 plus a pointer • Insert a pair with key = 4 plus a pointer into parent.
94
InsertInsert8
417
6215 20
62
30 401
93
16 185 7
• Insert a pair with key = 8 plus a pointer i t tinto parent.
• There is no parent So create a new root• There is no parent. So, create a new root.95
InsertInsert
88
4 17
15 2062
30 401 93 16 185 7
• Height increases by 1• Height increases by 1.96