next up previous
Next: Up: Previous:

Insertion

1.
Go search for key until run off the end of the tree
2.
Put new key there

   Insert(T,x)                        ; x = pointer to new node
      p = NIL
      n = root(T)                     ; Search for key
      while n <> NIL
         p = n
         if key(x) < key(n)
         then n = left(n)
         else n = right(n)
      Parent(x) = p                   ; Insert new key
      if p = NIL
      then root(T) = x
      else if key(x) < key(p)
         then left(p) = x
         else right(p) = x

Insert is O(h)


next up previous
Next: Up: Previous: