Ejemplo n.º 1
0
 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 = commonAncestor0(root, n3, n7);
   System.out.println(ancestor.data);
 }
  public static void main(String[] args) {
    // Create balanced tree
    int[] array = {0, 1, 2, 3, 5, 6, 7, 8, 9, 10};
    TreeNode root = TreeNode.createMinimalBST(array);

    System.out.println("Is balanced? " + isBalanced(root));

    root.insertInOrder(4); // Add 4 to make it unbalanced

    System.out.println("Is balanced? " + isBalanced(root));
  }
Ejemplo n.º 3
0
 public static void main(String[] args) {
   int[] array = {5, 3, 6, 1, 9, 11};
   TreeNode root = new TreeNode(20);
   for (int a : array) {
     root.insertInOrder(a);
   }
   TreeNode n1 = root.find(1);
   TreeNode n9 = root.find(9);
   TreeNode ancestor = commonAncestor(root, n1, n9);
   System.out.println(ancestor.data);
 }
Ejemplo n.º 4
0
 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(10);
   TreeNode n7 = root.find(6);
   TreeNode ancestor = commonAncestor(root, n3, n7);
   if (ancestor != null) {
     System.out.println(ancestor.data);
   } else {
     System.out.println("null");
   }
 }
Ejemplo n.º 5
0
 // Test
 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 = nextNode(node);
     if (next != null) {
       System.out.println(node.data + "->" + next.data);
     } else {
       System.out.println(node.data + "->" + null);
     }
   }
 }
Ejemplo n.º 6
0
 public static void main(String[] args) {
   int[] array = {3, 5, 7};
   TreeNode node = TreeNode.createMinimalBST(array);
   node.left.data = 5;
   System.out.println(checkBST0(node));
 }
Ejemplo n.º 7
0
 public static void main(String[] args) {
   int[] array = {3, 5, 7, 10, 13, 15, 20};
   TreeNode node = TreeNode.createMinimalBST(array);
   // node.left.right.data = 3;
   System.out.println(checkBST(node));
 }