| | | | | |

Implementing Get: Unsuccessful Search

T& BST<T,P>::Get (const T& t) // NOT const!
{
  Node * n = root_;  // travels down the tree
  Node * p = 0;      // maintain as parent of n
  bool left;
  while(n)           // search loop
  {
    ... (see previous slide)
  }
  // if we arrive here, search has failed
  // p points to the last node in the search path 
  // "left" tells us which direction we took from p to n == nullptr
  n = NewNode(t);                            // create new node with value t
  (left ? p->lchild_ = n : p->rchild_ = n);  // attach n to left or right of p
  return n->value_;                          // return by reference - see return type
}

| | Top of Page | 12. Binary Search Trees - 11 of 27