courses/coursera-sandiego-algorithms/data-structures/assignment004/set_range_sum/set_range_sum.cpp
Vertex* find(Vertex*& root, int key) {
  // DebugStream() << "    finding " << key << std::endl;
  // DebugStream() << "        before----------------------" << std::endl;
  // printTree(root, 4);

  if( root!= nullptr && root->key == key) return root;

  Vertex* v = root;
  Vertex* last = root;
  Vertex* next = NULL;
  while (v != NULL) {
    if (v->key >= key && (next == NULL || v->key < next->key)) {
      next = v;
    }
    last = v;
    if (v->key == key) {
      break;      
    }
    if (v->key < key) {
      v = v->right;
    } else {
      v = v->left;
    }
  }
  // if( last != nullptr) DebugStream() << "    last is " << last->key <<std::endl;
  splay(root, last);
  // DebugStream() << "      after----------------------" << std::endl;
  // printTree(root, 4);

  return next;
}
// Searches for the given key in the tree with the given root
// and calls splay for the deepest visited node after that.
// If found, returns a pointer to the node with the given key.
// Otherwise, returns a pointer to the node with the smallest
// bigger key (next value in the order).
// If the key is bigger than all keys in the tree, 
// returns NULL.
Searches for the given key in the tree with the given root
and calls splay for the deepest visited node after that.
If found, returns a pointer to the node with the given key.
Otherwise, returns a pointer to the node with the smallest
bigger key (next value in the order).
If the key is bigger than all keys in the tree,
returns NULL.
Searches for the given key in the tree with the given root and calls splay for the deepest visited node after that . If found returns a pointer to the node with the given key . Otherwise returns a pointer to the node with the smallest bigger key ( next value in the order ) . If the key is bigger than all keys in the tree returns NULL .
Vertex* find(Vertex*& root, int key) {
  if( root!= nullptr && root->key == key) return root;
  Vertex* v = root;
  Vertex* last = root;
  Vertex* next = NULL;
  while (v != NULL) {
    if (v->key >= key && (next == NULL || v->key < next->key)) {
      next = v;
    }
    last = v;
    if (v->key == key) {
      break;      
    }
    if (v->key < key) {
      v = v->right;
    } else {
      v = v->left;
    }
  }
  splay(root, last);
  return next;
}
Vertex * find ( Vertex * & root , int key ) { if ( root != nullptr && root -> key == key ) return root ; Vertex * v = root ; Vertex * last = root ; Vertex * next = NULL ; while ( v != NULL ) { if ( v -> key >= key && ( next == NULL || v -> key < next -> key ) ) { next = v ; } last = v ; if ( v -> key == key ) { break ; } if ( v -> key < key ) { v = v -> right ; } else { v = v -> left ; } } splay ( root , last ) ; return next ; }
Searches for the given key in the tree with the given root
and calls splay for the deepest visited node after that.
Searches for the given key in the tree with the given root and calls splay for the deepest visited node after that .
// DebugStream() << "    finding " << key << std::endl; // DebugStream() << "        before----------------------" << std::endl; // printTree(root, 4); // if( last != nullptr) DebugStream() << "    last is " << last->key <<std::endl; // DebugStream() << "      after----------------------" << std::endl; // printTree(root, 4);