From 2fda9e7e23a3b86ac6062fa60eefcd2c17ada7af Mon Sep 17 00:00:00 2001 From: gardncl Date: Wed, 26 Apr 2017 20:17:08 -0400 Subject: [PATCH 01/15] Module for Chapter 10: Binary Trees created as well as all method stubs --- binarytrees/README.md | 19 ++++++++++ binarytrees/pom.xml | 23 ++++++++++++ .../src/main/java/ComputeExterior.java | 24 +++++++++++++ .../src/main/java/ComputeKthNodeInorder.java | 16 +++++++++ .../src/main/java/ComputeLCAWithParent.java | 15 ++++++++ .../java/ComputeLowestCommonAncestor.java | 15 ++++++++ .../main/java/ComputeRightSiblingTree.java | 14 ++++++++ .../src/main/java/ComputeSuccessor.java | 15 ++++++++ .../src/main/java/FindRootToLeafSum.java | 17 +++++++++ .../java/ImplementInorderSpaceEfficient.java | 18 ++++++++++ .../src/main/java/InorderIterative.java | 18 ++++++++++ .../src/main/java/IsHeightBalanced.java | 15 ++++++++ binarytrees/src/main/java/IsSymmetric.java | 14 ++++++++ .../src/main/java/LockingBinaryTree.java | 20 +++++++++++ .../src/main/java/PreorderIterative.java | 18 ++++++++++ .../src/main/java/ReconstructBinaryTree.java | 17 +++++++++ .../ReconstructBinaryTreeWithMarkers.java | 17 +++++++++ binarytrees/src/main/java/SumRootToLeaf.java | 15 ++++++++ .../src/main/java/TreeToLinkedList.java | 20 +++++++++++ datastructures/src/main/java/BinaryTree.java | 16 +++++++++ .../src/main/java/BinaryTreeNode.java | 14 -------- pom.xml | 3 +- .../src/main/java/ComputeBinaryTreeNodes.java | 2 +- .../test/java/ComputeBinaryTreeNodesTest.java | 36 +++++++++---------- 24 files changed, 367 insertions(+), 34 deletions(-) create mode 100644 binarytrees/README.md create mode 100644 binarytrees/pom.xml create mode 100644 binarytrees/src/main/java/ComputeExterior.java create mode 100644 binarytrees/src/main/java/ComputeKthNodeInorder.java create mode 100644 binarytrees/src/main/java/ComputeLCAWithParent.java create mode 100644 binarytrees/src/main/java/ComputeLowestCommonAncestor.java create mode 100644 binarytrees/src/main/java/ComputeRightSiblingTree.java create mode 100644 binarytrees/src/main/java/ComputeSuccessor.java create mode 100644 binarytrees/src/main/java/FindRootToLeafSum.java create mode 100644 binarytrees/src/main/java/ImplementInorderSpaceEfficient.java create mode 100644 binarytrees/src/main/java/InorderIterative.java create mode 100644 binarytrees/src/main/java/IsHeightBalanced.java create mode 100644 binarytrees/src/main/java/IsSymmetric.java create mode 100644 binarytrees/src/main/java/LockingBinaryTree.java create mode 100644 binarytrees/src/main/java/PreorderIterative.java create mode 100644 binarytrees/src/main/java/ReconstructBinaryTree.java create mode 100644 binarytrees/src/main/java/ReconstructBinaryTreeWithMarkers.java create mode 100644 binarytrees/src/main/java/SumRootToLeaf.java create mode 100644 binarytrees/src/main/java/TreeToLinkedList.java create mode 100644 datastructures/src/main/java/BinaryTree.java delete mode 100644 datastructures/src/main/java/BinaryTreeNode.java diff --git a/binarytrees/README.md b/binarytrees/README.md new file mode 100644 index 0000000..00d0c39 --- /dev/null +++ b/binarytrees/README.md @@ -0,0 +1,19 @@ +# Chapter 10: Binary Trees + +* 10.1 IsHeightBalanced +* 10.2 IsSymmetric +* 10.3 ComputeLowestCommonAncestor +* 10.4 ComputeLCAWithParent +* 10.5 SumRootToLeaf +* 10.6 FindRootToLeafSum +* 10.7 InorderIterative +* 10.8 PreorderIterative +* 10.9 ComputeKthNodeInorder +* 10.10 ComputeSuccessor +* 10.11 ImplementInorderSpaceEfficient +* 10.12 ReconstructBinaryTree +* 10.13 ReconstructBinaryTreeWithMarkers +* 10.14 TreeToLinkedList +* 10.15 ComputeExterior +* 10.16 ComputeRightSiblingTree +* 10.17 LockingBinaryTree \ No newline at end of file diff --git a/binarytrees/pom.xml b/binarytrees/pom.xml new file mode 100644 index 0000000..a832edb --- /dev/null +++ b/binarytrees/pom.xml @@ -0,0 +1,23 @@ + + + + elements-of-programming-interviews + gardncl + 1.4 + + 4.0.0 + + binarytrees + Chapter 10: Binary Trees + + + gardncl + datastructures + 1.4 + + + + + \ No newline at end of file diff --git a/binarytrees/src/main/java/ComputeExterior.java b/binarytrees/src/main/java/ComputeExterior.java new file mode 100644 index 0000000..73cea54 --- /dev/null +++ b/binarytrees/src/main/java/ComputeExterior.java @@ -0,0 +1,24 @@ +import java.util.Collections; +import java.util.List; + +public class ComputeExterior { + + /* + 10.15 + + The exterior of a binary tree is the following sequence + of nodes: the nodes from the root to the leftmost leaf, + followed by teh leaves in the left-to-right order, + followed by the nodes from the rightmost leaf to the root. + (By leftmost (rightmost) left, we mean the leaf that appears + first (last) in an in-order traversal.) For example, + the exterior of the binary tree in Figure 10.1 on Page 151 + is [A,B,C,D,E,H,M,N,P,O,I]. + */ + + public static List> exteriorBinaryTree(BinaryTree tree) { + + return Collections.emptyList(); + } + +} diff --git a/binarytrees/src/main/java/ComputeKthNodeInorder.java b/binarytrees/src/main/java/ComputeKthNodeInorder.java new file mode 100644 index 0000000..5f3df88 --- /dev/null +++ b/binarytrees/src/main/java/ComputeKthNodeInorder.java @@ -0,0 +1,16 @@ +public class ComputeKthNodeInorder { + + /* + 10.9 + + Write a program that efficiently computes the + kth node appearing in an in-order traversal. + Assume that each node stores the number of + nodes in the subtree rooted at that node. + */ + + public static BinaryTree findKthNodeBinaryTree(BinaryTree tree, int k) { + + return new BinaryTree(0); + } +} diff --git a/binarytrees/src/main/java/ComputeLCAWithParent.java b/binarytrees/src/main/java/ComputeLCAWithParent.java new file mode 100644 index 0000000..57dac47 --- /dev/null +++ b/binarytrees/src/main/java/ComputeLCAWithParent.java @@ -0,0 +1,15 @@ +public class ComputeLCAWithParent { + + /* + 10.4 + + Given two nodes in a binary tree, design + an algorithm that computes their LCA. + Assume that each node has a parent pointer. + */ + + public static BinaryTree LCA(BinaryTree node0, BinaryTree node1) { + + return new BinaryTree<>(0); + } +} diff --git a/binarytrees/src/main/java/ComputeLowestCommonAncestor.java b/binarytrees/src/main/java/ComputeLowestCommonAncestor.java new file mode 100644 index 0000000..d5ce741 --- /dev/null +++ b/binarytrees/src/main/java/ComputeLowestCommonAncestor.java @@ -0,0 +1,15 @@ +public class ComputeLowestCommonAncestor { + + /* + 10.3 + + Design an algorithm for computing the LCA + of two nodes in a binary tree in which nodes + do not have a parent field. + */ + + public static BinaryTree LCA(BinaryTree tree, BinaryTree node0, BinaryTree node1) { + + return new BinaryTree<>(0); + } +} diff --git a/binarytrees/src/main/java/ComputeRightSiblingTree.java b/binarytrees/src/main/java/ComputeRightSiblingTree.java new file mode 100644 index 0000000..e25e810 --- /dev/null +++ b/binarytrees/src/main/java/ComputeRightSiblingTree.java @@ -0,0 +1,14 @@ +public class ComputeRightSiblingTree { + + /* + 10.16 + + Write a program that takes a perfect binary tree, + and sets each node's level-next field to the + node on its right, if one exists. + */ + + public static void constructRightSibling(BinaryTree tree) { + + } +} diff --git a/binarytrees/src/main/java/ComputeSuccessor.java b/binarytrees/src/main/java/ComputeSuccessor.java new file mode 100644 index 0000000..e1e735f --- /dev/null +++ b/binarytrees/src/main/java/ComputeSuccessor.java @@ -0,0 +1,15 @@ +public class ComputeSuccessor { + + /* + 10.10 + + Design an algorithm that computes the successor + of a node in a binary tree. Assume the each + node stores its parent. + */ + + public static BinaryTree findSuccessor(BinaryTree node) { + + return new BinaryTree(0); + } +} diff --git a/binarytrees/src/main/java/FindRootToLeafSum.java b/binarytrees/src/main/java/FindRootToLeafSum.java new file mode 100644 index 0000000..0a14dbc --- /dev/null +++ b/binarytrees/src/main/java/FindRootToLeafSum.java @@ -0,0 +1,17 @@ +public class FindRootToLeafSum { + + /* + 10.6 + + Write a program which takes as input an integer + and a binary tree with integer node weights, and + checks if there exists a leaf whose path weight + equals the given integer. + */ + + public static boolean hasPathSum(BinaryTree tree, int targetSum) { + + return false; + } + +} diff --git a/binarytrees/src/main/java/ImplementInorderSpaceEfficient.java b/binarytrees/src/main/java/ImplementInorderSpaceEfficient.java new file mode 100644 index 0000000..d64d872 --- /dev/null +++ b/binarytrees/src/main/java/ImplementInorderSpaceEfficient.java @@ -0,0 +1,18 @@ +import java.util.Collections; +import java.util.List; + +public class ImplementInorderSpaceEfficient { + + /* + 10.11 + + Write a non-recursive program for computing the in-order + traversal sequence for a binary tree. Assume nodes have + parent fields. + */ + + public static List inorderTraversal(BinaryTree tree) { + + return Collections.emptyList(); + } +} diff --git a/binarytrees/src/main/java/InorderIterative.java b/binarytrees/src/main/java/InorderIterative.java new file mode 100644 index 0000000..ec2a20b --- /dev/null +++ b/binarytrees/src/main/java/InorderIterative.java @@ -0,0 +1,18 @@ +import java.util.Collections; +import java.util.List; + +public class InorderIterative { + + /* + 10.7 + + Write a program which takes as input a binary tree + and performs an in-order traversal of the tree. + Do not use recursion. Nodes do not contain parent references. + */ + + public static List BSTInOrder(BinaryTree tree) { + + return Collections.emptyList(); + } +} diff --git a/binarytrees/src/main/java/IsHeightBalanced.java b/binarytrees/src/main/java/IsHeightBalanced.java new file mode 100644 index 0000000..0916fe0 --- /dev/null +++ b/binarytrees/src/main/java/IsHeightBalanced.java @@ -0,0 +1,15 @@ +public class IsHeightBalanced { + + /* + 10.1 + + Write a program that takes as input the root of + a binary tree and checks whether the tree is + height-balanced. + */ + + public static boolean isBalanced(BinaryTree tree) { + + return false; + } +} diff --git a/binarytrees/src/main/java/IsSymmetric.java b/binarytrees/src/main/java/IsSymmetric.java new file mode 100644 index 0000000..d57851a --- /dev/null +++ b/binarytrees/src/main/java/IsSymmetric.java @@ -0,0 +1,14 @@ +public class IsSymmetric { + + /* + 10.2 + + Write a program that checks whether a binary tree is symmetric. + */ + + public boolean isSymmetric(BinaryTree tree) { + + return false; + } + +} diff --git a/binarytrees/src/main/java/LockingBinaryTree.java b/binarytrees/src/main/java/LockingBinaryTree.java new file mode 100644 index 0000000..08ba3f1 --- /dev/null +++ b/binarytrees/src/main/java/LockingBinaryTree.java @@ -0,0 +1,20 @@ +public class LockingBinaryTree extends BinaryTree{ + + /* + 10.17 + + Write the following methods for the binary tree node class: + 1. A function to test if the node is locked. + 2. A function to lock the node. If the node cannot be locked, + return false, otherwise lock it and return true. + 3. A function to unlock the node. + Assume that each node has a parent field, The API will be used in a + single threaded program, so there is no need for concurrency + constructs such as mutexes or synchronization. + */ + + + public LockingBinaryTree(Object data) { + super(data); + } +} diff --git a/binarytrees/src/main/java/PreorderIterative.java b/binarytrees/src/main/java/PreorderIterative.java new file mode 100644 index 0000000..094ff0e --- /dev/null +++ b/binarytrees/src/main/java/PreorderIterative.java @@ -0,0 +1,18 @@ +import java.util.Collections; +import java.util.List; + +public class PreorderIterative { + + /* + 10.7 + + Write a program which takes as input a binary tree + and performs an pre-order traversal of the tree. + Do not use recursion. Nodes do not contain parent references. + */ + + public static List BSTPreOrder(BinaryTree tree) { + + return Collections.emptyList(); + } +} diff --git a/binarytrees/src/main/java/ReconstructBinaryTree.java b/binarytrees/src/main/java/ReconstructBinaryTree.java new file mode 100644 index 0000000..afb73b4 --- /dev/null +++ b/binarytrees/src/main/java/ReconstructBinaryTree.java @@ -0,0 +1,17 @@ +import java.util.List; + +public class ReconstructBinaryTree { + + /* + 10.12 + + Given an in-order traversal sequence and a pre-order + traversal sequence of a binary tree write a program + to reconstruct the tree. Assume each node has a unique key. + */ + + public static BinaryTree binaryTreeFromPreorderInorder(List preorder, List inorder) { + + return new BinaryTree(0); + } +} diff --git a/binarytrees/src/main/java/ReconstructBinaryTreeWithMarkers.java b/binarytrees/src/main/java/ReconstructBinaryTreeWithMarkers.java new file mode 100644 index 0000000..7b1e0cd --- /dev/null +++ b/binarytrees/src/main/java/ReconstructBinaryTreeWithMarkers.java @@ -0,0 +1,17 @@ +import java.util.List; + +public class ReconstructBinaryTreeWithMarkers { + + /* + 10.13 + + Design an algorithm for reconstructing a binary tree + from a pre-order traversal visit sequence that uses + null to mark empty sequences. + */ + + public static BinaryTree reconstructPreorder(List preorder) { + + return new BinaryTree(0); + } +} diff --git a/binarytrees/src/main/java/SumRootToLeaf.java b/binarytrees/src/main/java/SumRootToLeaf.java new file mode 100644 index 0000000..14ab67e --- /dev/null +++ b/binarytrees/src/main/java/SumRootToLeaf.java @@ -0,0 +1,15 @@ +public class SumRootToLeaf { + + /* + 10.5 + + Design an algorithm to compute the sum of + the binary numbers represented by the + root-to-leaf paths. + */ + + public static int sumRootToLeaf(BinaryTree tree) { + + return 0; + } +} diff --git a/binarytrees/src/main/java/TreeToLinkedList.java b/binarytrees/src/main/java/TreeToLinkedList.java new file mode 100644 index 0000000..584173f --- /dev/null +++ b/binarytrees/src/main/java/TreeToLinkedList.java @@ -0,0 +1,20 @@ +import java.util.Collections; +import java.util.List; + +public class TreeToLinkedList { + + /* + 10.14 + + Given a binary tree, compute the linked list from the leaves + of teh binary tree. The leaves should appear in left-to-right + order. For example, when applied to the binary tree in figure + 10.1 on Page 151, your function should return [D,E,H,M,N,P]. + */ + + public static List> createListOfLeaves(BinaryTree tree) { + + return Collections.emptyList(); + } + +} diff --git a/datastructures/src/main/java/BinaryTree.java b/datastructures/src/main/java/BinaryTree.java new file mode 100644 index 0000000..e57547a --- /dev/null +++ b/datastructures/src/main/java/BinaryTree.java @@ -0,0 +1,16 @@ +public class BinaryTree { + public T data; + public BinaryTree left, right; + + + + public BinaryTree(T data) { + this.data = data; + } + + public BinaryTree(T data, BinaryTree left, BinaryTree right) { + this.data = data; + this.left = left; + this.right = right; + } +} diff --git a/datastructures/src/main/java/BinaryTreeNode.java b/datastructures/src/main/java/BinaryTreeNode.java deleted file mode 100644 index 6bfffc1..0000000 --- a/datastructures/src/main/java/BinaryTreeNode.java +++ /dev/null @@ -1,14 +0,0 @@ -public class BinaryTreeNode { - public T data; - public BinaryTreeNode left, right; - - public BinaryTreeNode(T data) { - this.data = data; - } - - public BinaryTreeNode(T data, BinaryTreeNode left, BinaryTreeNode right) { - this.data = data; - this.left = left; - this.right = right; - } -} diff --git a/pom.xml b/pom.xml index 26efdc5..98cfe49 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ gardncl elements-of-programming-interviews - 1.4 + 1.5 datastructures @@ -14,6 +14,7 @@ strings linkedlists stacksandqueues + binarytrees pom diff --git a/stacksandqueues/src/main/java/ComputeBinaryTreeNodes.java b/stacksandqueues/src/main/java/ComputeBinaryTreeNodes.java index 1a506f3..bd58771 100644 --- a/stacksandqueues/src/main/java/ComputeBinaryTreeNodes.java +++ b/stacksandqueues/src/main/java/ComputeBinaryTreeNodes.java @@ -14,7 +14,7 @@ public class ComputeBinaryTreeNodes { figure 10.1 on page 151. */ - public static List> binaryTreeDepthOrder(BinaryTreeNode tree) { + public static List> binaryTreeDepthOrder(BinaryTree tree) { return Collections.emptyList(); } diff --git a/stacksandqueues/src/test/java/ComputeBinaryTreeNodesTest.java b/stacksandqueues/src/test/java/ComputeBinaryTreeNodesTest.java index 1cb2fe4..eae0996 100644 --- a/stacksandqueues/src/test/java/ComputeBinaryTreeNodesTest.java +++ b/stacksandqueues/src/test/java/ComputeBinaryTreeNodesTest.java @@ -8,7 +8,7 @@ public class ComputeBinaryTreeNodesTest { private List> expected; - private BinaryTreeNode input; + private BinaryTree input; @Test public void binaryTreeDepthOrder1() { @@ -20,27 +20,27 @@ public void binaryTreeDepthOrder1() { Arrays.asList(17,401,257), Arrays.asList(641) ); - input = new BinaryTreeNode<>(314); - input.left = new BinaryTreeNode<>(6); - input.left.left = new BinaryTreeNode<>(271); - input.left.left.left = new BinaryTreeNode<>(28); - input.left.left.right = new BinaryTreeNode<>(0); - input.left.right = new BinaryTreeNode<>(561); - input.left.right.right = new BinaryTreeNode<>(3); - input.left.right.left = new BinaryTreeNode<>(17); - input.right = new BinaryTreeNode<>(6); - input.right.left = new BinaryTreeNode<>(2); - input.right.left.right = new BinaryTreeNode<>(1); - input.right.left.right.left = new BinaryTreeNode<>(401); - input.right.left.right.left.right = new BinaryTreeNode<>(641); - input.right.left.right.right = new BinaryTreeNode<>(257); - input.right.right = new BinaryTreeNode<>(271); - input.right.right.right = new BinaryTreeNode<>(28); + input = new BinaryTree<>(314); + input.left = new BinaryTree<>(6); + input.left.left = new BinaryTree<>(271); + input.left.left.left = new BinaryTree<>(28); + input.left.left.right = new BinaryTree<>(0); + input.left.right = new BinaryTree<>(561); + input.left.right.right = new BinaryTree<>(3); + input.left.right.left = new BinaryTree<>(17); + input.right = new BinaryTree<>(6); + input.right.left = new BinaryTree<>(2); + input.right.left.right = new BinaryTree<>(1); + input.right.left.right.left = new BinaryTree<>(401); + input.right.left.right.left.right = new BinaryTree<>(641); + input.right.left.right.right = new BinaryTree<>(257); + input.right.right = new BinaryTree<>(271); + input.right.right.right = new BinaryTree<>(28); test(expected, input); } - private void test(List> expected, BinaryTreeNode input) { + private void test(List> expected, BinaryTree input) { assertEquals(expected, ComputeBinaryTreeNodes.binaryTreeDepthOrder(input)); } } \ No newline at end of file From ddf29088e1c95e88a452941c7c2884001baf83c0 Mon Sep 17 00:00:00 2001 From: gardncl Date: Thu, 27 Apr 2017 18:14:12 -0400 Subject: [PATCH 02/15] 10.1, 10.2, 10.3, and new utility for generating preset binary trees --- binarytrees/pom.xml | 6 +++ binarytrees/src/main/java/IsSymmetric.java | 2 +- .../java/ComputeLowestCommonAncestorTest.java | 45 ++++++++++++++++ .../src/test/java/IsHeightBalancedTest.java | 46 ++++++++++++++++ .../src/test/java/IsSymmetricTest.java | 54 +++++++++++++++++++ .../test/java/ComputeBinaryTreeNodesTest.java | 17 +----- utils/src/main/java/BinaryTreeUtil.java | 47 ++++++++++++++++ 7 files changed, 200 insertions(+), 17 deletions(-) create mode 100644 binarytrees/src/test/java/ComputeLowestCommonAncestorTest.java create mode 100644 binarytrees/src/test/java/IsHeightBalancedTest.java create mode 100644 binarytrees/src/test/java/IsSymmetricTest.java create mode 100644 utils/src/main/java/BinaryTreeUtil.java diff --git a/binarytrees/pom.xml b/binarytrees/pom.xml index a832edb..e185ccd 100644 --- a/binarytrees/pom.xml +++ b/binarytrees/pom.xml @@ -17,7 +17,13 @@ datastructures 1.4 + + gardncl + utils + 1.4 + + \ No newline at end of file diff --git a/binarytrees/src/main/java/IsSymmetric.java b/binarytrees/src/main/java/IsSymmetric.java index d57851a..43ba67c 100644 --- a/binarytrees/src/main/java/IsSymmetric.java +++ b/binarytrees/src/main/java/IsSymmetric.java @@ -6,7 +6,7 @@ public class IsSymmetric { Write a program that checks whether a binary tree is symmetric. */ - public boolean isSymmetric(BinaryTree tree) { + public static boolean isSymmetric(BinaryTree tree) { return false; } diff --git a/binarytrees/src/test/java/ComputeLowestCommonAncestorTest.java b/binarytrees/src/test/java/ComputeLowestCommonAncestorTest.java new file mode 100644 index 0000000..d6aa705 --- /dev/null +++ b/binarytrees/src/test/java/ComputeLowestCommonAncestorTest.java @@ -0,0 +1,45 @@ +import org.junit.Test; + +import static org.junit.Assert.*; + +public class ComputeLowestCommonAncestorTest { + + private BinaryTree expected; + private BinaryTree tree; + private BinaryTree node0; + private BinaryTree node1; + + @Test + public void LCA1() throws Exception { + tree = BinaryTreeUtil.getEvenTree(); + node0 = tree.right; + node1 = tree.left; + expected = tree; + + test(expected, tree, node0, node1); + } + + @Test + public void LCA2() throws Exception { + tree = BinaryTreeUtil.getFigureTenDotOne(); + node0 = tree.right.left.right.left.right; + node1 = tree.right.left.right.right; + expected = tree.right.left.right; + + test(expected, tree, node0, node1); + } + + @Test + public void LCA3() throws Exception { + tree = BinaryTreeUtil.getOddTree(); + node0 = tree.left; + node1 = tree.left.left; + expected = tree.left; + + test(expected, tree, node0, node1); + } + + private void test(BinaryTree expected, BinaryTree tree, BinaryTree node0, BinaryTree node1) { + assertEquals(expected, ComputeLowestCommonAncestor.LCA(tree,node0,node1)); + } +} \ No newline at end of file diff --git a/binarytrees/src/test/java/IsHeightBalancedTest.java b/binarytrees/src/test/java/IsHeightBalancedTest.java new file mode 100644 index 0000000..36272b5 --- /dev/null +++ b/binarytrees/src/test/java/IsHeightBalancedTest.java @@ -0,0 +1,46 @@ +import org.junit.Test; + +import static org.junit.Assert.*; + +public class IsHeightBalancedTest { + + private boolean expected; + private BinaryTree tree; + + @Test + public void isBalanced1() throws Exception { + expected = true; + tree = BinaryTreeUtil.getEvenTree(); + + test(expected, tree); + } + + @Test + public void isBalanced2() throws Exception { + expected = false; + tree = BinaryTreeUtil.getOddTree(); + + test(expected, tree); + } + + @Test + public void isBalanced3() throws Exception { + expected = true; + tree = BinaryTreeUtil.getFullTree(); + + test(expected, tree); + } + + @Test + public void isBalanced4() throws Exception { + expected = true; + tree = BinaryTreeUtil.getFigureTenDotOne(); + + test(expected, tree); + } + + private void test(boolean expected, BinaryTree tree) { + assertEquals(expected, IsHeightBalanced.isBalanced(tree)); + } + +} \ No newline at end of file diff --git a/binarytrees/src/test/java/IsSymmetricTest.java b/binarytrees/src/test/java/IsSymmetricTest.java new file mode 100644 index 0000000..d8fcfd9 --- /dev/null +++ b/binarytrees/src/test/java/IsSymmetricTest.java @@ -0,0 +1,54 @@ +import org.junit.Test; + +import static org.junit.Assert.*; + +public class IsSymmetricTest { + + private boolean expected; + private BinaryTree tree; + + @Test + public void isSymmetric1() throws Exception { + expected = true; + tree = BinaryTreeUtil.getEvenTree(); + + test(expected, tree); + } + + @Test + public void isSymmetric2() throws Exception { + expected = false; + tree = BinaryTreeUtil.getFigureTenDotOne(); + + test(expected, tree); + } + + @Test + public void isSymmetric3() throws Exception { + expected = true; + tree = new BinaryTree<>(0); + tree.right = new BinaryTree<>(1); + tree.right.left = new BinaryTree<>(2); + tree.left = new BinaryTree<>(1); + tree.left.right = new BinaryTree<>(2); + + test(expected, tree); + } + + @Test + public void isSymmetric4() throws Exception { + expected = false; + tree = new BinaryTree<>(0); + tree.right = new BinaryTree<>(1); + tree.right.left = new BinaryTree<>(17); + tree.left = new BinaryTree<>(1); + tree.left.right = new BinaryTree<>(42); + + test(expected, tree); + } + + private void test(boolean expected, BinaryTree tree) { + assertEquals(expected, IsSymmetric.isSymmetric(tree)); + } + +} \ No newline at end of file diff --git a/stacksandqueues/src/test/java/ComputeBinaryTreeNodesTest.java b/stacksandqueues/src/test/java/ComputeBinaryTreeNodesTest.java index eae0996..9e9a122 100644 --- a/stacksandqueues/src/test/java/ComputeBinaryTreeNodesTest.java +++ b/stacksandqueues/src/test/java/ComputeBinaryTreeNodesTest.java @@ -20,22 +20,7 @@ public void binaryTreeDepthOrder1() { Arrays.asList(17,401,257), Arrays.asList(641) ); - input = new BinaryTree<>(314); - input.left = new BinaryTree<>(6); - input.left.left = new BinaryTree<>(271); - input.left.left.left = new BinaryTree<>(28); - input.left.left.right = new BinaryTree<>(0); - input.left.right = new BinaryTree<>(561); - input.left.right.right = new BinaryTree<>(3); - input.left.right.left = new BinaryTree<>(17); - input.right = new BinaryTree<>(6); - input.right.left = new BinaryTree<>(2); - input.right.left.right = new BinaryTree<>(1); - input.right.left.right.left = new BinaryTree<>(401); - input.right.left.right.left.right = new BinaryTree<>(641); - input.right.left.right.right = new BinaryTree<>(257); - input.right.right = new BinaryTree<>(271); - input.right.right.right = new BinaryTree<>(28); + input = BinaryTreeUtil.getFigureTenDotOne(); test(expected, input); } diff --git a/utils/src/main/java/BinaryTreeUtil.java b/utils/src/main/java/BinaryTreeUtil.java new file mode 100644 index 0000000..3c7364a --- /dev/null +++ b/utils/src/main/java/BinaryTreeUtil.java @@ -0,0 +1,47 @@ +public class BinaryTreeUtil { + + public static BinaryTree getEvenTree() { + BinaryTree tree = new BinaryTree<>(0); + tree.left = new BinaryTree<>(1); + tree.right = new BinaryTree<>(2); + return tree; + } + + public static BinaryTree getOddTree() { + BinaryTree tree = new BinaryTree<>(0); + tree.left = new BinaryTree<>(1); + tree.left.left = new BinaryTree<>(2); + return tree; + } + + public static BinaryTree getFullTree() { + BinaryTree tree = new BinaryTree<>(0); + tree.left = new BinaryTree<>(1); + tree.left.left = new BinaryTree<>(2); + tree.left.right = new BinaryTree<>(3); + tree.right = new BinaryTree<>(4); + tree.right.left = new BinaryTree<>(5); + tree.right.right = new BinaryTree<>(6); + return tree; + } + + public static BinaryTree getFigureTenDotOne() { + BinaryTree tree = new BinaryTree<>(314); + tree.left = new BinaryTree<>(6); + tree.left.left = new BinaryTree<>(271); + tree.left.left.left = new BinaryTree<>(28); + tree.left.left.right = new BinaryTree<>(0); + tree.left.right = new BinaryTree<>(561); + tree.left.right.right = new BinaryTree<>(3); + tree.left.right.left = new BinaryTree<>(17); + tree.right = new BinaryTree<>(6); + tree.right.left = new BinaryTree<>(2); + tree.right.left.right = new BinaryTree<>(1); + tree.right.left.right.left = new BinaryTree<>(401); + tree.right.left.right.left.right = new BinaryTree<>(641); + tree.right.left.right.right = new BinaryTree<>(257); + tree.right.right = new BinaryTree<>(271); + tree.right.right.right = new BinaryTree<>(28); + return tree; + } +} From 41c706863d222fc53d0752dba2cb59914d1ad231 Mon Sep 17 00:00:00 2001 From: gardncl Date: Thu, 27 Apr 2017 18:34:23 -0400 Subject: [PATCH 03/15] 10.4 --- .../src/main/java/ComputeLCAWithParent.java | 4 +- .../test/java/ComputeLCAWithParentTest.java | 47 +++++++++++++++++++ datastructures/src/main/java/BinaryTree.java | 14 ++++++ .../src/main/java/BinaryTreeParent.java | 30 ++++++++++++ 4 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 binarytrees/src/test/java/ComputeLCAWithParentTest.java create mode 100644 datastructures/src/main/java/BinaryTreeParent.java diff --git a/binarytrees/src/main/java/ComputeLCAWithParent.java b/binarytrees/src/main/java/ComputeLCAWithParent.java index 57dac47..3dc9c8b 100644 --- a/binarytrees/src/main/java/ComputeLCAWithParent.java +++ b/binarytrees/src/main/java/ComputeLCAWithParent.java @@ -8,8 +8,8 @@ public class ComputeLCAWithParent { Assume that each node has a parent pointer. */ - public static BinaryTree LCA(BinaryTree node0, BinaryTree node1) { + public static BinaryTreeParent LCA(BinaryTreeParent node0, BinaryTreeParent node1) { - return new BinaryTree<>(0); + return new BinaryTreeParent<>(0); } } diff --git a/binarytrees/src/test/java/ComputeLCAWithParentTest.java b/binarytrees/src/test/java/ComputeLCAWithParentTest.java new file mode 100644 index 0000000..f77ae7f --- /dev/null +++ b/binarytrees/src/test/java/ComputeLCAWithParentTest.java @@ -0,0 +1,47 @@ +import org.junit.Test; + +import static org.junit.Assert.*; + +public class ComputeLCAWithParentTest { + + private BinaryTreeParent expected; + private BinaryTreeParent node0; + private BinaryTreeParent node1; + + @Test + public void LCA1() throws Exception { + expected = new BinaryTreeParent<>(0); + expected.setLeft(new BinaryTreeParent<>(1)); + node0 = (BinaryTreeParent) expected.left; + expected.setRight(new BinaryTreeParent<>(2)); + node1 = (BinaryTreeParent) expected.right; + + test(expected, node0, node1); + } + + @Test + public void LCA2() throws Exception { + expected = new BinaryTreeParent<>(0); + expected.setRight(new BinaryTreeParent<>(1)); + node0 = expected; + ((BinaryTreeParent)expected.right).setRight(new BinaryTreeParent<>(2)); + node1 = (BinaryTreeParent) expected.right.right; + + + test(expected, node0, node1); + } + + @Test + public void LCA3() throws Exception { + expected = null; + node0 = new BinaryTreeParent<>(0); + node1 = new BinaryTreeParent<>(1); + + test(expected, node0, node1); + } + + private void test(BinaryTreeParent expected, BinaryTreeParent node0, BinaryTreeParent node1) { + assertEquals(expected, ComputeLCAWithParent.LCA(node0, node1)); + } + +} \ No newline at end of file diff --git a/datastructures/src/main/java/BinaryTree.java b/datastructures/src/main/java/BinaryTree.java index e57547a..96d9652 100644 --- a/datastructures/src/main/java/BinaryTree.java +++ b/datastructures/src/main/java/BinaryTree.java @@ -13,4 +13,18 @@ public BinaryTree(T data, BinaryTree left, BinaryTree right) { this.left = left; this.right = right; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + BinaryTree that = (BinaryTree) o; + + if (data != null ? !data.equals(that.data) : that.data != null) return false; + if (left != null ? !left.equals(that.left) : that.left != null) return false; + return right != null ? right.equals(that.right) : that.right == null; + } + + } diff --git a/datastructures/src/main/java/BinaryTreeParent.java b/datastructures/src/main/java/BinaryTreeParent.java new file mode 100644 index 0000000..f6b1fa7 --- /dev/null +++ b/datastructures/src/main/java/BinaryTreeParent.java @@ -0,0 +1,30 @@ +public class BinaryTreeParent extends BinaryTree { + + public BinaryTree parent; + + public BinaryTreeParent(T data) { + super(data); + } + + public void setRight(BinaryTreeParent node) { + this.right = node; + node.parent = this; + } + + public void setLeft(BinaryTreeParent node) { + this.left = node; + node.parent = this; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + + BinaryTreeParent that = (BinaryTreeParent) o; + + return parent != null ? parent.equals(that.parent) : that.parent == null; + } + +} From ad39077a493de242aef67e024e40e0d404ea0214 Mon Sep 17 00:00:00 2001 From: gardncl Date: Thu, 27 Apr 2017 19:00:50 -0400 Subject: [PATCH 04/15] 10.5 --- .../src/test/java/SumRootToLeafTest.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 binarytrees/src/test/java/SumRootToLeafTest.java diff --git a/binarytrees/src/test/java/SumRootToLeafTest.java b/binarytrees/src/test/java/SumRootToLeafTest.java new file mode 100644 index 0000000..809f6e8 --- /dev/null +++ b/binarytrees/src/test/java/SumRootToLeafTest.java @@ -0,0 +1,52 @@ +import org.junit.Test; + +import static org.junit.Assert.*; + +public class SumRootToLeafTest { + + private int expected; + private BinaryTree tree; + + @Test + public void sumRootToLeaf1() throws Exception { + expected = 6; + tree = new BinaryTree<>(0); + tree.right = new BinaryTree<>(1); + tree.right.right = new BinaryTree<>(0); + tree.left = new BinaryTree<>(0); + tree.left.left = new BinaryTree<>(1); + + test(expected, tree); + } + + @Test + public void sumRootToLeaf2() throws Exception { + expected = 8; + tree = new BinaryTree<>(1); + tree.right = new BinaryTree<>(1); + tree.right.right = new BinaryTree<>(0); + tree.left = new BinaryTree<>(0); + tree.left.left = new BinaryTree<>(1); + + test(expected, tree); + } + + @Test + public void sumRootToLeaf3() throws Exception { + expected = 16; + tree = new BinaryTree<>(1); + tree.right = new BinaryTree<>(1); + tree.right.right = new BinaryTree<>(0); + tree.right.left = new BinaryTree<>(1); + tree.left = new BinaryTree<>(0); + tree.left.left = new BinaryTree<>(1); + tree.left.right = new BinaryTree<>(0); + + test(expected, tree); + } + + private void test(int expected, BinaryTree tree) { + assertEquals(expected, SumRootToLeaf.sumRootToLeaf(tree)); + } + +} \ No newline at end of file From ad0fdebd11f73df047cbb0e1b73f99f003fde410 Mon Sep 17 00:00:00 2001 From: gardncl Date: Thu, 27 Apr 2017 19:15:56 -0400 Subject: [PATCH 05/15] 10.6 & 10.7 --- .../src/test/java/FindRootToLeafSumTest.java | 51 +++++++++++++++++++ .../src/test/java/InorderIterativeTest.java | 41 +++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 binarytrees/src/test/java/FindRootToLeafSumTest.java create mode 100644 binarytrees/src/test/java/InorderIterativeTest.java diff --git a/binarytrees/src/test/java/FindRootToLeafSumTest.java b/binarytrees/src/test/java/FindRootToLeafSumTest.java new file mode 100644 index 0000000..2fe83a9 --- /dev/null +++ b/binarytrees/src/test/java/FindRootToLeafSumTest.java @@ -0,0 +1,51 @@ +import org.junit.Test; + +import static org.junit.Assert.*; + +public class FindRootToLeafSumTest { + + private boolean expected; + private BinaryTree tree; + private int targetSum; + + @Test + public void hasPathSum1() throws Exception { + expected = true; + tree = BinaryTreeUtil.getFullTree(); + targetSum = 10; + + test(expected, tree, targetSum); + } + + @Test + public void hasPathSum2() throws Exception { + expected = false; + tree = BinaryTreeUtil.getFullTree(); + targetSum = 5; + + test(expected, tree, targetSum); + } + + @Test + public void hasPathSum3() throws Exception { + expected = true; + tree = BinaryTreeUtil.getFigureTenDotOne(); + targetSum = 519; + + test(expected, tree, targetSum); + } + + @Test + public void hasPathSum4() throws Exception { + expected = false; + tree = BinaryTreeUtil.getFigureTenDotOne(); + targetSum = 591; + + test(expected, tree, targetSum); + } + + private void test(boolean expected, BinaryTree tree, int targetSum) { + assertEquals(expected, FindRootToLeafSum.hasPathSum(tree, targetSum)); + } + +} \ No newline at end of file diff --git a/binarytrees/src/test/java/InorderIterativeTest.java b/binarytrees/src/test/java/InorderIterativeTest.java new file mode 100644 index 0000000..bab996b --- /dev/null +++ b/binarytrees/src/test/java/InorderIterativeTest.java @@ -0,0 +1,41 @@ +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.*; + +public class InorderIterativeTest { + + private List expected; + private BinaryTree tree; + + @Test + public void BSTInOrder1() throws Exception { + expected = Arrays.asList(2,1,0); + tree = BinaryTreeUtil.getOddTree(); + + test(expected, tree); + } + + @Test + public void BSTInOrder2() throws Exception { + expected = Arrays.asList(2,1,3,0,5,4,6); + tree = BinaryTreeUtil.getFullTree(); + + test(expected, tree); + } + + @Test + public void BSTInOrder3() throws Exception { + expected = Arrays.asList(28,271,0,6,561,17,3,314,2,401,614,1,257,6,271,28); + tree = BinaryTreeUtil.getFigureTenDotOne(); + + test(expected, tree); + } + + private void test(List expected, BinaryTree tree) { + assertEquals(expected, InorderIterative.BSTInOrder(tree)); + } + +} \ No newline at end of file From 457776f96a579c13f3b2aff80b5c744a8be60465 Mon Sep 17 00:00:00 2001 From: gardncl Date: Thu, 27 Apr 2017 19:21:15 -0400 Subject: [PATCH 06/15] 10.8 --- .../src/main/java/PreorderIterative.java | 4 +- .../src/test/java/PreorderIterativeTest.java | 41 +++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 binarytrees/src/test/java/PreorderIterativeTest.java diff --git a/binarytrees/src/main/java/PreorderIterative.java b/binarytrees/src/main/java/PreorderIterative.java index 094ff0e..d335718 100644 --- a/binarytrees/src/main/java/PreorderIterative.java +++ b/binarytrees/src/main/java/PreorderIterative.java @@ -4,10 +4,10 @@ public class PreorderIterative { /* - 10.7 + 10.8 Write a program which takes as input a binary tree - and performs an pre-order traversal of the tree. + and performs a pre-order traversal of the tree. Do not use recursion. Nodes do not contain parent references. */ diff --git a/binarytrees/src/test/java/PreorderIterativeTest.java b/binarytrees/src/test/java/PreorderIterativeTest.java new file mode 100644 index 0000000..2c022ec --- /dev/null +++ b/binarytrees/src/test/java/PreorderIterativeTest.java @@ -0,0 +1,41 @@ +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.*; + +public class PreorderIterativeTest { + + private List expected; + private BinaryTree tree; + + @Test + public void BSTPreOrder1() throws Exception { + expected = Arrays.asList(0,1,2); + tree = BinaryTreeUtil.getOddTree(); + + test(expected, tree); + } + + @Test + public void BSTPreOrder2() throws Exception { + expected = Arrays.asList(0,1,2,3,4,5,6); + tree = BinaryTreeUtil.getFullTree(); + + test(expected, tree); + } + + @Test + public void BSTPreOrder3() throws Exception { + expected = Arrays.asList(314,6,271,28,0,561,3,17,6,2,1,401,641,257,271,28); + tree = BinaryTreeUtil.getFigureTenDotOne(); + + test(expected, tree); + } + + private void test(List expected, BinaryTree tree) { + assertEquals(expected, PreorderIterative.BSTPreOrder(tree)); + } + +} \ No newline at end of file From 1b25c6a44bb75534dae2fbe625e8ac78e62f1a75 Mon Sep 17 00:00:00 2001 From: gardncl Date: Thu, 27 Apr 2017 19:32:45 -0400 Subject: [PATCH 07/15] 10.9 --- .../test/java/ComputeKthNodeInorderTest.java | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 binarytrees/src/test/java/ComputeKthNodeInorderTest.java diff --git a/binarytrees/src/test/java/ComputeKthNodeInorderTest.java b/binarytrees/src/test/java/ComputeKthNodeInorderTest.java new file mode 100644 index 0000000..89986a5 --- /dev/null +++ b/binarytrees/src/test/java/ComputeKthNodeInorderTest.java @@ -0,0 +1,66 @@ +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.*; + +public class ComputeKthNodeInorderTest { + + private BinaryTree expected; + private static BinaryTree tree; + private int k; + + static { + tree = new BinaryTree<>(5); + tree.left = new BinaryTree<>(2); + tree.left.left = new BinaryTree<>(1); + tree.right = new BinaryTree<>(2); + tree.right.left = new BinaryTree<>(1); + } + + @Test + public void findKthNodeBinaryTree1() throws Exception { + k = 0; + expected = tree.left.left; + + test(expected, k); + } + + @Test + public void findKthNodeBinaryTree2() throws Exception { + k = 1; + expected = tree.left; + + test(expected, k); + } + + @Test + public void findKthNodeBinaryTree3() throws Exception { + k = 2; + expected = tree; + + test(expected, k); + } + + @Test + public void findKthNodeBinaryTree4() throws Exception { + k = 3; + expected = tree.right.left; + + test(expected, k); + } + + @Test + public void findKthNodeBinaryTree5() throws Exception { + k = 4; + expected = tree.right; + + test(expected, k); + } + + private void test(BinaryTree expected, int k) { + assertEquals(expected, ComputeKthNodeInorder.findKthNodeBinaryTree(tree, k)); + } + +} \ No newline at end of file From e1cb0efc7e5c5b6fe420a3ae0ba00c729b84d375 Mon Sep 17 00:00:00 2001 From: gardncl Date: Thu, 27 Apr 2017 19:50:25 -0400 Subject: [PATCH 08/15] 10.10 --- .../src/test/java/ComputeSuccessorTest.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 binarytrees/src/test/java/ComputeSuccessorTest.java diff --git a/binarytrees/src/test/java/ComputeSuccessorTest.java b/binarytrees/src/test/java/ComputeSuccessorTest.java new file mode 100644 index 0000000..37c915c --- /dev/null +++ b/binarytrees/src/test/java/ComputeSuccessorTest.java @@ -0,0 +1,50 @@ +import org.junit.Test; + +import static org.junit.Assert.*; + +public class ComputeSuccessorTest { + + private BinaryTree expected; + private static BinaryTree node; + + @Test + public void findSuccessor1() throws Exception { + final BinaryTree tree = BinaryTreeUtil.getEvenTree(); + expected = tree; + node = tree.left; + + test(expected, node); + } + + @Test + public void findSuccessor2() throws Exception { + final BinaryTree tree = BinaryTreeUtil.getFullTree(); + expected = tree; + node = tree.left.right; + + test(expected, node); + } + + @Test + public void findSuccessor3() throws Exception { + final BinaryTree tree = BinaryTreeUtil.getFigureTenDotOne(); + expected = tree; + node = tree.left.right.right; + + test(expected, node); + } + + @Test + public void findSuccessor4() throws Exception { + final BinaryTree tree = BinaryTreeUtil.getFigureTenDotOne(); + expected = tree.right.left; + node = tree; + + test(expected, node); + } + + private void test(BinaryTree expected, BinaryTree node) { + assertEquals(expected, ComputeSuccessor.findSuccessor(node)); + } + +} \ No newline at end of file From 650831cb880bddb70e8bf0a9e9427a655fbafd4c Mon Sep 17 00:00:00 2001 From: gardncl Date: Thu, 27 Apr 2017 19:52:11 -0400 Subject: [PATCH 09/15] 10.11 --- .../ImplementInorderSpaceEfficientTest.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 binarytrees/src/test/java/ImplementInorderSpaceEfficientTest.java diff --git a/binarytrees/src/test/java/ImplementInorderSpaceEfficientTest.java b/binarytrees/src/test/java/ImplementInorderSpaceEfficientTest.java new file mode 100644 index 0000000..4ec2abc --- /dev/null +++ b/binarytrees/src/test/java/ImplementInorderSpaceEfficientTest.java @@ -0,0 +1,41 @@ +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.*; + +public class ImplementInorderSpaceEfficientTest { + + private List expected; + private BinaryTree tree; + + @Test + public void inorderTraversal1() throws Exception { + expected = Arrays.asList(2,1,0); + tree = BinaryTreeUtil.getOddTree(); + + test(expected, tree); + } + + @Test + public void inorderTraversal2() throws Exception { + expected = Arrays.asList(2,1,3,0,5,4,6); + tree = BinaryTreeUtil.getFullTree(); + + test(expected, tree); + } + + @Test + public void inorderTraversal3() throws Exception { + expected = Arrays.asList(28,271,0,6,561,17,3,314,2,401,614,1,257,6,271,28); + tree = BinaryTreeUtil.getFigureTenDotOne(); + + test(expected, tree); + } + + private void test(List expected, BinaryTree tree) { + assertEquals(expected, ImplementInorderSpaceEfficient.inorderTraversal(tree)); + } + +} \ No newline at end of file From 2369e575614480438d3d410ce74caba165b3cc52 Mon Sep 17 00:00:00 2001 From: gardncl Date: Thu, 27 Apr 2017 19:58:35 -0400 Subject: [PATCH 10/15] 10.12 --- .../test/java/ReconstructBinaryTreeTest.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 binarytrees/src/test/java/ReconstructBinaryTreeTest.java diff --git a/binarytrees/src/test/java/ReconstructBinaryTreeTest.java b/binarytrees/src/test/java/ReconstructBinaryTreeTest.java new file mode 100644 index 0000000..36eac02 --- /dev/null +++ b/binarytrees/src/test/java/ReconstructBinaryTreeTest.java @@ -0,0 +1,45 @@ +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.*; + +public class ReconstructBinaryTreeTest { + + private BinaryTree expected; + private List preorder; + private List inorder; + + @Test + public void binaryTreeFromPreorderInorder1() throws Exception { + expected = BinaryTreeUtil.getOddTree(); + preorder = Arrays.asList(0,1,2); + inorder = Arrays.asList(2,1,0); + + test(expected, preorder, inorder); + } + + @Test + public void binaryTreeFromPreorderInorder2() throws Exception { + expected = BinaryTreeUtil.getFullTree(); + preorder = Arrays.asList(0,1,2,3,4,5,6); + inorder = Arrays.asList(2,1,3,0,5,4,6); + + test(expected, preorder, inorder); + } + + @Test + public void binaryTreeFromPreorderInorder3() throws Exception { + expected = BinaryTreeUtil.getFigureTenDotOne(); + preorder = Arrays.asList(314,6,271,28,0,561,3,17,6,2,1,401,641,257,271,28); + inorder = Arrays.asList(28,271,0,6,561,17,3,314,2,401,614,1,257,6,271,28); + + test(expected, preorder, inorder); + } + + private void test(BinaryTree expected, List preorder, List inorder) { + assertEquals(expected, ReconstructBinaryTree.binaryTreeFromPreorderInorder(preorder, inorder)); + } + +} \ No newline at end of file From 7be43418d1fb1ae696b6c9b7bb11d2b6d8e402f1 Mon Sep 17 00:00:00 2001 From: gardncl Date: Thu, 27 Apr 2017 20:06:30 -0400 Subject: [PATCH 11/15] 10.13 --- .../test/java/ReconstructBinaryTreeTest.java | 2 +- .../ReconstructBinaryTreeWithMarkersTest.java | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 binarytrees/src/test/java/ReconstructBinaryTreeWithMarkersTest.java diff --git a/binarytrees/src/test/java/ReconstructBinaryTreeTest.java b/binarytrees/src/test/java/ReconstructBinaryTreeTest.java index 36eac02..cf62117 100644 --- a/binarytrees/src/test/java/ReconstructBinaryTreeTest.java +++ b/binarytrees/src/test/java/ReconstructBinaryTreeTest.java @@ -37,7 +37,7 @@ public void binaryTreeFromPreorderInorder3() throws Exception { test(expected, preorder, inorder); } - + private void test(BinaryTree expected, List preorder, List inorder) { assertEquals(expected, ReconstructBinaryTree.binaryTreeFromPreorderInorder(preorder, inorder)); } diff --git a/binarytrees/src/test/java/ReconstructBinaryTreeWithMarkersTest.java b/binarytrees/src/test/java/ReconstructBinaryTreeWithMarkersTest.java new file mode 100644 index 0000000..7dacc6d --- /dev/null +++ b/binarytrees/src/test/java/ReconstructBinaryTreeWithMarkersTest.java @@ -0,0 +1,41 @@ +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.*; + +public class ReconstructBinaryTreeWithMarkersTest { + + private BinaryTree expected; + private List sequence; + + @Test + public void reconstructPreorder1() throws Exception { + expected = BinaryTreeUtil.getEvenTree(); + sequence = Arrays.asList(0,1,null,null,2,null,null); + + test(expected, sequence); + } + + @Test + public void reconstructPreorder2() throws Exception { + expected = BinaryTreeUtil.getFullTree(); + sequence = Arrays.asList(0,1,2,null,null,3,null,null,4,5,null,null,6,null,null); + + test(expected, sequence); + } + + @Test + public void reconstructPreorder3() throws Exception { + expected = BinaryTreeUtil.getFigureTenDotOne(); + sequence = Arrays.asList(314,6,271,28,null,null,0,null,null,561,null,3,17,null,null,null,6,2,null,1,401,null,641,null,null,257,null,null,271,null,28,null,null); + + test(expected, sequence); + } + + private void test(BinaryTree expected, List sequence) { + assertEquals(expected, ReconstructBinaryTreeWithMarkers.reconstructPreorder(sequence)); + } + +} From 0c138771345c0367aa1cf1b77872a712b3ea8f13 Mon Sep 17 00:00:00 2001 From: gardncl Date: Thu, 27 Apr 2017 20:17:35 -0400 Subject: [PATCH 12/15] 10.14 --- .../src/test/java/TreeToLinkedListTest.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 binarytrees/src/test/java/TreeToLinkedListTest.java diff --git a/binarytrees/src/test/java/TreeToLinkedListTest.java b/binarytrees/src/test/java/TreeToLinkedListTest.java new file mode 100644 index 0000000..b2bb5c5 --- /dev/null +++ b/binarytrees/src/test/java/TreeToLinkedListTest.java @@ -0,0 +1,56 @@ +import org.junit.Test; + +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +import static org.junit.Assert.*; + +public class TreeToLinkedListTest { + + private List> expected; + private BinaryTree tree; + + @Test + public void createListOfLeaves1() throws Exception { + tree = BinaryTreeUtil.getEvenTree(); + expected = new LinkedList<>(Arrays.asList(tree.left, tree.right)); + + test(expected, tree); + } + + @Test + public void createListOfLeaves2() throws Exception { + tree = BinaryTreeUtil.getFullTree(); + expected = new LinkedList<>( + Arrays.asList( + tree.left.left, + tree.left.right, + tree.right.left, + tree.right.right) + ); + + test(expected, tree); + } + + @Test + public void createListOfLeaves3() throws Exception { + tree = BinaryTreeUtil.getFigureTenDotOne(); + expected = new LinkedList<>( + Arrays.asList( + tree.left.left.left, + tree.left.left.right, + tree.left.right.right.left, + tree.right.left.right.left.right, + tree.right.left.right.right, + tree.right.right.right) + ); + + test(expected, tree); + } + + private void test(List> expected, BinaryTree tree) { + assertEquals(expected,TreeToLinkedList.createListOfLeaves(tree)); + } + +} \ No newline at end of file From 1d6880a622dd98c5230cc3ba0ca6ae89e15ff532 Mon Sep 17 00:00:00 2001 From: gardncl Date: Thu, 27 Apr 2017 20:45:20 -0400 Subject: [PATCH 13/15] 10.15 --- .../src/test/java/ComputeExteriorTest.java | 68 +++++++++++++++++++ .../src/test/java/TreeToLinkedListTest.java | 6 +- 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 binarytrees/src/test/java/ComputeExteriorTest.java diff --git a/binarytrees/src/test/java/ComputeExteriorTest.java b/binarytrees/src/test/java/ComputeExteriorTest.java new file mode 100644 index 0000000..325080c --- /dev/null +++ b/binarytrees/src/test/java/ComputeExteriorTest.java @@ -0,0 +1,68 @@ +import org.junit.Test; + +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +import static org.junit.Assert.*; + +public class ComputeExteriorTest { + + private List> expected; + private BinaryTree tree; + + @Test + public void exteriorBinaryTree1() throws Exception { + tree = BinaryTreeUtil.getEvenTree(); + expected = new LinkedList<>( + Arrays.asList( + tree, + tree.left, + tree.right) + ); + + test(expected, tree); + } + + @Test + public void exteriorBinaryTree2() throws Exception { + tree = BinaryTreeUtil.getFullTree(); + expected = new LinkedList<>( + Arrays.asList( + tree, + tree.left, + tree.left.right, + tree.right.left, + tree.right.right, + tree.right) + ); + + test(expected, tree); + } + + @Test//a b c d e h m n p o i + public void exteriorBinaryTree3() throws Exception { + tree = BinaryTreeUtil.getFigureTenDotOne(); + expected = new LinkedList<>( + Arrays.asList( + tree, + tree.left, + tree.left.left, + tree.left.left.left, + tree.left.left.right, + tree.left.right.right.left, + tree.right.left.right.left.right, + tree.right.left.right.right, + tree.right.right.right, + tree.right.right, + tree.right) + ); + + test(expected, tree); + } + + private void test(List> expected, BinaryTree tree) { + assertEquals(expected,ComputeExterior.exteriorBinaryTree(tree)); + } + +} \ No newline at end of file diff --git a/binarytrees/src/test/java/TreeToLinkedListTest.java b/binarytrees/src/test/java/TreeToLinkedListTest.java index b2bb5c5..0e51118 100644 --- a/binarytrees/src/test/java/TreeToLinkedListTest.java +++ b/binarytrees/src/test/java/TreeToLinkedListTest.java @@ -14,7 +14,11 @@ public class TreeToLinkedListTest { @Test public void createListOfLeaves1() throws Exception { tree = BinaryTreeUtil.getEvenTree(); - expected = new LinkedList<>(Arrays.asList(tree.left, tree.right)); + expected = new LinkedList<>( + Arrays.asList( + tree.left, + tree.right) + ); test(expected, tree); } From 7a3a457af05c7e11e70ef1467c057f7bc4710b16 Mon Sep 17 00:00:00 2001 From: gardncl Date: Thu, 27 Apr 2017 21:33:51 -0400 Subject: [PATCH 14/15] 10.16 --- .../main/java/ComputeRightSiblingTree.java | 2 +- .../java/ComputeRightSiblingTreeTest.java | 58 +++++++++++++++++++ .../src/main/java/BinaryTreeLN.java | 20 +++++++ 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 binarytrees/src/test/java/ComputeRightSiblingTreeTest.java create mode 100644 datastructures/src/main/java/BinaryTreeLN.java diff --git a/binarytrees/src/main/java/ComputeRightSiblingTree.java b/binarytrees/src/main/java/ComputeRightSiblingTree.java index e25e810..3f92c71 100644 --- a/binarytrees/src/main/java/ComputeRightSiblingTree.java +++ b/binarytrees/src/main/java/ComputeRightSiblingTree.java @@ -8,7 +8,7 @@ public class ComputeRightSiblingTree { node on its right, if one exists. */ - public static void constructRightSibling(BinaryTree tree) { + public static void constructRightSibling(BinaryTreeLN tree) { } } diff --git a/binarytrees/src/test/java/ComputeRightSiblingTreeTest.java b/binarytrees/src/test/java/ComputeRightSiblingTreeTest.java new file mode 100644 index 0000000..5cfc518 --- /dev/null +++ b/binarytrees/src/test/java/ComputeRightSiblingTreeTest.java @@ -0,0 +1,58 @@ +import org.junit.Test; + +import static org.junit.Assert.*; + +public class ComputeRightSiblingTreeTest { + + private BinaryTreeLN expected; + private BinaryTreeLN tree; + + @Test + public void constructRightSibling1() throws Exception { + expected = new BinaryTreeLN<>('A'); + expected.left = new BinaryTreeLN<>('B'); + expected.right = new BinaryTreeLN<>('C'); + ((BinaryTreeLN)expected.left).levelNext = (BinaryTreeLN)expected.right; + + tree = new BinaryTreeLN<>('A'); + tree.left = new BinaryTreeLN<>('B'); + tree.right = new BinaryTreeLN<>('C'); + + test(expected, tree); + } + + @Test + public void constructRightSibling2() throws Exception { + expected = new BinaryTreeLN<>('A'); + expected.left = new BinaryTreeLN<>('B'); + expected.right = new BinaryTreeLN<>('D'); + expected.left.left = new BinaryTreeLN<>('C'); + expected.right.left = new BinaryTreeLN<>('E'); + expected.right.left.left = new BinaryTreeLN<>('F'); + expected.right.right = new BinaryTreeLN<>('G'); + expected.right.right.left = new BinaryTreeLN<>('H'); + expected.right.right.right = new BinaryTreeLN<>('I'); + ((BinaryTreeLN)expected.left).levelNext = (BinaryTreeLN)expected.right; + ((BinaryTreeLN) expected.left.left).levelNext = (BinaryTreeLN)expected.right.left; + ((BinaryTreeLN)expected.right.left).levelNext = (BinaryTreeLN)expected.right.right; + ((BinaryTreeLN)expected.right.left.left).levelNext = (BinaryTreeLN) expected.right.right.left; + ((BinaryTreeLN)expected.right.right.left).levelNext = (BinaryTreeLN) expected.right.right.right; + + tree = new BinaryTreeLN<>('A'); + tree.left = new BinaryTreeLN<>('B'); + tree.left.left = new BinaryTreeLN<>('C'); + tree.right.left = new BinaryTreeLN<>('E'); + tree.right.left.left = new BinaryTreeLN<>('F'); + tree.right.right = new BinaryTreeLN<>('G'); + tree.right.right.left = new BinaryTreeLN<>('H'); + tree.right.right.right = new BinaryTreeLN<>('I'); + + test(expected, tree); + } + + public void test(BinaryTreeLN expected, BinaryTreeLN tree) { + ComputeRightSiblingTree.constructRightSibling(tree); + assertEquals(expected, tree); + } + +} \ No newline at end of file diff --git a/datastructures/src/main/java/BinaryTreeLN.java b/datastructures/src/main/java/BinaryTreeLN.java new file mode 100644 index 0000000..21807dd --- /dev/null +++ b/datastructures/src/main/java/BinaryTreeLN.java @@ -0,0 +1,20 @@ +public class BinaryTreeLN extends BinaryTree { + + public BinaryTreeLN levelNext; + + public BinaryTreeLN(T data) { + super(data); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + + BinaryTreeLN that = (BinaryTreeLN) o; + + return levelNext != null ? levelNext.equals(that.levelNext) : that.levelNext == null; + } + +} From 644572a2d10929b0659c68233a9f9136364d4dd6 Mon Sep 17 00:00:00 2001 From: gardncl Date: Thu, 27 Apr 2017 21:52:37 -0400 Subject: [PATCH 15/15] 10.17. all tests for chapter 10 done --- .../src/main/java/LockingBinaryTree.java | 18 +++++++-- .../src/test/java/LockingBinaryTreeTest.java | 37 +++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 binarytrees/src/test/java/LockingBinaryTreeTest.java diff --git a/binarytrees/src/main/java/LockingBinaryTree.java b/binarytrees/src/main/java/LockingBinaryTree.java index 08ba3f1..5aa8196 100644 --- a/binarytrees/src/main/java/LockingBinaryTree.java +++ b/binarytrees/src/main/java/LockingBinaryTree.java @@ -1,4 +1,4 @@ -public class LockingBinaryTree extends BinaryTree{ +public class LockingBinaryTree extends BinaryTree { /* 10.17 @@ -13,8 +13,20 @@ public class LockingBinaryTree extends BinaryTree{ constructs such as mutexes or synchronization. */ - - public LockingBinaryTree(Object data) { + public LockingBinaryTree(Integer data) { super(data); } + + public boolean isLocked() { + return false; + } + + public boolean lock() { + return false; + } + + public void unlock() { + + } + } diff --git a/binarytrees/src/test/java/LockingBinaryTreeTest.java b/binarytrees/src/test/java/LockingBinaryTreeTest.java new file mode 100644 index 0000000..78c4092 --- /dev/null +++ b/binarytrees/src/test/java/LockingBinaryTreeTest.java @@ -0,0 +1,37 @@ +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class LockingBinaryTreeTest { + + @Test + public void lockingBinaryTree() { + LockingBinaryTree lockingBinaryTree = new LockingBinaryTree(0); + lockingBinaryTree.left = new LockingBinaryTree(1); + lockingBinaryTree.right = new LockingBinaryTree(2); + lockingBinaryTree.left.left = new LockingBinaryTree(3); + + assertFalse(((LockingBinaryTree)lockingBinaryTree.left).isLocked()); + ((LockingBinaryTree)lockingBinaryTree.left).lock(); + assertTrue(((LockingBinaryTree)lockingBinaryTree.left).isLocked()); + ((LockingBinaryTree)lockingBinaryTree.left).unlock(); + + assertFalse(((LockingBinaryTree)lockingBinaryTree.left).isLocked()); + lockingBinaryTree.lock(); + assertTrue(((LockingBinaryTree)lockingBinaryTree.left).isLocked()); + lockingBinaryTree.unlock(); + + assertFalse(((LockingBinaryTree)lockingBinaryTree.left).isLocked()); + ((LockingBinaryTree)lockingBinaryTree.left.left).lock(); + assertTrue(((LockingBinaryTree)lockingBinaryTree.left).isLocked()); + ((LockingBinaryTree)lockingBinaryTree.left.left).unlock(); + + + assertFalse(((LockingBinaryTree)lockingBinaryTree.left).isLocked()); + ((LockingBinaryTree)lockingBinaryTree.right).lock(); + assertFalse(((LockingBinaryTree)lockingBinaryTree.left).isLocked()); + ((LockingBinaryTree)lockingBinaryTree.right).unlock(); + } + +} \ No newline at end of file