BAEL-4864: Fix traverseInOrderWithoutRecursion method (#10588)
Co-authored-by: Krzysztof Woyke <krzysztof.woyke.sp@lhsystems.com>
This commit is contained in:
parent
9a580ea9fc
commit
23503a0673
|
@ -148,48 +148,46 @@ public class BinaryTree {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void traverseInOrderWithoutRecursion() {
|
public void traverseInOrderWithoutRecursion() {
|
||||||
Stack<Node> stack = new Stack<Node>();
|
Stack<Node> stack = new Stack<>();
|
||||||
Node current = root;
|
Node current = root;
|
||||||
stack.push(root);
|
|
||||||
while(! stack.isEmpty()) {
|
while (current != null || !stack.isEmpty()) {
|
||||||
while(current.left != null) {
|
while (current != null) {
|
||||||
|
stack.push(current);
|
||||||
current = current.left;
|
current = current.left;
|
||||||
stack.push(current);
|
|
||||||
}
|
|
||||||
current = stack.pop();
|
|
||||||
visit(current.value);
|
|
||||||
if(current.right != null) {
|
|
||||||
current = current.right;
|
|
||||||
stack.push(current);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Node top = stack.pop();
|
||||||
|
visit(top.value);
|
||||||
|
current = top.right;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void traversePreOrderWithoutRecursion() {
|
public void traversePreOrderWithoutRecursion() {
|
||||||
Stack<Node> stack = new Stack<Node>();
|
Stack<Node> stack = new Stack<>();
|
||||||
Node current = root;
|
Node current = root;
|
||||||
stack.push(root);
|
stack.push(root);
|
||||||
while(! stack.isEmpty()) {
|
|
||||||
|
while (current != null && !stack.isEmpty()) {
|
||||||
current = stack.pop();
|
current = stack.pop();
|
||||||
visit(current.value);
|
visit(current.value);
|
||||||
|
|
||||||
if(current.right != null)
|
if (current.right != null)
|
||||||
stack.push(current.right);
|
stack.push(current.right);
|
||||||
|
|
||||||
if(current.left != null)
|
if (current.left != null)
|
||||||
stack.push(current.left);
|
stack.push(current.left);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void traversePostOrderWithoutRecursion() {
|
public void traversePostOrderWithoutRecursion() {
|
||||||
Stack<Node> stack = new Stack<Node>();
|
Stack<Node> stack = new Stack<>();
|
||||||
Node prev = root;
|
Node prev = root;
|
||||||
Node current = root;
|
Node current = root;
|
||||||
stack.push(root);
|
stack.push(root);
|
||||||
|
|
||||||
while (!stack.isEmpty()) {
|
while (current != null && !stack.isEmpty()) {
|
||||||
current = stack.peek();
|
current = stack.peek();
|
||||||
boolean hasChild = (current.left != null || current.right != null);
|
boolean hasChild = (current.left != null || current.right != null);
|
||||||
boolean isPrevLastChild = (prev == current.right || (prev == current.left && current.right == null));
|
boolean isPrevLastChild = (prev == current.right || (prev == current.left && current.right == null));
|
||||||
|
|
|
@ -13,7 +13,7 @@ public class BinaryTreeUnitTest {
|
||||||
|
|
||||||
BinaryTree bt = createBinaryTree();
|
BinaryTree bt = createBinaryTree();
|
||||||
|
|
||||||
assertTrue(!bt.isEmpty());
|
assertFalse(bt.isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -72,6 +72,7 @@ public class BinaryTreeUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void it_deletes_the_root() {
|
public void it_deletes_the_root() {
|
||||||
|
|
||||||
int value = 12;
|
int value = 12;
|
||||||
BinaryTree bt = new BinaryTree();
|
BinaryTree bt = new BinaryTree();
|
||||||
bt.add(value);
|
bt.add(value);
|
||||||
|
@ -91,6 +92,14 @@ public class BinaryTreeUnitTest {
|
||||||
bt.traverseInOrderWithoutRecursion();
|
bt.traverseInOrderWithoutRecursion();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAnEmptyBinaryTree_WhenTraversingInOrderWithoutRecursion_ThenNoException() {
|
||||||
|
|
||||||
|
BinaryTree empty = new BinaryTree();
|
||||||
|
|
||||||
|
empty.traverseInOrderWithoutRecursion();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenABinaryTree_WhenTraversingPreOrder_ThenPrintValues() {
|
public void givenABinaryTree_WhenTraversingPreOrder_ThenPrintValues() {
|
||||||
|
|
||||||
|
@ -101,6 +110,14 @@ public class BinaryTreeUnitTest {
|
||||||
bt.traversePreOrderWithoutRecursion();
|
bt.traversePreOrderWithoutRecursion();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAnEmptyBinaryTree_WhenTraversingPreOrderWithoutRecursion_ThenNoException() {
|
||||||
|
|
||||||
|
BinaryTree empty = new BinaryTree();
|
||||||
|
|
||||||
|
empty.traversePreOrderWithoutRecursion();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenABinaryTree_WhenTraversingPostOrder_ThenPrintValues() {
|
public void givenABinaryTree_WhenTraversingPostOrder_ThenPrintValues() {
|
||||||
|
|
||||||
|
@ -111,6 +128,14 @@ public class BinaryTreeUnitTest {
|
||||||
bt.traversePostOrderWithoutRecursion();
|
bt.traversePostOrderWithoutRecursion();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAnEmptyBinaryTree_WhenTraversingPostOrderWithoutRecursion_ThenNoException() {
|
||||||
|
|
||||||
|
BinaryTree empty = new BinaryTree();
|
||||||
|
|
||||||
|
empty.traversePostOrderWithoutRecursion();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenABinaryTree_WhenTraversingLevelOrder_ThenPrintValues() {
|
public void givenABinaryTree_WhenTraversingLevelOrder_ThenPrintValues() {
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue