0

I was given a question by my class teacher to perform the insertion in a 2-3 tree.

enter image description here

What I did was the upper method. And what he wanted is the method below. Can you please tell me which is the correct method as I've looked onto web and I can see both the method there. But I still don't know why I lost 10 marks! Thanks in advance for help.

Dangling Cruze
  • 3,283
  • 3
  • 32
  • 43

1 Answers1

1

Let me start by saying there are many different versions of a 2-3 trees, so it can be confusing. Really they only differ on the way the store values. Some only store values in leafs and other store values in every node.

I believe your teacher is using latter definition of 2-3 trees. So the problem with your tree is the "root" node has the same values as it's children, a node will never share the same values as it's children. Although, I don't think what your teacher supplied is a true 2-3 tree either. A 2-3 tree will split when a node contains 3 values. I would expect below to be the proper output:

Add 4 to tree:

(4)

Add 7 to tree:

(4,7)

Add 6 to tree:

(4,6,7)
*splits*
  (6)
(4) (7)

Once the root node get's 3 values then it splits into a 1-2 tree.

If you were to insert 8:

  (6)
(4) (7,8)

If you were to insert 9:

   (6)
 (4) (7,8,9)
 *push-up*
   (6,8)
(4) (7) (9)

When a non-root node get's 3 values; push middle value up to parent, if pushing up the value the value makes the parent have 3 values then the parent will split.

Justin
  • 4,196
  • 4
  • 24
  • 48
  • Isn't it true that the parent node will contain 2 values : min on left subtree and max on middle subtree. And a parent will always have 2 and 3 nodes only, nor less neither more? So writing `(7,8)` together doesn't defy the concept? Confused! – Dangling Cruze Sep 29 '13 at 14:14
  • @Cruze As to your first question, it depends on the particular implementation of the 2-3 tree which is what makes answering your question hard to know the correct answer without knowing what your teacher is expecting. – Justin Sep 29 '13 at 14:19
  • @Cruze If you look at this page, it describes the 2-3 tree as I explained it: http://www.cs.nyu.edu/courses/spring98/V22.0102/btrees.html But this describes a 2-3 tree as you expect: http://cs.engr.uky.edu/~lewis/essays/algorithms/2-3trees/trees2-3.html – Justin Sep 29 '13 at 14:21
  • My teacher is expecting the same as you've answered. And i can't figure out what to write out in the main exam. :/ – Dangling Cruze Sep 29 '13 at 14:21
  • @Cruze I'll try to describe the rules in my answer a bit better. – Justin Sep 29 '13 at 14:24
  • I understood both the algorithms by the way. Thanks for help. :) – Dangling Cruze Sep 29 '13 at 14:28
  • @Cruze No problem. Do you need anymore help? BTW 2-3 trees are the most confusing of the balanced trees because of conflicting definition. But after you choose a definition they are pretty easy to implement :-) – Justin Sep 29 '13 at 14:31