diff --git a/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/reversingtree/TreeNode.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/reversingtree/TreeNode.java index 7905b752a9..a6ec2277e5 100644 --- a/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/reversingtree/TreeNode.java +++ b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/reversingtree/TreeNode.java @@ -1,42 +1,42 @@ -package com.baeldung.algorithms.reversingtree; - -public class TreeNode { - - private int value; - private TreeNode rightChild; - private TreeNode leftChild; - - public int getValue() { - return value; - } - - public void setValue(int value) { - this.value = value; - } - - public TreeNode getRightChild() { - return rightChild; - } - - public void setRightChild(TreeNode rightChild) { - this.rightChild = rightChild; - } - - public TreeNode getLeftChild() { - return leftChild; - } - - public void setLeftChild(TreeNode leftChild) { - this.leftChild = leftChild; - } - - public TreeNode(int value, TreeNode rightChild, TreeNode leftChild) { - this.value = value; - this.rightChild = rightChild; - this.leftChild = leftChild; - } - - public TreeNode(int value) { - this.value = value; - } -} +package com.baeldung.algorithms.reversingtree; + +public class TreeNode { + + private int value; + private TreeNode rightChild; + private TreeNode leftChild; + + public int getValue() { + return value; + } + + public void setValue(int value) { + this.value = value; + } + + public TreeNode getRightChild() { + return rightChild; + } + + public void setRightChild(TreeNode rightChild) { + this.rightChild = rightChild; + } + + public TreeNode getLeftChild() { + return leftChild; + } + + public void setLeftChild(TreeNode leftChild) { + this.leftChild = leftChild; + } + + public TreeNode(int value, TreeNode leftChild, TreeNode rightChild) { + this.value = value; + this.rightChild = rightChild; + this.leftChild = leftChild; + } + + public TreeNode(int value) { + this.value = value; + } +} diff --git a/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/reversingtree/TreeReverser.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/reversingtree/TreeReverser.java index 6d3a9ddd31..162119d390 100644 --- a/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/reversingtree/TreeReverser.java +++ b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/reversingtree/TreeReverser.java @@ -1,68 +1,53 @@ -package com.baeldung.algorithms.reversingtree; - -import java.util.LinkedList; - -public class TreeReverser { - - public TreeNode createBinaryTree() { - - TreeNode leaf1 = new TreeNode(3); - TreeNode leaf2 = new TreeNode(1); - TreeNode leaf3 = new TreeNode(9); - TreeNode leaf4 = new TreeNode(6); - - TreeNode nodeLeft = new TreeNode(2, leaf1, leaf2); - TreeNode nodeRight = new TreeNode(7, leaf3, leaf4); - - TreeNode root = new TreeNode(4, nodeRight, nodeLeft); - - return root; - } - - public void reverseRecursive(TreeNode treeNode) { - if (treeNode == null) { - return; - } - - TreeNode temp = treeNode.getLeftChild(); - treeNode.setLeftChild(treeNode.getRightChild()); - treeNode.setRightChild(temp); - - reverseRecursive(treeNode.getLeftChild()); - reverseRecursive(treeNode.getRightChild()); - } - - public void reverseIterative(TreeNode treeNode) { - LinkedList queue = new LinkedList(); - - if (treeNode != null) { - queue.add(treeNode); - } - - while (!queue.isEmpty()) { - - TreeNode node = queue.poll(); - if (node.getLeftChild() != null) - queue.add(node.getLeftChild()); - if (node.getRightChild() != null) - queue.add(node.getRightChild()); - - TreeNode temp = node.getLeftChild(); - node.setLeftChild(node.getRightChild()); - node.setRightChild(temp); - } - } - - public String toString(TreeNode root) { - if (root == null) { - return ""; - } - - StringBuffer buffer = new StringBuffer(String.valueOf(root.getValue())).append(" "); - - buffer.append(toString(root.getLeftChild())); - buffer.append(toString(root.getRightChild())); - - return buffer.toString(); - } -} +package com.baeldung.algorithms.reversingtree; + +import java.util.LinkedList; + +public class TreeReverser { + + public void reverseRecursive(TreeNode treeNode) { + if (treeNode == null) { + return; + } + + TreeNode temp = treeNode.getLeftChild(); + treeNode.setLeftChild(treeNode.getRightChild()); + treeNode.setRightChild(temp); + + reverseRecursive(treeNode.getLeftChild()); + reverseRecursive(treeNode.getRightChild()); + } + + public void reverseIterative(TreeNode treeNode) { + LinkedList queue = new LinkedList(); + + if (treeNode != null) { + queue.add(treeNode); + } + + while (!queue.isEmpty()) { + + TreeNode node = queue.poll(); + if (node.getLeftChild() != null) + queue.add(node.getLeftChild()); + if (node.getRightChild() != null) + queue.add(node.getRightChild()); + + TreeNode temp = node.getLeftChild(); + node.setLeftChild(node.getRightChild()); + node.setRightChild(temp); + } + } + + public String toString(TreeNode root) { + if (root == null) { + return ""; + } + + StringBuffer buffer = new StringBuffer(String.valueOf(root.getValue())).append(" "); + + buffer.append(toString(root.getLeftChild())); + buffer.append(toString(root.getRightChild())); + + return buffer.toString(); + } +} diff --git a/algorithms-miscellaneous-2/src/test/java/com/baeldung/algorithms/reversingtree/TreeReverserUnitTest.java b/algorithms-miscellaneous-2/src/test/java/com/baeldung/algorithms/reversingtree/TreeReverserUnitTest.java index cbc265fae1..44fac57361 100644 --- a/algorithms-miscellaneous-2/src/test/java/com/baeldung/algorithms/reversingtree/TreeReverserUnitTest.java +++ b/algorithms-miscellaneous-2/src/test/java/com/baeldung/algorithms/reversingtree/TreeReverserUnitTest.java @@ -1,33 +1,47 @@ -package com.baeldung.algorithms.reversingtree; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.jupiter.api.Test; - -public class TreeReverserUnitTest { - - @Test - public void givenTreeWhenReversingRecursivelyThenReversed() { - TreeReverser reverser = new TreeReverser(); - - TreeNode treeNode = reverser.createBinaryTree(); - - reverser.reverseRecursive(treeNode); - - assertEquals("4 7 9 6 2 3 1", reverser.toString(treeNode) - .trim()); - } - - @Test - public void givenTreeWhenReversingIterativelyThenReversed() { - TreeReverser reverser = new TreeReverser(); - - TreeNode treeNode = reverser.createBinaryTree(); - - reverser.reverseIterative(treeNode); - - assertEquals("4 7 9 6 2 3 1", reverser.toString(treeNode) - .trim()); - } - -} +package com.baeldung.algorithms.reversingtree; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +public class TreeReverserUnitTest { + + @Test + public void givenTreeWhenReversingRecursivelyThenReversed() { + TreeReverser reverser = new TreeReverser(); + + TreeNode treeNode = createBinaryTree(); + + reverser.reverseRecursive(treeNode); + + assertEquals("4 7 9 6 2 3 1", reverser.toString(treeNode) + .trim()); + } + + @Test + public void givenTreeWhenReversingIterativelyThenReversed() { + TreeReverser reverser = new TreeReverser(); + + TreeNode treeNode = createBinaryTree(); + + reverser.reverseIterative(treeNode); + + assertEquals("4 7 9 6 2 3 1", reverser.toString(treeNode) + .trim()); + } + + private TreeNode createBinaryTree() { + + TreeNode leaf1 = new TreeNode(1); + TreeNode leaf2 = new TreeNode(3); + TreeNode leaf3 = new TreeNode(6); + TreeNode leaf4 = new TreeNode(9); + + TreeNode nodeRight = new TreeNode(7, leaf3, leaf4); + TreeNode nodeLeft = new TreeNode(2, leaf1, leaf2); + + TreeNode root = new TreeNode(4, nodeLeft, nodeRight); + + return root; + } +}