Search code examples
data-structures2-3-4-tree

2-3-4 Tree generating from a list of numbers


I have the list of numbers 50,40,60,30,70. Lets assume I would like to insert these to an empty 2-3-4 Tree. Which one of these numbers would be the parent root of the tree and why? Is it the insertion order, is it how big the number is? I would like to be able to draw 234Tree when I'm giving a list of numbers. I can't seem to do that because I don't know which one to use as a parent root to start with. Simply, what factor specifies the parent root of this tree.


Solution

  • In a balanced tree data structure, the root element will usually contain a value close to the median of the items that have been added to it. However, because the tree will usually not be perfectly balanced, you may not have the exact median in the root. The exact structure of the tree may be dependent on the order the values were added to it.

    In your question, you mention adding five items to a 2-3-4 tree. That will always end up with a two-level tree structure, but the exact structure will vary depending on the order the elements are added. If you add them in the order they're listed in the question, you'll get:

    root -> <50>
           /    \
      <30,40>  <60,70>
    

    But if you added the elements in another order, you could have 40 or 60 in the root and 50 in one of the leaf nodes.