Exemplo n.º 1
0
  public static void main(String[] args) {
    LinkedList yes = new LinkedList();
    Node A = yes.add(4);
    yes.add(3);
    yes.add(5);
    yes.add(1);
    yes.add(8);
    Node B = yes.add(2);
    // yes.printBackwards(B);

    Queue yes2 = new Queue();
    Node C = yes2.enqueue(4);
    yes2.enqueue(3);
    yes2.enqueue(5);
    yes2.enqueue(1);
    yes2.enqueue(8);
    yes2.dequeue();
    yes2.dequeue();
    int m = yes2.peek();
    // System.out.println(m);

    Stack yes3 = new Stack();
    Node D = yes3.push(3);
    yes3.push(5);
    yes3.push(6);
    yes3.push(1);
    yes3.push(8);
    /*int p = yes3.peek();
    System.out.println(p);
    yes3.pop();
    p = yes3.peek();
    System.out.println(p);
    yes3.pop();
    p = yes3.peek();
    System.out.println(p);
    yes3.pop();
    p = yes3.peek();
    System.out.println(p);
    yes3.pop();
    p = yes3.peek();
    System.out.println(p);*/

    Tree yes4 = new Tree();
    TreeNode E = yes4.add(5);
    yes4.add(3);
    yes4.add(7);
    yes4.add(1);
    yes4.add(9);

    yes4.inOrder(E);
    yes4.preOrder(E);
    yes4.postOrder(E);
  }
Exemplo n.º 2
0
 public static void main(String[] args) {
   Tree<Integer> tree = new Tree<>();
   tree.add(tree.root, 7);
   tree.add(tree.root, 3);
   tree.add(tree.root, 1);
   tree.add(tree.root, 25);
   tree.add(tree.root, 77);
   tree.add(tree.root, 2);
   tree.add(tree.root, 6);
   System.out.println(tree.find(6));
   System.out.println(tree.findRec(tree.root, 6));
   System.out.println(tree.findFstKeyLargerK(64));
   System.out.println(tree.findFstKeyLargerK(1));
 }
Exemplo n.º 3
0
  public static void main(String[] args) {
    Tree t = new Tree();
    int values[] = {8, 4, 16, 2, 6, 12, 20, 1, 10, 14, 13, 15};
    for (int i = 0; i < values.length; i++) {
      t.add(values[i]);
    }

    int v = 30;
    System.out.print("Path to" + v + ":");
    t.printPath(v);

    if (t.isLeaf(v)) {
      System.out.println("" + v + "is a leaf");
    } else {
      System.out.println("" + v + "doesn`t a leaf");
    }
    t.recursivePrint();

    int r = t.recursiveCount();
    System.out.println("total nodes: " + r);

    t.rDraw();
    System.out.println("" + t.CountOdds());

    t.reversePrint();
    System.out.println("");

    System.out.println("PrintSonAlone");
    t.PrintSonAlone();

    t.printMissings();
  }
Exemplo n.º 4
0
  public static Tree newTree(int... datas) {
    if (datas == null || datas.length < 1) return null;
    Node root = new Node(datas[0]);
    Tree tree = new Tree(root);

    for (int i = 1; i < datas.length; i++) {
      tree.add(datas[i]);
    }

    return tree;
  }
  private static Tree sortConstructorsByParamsCount(final ReflectConstructor[] constructors) {
    Tree sortedConstructors = null;

    // sort constructors by parameter count
    for (int i = 0; i < constructors.length; i++) {
      int parameterCount = constructors[i].getParameterTypes().length;
      sortedConstructors =
          Tree.add(
              sortedConstructors,
              new TreeIntObject(i + constructors.length * parameterCount, constructors[i]));
    }
    return sortedConstructors;
  }
Exemplo n.º 6
0
  /** Test program */
  public static void main(String[] args) {
    Random rng = new Random(0);
    System.out.println("--------------- Q2_TreePath Tests---------------");
    String[] ids = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"};
    String[] names = {
      "john", "david", "brian", "steve", "ken", "nancy", "karen", "susan", "brad", "mary"
    };
    Vector<String> namesUsed = new Vector<String>();

    int nTrees = 2;
    int nSearches = 4;
    for (int n = 0; n < nTrees; n++) {
      // generate a random ordering of the ids.
      for (int swap = 0; swap < ids.length; swap++) {
        int k = rng.nextInt(ids.length);
        String temp = ids[k];
        ids[k] = ids[swap];
        ids[swap] = temp;
      }

      Tree tree = new Tree();
      for (int i = 0; i < ids.length; i++) {
        String name = names[i % names.length];
        tree.add(ids[i], name);
        namesUsed.add(name);
      }

      System.out.println(
          "--------------- Tree: " + n + " " + "(" + ids.length + " nodes ) ----------------");
      System.out.println(tree);

      for (int search = 0; search < nSearches; search++) {
        String name = namesUsed.remove(rng.nextInt(namesUsed.size()));
        String path = tree.findPath(name);
        System.out.println("Path to " + name + ": " + path);
      }
      String path = tree.findPath("NotThere");
      System.out.println("Path to NotThere: " + path);
    }
  }