diff options
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/TreeTraversal.java | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/src/main/TreeTraversal.java b/src/main/TreeTraversal.java index fee494c..d69164e 100644 --- a/src/main/TreeTraversal.java +++ b/src/main/TreeTraversal.java @@ -125,8 +125,15 @@ public class TreeTraversal { currentIndex = left[currentIndex]; keyIndexStack.push(currentIndex); } else { - result.add(key[keyIndexStack.pop()]); - walkLeft = false; + if (right[currentIndex] == -1) { + result.add(key[currentIndex]); + walkLeft = false; + keyIndexStack.pop(); + currentIndex = keyIndexStack.peek(); + } else { + currentIndex = right[currentIndex]; + keyIndexStack.push(currentIndex); + } } } else { if (right[currentIndex] != -1) { @@ -134,10 +141,9 @@ public class TreeTraversal { keyIndexStack.push(currentIndex); walkLeft = true; } else { - if (!keyIndexStack.empty()) { - currentIndex = keyIndexStack.pop(); - result.add(key[currentIndex]); - } + result.add(key[currentIndex]); + keyIndexStack.pop(); + currentIndex = keyIndexStack.peek(); } } } |