code: function TreeIterator(tree) {
  
      // the tree, current slot index, and previous and next pointers are private attributes
      // so methods that use them are defined in the constructor
      var currentSlot = 0;  // the slot before the first item
      var previous = undefined;
      var next = minimum(tree.root);
  
      this.toStart = function() {
          currentSlot = 0;  // the slot before the first item
          previous = undefined;
          next = minimum(tree.root);
      };
  
      this.toSlot = function(slot) {
          currentSlot = slot;
          previous = tree.node(slot - 1);  // javascript index of item before the slot
          next = successor(previous);
      };
  
      this.toEnd = function() {
          currentSlot = tree.size;  // the slot after the last item
          previous = maximum(tree.root);
          next = undefined;
      };
  
      this.hasPrevious = function() {
          return currentSlot > 0;
      };
  
      this.hasNext = function() {
          return currentSlot < tree.size;
      };
  
      this.getPrevious = function() {
          if (!this.hasPrevious()) return;
          const value = previous.value;
          next = previous;
          previous = predecessor(next);
          currentSlot--;
          return value;
      };
  
      this.getNext = function() {
          if (!this.hasNext()) return;
          const value = next.value;
          previous = next;
          next = successor(previous);
          currentSlot++;
          return value;
      };
  
      return this;
  }
docs: PRIVATE CLASSES /*
  The set class is backed by a binary tree (treap) structure. Therefore,
  it can be traversed more efficiently using a custom iterator. This class implements a tree iterator.