[prev] 59 [next]

Insertion at Root (cont)

Algorithm for inserting at root:

insertAtRoot(t, it):
|  Input tree t, item it to be inserted
|  Output modified tree with item at root
|
|  if t is empty tree then
|    t = new node containing item
|  else if it < root(t) then
|    left(t) = insertAtRoot(left(t), it)
|    t = rotateRight(t)
|  else if it > root(t) then
|    right(t) = insertAtRoot(right(t), it)
|    t = rotateLeft(t)
|  end if
|  return t;