Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6.  
  7.  
  8.  
  9. class BinarySearchTree <E extends Comparable<E>> {
  10.  
  11. private Node root;
  12. public int size = 0;
  13.  
  14. /**
  15. * Node class - identifies a particular element with the value it holds and
  16. * the address of the subsequent element.
  17. * @author Andy Van Heuit
  18. */
  19. private class Node{
  20.  
  21. public final E value;
  22. public Node leftChild, rightChild;
  23.  
  24. private Node(E value){
  25. this.value = value;
  26. leftChild = null;
  27. rightChild = null;
  28. }
  29.  
  30. public String toString(){
  31. return "(" + value + ")";
  32. }
  33. }
  34.  
  35. public BinarySearchTree(){
  36. root = null;
  37. }
  38.  
  39. public void insert(E value){
  40. if(root == null){
  41. root = new Node(value);
  42. size++;
  43. return;
  44. }
  45.  
  46. else{
  47.  
  48. Node newNode = root;
  49.  
  50. while(newNode != null){
  51.  
  52. if(value.compareTo(newNode.value) < 0){
  53. newNode = newNode.leftChild;
  54. }
  55.  
  56. else{
  57. newNode = newNode.rightChild;
  58. }
  59.  
  60. }
  61.  
  62. newNode = new Node(value);
  63. size++;
  64.  
  65. }
  66.  
  67. }
  68.  
  69.  
  70. public boolean has(E value){
  71.  
  72. if(root == null) return false;
  73.  
  74. else{
  75.  
  76. Node nextNode = null;
  77.  
  78. for(Node compare = root; compare != null; compare = nextNode){
  79.  
  80. if(value.compareTo(compare.value) == 0){
  81. return true;
  82. }
  83.  
  84. else{
  85.  
  86. if(value.compareTo(compare.value) <= 0){
  87. nextNode = compare.leftChild;
  88. }
  89.  
  90. else{
  91. nextNode = compare.rightChild;
  92. }
  93.  
  94. }
  95.  
  96. }
  97. }
  98.  
  99. return false;
  100.  
  101. }
  102.  
  103. public E findMin(){
  104. Node min = root;
  105.  
  106. while(min.leftChild != null && min != null){
  107. min = min.leftChild;
  108. }
  109.  
  110. return min.value;
  111.  
  112. }
  113.  
  114. public E findMax(){
  115. Node max = root;
  116.  
  117. while(max.rightChild != null){
  118. max = max.rightChild;
  119. }
  120.  
  121. return max.value;
  122.  
  123. }
  124.  
  125. public int getSize(){
  126. return size;
  127. }
  128.  
  129. public void print(){
  130. print(root);
  131. System.out.println();
  132. }
  133.  
  134. private void print(Node node){
  135. if(node == null) return;
  136.  
  137. print(node.leftChild);
  138. print(node.rightChild);
  139.  
  140. System.out.println(node.value + " ");
  141. }
  142.  
  143. private static String[] loadWordFile(String filename){
  144.  
  145. ArrayList<String> getWords = new ArrayList<String>();
  146. Scanner fileReader;
  147.  
  148. try{
  149. fileReader = new Scanner(new File(filename));
  150. }
  151.  
  152. catch(FileNotFoundException e){
  153. System.err.println("File " + filename + " not found.");
  154. System.exit(1);
  155. return null;
  156. }
  157.  
  158. while(fileReader.hasNextLine()){
  159. getWords.add(fileReader.nextLine());
  160. }
  161.  
  162. fileReader.close();
  163. return getWords.toArray(new String[getWords.size()]);
  164.  
  165. }
  166.  
  167. public static String[] shuffle(String[] set) {
  168. for (int i = 1; i < set.length; i++) {
  169. int j = (int) (Math.random() * (i + 1));
  170. String swap = set[i];
  171. set[i] = set[j];
  172. set[j] = swap;
  173. }
  174. return set;
  175. }
  176.  
  177. public static void main(String[] args){
  178.  
  179. BinarySearchTree<String> tree = new BinarySearchTree<String>();
  180.  
  181. String[] test = loadWordFile("english.lex");
  182.  
  183. shuffle(test);
  184.  
  185. for(int i = 0; i < test.length; i++){
  186. tree.insert(test[i]);
  187. }
  188.  
  189. System.out.println(tree.getSize());
  190. System.out.println(tree.root);
  191. System.out.println("First element: " + tree.findMin());
  192. System.out.println("Last element: " + tree.findMax());
  193. System.out.println();
  194.  
  195. Scanner inputScanner;
  196. System.out.println(">");
  197. for(int i = 1; i > 0; i++){
  198. inputScanner = new Scanner(System.in);
  199. String input = inputScanner.nextLine();
  200.  
  201. if(input.equals("")){
  202. System.out.println("Goodbye!");
  203. System.exit(1);
  204. }
  205.  
  206. else{
  207.  
  208. if(tree.has(input)){
  209.  
  210. System.out.println("\""+ input + "\" is a valid word!");
  211.  
  212. }
  213.  
  214. else{
  215.  
  216. System.out.println("\"" + input + "\" is not a valid word.");
  217.  
  218. }
  219. }
  220. }
  221.  
  222. }
  223.  
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement