From 6938c409f95d85157ffdc716ce3cf4216ca403cf Mon Sep 17 00:00:00 2001 From: Abir Bhushan Date: Mon, 15 Apr 2019 11:14:22 +0100 Subject: [PATCH 1/4] Create DepthFirstSearch.java --- .../java/com/search/DepthFirstSearch.java | 126 ++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 src/main/java/com/search/DepthFirstSearch.java diff --git a/src/main/java/com/search/DepthFirstSearch.java b/src/main/java/com/search/DepthFirstSearch.java new file mode 100644 index 00000000..fe16e421 --- /dev/null +++ b/src/main/java/com/search/DepthFirstSearch.java @@ -0,0 +1,126 @@ +package src.main.java.com.search; + +/** + * Searching is faster in sorted structures. Binary search is O(log n). + * However, the cost of sorting is O(n ยท log n). + * What to do when adding or removing elements? Sort again? No. + * Create efficient data structures to maintain sorted sequences, and search in them + * Key example: binary sorted tree, allowing O(log N) insert, remove and lookup. + + In comes, depth-first search + * Worst-case performance O(n) + * Best-case performance O(1) + * Average performance O(n) + * + * @author abir (https://github.com/abircb) + */ + +public class DepthFirstSearch { + + /** + * Depth-first search method + * + * @param tree- Binary tree to be searched + * @param value- Key being searched for + * @return Location of the key + */ + + public static > T find(T key, BinaryTree tree) { + return tree.get(key); + } + +} + +/** + * The BinaryTree class defines the structure of a binary tree + * Also contains a static nested class called TreeNode + * @param + * @author abir + */ +class BinaryTree> { + + private TreeNode root; + + /** + * @author abir + * @param

+ * This class defines what a node in a binary tree looks like + */ + private static class TreeNode

> { + + P key, value; + TreeNode

left, right; + + private TreeNode(P key, P value) { + this.key = key; + this.value = value; + this.left = null; + this.right = null; + } + + /** + * @param node + * adds the specified node + */ + private void add(TreeNode

node) { + if (node.key.compareTo(this.key) < 0) { + if(this.left == null) { + this.left = node; + } + else { + this.left.add(node); + } + } + + else { + if(this.right == null) { + this.right = node; + } + else { + this.right.add(node); + } + } + } + + /** + * @param key + * @return the tree node corresponding to the key + */ + private TreeNode

find(P key) { + if(key.compareTo(this.key) == 0) return this; + + else if(key.compareTo(this.key) < 0) { + if(this.left == null) return null; + else return this.left.find(key); + } + + else { + if(this.right == null) return null; + else return this.right.find(key); + } + } + + } + + public BinaryTree() { + this.root = null; + } + + public void add(T key, T value) { + TreeNode node = new TreeNode(key, value); + if(this.root == null) { + this.root = node; + } + else { + this.root.add(node); + } + } + + public T get(T key) { + if(this.root == null) return null; + + TreeNode node = this.root.find(key); + if(node == null) return null; + return node.value; + } +} From 4d9965cb945d59c5582e1f9cdfc7f59627638beb Mon Sep 17 00:00:00 2001 From: Abir Bhushan Date: Mon, 15 Apr 2019 11:38:59 +0100 Subject: [PATCH 2/4] Create DepthFirstSearchTest.java this test is created for the DepthFirstSearch.java file- that I added- containing the DepthFirstSearch algorithm --- .../java/com/search/DepthFirstSearchTest.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/test/java/com/search/DepthFirstSearchTest.java diff --git a/src/test/java/com/search/DepthFirstSearchTest.java b/src/test/java/com/search/DepthFirstSearchTest.java new file mode 100644 index 00000000..859abfb1 --- /dev/null +++ b/src/test/java/com/search/DepthFirstSearchTest.java @@ -0,0 +1,36 @@ +package src.test.java.com.search; + +import org.junit.Assert; +import org.junit.Test; +import src.main.java.com.search.DepthFirstSearch; +import src.main.java.com.search.BinaryTree; + +public class DepthFirstSearchTest { + @Test + public void testDepthFirstSearch() { + + BinaryTree tree1 = new BinaryTree(); + tree1.add(1,1); + tree1.add(2,2); + tree1.add(3,3); + tree1.add(4,4); + Assert.assertEquals("Incorrect index", 3, DepthFirstSearch.find(3, tree1)); + Assert.assertEquals("Incorrect index", 1, DepthFirstSearch.find(1, tree1)); + Assert.assertEquals("Incorrect index", null, DepthFirstSearch.find(0, tree1)); + Assert.assertEquals("Incorrect index", null, DepthFirstSearch.find(8, tree1)); + Assert.assertEquals("Incorrect index", null, DepthFirstSearch.find(-2, tree1)); + + BinaryTree tree2 = new BinaryTree(); + tree2.add("1","A"); + tree2.add("2","B"); + tree2.add("3","C"); + tree2.add("4","D"); + + Assert.assertEquals("Incorrect index", "C", LinearSearch.findIndex(tree2,"3")); + Assert.assertEquals("Incorrect index", "B", LinearSearch.findIndex(tree2,"2")); + Assert.assertEquals("Incorrect index", null, LinearSearch.findIndex(tree2,"F")); + + BinaryTree tree3 = new BinaryTree(); + Assert.assertEquals("Incorrect index", null, LinearSearch.findIndex(tree3, "")); + } +} From 785b570167f2016b2f49a9deb73106e70e264c87 Mon Sep 17 00:00:00 2001 From: Abir Bhushan Date: Tue, 16 Apr 2019 19:39:26 +0100 Subject: [PATCH 3/4] Update DepthFirstSearch.java --- src/main/java/com/search/DepthFirstSearch.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/search/DepthFirstSearch.java b/src/main/java/com/search/DepthFirstSearch.java index fe16e421..12ce4c11 100644 --- a/src/main/java/com/search/DepthFirstSearch.java +++ b/src/main/java/com/search/DepthFirstSearch.java @@ -10,7 +10,7 @@ package src.main.java.com.search; In comes, depth-first search * Worst-case performance O(n) * Best-case performance O(1) - * Average performance O(n) + * Average performance O(n) * * @author abir (https://github.com/abircb) */ @@ -26,8 +26,8 @@ public class DepthFirstSearch { */ public static > T find(T key, BinaryTree tree) { - return tree.get(key); - } + return tree.get(key); + } } From fa1503fa77e6904d55517dd5a869140880cb3956 Mon Sep 17 00:00:00 2001 From: Abir Bhushan Date: Fri, 10 May 2019 19:48:02 +0100 Subject: [PATCH 4/4] Apply suggestions from code review Update DepthFirstSearch.java - signature removed, and whitespaces resolved Co-Authored-By: Libin Yang --- src/main/java/com/search/DepthFirstSearch.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/search/DepthFirstSearch.java b/src/main/java/com/search/DepthFirstSearch.java index 12ce4c11..64df6bfb 100644 --- a/src/main/java/com/search/DepthFirstSearch.java +++ b/src/main/java/com/search/DepthFirstSearch.java @@ -12,7 +12,6 @@ package src.main.java.com.search; * Best-case performance O(1) * Average performance O(n) * - * @author abir (https://github.com/abircb) */ public class DepthFirstSearch { @@ -35,14 +34,12 @@ public class DepthFirstSearch { * The BinaryTree class defines the structure of a binary tree * Also contains a static nested class called TreeNode * @param - * @author abir */ class BinaryTree> { private TreeNode root; /** - * @author abir * @param

* This class defines what a node in a binary tree looks like */ @@ -87,10 +84,10 @@ class BinaryTree> { * @return the tree node corresponding to the key */ private TreeNode

find(P key) { - if(key.compareTo(this.key) == 0) return this; + if (key.compareTo(this.key) == 0) return this; else if(key.compareTo(this.key) < 0) { - if(this.left == null) return null; + if (this.left == null) return null; else return this.left.find(key); }