• Source
    1. import java.util.*;
    2.  
    3. class Node
    4. {
    5. public Integer value;
    6.  
    7. public int index;
    8. public int level;
    9.  
    10. public Node(int index, int value, int level)
    11. {
    12. this.value = value;
    13. this.index = index;
    14. this.level = level;
    15. }
    16. }
    17.  
    18. class Main
    19. {
    20. public static int leftChild(int index)
    21. {
    22. return index*2 + 1;
    23. }
    24.  
    25. public static int rightChild(int index)
    26. {
    27. return index*2 + 2;
    28. }
    29.  
    30. public static Vector<Node> convertToNodes(int tree[])
    31. {
    32. Vector<Node> vector = new Vector<Node>();
    33.  
    34. int nodeCount = 0;
    35. int currentLevel = 1;
    36. int levelNodes = 1; // first level has one node
    37.  
    38. for (int x = 0; x < tree.length; x++)
    39. {
    40. Node node = new Node(x, tree[x], currentLevel);
    41. vector.add(node);
    42.  
    43. if ( ++nodeCount >= levelNodes )
    44. {
    45. nodeCount = 0;
    46. levelNodes = levelNodes * 2;
    47. currentLevel++;
    48. }
    49. }
    50.  
    51. return vector;
    52. }
    53.  
    54. public static Node depthFirstSearch(int tree[], int find)
    55. {
    56. Vector<Node> treeNodes = convertToNodes(tree);
    57. Stack<Node> stack = new Stack<Node>();
    58.  
    59. stack.push(treeNodes.get(0));
    60.  
    61. while ( ! stack.isEmpty() )
    62. {
    63. Node n = stack.pop();
    64.  
    65. System.out.println("Currently searching level = " + n.level + ", index = " + n.index);
    66.  
    67. if ( n.value == null )
    68. continue;
    69.  
    70. if ( n.value == find )
    71. return n;
    72.  
    73. int leftIndex = leftChild(n.index);
    74.  
    75. if ( leftIndex >= treeNodes.size() )
    76. {
    77. continue;
    78. }
    79.  
    80. stack.push(treeNodes.get(rightChild(n.index)));
    81. stack.push(treeNodes.get(leftIndex));
    82. }
    83.  
    84. // Value was not found
    85. return null;
    86. }
    87.  
    88. public static Node breadthFirstSearch(int tree[], int find)
    89. {
    90. Vector<Node> treeNodes = convertToNodes(tree);
    91. Queue<Node> q = new LinkedList<Node>();
    92.  
    93. q.add(treeNodes.get(0));
    94.  
    95. while ( ! q.isEmpty() )
    96. {
    97. Node n = q.remove();
    98. System.out.println("Currently searching level = " + n.level + ", index = " + n.index);
    99.  
    100. if ( n.value == null )
    101. continue;
    102.  
    103. if ( n.value == find )
    104. return n;
    105.  
    106. int leftIndex = leftChild(n.index);
    107.  
    108. if ( leftIndex >= treeNodes.size() )
    109. continue;
    110.  
    111. q.add(treeNodes.get(leftIndex));
    112. q.add(treeNodes.get(rightChild(n.index)));
    113. }
    114.  
    115. // Value was not found
    116. return null;
    117. }
    118.  
    119. public static Node breadthFirstSearch_Recursive(Vector<Node> treeNodes, int find, Queue<Node> toSearch)
    120. {
    121. Node node = toSearch.poll();
    122.  
    123. if ( node == null )
    124. return null;
    125.  
    126. int leftIndex = leftChild(node.index);
    127. if ( leftIndex < treeNodes.size() )
    128. toSearch.add(treeNodes.get(leftIndex));
    129.  
    130. int rightIndex = rightChild(node.index);
    131. if ( rightIndex < treeNodes.size() )
    132. toSearch.add(treeNodes.get(rightIndex));
    133.  
    134. System.out.println("Currently searching level = " + node.level + ", index = " + node.index);
    135.  
    136. if ( node.value == null )
    137. return null;
    138.  
    139. if ( node.value == find )
    140. return node;
    141.  
    142. return breadthFirstSearch_Recursive(treeNodes, find, toSearch);
    143. }
    144.  
    145. public static Node breadthFirstSearch_Recursive(int tree[], int find)
    146. {
    147. Vector<Node> treeNodes = convertToNodes(tree);
    148.  
    149. Queue<Node> toSearch = new LinkedList<Node>();
    150. toSearch.add(treeNodes.get(0));
    151.  
    152. return breadthFirstSearch_Recursive(treeNodes, find, toSearch);
    153. }
    154.  
    155. public static Node depthFirstSearch_Recursive(Vector<Node> treeNodes, int index, int find)
    156. {
    157. Node node = treeNodes.get(index);
    158. System.out.println("Currently searching level = " + node.level + ", index = " + node.index);
    159.  
    160. if ( node.value == null )
    161. return null;
    162.  
    163. if ( node.value == find )
    164. return node;
    165.  
    166. int leftIndex = leftChild(node.index);
    167. if ( leftIndex >= treeNodes.size() )
    168. return null;
    169.  
    170. Node left = depthFirstSearch_Recursive(treeNodes, leftIndex, find);
    171. if ( left != null )
    172. {
    173. return left;
    174. }
    175.  
    176. int rightIndex = rightChild(node.index);
    177. if ( rightIndex >= treeNodes.size() )
    178. return null;
    179.  
    180. Node right = depthFirstSearch_Recursive(treeNodes, rightIndex, find);
    181. if ( right != null )
    182. {
    183. return right;
    184. }
    185.  
    186. return null;
    187. }
    188.  
    189. public static Node depthFirstSearch_Recursive(int tree[], int find)
    190. {
    191. Vector<Node> treeNodes = convertToNodes(tree);
    192. return depthFirstSearch_Recursive(treeNodes, 0, find);
    193. }
    194.  
    195. public static void main(String args[])
    196. {
    197. {
    198. Node found = breadthFirstSearch(new int[] {1, 2, 3, 4, 5, 6, 7}, 7);
    199. System.out.println("Found = " + found.value + ", index = " + found.index + ", level = " + found.level);
    200. }
    201. {
    202. Node found = depthFirstSearch(new int[] {1, 2, 3, 4, 5, 6, 7}, 7);
    203. System.out.println("Found = " + found.value + ", index = " + found.index + ", level = " + found.level);
    204. }
    205. {
    206. Node found = depthFirstSearch_Recursive(new int[] {1, 2, 3, 4, 5, 6, 7}, 7);
    207. System.out.println("Found = " + found.value + ", index = " + found.index + ", level = " + found.level);
    208. }
    209. {
    210. Node found = breadthFirstSearch_Recursive(new int[] {1, 2, 3, 4, 5, 6, 7}, 7);
    211. System.out.println("Found = " + found.value + ", index = " + found.index + ", level = " + found.level);
    212. }
    213. }
    214. }