Ejemplo n.º 1
0
  public static boolean find(Tree node, int value) {

    count++;

    if (value == node.getValue()) {
      System.out.println(count);
      return true;
    }

    if (value < node.getValue() && node.getLeft() != null) {
      return find(node.getLeft(), value);
    }

    if (value > node.getValue() && node.getRight() != null) {
      return find(node.getRight(), value);
    }

    return false;
  }
Ejemplo n.º 2
0
  private void smooth(Tree tag, Model model, Statistics statistics, Type type, double[] freqs) {
    int number = freqs.length;
    if (tag.getLeft() == null && tag.getRight() == null) {
      int tag_index = model.getTagTable().toIndex(tag.getName());
      for (int index = 0; index < number; index++) {
        setFreq(model, statistics, tag_index, index, type, freqs[index]);
      }
      return;
    }

    double[] fine_freqs = new double[number];

    if (tag.getLeft() == null || tag.getRight() == null) {
      Tree child_tag;
      if (tag.getLeft() == null) {
        child_tag = tag.getRight();
      } else {
        child_tag = tag.getLeft();
      }
      collectFreqs(child_tag, model, type, fine_freqs);
      smooth(freqs, fine_freqs, type);
      smooth(child_tag, model, statistics, type, fine_freqs);
      return;
    }

    collectFreqs(tag.getLeft(), model, type, fine_freqs);
    smooth(freqs, fine_freqs, type);
    smooth(tag.getLeft(), model, statistics, type, fine_freqs);

    collectFreqs(tag.getRight(), model, type, fine_freqs);
    smooth(freqs, fine_freqs, type);
    smooth(tag.getRight(), model, statistics, type, fine_freqs);
  }
Ejemplo n.º 3
0
  public static void insert(Tree node, int value) {

    if (node.left == null && node.right == null && node.getValue() == 0) {
      node.setValue(value);
      return;
    }

    Tree newNode = new Tree();
    newNode.setValue(value);

    if (value < node.getValue() && node.getLeft() == null) {
      node.setLeft(newNode);
      return;
    } else if (value < node.getValue() && node.getLeft() != null) {
      insert(node.getLeft(), value);
    } else if (value > node.getValue() && node.getRight() == null) {
      node.setRight(newNode);
      return;
    } else if (value > node.getValue() && node.getRight() != null) {
      insert(node.getRight(), value);
    }

    return;
  }
Ejemplo n.º 4
0
 private int isAVLWrapper(Tree<T> tree) {
   if (tree == null) {
     return 1;
   }
   return (isAVLWrapper(tree.getLeft()) - isAVLWrapper(tree.getRight()));
 }
Ejemplo n.º 5
0
 /* Search for n in a tree t, and return the subtree whose root is n
  * If n does not occur in the tree, throw an exception.
  */
 public static Tree find(int n, Tree t) {
   if (t.getEmpty()) throw new IllegalArgumentException("Value not present in search" + " tree");
   else if (n == t.getValue()) return t;
   else if (n < t.getValue()) return find(n, t.getLeft());
   else return find(n, t.getRight());
 }
Ejemplo n.º 6
0
 /* Insert n into a tree t in order, and return the resulting tree.
  */
 public static Tree insert(int n, Tree t) {
   if (t.getEmpty()) return new Tree(n, new Tree(), new Tree());
   else if (n <= t.getValue()) return new Tree(t.getValue(), insert(n, t.getLeft()), t.getRight());
   else return new Tree(t.getValue(), t.getLeft(), insert(n, t.getRight()));
 }