Skip to content

Commit 6938c40

Browse files
authored
Create DepthFirstSearch.java
1 parent 39806e3 commit 6938c40

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
* @author abir (https://github.com/abircb)
16+
*/
17+
18+
public class DepthFirstSearch {
19+
20+
/**
21+
* Depth-first search method
22+
*
23+
* @param tree- Binary tree to be searched
24+
* @param value- Key being searched for
25+
* @return Location of the key
26+
*/
27+
28+
public static <T extends Comparable<T>> T find(T key, BinaryTree<T> tree) {
29+
return tree.get(key);
30+
}
31+
32+
}
33+
34+
/**
35+
* The BinaryTree class defines the structure of a binary tree
36+
* Also contains a static nested class called TreeNode
37+
* @param <T>
38+
* @author abir
39+
*/
40+
class BinaryTree<T extends Comparable<T>> {
41+
42+
private TreeNode<T> root;
43+
44+
/**
45+
* @author abir
46+
* @param <P>
47+
* This class defines what a node in a binary tree looks like
48+
*/
49+
private static class TreeNode<P extends Comparable<P>> {
50+
51+
P key, value;
52+
TreeNode<P> left, right;
53+
54+
private TreeNode(P key, P value) {
55+
this.key = key;
56+
this.value = value;
57+
this.left = null;
58+
this.right = null;
59+
}
60+
61+
/**
62+
* @param node
63+
* adds the specified node
64+
*/
65+
private void add(TreeNode<P> node) {
66+
if (node.key.compareTo(this.key) < 0) {
67+
if(this.left == null) {
68+
this.left = node;
69+
}
70+
else {
71+
this.left.add(node);
72+
}
73+
}
74+
75+
else {
76+
if(this.right == null) {
77+
this.right = node;
78+
}
79+
else {
80+
this.right.add(node);
81+
}
82+
}
83+
}
84+
85+
/**
86+
* @param key
87+
* @return the tree node corresponding to the key
88+
*/
89+
private TreeNode<P> find(P key) {
90+
if(key.compareTo(this.key) == 0) return this;
91+
92+
else if(key.compareTo(this.key) < 0) {
93+
if(this.left == null) return null;
94+
else return this.left.find(key);
95+
}
96+
97+
else {
98+
if(this.right == null) return null;
99+
else return this.right.find(key);
100+
}
101+
}
102+
103+
}
104+
105+
public BinaryTree() {
106+
this.root = null;
107+
}
108+
109+
public void add(T key, T value) {
110+
TreeNode<T> node = new TreeNode<T>(key, value);
111+
if(this.root == null) {
112+
this.root = node;
113+
}
114+
else {
115+
this.root.add(node);
116+
}
117+
}
118+
119+
public T get(T key) {
120+
if(this.root == null) return null;
121+
122+
TreeNode<T> node = this.root.find(key);
123+
if(node == null) return null;
124+
return node.value;
125+
}
126+
}

0 commit comments

Comments
 (0)