diff --git a/src/main/java/com/ossez/lang/tutorial/utils/TreeUtils.java b/src/main/java/com/ossez/lang/tutorial/utils/TreeUtils.java new file mode 100644 index 0000000000..ead47224c0 --- /dev/null +++ b/src/main/java/com/ossez/lang/tutorial/utils/TreeUtils.java @@ -0,0 +1,55 @@ +package com.ossez.lang.tutorial.utils; + +import java.util.ArrayList; + +import com.ossez.lang.tutorial.models.TreeNode; + +/** + * + * @author YuCheng + * + */ +public class TreeUtils { + + public static TreeNode initTree(String data) { + // NULL CHECK + if (data.equals("{}")) { + return null; + } + + ArrayList treeList = new ArrayList(); + + data = data.replace("{", ""); + data = data.replace("}", ""); + String[] vals = data.split(","); + + // INSERT ROOT + TreeNode root = new TreeNode(Integer.parseInt(vals[0])); + treeList.add(root); + + int index = 0; + boolean isLeftChild = true; + for (int i = 1; i < vals.length; i++) { + if (!vals[i].equals("#")) { + TreeNode node = new TreeNode(Integer.parseInt(vals[i])); + if (isLeftChild) { + treeList.get(index).left = node; + } else { + treeList.get(index).right = node; + } + treeList.add(node); + } + + // LEVEL + if (!isLeftChild) { + index++; + } + + // MOVE TO RIGHT OR NEXT LEVEL + isLeftChild = !isLeftChild; + } + + return root; + + } +} diff --git a/src/test/java/com/ossez/lang/tutorial/tests/TreeTest.java b/src/test/java/com/ossez/lang/tutorial/tests/TreeTest.java new file mode 100644 index 0000000000..ee67e07c05 --- /dev/null +++ b/src/test/java/com/ossez/lang/tutorial/tests/TreeTest.java @@ -0,0 +1,85 @@ +package com.ossez.lang.tutorial.tests; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.ossez.lang.tutorial.models.TreeNode; +import com.ossez.lang.tutorial.utils.TreeUtils; + +/** + * + * @author YuCheng + * + */ +public class TreeTest { + private final static Logger logger = LoggerFactory.getLogger(TreeTest.class); + private static List loopList = new ArrayList(); + + @Test + public void testMain() { + logger.debug("TREE TEST"); + String data = "{1,2,3,4,5,#,6,#,#,7,8,#,#}"; + TreeNode treeNode = TreeUtils.initTree(data); + + // PRE + loopList = new ArrayList(); + preOrderTraverselRecursion(treeNode); + System.out.println(loopList); + + // IN + loopList = new ArrayList(); + inOrderTraverselRecursion(treeNode); + System.out.println(loopList); + + // POST + loopList = new ArrayList(); + postOrderTraversalRecursion(treeNode); + System.out.println(loopList); + } + + + + + /** + * + * @param root + */ + public void preOrderTraverselRecursion(TreeNode root) { + if (root != null) { + loopList.add(root.val); + preOrderTraverselRecursion(root.left); + preOrderTraverselRecursion(root.right); + } + } + + /** + * + * @param root + */ + public void inOrderTraverselRecursion(TreeNode root) { + if (root != null) { + inOrderTraverselRecursion(root.left); + loopList.add(root.val); + inOrderTraverselRecursion(root.right); + } + + } + + /** + * + * @param root + */ + public void postOrderTraversalRecursion(TreeNode root) { + if (root != null) { + postOrderTraversalRecursion(root.left); + postOrderTraversalRecursion(root.right); + loopList.add(root.val); + } + + } + +}