File: SparseListNode.java

package info (click to toggle)
libglazedlists-java 1.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,024 kB
  • ctags: 4,252
  • sloc: java: 22,561; xml: 818; sh: 51; makefile: 5
file content (825 lines) | stat: -rw-r--r-- 26,763 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
/* Glazed Lists                                                 (c) 2003-2006 */
/* http://publicobject.com/glazedlists/                      publicobject.com,*/
/*                                                     O'Dell Engineering Ltd.*/
package ca.odell.glazedlists.impl.adt;

// for iterators
import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * A SparseListNode models a node in an SparseList.  This class
 * does the bulk of the heavy lifting for SparseList.
 *
 * @author <a href="mailto:kevin@swank.ca">Kevin Maltby</a>
 *
 */
public final class SparseListNode {

    /** the parent node */
    private SparseListNode parent;

    /** the tree that this node is a member of */
    private SparseList host;

    /** the left and right child nodes */
    private SparseListNode left = null;
    private SparseListNode right = null;

    /** the size of the left subtree and right subtrees including empty space */
    private int totalRightSize = 0;
    private int totalLeftSize = 0;

    /** the amount of empty space that preceeds this node */
    private int emptySpace = 0;

    /** the height of this subtree */
    private int height = 1;

    /** the value at this node */
    private Object value = null;

    /**
     * Creates a new SparseListNode with the specified parent node, host tree and value.
     */
    SparseListNode(SparseList host, SparseListNode parent, Object value) {
        this.host = host;
        this.parent = parent;
        this.value = value;
    }

    /**
     * This is a convienience constructor for creating a new SparseListNode
     * with a given value and amount of preceeding empty space.
     */
    SparseListNode(SparseList host, SparseListNode parent, Object value, int emptySpace) {
        this(host, parent, value);
        this.emptySpace = emptySpace;
    }

    /**
     * Returns the size of the subtree rooted at this node
     */
    int size() {
        return totalLeftSize + emptySpace + totalRightSize + 1;
    }

    /**
     * Inserts a value into the host tree.
     */
    void insert(int index, Object value) {
        int localizedIndex = index - totalLeftSize;

        // Recurse to the Left adjusting sizes as you go
        if(localizedIndex < 0) {
            totalLeftSize++;
            left.insert(index, value);

        // Recurse to the Right adjusting sizes as you go
        } else if(localizedIndex > emptySpace) {
            totalRightSize++;
            right.insert(localizedIndex - emptySpace - 1, value);

        // Insert in the middle of the empty space
        } else if(localizedIndex < emptySpace) {
            emptySpace -= localizedIndex;
            totalLeftSize += localizedIndex + 1;
            if(left == null) {
                left = new SparseListNode(host, this, value, localizedIndex);
                ensureAVL();
            } else {
                left.insertAtEnd(value, localizedIndex);
            }

        // Insert at the same index as this node
        } else {
            insertAtThisNode(value);
        }
    }

    /**
     * Inserts a value into the host tree at an index where a value already
     * exists.  This will offset the current node's value by 1.
     */
    private void insertAtThisNode(Object value) {
        SparseListNode replacement = new SparseListNode(host, parent, value, emptySpace);
        emptySpace = 0;
        replacement.height = height;
        height = 1;
        replacement.totalRightSize = totalRightSize + 1;
        // Since the left side will be unaffected by this insert, just 'move' it onto the replacement
        replacement.left = left;
        if(left != null) {
            replacement.left.parent = replacement;
            replacement.totalLeftSize = totalLeftSize;
            totalLeftSize = 0;
            left = null;
        }

        // Notify the host tree that the root has changed
        if(parent == null) host.setRootNode(replacement);

        // Replace this with the new child in the parent
        else parent.replace(this, replacement);

        // Move this to the right child of the replacement
        if(right == null) {
            parent = replacement;
            replacement.right = this;
            replacement.ensureAVL();

        // Move this to be the smallest node in the right subtree
        } else {
            replacement.right = right;
            replacement.right.parent = replacement;
            totalRightSize = 0;
            right = null;
            replacement.right.moveToSmallest(this);
        }
    }

    /**
     * Inserts a value at the end of the tree rooted at this.
     */
    void insertAtEnd(Object value, int leadingNulls) {
        // Adjust sizes during recursion
        totalRightSize += leadingNulls + 1;

        // Recurse to the right
        if(right != null) right.insertAtEnd(value, leadingNulls);

        // Insert on the right
        else {
            right = new SparseListNode(host, this, value, leadingNulls);
            ensureAVL();
        }
    }

    /**
     * Inserts multiple null values as empty space in the host tree.
     */
    void insertEmptySpace(int index, int length) {
        int localizedIndex = index - totalLeftSize;

        // Recurse to the Left
        if(localizedIndex < 0) {
            totalLeftSize += length;
            left.insertEmptySpace(index, length);

        // Recurse to the Right
        } else if(localizedIndex > emptySpace) {
            totalRightSize += length;
            right.insertEmptySpace(localizedIndex - emptySpace - 1, length);

        // Insert at this node
        } else {
            emptySpace += length;
        }
    }

    /**
     * Moves a given node to be the smallest node in the subtree rooted at
     * this.
     */
    private void moveToSmallest(SparseListNode movingNode) {
        // Adjust sizes during recursion
        totalLeftSize += movingNode.emptySpace + 1;

        // Recurse to the left
        if(left != null) {
            left.moveToSmallest(movingNode);

        // Add the node as a left child of this
        } else {
            // Add the moving node on the left
            movingNode.parent = this;
            left = movingNode;

            // Adjust heights and rotate if necessary
            ensureAVL();
        }
    }

    /**
     * Gets the index of the value in this node.  This is NOT the index of the
     * first null indexed by this node.
     */
    public int getIndex() {
        if(parent != null) return parent.getIndex(this) + totalLeftSize + emptySpace;
        return totalLeftSize + emptySpace;
    }
    private int getIndex(SparseListNode child) {
        // the child is on the left, return the index recursively
        if(child == left) {
            if(parent != null) return parent.getIndex(this);
            return 0;

        // the child is on the right, return the index recursively
        } else {
            if(parent != null) return parent.getIndex(this) + totalLeftSize + emptySpace + 1;
            return totalLeftSize + emptySpace + 1;
        }
    }

    /**
     * Gets the node with the given index, or null if that index is empty.
     */
    SparseListNode getNode(int index) {
        int localizedIndex = index - totalLeftSize;

        // Recurse to the Left
        if(localizedIndex < 0) return left.getNode(index);

        // Recurse to the Right
        else if(localizedIndex > emptySpace) return right.getNode(localizedIndex - emptySpace - 1);

        // Get a null from the middle of the empty space
        else if(localizedIndex < emptySpace) return null;

        // Get this node
        else return this;
    }

    /**
     * Gets the value of this node.
     */
    public Object getValue() {
        return value;
    }

    /**
     * Sets the value of this node and returns the replaced value.
     * If the value is set to null, this node  will be removed from
     * the tree and clear() will be called.
     */
    public Object setValue(Object value) {
        // Just a simple set operation
        if(value != null) {
            Object oldValue = this.value;
            this.value = value;
            return oldValue;

        // This node must be removed and replaced with empty space
        } else {
            emptySpace++;
            return unlink();
        }
    }

    /**
     * Sets the value of the node at a given index.
     */
    Object set(int index, Object value) {
        int localizedIndex = index - totalLeftSize;

        // Recurse to the Left
        if(localizedIndex < 0) {
            return left.set(index, value);

        // Recurse to the Right
        } else if(localizedIndex > emptySpace) {
            return right.set(localizedIndex - emptySpace - 1, value);

        // Set a value in the middle of the empty space
        } else if(localizedIndex < emptySpace) {
            if(value == null) return null;
            emptySpace--;
            insert(index, value);
            return null;

        // Set the value in this node
        } else {
            return setValue(value);
        }
    }

    /**
     * Removes and returns the value at the given index.
     */
    Object remove(int index) {
        int localizedIndex = index - totalLeftSize;

        // Recurse to the Left
        if(localizedIndex < 0) {
            totalLeftSize--;
            return left.remove(index);

        // Recurse to the Right
        } else if(localizedIndex > emptySpace) {
            totalRightSize--;
            return right.remove(localizedIndex - emptySpace - 1);

        // Remove from the middle of the empty space
        } else if(localizedIndex < emptySpace) {
            emptySpace--;
            return null;

        // Remove from the value in this node
        } else {
            return unlink();
        }
    }

    /**
     * Unlinks this node from the tree and clears it.
     */
    private Object unlink() {
        int index = -1;
        SparseListNode replacement = null;
        boolean isLeftChild = false;

        // Two children exist
        if(right != null && left != null) {
            return unlinkFromTwoChildren();

        // Only a right child exists
        } else if(right != null) {
            replacement = right;
            replacement.parent = parent;
            replacement.emptySpace += emptySpace;

        // A left child or no child exists, which are handled almost the same way
        } else {

            // Only a left child exists
            if(left != null) {
                replacement = left;
                replacement.parent = parent;

            // No children exist
            } else replacement = null;

            // Parent is null so empty space moves to the trailing nulls iff it is significant
            if(parent == null) index = emptySpace == 0 ? -1 : host.size();

            // This is a left child so empty space goes to the parent
            else if(parent.left == this) {
                isLeftChild = true;
                parent.emptySpace += emptySpace;
                parent.totalLeftSize -= emptySpace;

            // Find the index of the empty space to insert it later iff it is significant
            } else if(emptySpace != 0) index = getIndex() - emptySpace;
        }

        // This wasn't the root of the tree
        if(parent != null) {
            parent.replace(this, replacement);
            parent.ensureAVL();

        // This was the root so replace the reference in the host
        } else {
            host.setRootNode(replacement);
        }

        // Empty space needs to be reinserted elsewhere
        if(index != -1) {
            if(parent != null) parent.prepareForReinsert(isLeftChild, emptySpace);
            host.addNulls(index, emptySpace);
        }
        return clear();
    }

    /**
     * Unlinks this node in the special case where this node has both
     * a left and right child.
     */
    private Object unlinkFromTwoChildren() {
        // Get the replacement from the right subtree
        SparseListNode replacement = right.pruneSmallestChild();
        SparseListNode repParent = replacement.parent;
        replacement.emptySpace += emptySpace;
        replacement.height = height;

        // left subtree is unaffected so move it and cache sizes
        replacement.left = left;
        replacement.left.parent = replacement;
        replacement.totalLeftSize = totalLeftSize;

        // adjust replacement's parent link to this.parent
        replacement.parent = parent;

        // Notify the host tree that the root has changed
        if(parent == null) host.setRootNode(replacement);

        // Replace this with the new child in the parent
        else parent.replace(this, replacement);

        // The smallest node is the right child of this
        if(repParent == this) replacement.ensureAVL();

        //  The smallest node is a left child in the right subtree
        else {
            // linking on the right subtree needs updating
            repParent.left = replacement.right;
            if(repParent.left != null) repParent.left.parent = repParent;
            repParent.totalLeftSize = replacement.totalRightSize;
            replacement.right = right;
            replacement.right.parent = replacement;
            replacement.totalRightSize = replacement.right.size();
            repParent.ensureAVL();
        }
        return clear();
    }

    /**
     * Prunes and returns the smallest child of the subtree rooted at this.
     * Tree references are maintained out of necessity of the calling method,
     * but sizes in the subtree are corrected accordingly.
     */
    private SparseListNode pruneSmallestChild() {
        // Recurse to the left
        if(left != null) {
            SparseListNode prunedNode = left.pruneSmallestChild();
            totalLeftSize -= prunedNode.emptySpace + 1;
            return prunedNode;

        // return this node
        } else return this;
    }

    /**
     * Prepares this tree to have length nulls reinserted.  This method
     * recurses up the tree altering sizes so that the tree is in a
     * consistent state for addNulls() to be called on the host tree.
     */
    private void prepareForReinsert(boolean leftChild, int length) {
        // left subtree is smaller
        if(leftChild) totalLeftSize -= length;

        // right subtree is smaller
        else totalRightSize -= length;

        // recurse up the tree to the root
        if(parent != null) parent.prepareForReinsert(parent.left == this, length);

        // Notify the tree size has changed
        else host.treeSizeChanged();
    }

    /**
     * Clears this node and returns the value it had.
     */
    private Object clear() {
        // clear the children
        left = null;
        totalLeftSize = 0;
        right = null;
        totalRightSize = 0;

        // clear this node and return value
        host = null;
        parent = null;
        emptySpace = 0;
        height = -1;
        Object thisValue = value;
        value = null;
        return thisValue;
    }

    /**
     * Ensures that the tree satisfies the AVL property.  It is sufficient to
     * recurse up the tree only as long as height recalculations are needed.
     * As such, this method is intended to be called only on a node whose height
     * may be out of sync due to an insertion or deletion.  For example, calling
     * this method on a leaf node will not guarantee that this tree satisfies the
     * AVL property as it will not recurse.
     */
    private void ensureAVL() {
        int oldHeight = height;
        recalculateHeight();
        avlRotate();

        // If adjustments were made, recurse up the tree
        if(height != oldHeight && parent != null) parent.ensureAVL();
    }

    /**
     * Replaces a given child with the replacement node
     */
    private void replace(SparseListNode child, SparseListNode replacement) {
        // replacing the left child
        if(child == left) left = replacement;

        // Replacing the right child
        else right = replacement;
    }

    /**
     * Recalculates the cached height at this level.
     */
    private void recalculateHeight() {
        int leftHeight = left == null ? 0 : left.height;
        int rightHeight = right == null ? 0 : right.height;
        height = 1 + Math.max(leftHeight, rightHeight);
    }

    /**
     * Determines if AVL rotations are required and performs them if they are.
     */
    private void avlRotate() {
        // look up the left and right heights
        int leftHeight = (left != null ? left.height : 0);
        int rightHeight = (right != null ? right.height : 0);

        // rotations will be on the left
        if(leftHeight - rightHeight >= 2) {
            // determine if a double rotation is necessary
            int leftLeftHeight = (left.left != null ? left.left.height : 0);
            int leftRightHeight = (left.right != null ? left.right.height : 0);

            // Perform first half of double rotation if necessary
            if(leftRightHeight > leftLeftHeight) left.rotateRight();

            // Do the rotation for this node
            rotateLeft();

        // rotations will be on the right
        } else if(rightHeight - leftHeight >= 2) {
            // determine if a double rotation is necessary
            int rightLeftHeight = (right.left != null ? right.left.height : 0);
            int rightRightHeight = (right.right != null ? right.right.height : 0);

            // Perform first half of double rotation if necessary
            if(rightLeftHeight > rightRightHeight) right.rotateLeft();

            // Do the rotation for this node
            rotateRight();
        }
    }

    /**
     * AVL-Rotates this subtree with its left child.
     */
    private void rotateLeft() {
        // The replacement node is on the left
        SparseListNode replacement = left;

        // take the right child of the replacement as my left child
        left = replacement.right;
        totalLeftSize = replacement.totalRightSize;
        if(replacement.right != null) replacement.right.parent = this;

        // set the right child of the replacement to this
        replacement.right = this;
        replacement.totalRightSize = size();

        // set the replacement's parent to my parent and mine to the replacement
        if(parent != null) parent.replace(this, replacement);

        // set a new tree root
        else host.setRootNode(replacement);

        // fix parent links on this and the replacement
        replacement.parent = parent;
        parent = replacement;

        // recalculate height at this node
        recalculateHeight();

        // require height to be recalculated on the replacement node
        replacement.height = 0;
    }

    /**
     * AVL-Rotates this subtree with its right child.
     */
    private void rotateRight() {
        // The replacement node is on the right
        SparseListNode replacement = right;

        // take the left child of the replacement as my right child
        right = replacement.left;
        totalRightSize = replacement.totalLeftSize;
        if(replacement.left != null) replacement.left.parent = this;

        // set the left child of the replacement to this
        replacement.left = this;
        replacement.totalLeftSize = size();

        // set the replacement's parent to my parent and mine to the replacement
        if(parent != null) parent.replace(this, replacement);

        // set a new tree root
        else host.setRootNode(replacement);

        // fix parent links on this and the replacement
        replacement.parent = parent;
        parent = replacement;

        // recalculate height at this node
        recalculateHeight();

        // require height to be recalculated on the replacement node
        replacement.height = 0;
    }

    /**
     * For debugging purposes.
     */
    @Override
    public String toString() {
        return "[ " + left + " <"+emptySpace+"> " + value +" <"+height+"> "
            + right + " ]";
    }

    /**
     * Corrects all the cached sizes up the tree by the given offsets starting
     * from this so an Iterator can perform a fast remove.
     */
    private void correctSizes(int sizeChange) {
        if(parent != null)  {
            // left subtree has changed in size
            if(parent.left == this) totalLeftSize += sizeChange;

            // right subtree has changed in size
            else totalRightSize += sizeChange;

            // recurse up the tree to the root
            parent.correctSizes(sizeChange);

        // Notify the host tree that the size has changed
        } else host.treeSizeChanged();
    }

    /**
     * A specialized Iterator that will significantly outperform the default
     * one provided by AbstractList when acting on this ADT.
     */
    final static class SparseListIterator implements Iterator {

        /** the current SparseListNode being inspected */
        private SparseListNode currentNode = null;

        /** the number of times the current node has been requested */
        private int timesRequested = -1;

        /** a reference to the SparseList for removal of trailing nulls */
        private SparseList sparseList = null;

        /** the size of the actual tree within the SparseList*/
        private int treeSize = 0;

        /** the size of the list */
        private int size = 0;

        /** the current index being inspected */
        private int index = -1;

        /**
         * Creates a new Iterator that is optimized for SparseLists.
         */
        SparseListIterator(SparseList sparseList, SparseListNode root) {
            // move the Iterator to the start position.
            if(root != null) {
                this.treeSize = root.size();
                currentNode = root;
                while(currentNode.left != null) {
                    currentNode = currentNode.left;
                }
            }
            this.sparseList = sparseList;
            this.size = sparseList.size();
        }

        /**
         * Returns whether or not there are more values in the SparseList to
         * iterate over.
         */
        public boolean hasNext() {
            if(index >= treeSize - 1 && index == size - 1) {
                return false;
            }
            return true;
        }

        /**
         * Gets the next value in this SparseList.
         */
        public Object next() {
            // iterate on this node
            timesRequested++;
            index++;

            // handle the empty tree case
            if(currentNode == null) {
                // beyond the tree in the trailing nulls
                if(index < size) {
                    return null;

                // at the end of the list
                } else {
                    throw new NoSuchElementException();
                }

            // at the edge of the current node
            } else if(timesRequested > currentNode.emptySpace) {
                // move to the next node
                if(index < treeSize) {
                    findNextNode();
                    timesRequested = 0;

                // act on the trailing nulls
                } else {
                    // beyond the tree in the trailing nulls
                    if(index < size) {
                        return null;

                    // at the end of the list
                    } else {
                        throw new NoSuchElementException();
                    }
                }
            }

            // next() was a null value
            if(timesRequested < currentNode.emptySpace) {
                return null;

            // next() was the value of this node
            } else if(timesRequested == currentNode.emptySpace) {
                return currentNode.value;

            // the iterator is out of state
            } else {
                throw new IllegalStateException();
            }
        }

        /**
         * Removes the current value at the Iterator from the SparseList.
         *
         * @throws UnsupportedOperationException This feature is not yet implemented.
         *
         */
        public void remove() {
            // handle the uninitialized iterator case
            if(timesRequested == -1) {
                throw new IllegalStateException("Cannot remove() without a prior call to next()");

            // remove from the trailing nulls
            } else if(currentNode == null || index >= treeSize) {
                sparseList.remove(index);

            // remove a null
            } else if(timesRequested < currentNode.emptySpace) {
                currentNode.correctSizes(-1);
                currentNode.emptySpace--;

            // remove a value
            } else if(timesRequested == currentNode.emptySpace) {
                currentNode.correctSizes(-1);
                SparseListNode nodeToRemove = currentNode;
                findNextNode();
                timesRequested = -1;
                nodeToRemove.unlink();

            // the iterator is out of state
            } else {
                throw new IllegalStateException();
            }
        }

        /**
         * Finds the next node in the tree.
         */
        private void findNextNode() {
            //  go into the right subtree for the next node
            if(currentNode.right != null) {
                currentNode = currentNode.right;
                while(currentNode.left != null) {
                    currentNode = currentNode.left;
                }

            // go to the parent for the next node
            } else if(currentNode.parent.left == currentNode) {
                currentNode = currentNode.parent;

            // get out of the right subtree
            } else if(currentNode.parent.right == currentNode) {
                // move to the top of the current subtree
                while(currentNode.parent.right == currentNode) {
                    currentNode = currentNode.parent;
                }
                // Move up one more node to leave the subtree
                currentNode = currentNode.parent;

            // the iterator is out of state
            } else {
                throw new IllegalStateException();
            }
        }

        /**
         * Finds the previous node in the tree.
         */
        private void findPreviousNode() {
            throw new UnsupportedOperationException("Not implemented yet.");
        }

        @Override
        public String toString() {
            return "Accessing " + currentNode + " for the " + timesRequested + " time.";
        }
    }
}