Revering tree update (#6561)
* Add files via upload * Add files via upload * Add files via upload * Add files via upload * Add files via upload
This commit is contained in:
parent
871ee3ccce
commit
b7fbf839f9
|
@ -1,42 +1,42 @@
|
||||||
package com.baeldung.algorithms.reversingtree;
|
package com.baeldung.algorithms.reversingtree;
|
||||||
|
|
||||||
public class TreeNode {
|
public class TreeNode {
|
||||||
|
|
||||||
private int value;
|
private int value;
|
||||||
private TreeNode rightChild;
|
private TreeNode rightChild;
|
||||||
private TreeNode leftChild;
|
private TreeNode leftChild;
|
||||||
|
|
||||||
public int getValue() {
|
public int getValue() {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setValue(int value) {
|
public void setValue(int value) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TreeNode getRightChild() {
|
public TreeNode getRightChild() {
|
||||||
return rightChild;
|
return rightChild;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRightChild(TreeNode rightChild) {
|
public void setRightChild(TreeNode rightChild) {
|
||||||
this.rightChild = rightChild;
|
this.rightChild = rightChild;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TreeNode getLeftChild() {
|
public TreeNode getLeftChild() {
|
||||||
return leftChild;
|
return leftChild;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLeftChild(TreeNode leftChild) {
|
public void setLeftChild(TreeNode leftChild) {
|
||||||
this.leftChild = leftChild;
|
this.leftChild = leftChild;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TreeNode(int value, TreeNode rightChild, TreeNode leftChild) {
|
public TreeNode(int value, TreeNode leftChild, TreeNode rightChild) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
this.rightChild = rightChild;
|
this.rightChild = rightChild;
|
||||||
this.leftChild = leftChild;
|
this.leftChild = leftChild;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TreeNode(int value) {
|
public TreeNode(int value) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,68 +1,53 @@
|
||||||
package com.baeldung.algorithms.reversingtree;
|
package com.baeldung.algorithms.reversingtree;
|
||||||
|
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
|
||||||
public class TreeReverser {
|
public class TreeReverser {
|
||||||
|
|
||||||
public TreeNode createBinaryTree() {
|
public void reverseRecursive(TreeNode treeNode) {
|
||||||
|
if (treeNode == null) {
|
||||||
TreeNode leaf1 = new TreeNode(3);
|
return;
|
||||||
TreeNode leaf2 = new TreeNode(1);
|
}
|
||||||
TreeNode leaf3 = new TreeNode(9);
|
|
||||||
TreeNode leaf4 = new TreeNode(6);
|
TreeNode temp = treeNode.getLeftChild();
|
||||||
|
treeNode.setLeftChild(treeNode.getRightChild());
|
||||||
TreeNode nodeLeft = new TreeNode(2, leaf1, leaf2);
|
treeNode.setRightChild(temp);
|
||||||
TreeNode nodeRight = new TreeNode(7, leaf3, leaf4);
|
|
||||||
|
reverseRecursive(treeNode.getLeftChild());
|
||||||
TreeNode root = new TreeNode(4, nodeRight, nodeLeft);
|
reverseRecursive(treeNode.getRightChild());
|
||||||
|
}
|
||||||
return root;
|
|
||||||
}
|
public void reverseIterative(TreeNode treeNode) {
|
||||||
|
LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
|
||||||
public void reverseRecursive(TreeNode treeNode) {
|
|
||||||
if (treeNode == null) {
|
if (treeNode != null) {
|
||||||
return;
|
queue.add(treeNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
TreeNode temp = treeNode.getLeftChild();
|
while (!queue.isEmpty()) {
|
||||||
treeNode.setLeftChild(treeNode.getRightChild());
|
|
||||||
treeNode.setRightChild(temp);
|
TreeNode node = queue.poll();
|
||||||
|
if (node.getLeftChild() != null)
|
||||||
reverseRecursive(treeNode.getLeftChild());
|
queue.add(node.getLeftChild());
|
||||||
reverseRecursive(treeNode.getRightChild());
|
if (node.getRightChild() != null)
|
||||||
}
|
queue.add(node.getRightChild());
|
||||||
|
|
||||||
public void reverseIterative(TreeNode treeNode) {
|
TreeNode temp = node.getLeftChild();
|
||||||
LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
|
node.setLeftChild(node.getRightChild());
|
||||||
|
node.setRightChild(temp);
|
||||||
if (treeNode != null) {
|
}
|
||||||
queue.add(treeNode);
|
}
|
||||||
}
|
|
||||||
|
public String toString(TreeNode root) {
|
||||||
while (!queue.isEmpty()) {
|
if (root == null) {
|
||||||
|
return "";
|
||||||
TreeNode node = queue.poll();
|
}
|
||||||
if (node.getLeftChild() != null)
|
|
||||||
queue.add(node.getLeftChild());
|
StringBuffer buffer = new StringBuffer(String.valueOf(root.getValue())).append(" ");
|
||||||
if (node.getRightChild() != null)
|
|
||||||
queue.add(node.getRightChild());
|
buffer.append(toString(root.getLeftChild()));
|
||||||
|
buffer.append(toString(root.getRightChild()));
|
||||||
TreeNode temp = node.getLeftChild();
|
|
||||||
node.setLeftChild(node.getRightChild());
|
return buffer.toString();
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,33 +1,47 @@
|
||||||
package com.baeldung.algorithms.reversingtree;
|
package com.baeldung.algorithms.reversingtree;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
public class TreeReverserUnitTest {
|
public class TreeReverserUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenTreeWhenReversingRecursivelyThenReversed() {
|
public void givenTreeWhenReversingRecursivelyThenReversed() {
|
||||||
TreeReverser reverser = new TreeReverser();
|
TreeReverser reverser = new TreeReverser();
|
||||||
|
|
||||||
TreeNode treeNode = reverser.createBinaryTree();
|
TreeNode treeNode = createBinaryTree();
|
||||||
|
|
||||||
reverser.reverseRecursive(treeNode);
|
reverser.reverseRecursive(treeNode);
|
||||||
|
|
||||||
assertEquals("4 7 9 6 2 3 1", reverser.toString(treeNode)
|
assertEquals("4 7 9 6 2 3 1", reverser.toString(treeNode)
|
||||||
.trim());
|
.trim());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenTreeWhenReversingIterativelyThenReversed() {
|
public void givenTreeWhenReversingIterativelyThenReversed() {
|
||||||
TreeReverser reverser = new TreeReverser();
|
TreeReverser reverser = new TreeReverser();
|
||||||
|
|
||||||
TreeNode treeNode = reverser.createBinaryTree();
|
TreeNode treeNode = createBinaryTree();
|
||||||
|
|
||||||
reverser.reverseIterative(treeNode);
|
reverser.reverseIterative(treeNode);
|
||||||
|
|
||||||
assertEquals("4 7 9 6 2 3 1", reverser.toString(treeNode)
|
assertEquals("4 7 9 6 2 3 1", reverser.toString(treeNode)
|
||||||
.trim());
|
.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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue