예제 #1
0
  public static void main(String[] args) {
    Tree myTree = new Tree();

    myTree.insert(50);
    myTree.insert(11);
    myTree.insert(54);
    myTree.insert(45);
    myTree.insert(21);
    myTree.insert(82);
    myTree.insert(3);

    myTree.find(50);
    myTree.find(21);
    myTree.find(100); // test finding a non-existing node

    myTree.traverse(0); // in-order traverse

    myTree.getSuccessor2(myTree.root);
    myTree.getSuccessor2(myTree.root.leftChild); // look for successor of 11

    myTree.remove(82); // remove type 1
    System.out.print("After removing 82, the tree is :");
    myTree.traverse(0);
    myTree.insert(82); // recover
    System.out.print("After recovering 82, the tree is :");
    myTree.traverse(0);
    myTree.remove(54); // remove type 2
    System.out.print("After removing 54, the tree is :");
    myTree.traverse(0);
    myTree.remove(11); // remove type 3
    System.out.print("After removing 11, the tree is :");
    myTree.traverse(0);
  }
예제 #2
0
 public static void main(String[] args) throws java.lang.Exception {
   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
   Tree tree = new Tree();
   int n = Integer.parseInt(br.readLine());
   int nn = n - 1;
   TreeNode[] treeNodes = new TreeNode[n];
   int i = 0;
   while (n-- > 0) {
     String[] strs = br.readLine().split(" ");
     int r = Integer.parseInt(strs[1]);
     int n1 = Integer.parseInt(strs[0]);
     treeNodes[i] = new TreeNode(n1, r);
     i = i + 1;
   }
   boolean[] x = new boolean[nn + 1];
   while (nn-- > 0) {
     String[] strs = br.readLine().split(" ");
     int s = Integer.parseInt(strs[0]);
     int t = Integer.parseInt(strs[1]);
     if (!x[s]) {
       tree.insert(treeNodes[s]);
       x[s] = true;
     }
     if (!x[t]) {
       tree.insert(treeNodes[t]);
       x[t] = true;
     }
   }
   tree.levelOrder(tree.root);
 }
예제 #3
0
 public void queueTree() { // Builds the queue along with all the subtrees that exist within.
   for (int i = 0; i < freqTable.length; i++) {
     if (freqTable[i] > 0) {
       Tree t = new Tree();
       t.insert(freqTable[i], (char) (i + 65));
       theQ.insert(t);
     }
   }
 }
예제 #4
0
파일: App.java 프로젝트: H35am/Tree
  /**
   * De main wordt gebruikt om de tree te testen.
   *
   * @param args
   */
  public static void main(String[] args) {
    //		Programmeer een paar cases waarbij
    //		je laat zien dat de boom werkt
    Tree tree = new Tree();
    Student student = new Student(1, "Kees");
    Student student1 = new Student(2, "Jan");

    tree.insert(student);
    tree.insert(student1);

    Student findStudent = tree.find(2);

    System.out.print(findStudent.toString());

    //		voeg studenten met studentnummers �0802063� tot en met �0824836� toe

    //		Verwijder de root

    //		print de studenten op volgorde van studentnummer
  }
예제 #5
0
 public void
     makeHuffmanTree() { // Goes through the process of combining the two lowest priority subtrees
                         // and then replacing them within the queue until a single tree is left
   while (!theQ.isEmpty()) {
     Tree t = new Tree();
     Tree left = theQ.remove();
     Tree right = theQ.remove();
     t.insert(left.root.iData + right.root.iData, '+');
     t.root.leftChild = left.root;
     t.root.rightChild = right.root;
     if (theQ.isEmpty()) {
       huffTree = t;
     } else {
       theQ.insert(t);
     }
   }
 }
예제 #6
0
  public static void main(String args[]) {
    stack s = new stack();
    s.push(10);
    s.push(20);
    s.seek();
    System.out.println(s.pop());
    s.seek();
    StackLinked s1 = new StackLinked();
    s1.push(3, 4);
    s1.push(5, 6);
    s1.push(7, 6);
    s1.push(8, 6);
    s1.print();

    Tree t1 = new Tree();
    t1.insert(10, 4);
    t1.insert(6, 5);
    t1.insert(15, 4);
    t1.insert(8, 8);
    t1.insert(3, 7);
    t1.insert(1, 13);
    t1.insert(5, 13);
    t1.insert(15, 13);
    t1.insert(12, 13);

    System.out.println("InOrder for tree");

    t1.InOrder();
    System.out.println("Height for tree is" + t1.Height());
    t1.Del(7);
    System.out.println("After deletion of 7 PreOrder for tree");
    t1.InOrder();
    System.out.println("floor of number " + t1.floor(4));
    System.out.println("floor of number " + t1.floor(9));
    System.out.println("Level Order");
    t1.LevelOrder();
    LinkedList l = new LinkedList();
    l.add(6);
    l.add(3);
    l.add(2);
    l.add(8);
    l.add(7);
    l.add(1);
    l.print();
    l.Rev();
    l.print();
  }