0% found this document useful (0 votes)
36 views3 pages

Pseudo-Code For 2-4 Tree

This document outlines the pseudo-code for operations on a 2-4 tree, including creating an empty tree, searching for a key, splitting a node, and inserting a new key. It defines the structure of nodes, which can store up to 4 keys and have up to 4 child nodes. The insert operation first checks if the root is full, splits it if needed, and then calls auxiliary functions to insert the key into the appropriate node.

Uploaded by

Dức Trần
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views3 pages

Pseudo-Code For 2-4 Tree

This document outlines the pseudo-code for operations on a 2-4 tree, including creating an empty tree, searching for a key, splitting a node, and inserting a new key. It defines the structure of nodes, which can store up to 4 keys and have up to 4 child nodes. The insert operation first checks if the root is full, splits it if needed, and then calls auxiliary functions to insert the key into the appropriate node.

Uploaded by

Dức Trần
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Pseudo-code for 2-4 Tree

Youming Liu

Jan 22, 2013

1 Tree Node
A node x of 2-4 Tree has the following fields:

1. n, the number of keys currently stored in node x,

2. key1 ≤ key2 ≤ · · · ≤ keyn , the n keys themselves,

3. leaf , a boolean value that is TRUE if x is a leaf,

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

You might also like