Example #1
0
 public boolean addWord(String word) {
   if (word == null) {
     return false;
   }
   Node curNode = firstNode;
   for (int i = 0; i < word.length(); i++) {
     if (curNode.child == null) {
       curNode.child = new Node(word.charAt(i));
       curNode = curNode.child;
       // System.out.println(curNode.data);
     } else {
       curNode = curNode.child;
       // System.out.println(curNode.data);
       while (curNode.data != word.charAt(i)) {
         if (curNode.sibling == null) {
           curNode.sibling = new Node(word.charAt(i));
           curNode = curNode.sibling;
           // System.out.println(curNode.data+"Sibling");
         } else {
           curNode = curNode.sibling;
           // System.out.println(curNode.data);
         }
       }
     }
   }
   curNode.child = new Node(endChar);
   curNode = curNode.child;
   // System.out.println(curNode.data);
   numWords++;
   return true;
 }
Example #2
0
 // recursive version
 private void dfs1(Node node) {
   if (node != null) {
     node.visit();
     int numChildren = node.numChildren();
     for (int c = 0; c < numChildren; c++) dfs1(node.child(c));
   }
 } // end dfs1(Node) method
Example #3
0
 // recursive version with level cut-off
 private void dfs1a(Node node, int limit, int level) {
   if (node != null) {
     node.visit();
     if (level < limit) {
       int numChildren = node.numChildren();
       for (int c = 0; c < numChildren; c++) dfs1a(node.child(c), limit, level + 1);
     } // end inner if
   } // end outer if
 } // end dfs1a(Node, int, int) method
Example #4
0
  // non-recursive version with level cut-off
  private void dfs3(Node node, int limit) {
    Stack s = new Stack();
    Node currNode;
    int level = 0;
    node.setLevel(level);

    s.push(node);

    while (!s.empty()) {
      currNode = (Node) s.pop();
      level = currNode.level();
      currNode.visit();
      if (level < limit) {
        int numChildren = currNode.numChildren();
        for (int c = numChildren - 1; c >= 0; c--) {
          currNode.child(c).setLevel(level + 1);
          s.push(currNode.child(c));
        } // end for loop
      } // end inner if
    } // end while loop
  } // end dfs3(Node, int) method
Example #5
0
  // non-recursive version
  private void dfs2(Node node) {
    Stack s = new Stack();
    Node currNode;

    s.push(node);

    while (!s.empty()) {
      currNode = (Node) s.pop();
      currNode.visit();
      int numChildren = currNode.numChildren();
      for (int c = numChildren - 1; c >= 0; c--) s.push(currNode.child(c));
    } // end while loop
  } // end dfs2(Node) method
Example #6
0
  // non-recursive level search
  public void bfs() {
    if (root != null) {
      Queue q = new Queue();
      Node currNode = root;
      q.enqueue(currNode);

      while (!q.empty()) {
        currNode = (Node) q.dequeue();
        currNode.visit();
        int numChildren = currNode.numChildren();
        for (int c = 0; c < numChildren; c++) q.enqueue(currNode.child(c));
      }
    }
  } // end bfs method
Example #7
0
  // non-recursive level search
  public void bfs(int limit) {
    if (root != null) {
      Queue q = new Queue();
      Node currNode = root;
      int level = 0;
      currNode.setLevel(level);

      q.enqueue(currNode);

      while (!q.empty()) {
        currNode = (Node) q.dequeue();
        currNode.visit();
        level = currNode.level();
        if (level < limit) {
          int numChildren = currNode.numChildren();
          for (int c = 0; c < numChildren; c++) {
            currNode.child(c).setLevel(level + 1);
            q.enqueue(currNode.child(c));
          } // end for loop
        } // end inner if
      } // end while loop
    } // end outer if
  } // end bfs method