|
1 | 1 | package chapter4;
|
2 | 2 |
|
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.LinkedList; |
| 5 | + |
3 | 6 | public class Chapter4 {
|
4 | 7 | public static void main(String [] args){
|
5 |
| - //4.1 - No need to test. I just compared my code to the book |
6 |
| - //4.2 - Would have to implemented a Directed Graph, and proper Nodes, to test. |
| 8 | + test_DFS(); |
| 9 | + test_BFS(); |
| 10 | + test_PrintPreOrder(); |
| 11 | + test_PrintInOrder(); |
| 12 | + test_PrintPostOrder(); |
| 13 | + test_PrintLevelOrder(); |
| 14 | + test_FourPoint1(); |
| 15 | + test_FourPoint2(); |
| 16 | + test_FourPoint3(); |
| 17 | + test_FourPoint4(); |
| 18 | + test_FourPoint5(); |
| 19 | + test_FourPoint6(); |
| 20 | + test_FourPoint7(); |
| 21 | + test_FourPoint8(); |
| 22 | + test_FourPoint9(); |
| 23 | + } |
| 24 | + |
| 25 | + public static void test_DFS(){ |
| 26 | + GraphNode graphNode = GraphFunctions.createGraph(); |
| 27 | + System.out.println("\n*** Test DFS"); |
| 28 | + GraphFunctions.DFS(graphNode, 6); // searches for value 6 |
| 29 | + } |
| 30 | + |
| 31 | + public static void test_BFS(){ |
| 32 | + GraphNode graphNode = GraphFunctions.createGraph(); |
| 33 | + System.out.println("\n*** Test BFS"); |
| 34 | + GraphFunctions.DFS(graphNode, 6); // searches for value 6 |
| 35 | + } |
7 | 36 |
|
8 |
| - /*** 4.3 ***/ |
9 |
| - System.out.println("*** Test 4.3: BST from sorted array"); |
| 37 | + public static void test_PrintPreOrder(){ |
| 38 | + TreeNode tree = TreeFunctions.createBST(); |
| 39 | + System.out.println("\n*** Test print (pre-order)"); |
| 40 | + TreeFunctions.printPreOrder(tree); |
| 41 | + } |
| 42 | + |
| 43 | + public static void test_PrintInOrder(){ |
| 44 | + TreeNode tree = TreeFunctions.createBST(); |
| 45 | + System.out.println("\n\n*** Test print (in-order)"); |
| 46 | + TreeFunctions.printInOrder(tree); |
| 47 | + } |
| 48 | + |
| 49 | + public static void test_PrintPostOrder(){ |
| 50 | + TreeNode tree = TreeFunctions.createBST(); |
| 51 | + System.out.println("\n\n*** Test print (post-order)"); |
| 52 | + TreeFunctions.printPostOrder(tree); |
| 53 | + } |
| 54 | + |
| 55 | + public static void test_PrintLevelOrder(){ |
| 56 | + TreeNode tree = TreeFunctions.createBST(); |
| 57 | + System.out.println("\n\n*** Test print (level-order)"); |
| 58 | + TreeFunctions.printLevelOrder(tree); |
| 59 | + } |
| 60 | + |
| 61 | + public static void test_FourPoint1(){ |
| 62 | + TreeNode tree = TreeFunctions.createBST(); |
| 63 | + System.out.println("\n\n*** Test 4.1: (Balanced Trees)"); |
| 64 | + System.out.println("(Solution 1) Is balanced? " + FourPoint1.isBalanced(tree)); |
| 65 | + System.out.println("(Solution 2) Is balanced? " + FourPoint1.isBalanced_2(tree)); |
| 66 | + } |
| 67 | + |
| 68 | + /* Testing is not thorough, I just do a simple test */ |
| 69 | + public static void test_FourPoint2(){ |
| 70 | + System.out.println("\n*** Test 4.2: Route exists"); |
| 71 | + GraphNode node1 = new GraphNode(1); |
| 72 | + GraphNode node2 = new GraphNode(2); |
| 73 | + node1.addNeighbor(node2); |
| 74 | + System.out.println("Route exists? (should be true): " + FourPoint2.routeExists(node1, node2)); |
| 75 | + } |
| 76 | + |
| 77 | + public static void test_FourPoint3(){ |
| 78 | + System.out.println("\n*** Test 4.3: BST from sorted array"); |
10 | 79 | int [] sortedArray = {1, 2, 3, 4, 5, 6, 7};
|
11 | 80 | TreeNode BST = FourPoint3.createBST(sortedArray);
|
12 | 81 | System.out.print("In-Order print of tree: ");
|
13 |
| - FourPoint3.inOrderPrint(BST); |
14 |
| - |
15 |
| - //4.4 - My solution is same as book code. Too lazy to test. |
16 |
| - //4.5 - My solution is same as book code. Too lazy to test. |
17 |
| - //4.6 - My solution is same as book code. Too lazy to test. |
18 |
| - //4.7 - My solution is same as book code. Too lazy to test. |
19 |
| - //4.8 - My solution is same as book code. Too lazy to test. |
20 |
| - |
21 |
| - /*** 4.9 ***/ |
22 |
| - System.out.print("\n\n*** Test 4.9: Tree paths summing to value"); |
23 |
| - TreeNode n = new TreeNode(4); |
24 |
| - n.left = new TreeNode(1); |
25 |
| - n.left.left = new TreeNode(0); |
26 |
| - n.right = new TreeNode(-1); |
27 |
| - n.right.left = new TreeNode(2); |
28 |
| - n.right.right = new TreeNode(4); |
29 |
| - |
30 |
| - FourPoint9.findSum(n, 5); |
31 |
| - |
| 82 | + TreeFunctions.printInOrder(BST); |
| 83 | + } |
| 84 | + |
| 85 | + public static void test_FourPoint4(){ |
| 86 | + TreeNode tree = TreeFunctions.createBST(); |
| 87 | + System.out.println("\n\n*** Test 4.4: create lists by level"); |
| 88 | + ArrayList<LinkedList<TreeNode>> lists = FourPoint4.createLinkedLists(tree); |
| 89 | + for (LinkedList<TreeNode> list : lists){ |
| 90 | + System.out.print("\nNew level "); |
| 91 | + for (TreeNode node : list){ |
| 92 | + System.out.print(node); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public static void test_FourPoint5(){ |
| 98 | + TreeNode tree1 = TreeFunctions.createTree(); |
| 99 | + TreeNode tree2 = TreeFunctions.createBST(); |
| 100 | + System.out.println("\n\n*** Test 4.5: is BST?"); |
| 101 | + System.out.println("Should be false: " + FourPoint5.isBST(tree1)); |
| 102 | + System.out.println("Should be true: " + FourPoint5.isBST(tree2)); |
| 103 | + } |
| 104 | + |
| 105 | + public static void test_FourPoint6(){ |
| 106 | + TreeNode tree = TreeFunctions.createBST(); |
| 107 | + System.out.println("\n*** Test 4.6: Find inOrderSucc"); |
| 108 | + System.out.println("Should be 6: " + FourPoint6.inOrderSucc(tree)); |
| 109 | + System.out.println("Should be 5: " + FourPoint6.inOrderSucc(tree.left)); |
| 110 | + System.out.println("Should be null: " + FourPoint6.inOrderSucc(tree.right.right)); |
| 111 | + } |
| 112 | + |
| 113 | + public static void test_FourPoint7(){ |
| 114 | + TreeNode tree = TreeFunctions.createBST(); |
| 115 | + System.out.println("\n*** Test 4.7: Common ancestor"); |
| 116 | + System.out.println("Common ancestor should be 5: " + FourPoint7.commonAncestor(tree, tree.left, tree.right.right)); |
| 117 | + System.out.println("Common ancestor should be 8: " + FourPoint7.commonAncestor(tree, tree.right, tree.right.left)); |
| 118 | + } |
| 119 | + |
| 120 | + public static void test_FourPoint8(){ |
| 121 | + TreeNode tree1 = TreeFunctions.createTree(); |
| 122 | + TreeNode tree2 = TreeFunctions.createBST(); |
| 123 | + System.out.println("\n*** Test 4.8: subTree"); |
| 124 | + System.out.println("Should be false: " + FourPoint8.containsTree(tree1, tree2)); |
| 125 | + } |
| 126 | + |
| 127 | + public static void test_FourPoint9(){ |
| 128 | + System.out.print("\n*** Test 4.9: Tree paths summing to value"); |
| 129 | + TreeNode tree = TreeFunctions.createBST(); |
| 130 | + FourPoint9.findSum(tree, 6); |
32 | 131 | System.out.print("\n\nSolution version 2");
|
33 |
| - FourPoint9.findSum2(n, 5); |
34 |
| - |
35 |
| - |
| 132 | + FourPoint9.findSum2(tree, 6); |
36 | 133 | }
|
37 | 134 | }
|
0 commit comments