0

Is there a rule for how to split the node in 2-3-4 tree?

E.g. If I insert 3, 7, 4, 9 into the 2-3-4 tree:

Will it be split like this (yellow) or that (green) as shown here:

enter image description here

Are both valid?

user963241
  • 6,758
  • 19
  • 65
  • 93

1 Answers1

2

Green. You need to consider the algorithm steps. Check out the the wikipedia page for insertion steps. The key part is to split a 4-node (which has 3 values) by moving the middle value up a level before considering the next insert.

1. Insert 3 into blank. Result: 3         (a 2-node)
2. Insert 7.            Result: 3 - 7     (a 3-node)
3. Insert 4.            Result: 3 - 4 - 7 (a 4-node)
5. Insert 9. There is already a 4-node, so this must be split.
   The split will be to move 4 up a level, and 3 and 7 are now child nodes of 4
   (like your green diagram). 9 is then added next to the 7.
G42
  • 9,791
  • 2
  • 19
  • 34
  • But in case of 2-3 trees we split after inserting the value. So, that the middle becomes the root value. So, in case of 4-node it will be different? – user963241 Feb 17 '17 at 10:56
  • With a 2-3 tree, you have 2-values when dealing with a 3-node so there is no middle value. This is not the vase with 2-3-4 trees. I have actually seen a few algorithms that insert value then split for a 2-3-4 tree so it looks like **both are valid.** – G42 Feb 17 '17 at 11:36
  • Actually [the other method](https://www.cs.umd.edu/class/summer2016/cmsc132/lectures/Lecture17_2_3_4_tree.pdf) also used split **before** insert. – G42 Feb 17 '17 at 11:53