next up previous
Next: Examples Up: CSE 2320: Algorithms and Previous: Successor(n)

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
      if key(x) < key(p)
         then left(p) = x
         else right(p) = x

Insert is



Copyright © 1998 The University of Texas at Arlington