diff --git a/Code Practice/.classpath b/Code Practice/.classpath deleted file mode 100644 index 28e2b79..0000000 --- a/Code Practice/.classpath +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - diff --git a/Code Practice/.gitignore b/Code Practice/.gitignore deleted file mode 100644 index 5e56e04..0000000 --- a/Code Practice/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/bin diff --git a/Code Practice/.project b/Code Practice/.project deleted file mode 100644 index 71f5873..0000000 --- a/Code Practice/.project +++ /dev/null @@ -1,17 +0,0 @@ - - - my-java-code-samples - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - diff --git a/Code Practice/.settings/org.eclipse.jdt.core.prefs b/Code Practice/.settings/org.eclipse.jdt.core.prefs deleted file mode 100644 index 416f4fb..0000000 --- a/Code Practice/.settings/org.eclipse.jdt.core.prefs +++ /dev/null @@ -1,11 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 -org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=1.5 -org.eclipse.jdt.core.compiler.debug.lineNumber=generate -org.eclipse.jdt.core.compiler.debug.localVariable=generate -org.eclipse.jdt.core.compiler.debug.sourceFile=generate -org.eclipse.jdt.core.compiler.problem.assertIdentifier=error -org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.source=1.5 diff --git a/Code Practice/src/solutions/LinkedList.java b/Code Practice/src/solutions/LinkedList.java deleted file mode 100644 index 9fc7fd2..0000000 --- a/Code Practice/src/solutions/LinkedList.java +++ /dev/null @@ -1,89 +0,0 @@ -package solutions; - -import structures.LLNode; - -public class LinkedList { - - //ctci:P2.1 - public static LLNode removeDuplicates(LLNode head){ - LLNode n = head; - int ndata; - while (n.getNext() != null) { - ndata=n.getData(); - n.setNext(LLNode.deleteNode(n.getNext(),ndata)); - n = n.getNext(); - if (n == null){return head;} - } - return head; - } - - - //ctci:P2.2 - public static LLNode findNthLastElement(LLNode head, int nth){ - LLNode n = head; - int nf=0; - while (n.getNext() != null) { - n = n.getNext(); - nf++; - } - - nth=nf-nth; - if (nth <= 0){return head;} - n = head; - for (int i=0; i + 4.0.0 + com.example + my-java-code-samples + 1.0-SNAPSHOT + + + junit + junit + 4.13.2 + test + + + diff --git a/Code Practice/src/solutions/ArraysnStrings.java b/src/main/java/solutions/ArraysnStrings.java similarity index 70% rename from Code Practice/src/solutions/ArraysnStrings.java rename to src/main/java/solutions/ArraysnStrings.java index 4b4ff60..e1feff1 100644 --- a/Code Practice/src/solutions/ArraysnStrings.java +++ b/src/main/java/solutions/ArraysnStrings.java @@ -9,58 +9,62 @@ public class ArraysnStrings { - - //ctci:P1.1 - public static Boolean isUnique(String input){ - Set seenCharacters = new HashSet(); - for (char ch: input.toCharArray()){ - if (seenCharacters.contains(ch)){ - return false; - } - seenCharacters.add(ch); - } - return true; - } - - //ctci:P1.1 - public static Boolean isUniqueWOAddStructs(String input){ - int slen = input.length(); - for (int i=0; i < slen-1; i++ ){ - for (int j=i+1; j < slen; j++){ - if (input.charAt(i) == input.charAt(j)){ - return false; - } - } - } - return true; - } - - //ctci:P1.2 - public static String reverseString(String input){ - char[] inputCharA = input.toCharArray(); - int length = input.length(); - char[] outputCharA = new char[length]; - int j; - for (int i= 0; i < length; i++){ - j=length-1-i; - outputCharA[i]=inputCharA[j]; - } - return String.valueOf(outputCharA); - } - - //helper removeDuplicates - static void moveOneBack(char[] str, int i){ - char cc=str[i]; - while (cc != '\0'){ - str[i]=str[i+1]; - i=i+1; - cc=str[i]; - } - } - - //ctci:P1.3 - public static void removeDuplicates(char[] str) { - int i=0; + + //ctci:P1.1 + public static Boolean isUnique(String input){ + if(input == null || input.isEmpty()){ return true;} + + Set seenCharacters = new HashSet(); + for (char ch: input.toCharArray()){ + if (seenCharacters.contains(ch)){ + return false; + } + seenCharacters.add(ch); + } + return true; + } + + //ctci:P1.1 + public static Boolean isUniqueWOAddStructs(String input){ + if(input == null || input.isEmpty()){ return true;} + + int slen = input.length(); + for (int i=0; i < slen-1; i++ ){ + for (int j=i+1; j < slen; j++){ + if (input.charAt(i) == input.charAt(j)){ + return false; + } + } + } + return true; + } + + //ctci:P1.2 + public static String reverseString(String input){ + char[] inputCharA = input.toCharArray(); + int length = input.length(); + char[] outputCharA = new char[length]; + int j; + for (int i= 0; i < length; i++){ + j=length-1-i; + outputCharA[i]=inputCharA[j]; + } + return String.valueOf(outputCharA); + } + + //helper removeDuplicates + static void moveOneBack(char[] str, int i){ + char cc=str[i]; + while (cc != '\0'){ + str[i]=str[i+1]; + i=i+1; + cc=str[i]; + } + } + + //ctci:P1.3 + public static void removeDuplicates(char[] str) { + int i=0; int j; while(str[i]!= '\0'){ j=i+1; @@ -74,32 +78,34 @@ public static void removeDuplicates(char[] str) { i++; } } - - //ctci:P1.4 - public static boolean isAnagram(String str1, String str2){ - Map let2char = new HashMap(); //letter to character - //counts letters in str1 - for (char c: str1.toCharArray()){ + //ctci:P1.4 + public static boolean isAnagram(String str1, String str2){ + Map let2char = new HashMap(); //letter to character + + // counts letters in str1 + for (char c: str1.toCharArray()){ if ( let2char.containsKey(c)){ let2char.put(c, let2char.get(c) + 1); - } - else{ + }else{ let2char.put(c, 1); } } - int ccount; + int ccount; for (char c: str2.toCharArray()){ if ( let2char.containsKey(c)){ ccount=let2char.get(c); - if (ccount -1 == 0){let2char.remove(c);} - else {let2char.put(c, ccount-1);} - } - else{ return false;} - } - if (let2char.size() == 0){ return true;} - return false; + if (ccount -1 == 0){ + let2char.remove(c); + }else { + let2char.put(c, ccount-1); + } + }else{ + return false; + } + } + return let2char.isEmpty(); } //leetcode Palindrome Pairs @@ -218,5 +224,4 @@ private LinkedList> allPalindromesForEmpty(String[] words, int myi } return sol; } - } diff --git a/Code Practice/src/solutions/BitManipulation.java b/src/main/java/solutions/BitManipulation.java similarity index 100% rename from Code Practice/src/solutions/BitManipulation.java rename to src/main/java/solutions/BitManipulation.java diff --git a/Code Practice/src/solutions/HackerRank.java b/src/main/java/solutions/HackerRank.java similarity index 100% rename from Code Practice/src/solutions/HackerRank.java rename to src/main/java/solutions/HackerRank.java diff --git a/src/main/java/solutions/LinkedList.java b/src/main/java/solutions/LinkedList.java new file mode 100644 index 0000000..29996b4 --- /dev/null +++ b/src/main/java/solutions/LinkedList.java @@ -0,0 +1,88 @@ +package solutions; + +import structures.LinkedListNode; + +public class LinkedList { + + //ctci:P2.1 + public static LinkedListNode removeDuplicates(LinkedListNode head){ + LinkedListNode n = head; + int ndata; + while (n.next != null) { + ndata=n.data; + n.next=LinkedListNode.deleteNode(n.next,ndata); + n = n.next; + if (n == null){return head;} + } + return head; + } + + //ctci:P2.2 + public static LinkedListNode findNthLastElement(LinkedListNode head, int nth){ + LinkedListNode n = head; + int nf=0; + while (n.next != null) { + n = n.next; + nf++; + } + + nth=nf-nth; + if (nth <= 0){return head;} + n = head; + for (int i=0; i= 0; + } + + private static int isBalancedRecursive(BinaryTreeNode root) { + if(root == null) { + return 0; + } + + if(root.left == null && root.right == null) { //leaf + return 1; + } + + if(root.left == null) { + if(isBalancedRecursive(root.right) == 1) { //right is leaf + return 2; + } + return -1; + } + + if(root.right == null) { + if(isBalancedRecursive(root.left) == 1) { //left is leaf + return 2; + } + return -1; + } + int left = isBalancedRecursive(root.left); + int right = isBalancedRecursive(root.right); + if(left == -1 || right == -1 || Integer.max(left-right, right-left)>1) { + return -1; + } + return 1+Integer.max(left, right); + } + + //ctci:P4.3 + public static BinaryTreeNode makeTree(int[] num){ + + if(num == null || num.length == 0){ + return null; + } + + return makeTreeRecursive(num, 0, num.length); + } + + //s inclusive, e exclusive + private static BinaryTreeNode makeTreeRecursive(int[] num, int s, int e){ + if(e-s == 0){ + return null; + }else if (e-s == 1){ + return new BinaryTreeNode(num[s]); + } + + BinaryTreeNode r; + + if(e-s == 2){ + if(random.nextInt(2)==1){ + r=new BinaryTreeNode(num[s]); + r.right=new BinaryTreeNode(num[s+1]); + }else{ + r=new BinaryTreeNode(num[s+1]); + r.left=new BinaryTreeNode(num[s]); + } + }else{ + int mid=((e-s)/2)+s; + r=new BinaryTreeNode(num[mid]); + r.left=makeTreeRecursive(num, s, mid); + r.right=makeTreeRecursive(num, mid+1, e); + } + + return r; + + } + + //ctci:4.4 + public static LinkedListNode[] linkedListPerDepth(BinaryTreeNode bst){ + if(bst == null) { + return new LinkedListNode[0]; + } + ArrayList arr=new ArrayList(); + arr = linkedListPerDepthRecursive(bst, arr, 1); + + LinkedListNode[] sol = new LinkedListNode[arr.size()]; + return arr.toArray(sol); + } + + private static ArrayList linkedListPerDepthRecursive( + BinaryTreeNode bst, ArrayList arr, int d){ + if (bst == null) { + return arr; + } + + if(arr.size() < d) { + arr.add(new LinkedListNode(bst.val)); + }else{ + arr.get(d-1).appendToTail(bst.val); + } + + if(bst.left != null) { + arr = linkedListPerDepthRecursive(bst.left, arr, d+1); + } + + if(bst.right != null) { + arr = linkedListPerDepthRecursive(bst.right, arr, d+1); + } + + return arr; + } + + // p4.6 Design an algorithm and write code to find the first common ancestor of two nodes + // in a binary tree. Avoid storing additional nodes in a data structure. NOTE: This is not + // necessarily a binary search tree. + + //preorder-traversal (left and right first) compare lists + + +} diff --git a/src/main/java/structures/BinaryTreeNode.java b/src/main/java/structures/BinaryTreeNode.java new file mode 100644 index 0000000..6e8f58e --- /dev/null +++ b/src/main/java/structures/BinaryTreeNode.java @@ -0,0 +1,48 @@ +package structures; + +import java.util.List; + +public class BinaryTreeNode { + public int val; + public Object data; + public BinaryTreeNode left; + public BinaryTreeNode right; + + public BinaryTreeNode(int x){ + val=x; + left=null; + right=null; + } + + public BinaryTreeNode(int x, Object input){ + val=x; + left=null; + right=null; + data=input; + } + + public BinaryTreeNode(int index, Object input, int size, List others){ // ? + val=index; + left=null; + right=null; + if (size == 0) { + return; + }else if (size == 1) { + data = input; + return; + }else if (size == 2) {// 2 node tree + left= new BinaryTreeNode(0, others.get(0)); + data = others.get(1); + val=1; + return; + }else if (size == 3) {// 3 node tree + left= new BinaryTreeNode(0, others.get(0)); + data = others.get(1); + val=1; + right= new BinaryTreeNode(2, others.get(2)); + return; + } + data=input; + return; + } +} diff --git a/Code Practice/src/structures/LLNode.java b/src/main/java/structures/LinkedListNode.java similarity index 50% rename from Code Practice/src/structures/LLNode.java rename to src/main/java/structures/LinkedListNode.java index a8dbb53..bbaf48a 100644 --- a/Code Practice/src/structures/LLNode.java +++ b/src/main/java/structures/LinkedListNode.java @@ -1,31 +1,35 @@ package structures; //Linked list node -public class LLNode { - LLNode next = null; - int data; +public class LinkedListNode { + public LinkedListNode next = null; + public int data; - public LLNode(int d) { data = d; } + public LinkedListNode(int d) { data = d; } - public static LLNode linkedListFromArray(int[] da) { - LLNode head = new LLNode(da[0]); - LLNode n=head; + public static LinkedListNode linkedListFromArray(int[] da) { + if (da == null || da.length == 0) { + return null; + } + + LinkedListNode head = new LinkedListNode(da[0]); + LinkedListNode n=head; for(int i=1; i < da.length;i++){ - n.next=new LLNode(da[i]); + n.next=new LinkedListNode(da[i]); n=n.next; } return head; } public void appendToTail(int d) { - LLNode end = new LLNode(d); - LLNode n = this; + LinkedListNode end = new LinkedListNode(d); + LinkedListNode n = this; while (n.next != null) { n = n.next; } n.next = end; } - public static LLNode deleteNode(LLNode head, int d) { - LLNode n = head; + public static LinkedListNode deleteNode(LinkedListNode head, int d) { + LinkedListNode n = head; if (n.data == d) { return head.next; /* moved head */ } @@ -36,13 +40,13 @@ public static LLNode deleteNode(LLNode head, int d) { } n = n.next; } - return head; /* head didn�t change */ + return head; /* head didn't change */ } public void printLinkedList(){ System.out.print("LL: "); System.out.print(this.data); - LLNode n = this; + LinkedListNode n = this; while (n.next != null) { n = n.next; System.out.print("->"); @@ -50,20 +54,4 @@ public void printLinkedList(){ } System.out.println("->END"); } - - public int getData(){ - return this.data; - } - - public void setData(int data2) { - this.data=data2; - } - - public LLNode getNext() { - return this.next; - } - - public void setNext(LLNode next2) { - this.next=next2; - } } diff --git a/Code Practice/src/structures/LocalQueue.java b/src/main/java/structures/LocalQueue.java similarity index 50% rename from Code Practice/src/structures/LocalQueue.java rename to src/main/java/structures/LocalQueue.java index cc2cda2..20418d4 100644 --- a/Code Practice/src/structures/LocalQueue.java +++ b/src/main/java/structures/LocalQueue.java @@ -1,21 +1,21 @@ package structures; public class LocalQueue { - LLNode first, last; + LinkedListNode first, last; void enqueue(int item){ if (first == null){ - last = new LLNode(item); + last = new LinkedListNode(item); first=last; }else{ - last.setNext(new LLNode(item)); + last.next =new LinkedListNode(item); last=last.next; } } - LLNode dequeue(LLNode n){ + LinkedListNode dequeue(LinkedListNode n){ if (first != null){ - LLNode item=first; - first = first.getNext(); + LinkedListNode item=first; + first = first.next; return item; } return null; diff --git a/Code Practice/src/structures/LocalStack.java b/src/main/java/structures/LocalStack.java similarity index 57% rename from Code Practice/src/structures/LocalStack.java rename to src/main/java/structures/LocalStack.java index a5aba26..786693d 100644 --- a/Code Practice/src/structures/LocalStack.java +++ b/src/main/java/structures/LocalStack.java @@ -1,21 +1,21 @@ package structures; public class LocalStack { - LLNode top; + LinkedListNode top; public LocalStack() {} - LLNode pop(){ + LinkedListNode pop(){ if (top != null){ - LLNode item=top; - top=top.getNext(); + LinkedListNode item=top; + top=top.next; return item; } return null; } void push(int item){ - LLNode t = new LLNode(item); + LinkedListNode t = new LinkedListNode(item); t.next = top; top=t; } diff --git a/Code Practice/src/structures/MinStack.java b/src/main/java/structures/MinStack.java similarity index 63% rename from Code Practice/src/structures/MinStack.java rename to src/main/java/structures/MinStack.java index af9272f..e324ca5 100644 --- a/Code Practice/src/structures/MinStack.java +++ b/src/main/java/structures/MinStack.java @@ -2,12 +2,12 @@ //P3.2 public class MinStack extends LocalStack{ - LLNode min; + LinkedListNode min; int minData; @Override - LLNode pop(){ - LLNode toReturn=super.pop(); + LinkedListNode pop(){ + LinkedListNode toReturn=super.pop(); if (toReturn == min){ updateMin(); } @@ -16,16 +16,16 @@ LLNode pop(){ private void updateMin() { if (this.top == null){ - min=null; + min = null; }else{ - min=this.top; - minData=this.top.getData(); - LLNode n = this.top; + min = this.top; + minData = this.top.data; + LinkedListNode n = this.top; while(n.next != null){ n=n.next; - if (n.getData() < minData){ + if (n.data < minData){ min=n; - minData=n.getData(); + minData=n.data; } } } @@ -37,14 +37,13 @@ void push(int item){ if (min == null){ super.push(item); min=this.top; - minData=this.top.getData(); + minData=this.top.data; }else{ if (minData > item){ super.push(item); min=this.top; - minData=this.top.getData(); + minData=this.top.data; } } } - } diff --git a/Code Practice/src/structures/MyQueue.java b/src/main/java/structures/MyQueue.java similarity index 100% rename from Code Practice/src/structures/MyQueue.java rename to src/main/java/structures/MyQueue.java diff --git a/Code Practice/src/structures/RandomSet.java b/src/main/java/structures/RandomSet.java similarity index 100% rename from Code Practice/src/structures/RandomSet.java rename to src/main/java/structures/RandomSet.java diff --git a/Code Practice/src/structures/StackOfPlates.java b/src/main/java/structures/StackOfPlates.java similarity index 86% rename from Code Practice/src/structures/StackOfPlates.java rename to src/main/java/structures/StackOfPlates.java index afa5c65..8eb956f 100644 --- a/Code Practice/src/structures/StackOfPlates.java +++ b/src/main/java/structures/StackOfPlates.java @@ -12,18 +12,18 @@ public class StackOfPlates extends LocalStack{ int currentStackSize; @Override - LLNode pop(){ + LinkedListNode pop(){ if (currentStack != null){ if (currentStack.top != null){ - LLNode item=currentStack.top; - currentStack.top=currentStack.top.getNext(); + LinkedListNode item=currentStack.top; + currentStack.top=currentStack.top.next; currentStackSize--; return item; }else if (currentStackIndex != 0){ currentStackIndex--; currentStack= toStack.get(currentStackIndex); - LLNode item=currentStack.top; - currentStack.top=currentStack.top.getNext(); + LinkedListNode item=currentStack.top; + currentStack.top=currentStack.top.next; currentStackSize=maxElements-1; return item; } diff --git a/Code Practice/src/testSuite/TestArraysnStrings.java b/src/test/java/TestArraysnStrings.java similarity index 80% rename from Code Practice/src/testSuite/TestArraysnStrings.java rename to src/test/java/TestArraysnStrings.java index 66a8660..85081cc 100644 --- a/Code Practice/src/testSuite/TestArraysnStrings.java +++ b/src/test/java/TestArraysnStrings.java @@ -1,5 +1,3 @@ -package testSuite; - import static org.junit.Assert.*; import org.junit.Test; @@ -10,14 +8,14 @@ public class TestArraysnStrings { @Test public void testisUnique() { - assertEquals("'hi' must be true", true, ArraysnStrings.isUnique("hi")); - assertEquals("'hih' must be false", false, ArraysnStrings.isUnique("hih")); + assertTrue("'hi' must be true", ArraysnStrings.isUnique("hi")); + assertFalse("'hih' must be false", ArraysnStrings.isUnique("hih")); } @Test public void testisUniqueWOAddStructs() { - assertEquals("'hi' must be true", true, ArraysnStrings.isUniqueWOAddStructs("hi")); - assertEquals("'hih' must be false", false, ArraysnStrings.isUniqueWOAddStructs("hih")); + assertTrue("'hi' must be true", ArraysnStrings.isUniqueWOAddStructs("hi")); + assertFalse("'hih' must be false", ArraysnStrings.isUniqueWOAddStructs("hih")); } @Test diff --git a/Code Practice/src/testSuite/TestBitManipulation.java b/src/test/java/TestBitManipulation.java similarity index 97% rename from Code Practice/src/testSuite/TestBitManipulation.java rename to src/test/java/TestBitManipulation.java index eee1957..e2b4c67 100644 --- a/Code Practice/src/testSuite/TestBitManipulation.java +++ b/src/test/java/TestBitManipulation.java @@ -1,5 +1,3 @@ -package testSuite; - import static org.junit.Assert.*; import org.junit.Test; diff --git a/Code Practice/src/testSuite/TestLinkedList.java b/src/test/java/TestLinkedList.java similarity index 51% rename from Code Practice/src/testSuite/TestLinkedList.java rename to src/test/java/TestLinkedList.java index 7b63497..c507539 100644 --- a/Code Practice/src/testSuite/TestLinkedList.java +++ b/src/test/java/TestLinkedList.java @@ -1,30 +1,31 @@ -package testSuite; - import static org.junit.Assert.*; import org.junit.Test; import solutions.LinkedList; -import structures.LLNode; +import structures.LinkedListNode; public class TestLinkedList { @Test public void testNode() { - int[] lLIA={0,0,1,1,2,2,3,3,4,4,5}; - LLNode lL=LLNode.linkedListFromArray(lLIA); + int[] lla={0,0,1,1,2,2,3,3,4,4,5}; + LinkedListNode lL=LinkedListNode.linkedListFromArray(lla); lL.printLinkedList(); + int[] lla1={}; + LinkedListNode lL1=LinkedListNode.linkedListFromArray(lla1); + assertNull(lL1); } @Test public void testremoveDuplicates() { int[] lLIA={0,0,1,1,2,2,3,3,4,4,5}; - LLNode lL=LLNode.linkedListFromArray(lLIA); + LinkedListNode lL=LinkedListNode.linkedListFromArray(lLIA); //lL.printLinkedList(); lL=LinkedList.removeDuplicates(lL); //lL.printLinkedList(); - int[] lLIA2={0,0,1,1,2,2,3,3,4,4,}; - LLNode lL2=LLNode.linkedListFromArray(lLIA2); + int[] lLIA2={0,0,1,1,2,2,3,3,4,4}; + LinkedListNode lL2=LinkedListNode.linkedListFromArray(lLIA2); //lL2.printLinkedList(); lL2=LinkedList.removeDuplicates(lL2); //lL2.printLinkedList(); @@ -33,17 +34,17 @@ public void testremoveDuplicates() { @Test public void testfindNthLastElement(){ int[] lLIA={0,1,2,3,4,5}; - LLNode lL=LLNode.linkedListFromArray(lLIA); - LLNode thirdlast = LinkedList.findNthLastElement(lL, 3); - assertEquals("3rd last element is 2", 2, thirdlast.getData()); + LinkedListNode lL=LinkedListNode.linkedListFromArray(lLIA); + LinkedListNode thirdlast = LinkedList.findNthLastElement(lL, 3); + assertEquals("3rd last element is 2", 2, thirdlast.data); } @Test public void testdeleteMiddleNode(){ int[] lLIA={0,1,2,3,4,5}; - LLNode lL=LLNode.linkedListFromArray(lLIA); + LinkedListNode lL=LinkedListNode.linkedListFromArray(lLIA); lL.printLinkedList(); - LLNode middle=lL.getNext(); + LinkedListNode middle=lL.next; LinkedList.deleteMiddleNode(middle); lL.printLinkedList();; } @@ -52,12 +53,12 @@ public void testdeleteMiddleNode(){ public void testAddLL(){ System.out.println("p2.4"); int[] lLIA={1,3,5}; - LLNode lL=LLNode.linkedListFromArray(lLIA); + LinkedListNode lL=LinkedListNode.linkedListFromArray(lLIA); lL.printLinkedList(); int[] lLIA2={2,5,5,9}; - LLNode lL2=LLNode.linkedListFromArray(lLIA2); + LinkedListNode lL2=LinkedListNode.linkedListFromArray(lLIA2); lL2.printLinkedList(); - LLNode result=LinkedList.addLL(lL, lL2); + LinkedListNode result=LinkedList.addLL(lL, lL2); result.printLinkedList(); } diff --git a/Code Practice/src/testSuite/TestRandomSet.java b/src/test/java/TestRandomSet.java similarity index 94% rename from Code Practice/src/testSuite/TestRandomSet.java rename to src/test/java/TestRandomSet.java index 3ab716f..daaacb5 100644 --- a/Code Practice/src/testSuite/TestRandomSet.java +++ b/src/test/java/TestRandomSet.java @@ -1,6 +1,3 @@ -package testSuite; - - import org.junit.Test; import structures.RandomSet; diff --git a/Code Practice/src/testSuite/TestRecursive.java b/src/test/java/TestRecursive.java similarity index 97% rename from Code Practice/src/testSuite/TestRecursive.java rename to src/test/java/TestRecursive.java index 5dd53b2..098602f 100644 --- a/Code Practice/src/testSuite/TestRecursive.java +++ b/src/test/java/TestRecursive.java @@ -1,6 +1,3 @@ -package testSuite; - - import java.util.HashSet; import java.util.Iterator; import java.util.Set; diff --git a/Code Practice/src/testSuite/TestSortingAndSearching.java b/src/test/java/TestSortingAndSearching.java similarity index 96% rename from Code Practice/src/testSuite/TestSortingAndSearching.java rename to src/test/java/TestSortingAndSearching.java index 602bdfd..cebb16d 100644 --- a/Code Practice/src/testSuite/TestSortingAndSearching.java +++ b/src/test/java/TestSortingAndSearching.java @@ -1,5 +1,3 @@ -package testSuite; - import org.junit.Assert; import org.junit.Test; diff --git a/Code Practice/src/testSuite/TestStacksAndQueues.java b/src/test/java/TestStacksAndQueues.java similarity index 94% rename from Code Practice/src/testSuite/TestStacksAndQueues.java rename to src/test/java/TestStacksAndQueues.java index cdec8d1..53d0d77 100644 --- a/Code Practice/src/testSuite/TestStacksAndQueues.java +++ b/src/test/java/TestStacksAndQueues.java @@ -1,5 +1,3 @@ -package testSuite; - import static org.junit.Assert.*; import org.junit.Test; diff --git a/Code Practice/src/testSuite/TestTreesAndGraphs.java b/src/test/java/TestTreesAndGraphs.java similarity index 64% rename from Code Practice/src/testSuite/TestTreesAndGraphs.java rename to src/test/java/TestTreesAndGraphs.java index b4b3d3b..200ee4a 100644 --- a/Code Practice/src/testSuite/TestTreesAndGraphs.java +++ b/src/test/java/TestTreesAndGraphs.java @@ -1,4 +1,4 @@ -package testSuite; +import static org.junit.Assert.*; import java.util.ArrayList; import java.util.Collections; @@ -8,14 +8,61 @@ import solutions.TreesAndGraphs; import structures.BinaryTreeNode; +import structures.LinkedListNode; public class TestTreesAndGraphs { - + + @Test + public void testisBalanced() { + BinaryTreeNode unbal = new BinaryTreeNode(5); + unbal.left = new BinaryTreeNode(3); + unbal.left.left = new BinaryTreeNode(2); + unbal.left.right = new BinaryTreeNode(4); + BTreePrinter.printNode(unbal); + + assertFalse("unbal must be false", TreesAndGraphs.isBalanced(unbal)); + + BinaryTreeNode bal = new BinaryTreeNode(5); + bal.left = new BinaryTreeNode(3); + bal.left.left = new BinaryTreeNode(2); + bal.left.right = new BinaryTreeNode(4); + bal.right = new BinaryTreeNode(7); + bal.right.right = new BinaryTreeNode(8); + BTreePrinter.printNode(bal); + assertTrue("bal must be true", TreesAndGraphs.isBalanced(bal)); + } + @Test - public void test() { + public void testmakeTree() { int[] num = {1,2,3,4,5,6,7}; BTreePrinter.printNode(TreesAndGraphs.makeTree(num)); } + + @Test + public void testlinkedListPerDepth() { + BinaryTreeNode bal = new BinaryTreeNode(5); + bal.left = new BinaryTreeNode(3); + bal.left.left = new BinaryTreeNode(2); + bal.left.right = new BinaryTreeNode(4); + bal.right = new BinaryTreeNode(7); + bal.right.right = new BinaryTreeNode(8); + LinkedListNode[] llpd = TreesAndGraphs.linkedListPerDepth(bal); + assertNotNull("llpd not null",llpd); + assertEquals("llpd must have 3 linked lists", 3, llpd.length); + assertNotNull("1st linked list not null",llpd[0]); + assertEquals("1st element in 1st linked list is 5", 5, llpd[0].data); + + assertNotNull("2nd linked list not null",llpd[1]); + LinkedListNode ll = llpd[1]; + boolean isSeven = false; + boolean isThree = false; + while(ll != null) { + if(ll.data == 7) {isSeven = true;} + if(ll.data == 3) {isThree = true;} + ll=ll.next; + } + assertTrue("2nd linked list contains 5 and 7", isSeven && isThree); + } }