@Override
	public Iterator<Interval<T>> iterator() {
		if (root == null){
			return Collections.emptyIterator();
		}
		else {
			final TreeNodeIterator it = root.iterator();
			return new Iterator<Interval<T>>() {
				@Override
				public void remove() {
					if (it.currentNode.increasing.size() == 1){
						root = TreeNode.removeInterval(IntervalTree.this, root, it.currentInterval);

						// Rebuild the whole branch stack in the iterator, because we might have
						// moved nodes around and introduced new nodes into the branch. The rule
						// is, add all nodes to the branch stack, to which the current node is
						// a left descendant.
						TreeNode<T> node = root;
						it.stack = new Stack<>();

						// Continue pushing elements according to the aforementioned rule until
						// you reach the subtreeRoot - this is the root of the subtree, which
						// the iterator has marked for traversal next. This subtree must not
						// become a part of the branch stack, or otherwise you will iterate over
						// some intervals twice.
						while (node != it.subtreeRoot){
							if (it.currentNode.midpoint.compareTo(node.midpoint) < 0) {
								it.stack.push(node);
								node = node.left;
							}
							else {
								node = node.right;
							}
						}
					} else {
						it.remove();
					}
				}

				@Override
				public boolean hasNext() {
					return it.hasNext();
				}

				@Override
				public Interval<T> next() {
					return it.next();
				}
			};
		}
	}
@ Override public Iterator < Interval < T > > iterator ( ) { if ( root == null ) { return Collections . emptyIterator ( ) ; } else { final TreeNodeIterator it = root . iterator ( ) ; return new Iterator < Interval < T > > ( ) { @ Override public void remove ( ) { if ( it . currentNode . increasing . size ( ) == 1 ) { root = TreeNode . removeInterval ( IntervalTree . this , root , it . currentInterval ) ; TreeNode < T > node = root ; it . stack = new Stack < > ( ) ; while ( node != it . subtreeRoot ) { if ( it . currentNode . midpoint . compareTo ( node . midpoint ) < 0 ) { it . stack . push ( node ) ; node = node . left ; } else { node = node . right ; } } } else { it . remove ( ) ; } } @ Override public boolean hasNext ( ) { return it . hasNext ( ) ; } @ Override public Interval < T > next ( ) { return it . next ( ) ; } } ; } }
=========================================================================