2-3-4 Tree

2-3-4Tree
AnwarMamat
2-3-4Tree
• Self-balancingtree
• everyinternalnodehaseithertwo,three,orfourchildnodes.
• a2-nodehasonedataelement,andifinternalhastwochildnodes;
• a3-nodehastwodataelements,andifinternalhasthreechildnodes;
• a4-nodehasthreedataelements,andifinternalhasfourchildnodes.
2-3-4TreeProperties
• Everynode(leaforinternal)isa2-node,3-nodeora4-node,and
holdsone,two,orthreedataelements,respectively.
• Allleavesareatthesamedepth(thebottomlevel).
• Alldataiskeptinsortedorder.
• Treeheight.
•
•
•
•
Worstcase:lg N [all2-nodes]
Bestcase:log4N=1/2lg N[all4-nodes]
Between10and20for1millionnodes.
Between15and30for1billionnodes.
• Guaranteedlogarithmicperformanceforbothsearchandinsert.
2-3-4TreeInsertion
• Ifthecurrentnodeisa4-node:
• Removeandsavethemiddlevaluetogeta3-node.
• Splittheremaining3-nodeupintoapairof2-nodes(thenowmissingmiddlevalueis
handledinthenextstep).
• Ifthisistherootnode(whichthushasnoparent):
• themiddlevaluebecomesthenewroot2-nodeandthetreeheightincreasesby1.Ascend
intotheroot.
• Otherwise,pushthemiddlevalueupintotheparentnode.Ascendintotheparent
node.
• Findthechildwhoseintervalcontainsthevaluetobeinserted.
• Ifthatchildisaleaf,insertthevalueintothechildnodeandfinish.
• Otherwise,descendintothechildandrepeatfromstep1
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
2-3-4TreeExample:Insertion
1
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
2-3-4TreeExample
1
8
12
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
2-3-4TreeExample
Insert2
1
8
12
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
2-3-4TreeExample
8
Insert2
1
8
12
1
2
12
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
2-3-4TreeExample
8
1
2
Insert25
12
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
2-3-4TreeExample
8
1
2
8
Insert25
12
1
2
12 25
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
2-3-4TreeExample
8
Insert6
1
2
12 25
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
2-3-4TreeExample
8
8
Insert6
1
2
12 25
1
2
6
12 25
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
2-3-4TreeExample
8
1
2
6
Insert14
12 25
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
2-3-4TreeExample
8
1
2
6
8
Insert14
12 25
1
2
6
12 14 25
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
2-3-4TreeExample
Insert28
8
1
2
6
12 25
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
2-3-4TreeExample
Insert28
8 14
8
1
2
6
12 14 25
1
2
6
12
25 28
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
2-3-4TreeExample
Insert17
8 14
1 2 6
12
25 28
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
2-3-4TreeExample
8 14
8 14
1 2 6
12
25 28
Insert7
1
2
6
12
17 25 28
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
2-3-4TreeExample
8 14
1 2 6
12
17 25 28
Insert7
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
2-3-4TreeExample
8 14
1 2 6
12
2 8 14
17 25 28
Insert7
1
6 7
12
17 25 28
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
2-3-4TreeExample
2 8 14
1
6 7
12
Insert52
17 25 28
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
2-3-4TreeExample
2 8 14
1
6 7
12
17 25 28
Insert52
8
2
2 8 14 25
1
6 7
12
17 28 52
1
6 7
14 25
12
17
28 52
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
2-3-4TreeExample
8
2
1
6
14
7
12
Insert16
25
17
28 52
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
2-3-4TreeExample
8
2
1
6
14
7
12
8
25
17
28 52
2
1
Insert16
6
14
7
12
25
16
17
28
52
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
2-3-4TreeExample
8
2
1
6
14
7
Insert48
12
25
16
17
28 52
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
2-3-4TreeExample
8
2
1
6
14 25
7
12
16
17
28 52
8
Insert48
2
1
6
14
7
12
25
16
17
28
48
52
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
2-3-4TreeExample
8
2
1
6
14
7
Insert68
12
25
16
17
28 48 52
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
2-3-4TreeExample
8
2
1
6
14
7
12
25
16
17
28 48 52
8
2
14
Insert68
1
6
7
12
25
16
48
17
28
52
68
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
2-3-4TreeExample
8
2
1
6
14
7
12
25
16
Insert3,26
48
17
28
52
68
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
2-3-4TreeExample
8
2
14 25 48
8
1
6
7
12
16
17
28
52 68
2
14
25
48
16
17
Insert3,26
1
3
6
7
12
26
28
52
68
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
2-3-4TreeExample
8
2
1
14
3
6
7
12
25
48
16
17
Insert55
26
28
29
52
53
68
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
2-3-4TreeExample
8
2
1
14 25 48
3 6 7
12
16 17
26 28 29
52 53 68
8 25
2
Insert55
1
48 53
14
3 6 7
12
16 17
26 28 29
52
55 68
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
2-3-4TreeExample
8
25
2
1
48 53
14
3
6
7
12
Insert45
16
17
26
28 29
52
55
68
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
2-3-4TreeExample
8 25
2
1
48 53
14
3 6 7
12
16 17
26 28 29
55 68
52
8
25
Insert45
2
1
14
3
6
7
12
28
16
17
26
48
29 45
53
52
55
68
2-3-4Tree:Delete
• Leaf:
• Justdeletethekey
• Makesure that a leaf is not empty after deleting a key
Delete2
2-3-4Tree:Delete
• Leaf:
• Whenkey deletion would create an empty leaf,borrow akeyfrom leaf's
immediate siblings (i.e.to the left and then right) and try to.
2-3-4Tree:Delete
• Leaf:
• Ifsiblings are2-nodes(no immediate sibling from which toborrow a key),steal
akey from our parent by doing the opposite of a split.
Delete6
2-3-4Tree:Delete
• Whatifparentisa2-node (onekey)?
2-3-4Tree:Delete
• Whatifparentisa2-node (onekey)?
• Stealfromsiblings (parent’s)
• Merge
2-3-4Tree:Delete
• Whatifparentisa2-node (onekey)?
• Stealfromsiblings (parent’s)
• Merge
2-3-4Tree:Delete
• InternalNode:
• Deletethepredecessor, andswapitwiththenode tobedeleted.
Delete5:firstdelete4,thenswap4for5.
2-3-4Tree:Delete
• InternalNode:
• Deletethepredecessor, andswapitwiththenode tobedeleted.
• Keytodeletedmaymove.
Delete2:firstdelete1,thenswap1for2.
2-3-4TreeExample:Delete
8
2
1
25
14
3
6
7
12
28
16
Delete3,17,55
17
26
48
29 45
53
52
55
68
2-3-4TreeExample:Delete
8
2
1
25
14
6
7
12
28
16
26
48
29 45
53
52
Delete1:borrowfromsiblings(rotate)
68
2-3-4TreeExample:Delete
8
6
2
25
14
7
12
Delete1
28
16
26
48
29 45
53
52
68
2-3-4TreeExample:Delete
8
6
2
25
14
7
12
28
16
26
48
29 45
Delete52:borrowfromsibling
53
52
68
2-3-4TreeExample:Delete
8
6
2
25
14
7
12
28
16
26
45
29
Delete52:borrowfromsibling
53
48
68
2-3-4TreeExample:Delete
8
6
2
25
14
7
12
28
16
26
45
53
29
Delete48:borrowfromparent
48
68
2-3-4TreeExample:Delete
8
6
2
merge
7
25
14
12
28
16
26
29
53
45
Delete2:borrowfromparent,andparent
68
2-3-4TreeExample:Delete
25
8
6
7
14
12
28
16
26
29
53
45
Delete2:borrowfromparent,andparent
68
2-3-4TreeExample:Delete
25
8
6
7
14
12
28
16
26
29
53
45
Delete14:delete12,swap12for14
68
2-3-4TreeExample:Delete
25
7
6
12
8
28
16
26
29
53
45
Delete14:delete12,swap12for14
68
2-3-4TreeExample:Delete
25
7
6
12
8
28
16
26
29
53
45
Delete25:delete16,swap16for25
68
2-3-4TreeExample:Delete
16
7
6
28
8
12
26
29
53
45
Delete25:delete16,swap16for25
68
Represent2-3-4treeasaBST
• Use"internal"red edgesfor3- and4- nodes.
• Requirethat3-nodesbeleft-leaning.
Represent2-3-4treeasaBST
• ElementaryBSTsearchworks
• Easy-to-maintain1-1 correspondencewith2-3-4trees
• Treesthereforehaveperfectblack-linkbalance