0

I had thought I understood BSTs. That was until my Professor came along.

Let's say I have a BST:

   2
  / \
 1   3

Now if I were to insert 4, my tree would look like this:

   2
  / \
 1   3
      \
       4

but my Professor's tree would end up like this:

   2
  / \
 1   4
    / \
   3   4

Basically, he finds where the new node should be placed and places it there. He then changes the value of the new node's parent to the new node's value and makes the left child of the parent what the original parent node used to be.

I have looked around online but can't find anyone doing this.

What kind of insertion technique is this? Am I missing something? I don't think it would make a difference but this was specifically for AVL trees.

aanrv
  • 2,159
  • 5
  • 25
  • 37
  • In a `BST`, the `left/right` **children** must be **smaller** than the **parent**. (e.g. child `3` must be below `4` on the right side) See [**Inserting an element in Binary Tree**](http://stackoverflow.com/questions/16292786/inserting-an-element-in-binary-tree) – David C. Rankin Mar 19 '15 at 21:29
  • @DavidC.Rankin Left and right children must be smaller? Wouldn't that make a max heap? – aanrv Mar 19 '15 at 21:34

2 Answers2

0

I think he is trying to keep the tree strictly binary ,i.e., each node has 0 or exactly 2 children.

As i understand, in the example, the first and second 4s are added in their places. A left rotation (required for balancing the avl tree) brings the final shape.

sray
  • 584
  • 3
  • 8
0

Your insertion of key "4" into a BST is completely right, I can assure you. This is the orthodox and the simplest way to insert elements into a BST.

I think that you've misunderstood your professor, because your third illustration is incorrect at least because of the fact that in BST no duplicates are allowed, and you have element "4" occurring twice.

Vladimir
  • 457
  • 6
  • 17