Pseudo-Code For 2-4 Tree
Pseudo-Code For 2-4 Tree
Youming Liu
1 Tree Node
A node x of 2-4 Tree has the following fields:
4. c1 , c2 , · · · , cn ; let ki be any key stored in the subtree with root ci , then k1 ≤ key1 ≤ k2 ≤
· · · ≤ keyn .
2 Create()
root = ALLOCAT E − N ODE()
root.leaf = T RU E
root.n = 0
return root
3 Search(x,k)
i=1
while i ≤ x.n and k > x.keyi do
i = i + 1;
end while
if i ≤ x.n and k = x.keyi then
return (x, i)
end if
if x.leaf then
return NIL
else
return Search(x.ci , k)
end if
1
4 Split(x,i) -(auxiliary)
z = ALLOCAT E − N ODE()
y = x.ci
t = y.n
2
z.leaf = y.leaf
z.n = y.n − t
for j = 1 to t do
z.keyj = y.keyj+t
end for
if not y.leaf then
for j = 1 to t do
z.cj = y.cj+t
end for
end if
y.n = t
for j = x.n + 1 downto i + 1 do
x.cj+1 = x.cj
end for
x.ci+1 = z
for j = x.n downto i do
x.keyj+1 = x.keyj
end for
x.keyi = y.keyt
x.n = x.n + 1
5 InsertNonfull(x,k) -(auxiliary)
i = x.n
if x.leaf then
while i ≥ 1 and k < x.keyi do
x.keyi+1 = x.keyi
i=i−1
end while
x.keyi+1 = k
x.n = x.n + 1
else
while i ≥ 1 and k < x.keyi do
i=i−1
end while
i=i+1
if x.ci .n == 4 then
Split(x, i)
if k > x.keyi then
i=i+1
end if
end if
2
InsertN onf ull(x.ci , k)
end if
6 InsertMax(x,k) -(auxiliary)
i = x.n
if x.leaf then
x.keyi+1 = k
x.n = x.n + 1
else
x.keyi+1 = k
if x.ci .n == 4 then
Split(x, i)
i=i+1
end if
InsertM ax(x.ci , k)
end if
7 Insert(root, k)
i = root.n
if i == 4 then
s = ALLOCAT E − N ODE()
s.leaf = F ALSE
s.n = 1
s.c1 = root
s.key1 = root.key4
root = s
Split(s, 1)
if k > s.key2 then
InsertM ax(s, k)
else
InsertN onf ull(s, k)
end if
else
if k > s.keyi then
InsertM ax(root, k)
else
InsertN onf ull(root, k)
end if
end if