Embercode
✕
Binary Search TreesQuick quiz

BST iterator

6 questions. Covers BST iterator with a stack.

Questions 1 to 5 of 6

  1. 1. Using Tree, insert and BstIterator from this lesson, what does this code print? ``cpp Tree a; for (int k : {5, 2, 8, 1, 3}) a = insert(std::move(a), k); BstIterator it(a.get()); std::cout << it.stack_size() << ' '; std::cout << it.next() << ' ' << it.stack_size() << '\n'; ``
  2. 2. Using the same tree as the question before (keys 5, 2, 8, 1 and 3 inserted in that order) and DescendingIterator from this lesson, what does this code print? ``cpp DescendingIterator di(a.get()); while (di.has_next()) std::cout << di.next() << ' '; ``
  3. 3. In BstIterator::next(), why does the code call push_left on node->right, and not just push node->right on the stack?
  4. 4. A tree of the keys 1 to 1000 is built by inserting them in ascending order, and BstIterator walks the whole tree. What is the peak stack size?
  5. 5. What is the total work of a full walk over n keys with BstIterator, counting every push and pop?

5 question(s) left on this page.