Package CtCILibrary

Examples of CtCILibrary.TreeNode


public class Question
  public static void main(String[] args) {
    int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
   
    // We needed this code for other files, so check out the code in the library
    TreeNode root = TreeNode.createMinimalBST(array);
    System.out.println("Root? " + root.data);
    System.out.println("Created BST? " + root.isBST());
    System.out.println("Height: " + root.height());
  }
View Full Code Here


    }
    System.out.println();
  }

  public static void main(String [] args){
    TreeNode root = new TreeNode(5);
    root.left = new TreeNode(3);
    root.right = new TreeNode(1);
    root.left.left = new TreeNode(4);
    root.left.right = new TreeNode(8);
    root.right.left = new TreeNode(2);
    root.right.right = new TreeNode(6);
    findSum(root, 8);
  }
View Full Code Here

    return true;
  }

  public static void main(String[] args) {
    int[] array = {Integer.MIN_VALUE, Integer.MAX_VALUE - 2, Integer.MAX_VALUE - 1, Integer.MAX_VALUE};
    TreeNode node = TreeNode.createMinimalBST(array);
    //node.left.data = 5;
    //node.left.right.data = 3;
    System.out.println(checkBST(node));
  }
View Full Code Here

  public static void main(String[] args) {
    // t2 is a subtree of t1
    int[] array1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
    int[] array2 = {2, 4, 5, 8, 9, 10, 11};
   
    TreeNode t1 = AssortedMethods.createTreeFromArray(array1);
    TreeNode t2 = AssortedMethods.createTreeFromArray(array2);

    if (containsTree(t1, t2))
      System.out.println("t2 is a subtree of t1");
    else
      System.out.println("t2 is not a subtree of t1");

    // t4 is not a subtree of t3
    int[] array3 = {1, 2, 3};
    TreeNode t3 = AssortedMethods.createTreeFromArray(array1);
    TreeNode t4 = AssortedMethods.createTreeFromArray(array3);

    if (containsTree(t3, t4))
      System.out.println("t4 is a subtree of t3");
    else
      System.out.println("t4 is not a subtree of t3");
View Full Code Here

  }

  /* Create a tree that may or may not be a BST */
  public static TreeNode createTestTree() {
    /* Create a random BST */
    TreeNode head = AssortedMethods.randomBST(10, -10, 10);
   
    /* Insert an element into the BST and potentially ruin the BST property */
    TreeNode node = head;
    do {
      int n = AssortedMethods.randomIntInRange(-10, 10);
      int rand = AssortedMethods.randomIntInRange(0, 5);
      if (rand == 0) {
        node.data = n;
View Full Code Here

  }
 
  public static void main(String[] args) {
    /* Simple test -- create one */
    int[] array = {Integer.MIN_VALUE, 3, 5, 6, 10, 13, 15, Integer.MAX_VALUE};
    TreeNode node = TreeNode.createMinimalBST(array);
    //node.left.data = 6; // "ruin" the BST property by changing one of the elements
    node.print();
    boolean isBst = checkBST(node);
    System.out.println(isBst);
   
    /* More elaborate test -- creates 100 trees (some BST, some not) and compares the outputs of various methods. */
    /*for (int i = 0; i < 100; i++) {
View Full Code Here

   
    // Found right children -> return left most node of right subtree
    if (n.parent == null || n.right != null) {
      return leftMostChild(n.right);
    } else {
      TreeNode q = n;
      TreeNode x = q.parent;
      // Go up until we�re on left instead of right
      while (x != null && x.left != q) {
        q = x;
        x = x.parent;
      }
View Full Code Here

    return n;
  }
 
  public static void main(String[] args) {
    int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    TreeNode root = TreeNode.createMinimalBST(array);
    for (int i = 0; i < array.length; i++) {
      TreeNode node = root.find(array[i]);
      TreeNode next = inorderSucc(node);
      if (next != null) {
        System.out.println(node.data + "->" + next.data);
      } else {
        System.out.println(node.data + "->" + null);
      }
View Full Code Here

    else return null;
  }
 
  public static void main(String[] args) {
    int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    TreeNode root = TreeNode.createMinimalBST(array);
    TreeNode n3 = root.find(1);
    TreeNode n7 = root.find(7);
    TreeNode ancestor = commonAncestor(root, n3, n7);
    System.out.println(ancestor.data);
  }
View Full Code Here

    boolean is_p_on_left = covers(root.left, p);
    boolean is_q_on_left = covers(root.left, q);
    if (is_p_on_left != is_q_on_left) { // Nodes are on different side
      return root;
    }
    TreeNode child_side = is_p_on_left ? root.left : root.right;
    return commonAncestorHelper(child_side, p, q);
  }
View Full Code Here

TOP

Related Classes of CtCILibrary.TreeNode

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.