|
| 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 | +} |
0 commit comments