Skip to content

Commit af6daf7

Browse files
authored
Merge pull request TheAlgorithms#737 from abircb/Development
Added DepthFirstSearch.java and DepthFirstTestSearch.java
2 parents ada660a + fa1503f commit af6daf7

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package src.main.java.com.search;
2+
3+
/**
4+
* Searching is faster in sorted structures. Binary search is O(log n).
5+
* However, the cost of sorting is O(n · log n).
6+
* What to do when adding or removing elements? Sort again? No.
7+
* Create efficient data structures to maintain sorted sequences, and search in them
8+
* Key example: binary sorted tree, allowing O(log N) insert, remove and lookup.
9+
10+
In comes, depth-first search
11+
* Worst-case performance O(n)
12+
* Best-case performance O(1)
13+
* Average performance O(n)
14+
*
15+
*/
16+
17+
public class DepthFirstSearch {
18+
19+
/**
20+
* Depth-first search method
21+
*
22+
* @param tree- Binary tree to be searched
23+
* @param value- Key being searched for
24+
* @return Location of the key
25+
*/
26+
27+
public static <T extends Comparable<T>> T find(T key, BinaryTree<T> tree) {
28+
return tree.get(key);
29+
}
30+
31+
}
32+
33+
/**
34+
* The BinaryTree class defines the structure of a binary tree
35+
* Also contains a static nested class called TreeNode
36+
* @param <T>
37+
*/
38+
class BinaryTree<T extends Comparable<T>> {
39+
40+
private TreeNode<T> root;
41+
42+
/**
43+
* @param <P>
44+
* This class defines what a node in a binary tree looks like
45+
*/
46+
private static class TreeNode<P extends Comparable<P>> {
47+
48+
P key, value;
49+
TreeNode<P> left, right;
50+
51+
private TreeNode(P key, P value) {
52+
this.key = key;
53+
this.value = value;
54+
this.left = null;
55+
this.right = null;
56+
}
57+
58+
/**
59+
* @param node
60+
* adds the specified node
61+
*/
62+
private void add(TreeNode<P> node) {
63+
if (node.key.compareTo(this.key) < 0) {
64+
if(this.left == null) {
65+
this.left = node;
66+
}
67+
else {
68+
this.left.add(node);
69+
}
70+
}
71+
72+
else {
73+
if(this.right == null) {
74+
this.right = node;
75+
}
76+
else {
77+
this.right.add(node);
78+
}
79+
}
80+
}
81+
82+
/**
83+
* @param key
84+
* @return the tree node corresponding to the key
85+
*/
86+
private TreeNode<P> find(P key) {
87+
if (key.compareTo(this.key) == 0) return this;
88+
89+
else if(key.compareTo(this.key) < 0) {
90+
if (this.left == null) return null;
91+
else return this.left.find(key);
92+
}
93+
94+
else {
95+
if(this.right == null) return null;
96+
else return this.right.find(key);
97+
}
98+
}
99+
100+
}
101+
102+
public BinaryTree() {
103+
this.root = null;
104+
}
105+
106+
public void add(T key, T value) {
107+
TreeNode<T> node = new TreeNode<T>(key, value);
108+
if(this.root == null) {
109+
this.root = node;
110+
}
111+
else {
112+
this.root.add(node);
113+
}
114+
}
115+
116+
public T get(T key) {
117+
if(this.root == null) return null;
118+
119+
TreeNode<T> node = this.root.find(key);
120+
if(node == null) return null;
121+
return node.value;
122+
}
123+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package src.test.java.com.search;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import src.main.java.com.search.DepthFirstSearch;
6+
import src.main.java.com.search.BinaryTree;
7+
8+
public class DepthFirstSearchTest {
9+
@Test
10+
public void testDepthFirstSearch() {
11+
12+
BinaryTree<Integer> tree1 = new BinaryTree<Integer>();
13+
tree1.add(1,1);
14+
tree1.add(2,2);
15+
tree1.add(3,3);
16+
tree1.add(4,4);
17+
Assert.assertEquals("Incorrect index", 3, DepthFirstSearch.find(3, tree1));
18+
Assert.assertEquals("Incorrect index", 1, DepthFirstSearch.find(1, tree1));
19+
Assert.assertEquals("Incorrect index", null, DepthFirstSearch.find(0, tree1));
20+
Assert.assertEquals("Incorrect index", null, DepthFirstSearch.find(8, tree1));
21+
Assert.assertEquals("Incorrect index", null, DepthFirstSearch.find(-2, tree1));
22+
23+
BinaryTree<String> tree2 = new BinaryTree<String>();
24+
tree2.add("1","A");
25+
tree2.add("2","B");
26+
tree2.add("3","C");
27+
tree2.add("4","D");
28+
29+
Assert.assertEquals("Incorrect index", "C", LinearSearch.findIndex(tree2,"3"));
30+
Assert.assertEquals("Incorrect index", "B", LinearSearch.findIndex(tree2,"2"));
31+
Assert.assertEquals("Incorrect index", null, LinearSearch.findIndex(tree2,"F"));
32+
33+
BinaryTree<String> tree3 = new BinaryTree<String>();
34+
Assert.assertEquals("Incorrect index", null, LinearSearch.findIndex(tree3, ""));
35+
}
36+
}

0 commit comments

Comments
 (0)