コード例 #1
0
 public static void main(String[] args) {
   // balanced binary tree test
   // 3
   // 2 5
   // 1 4 6
   BinaryTreeNode<Integer> tree = new BinaryTreeNode<>();
   tree.setLeft(new BinaryTreeNode<Integer>());
   tree.getLeft().setLeft(new BinaryTreeNode<Integer>());
   tree.setRight(new BinaryTreeNode<Integer>());
   tree.getRight().setLeft(new BinaryTreeNode<Integer>());
   tree.getRight().setRight(new BinaryTreeNode<Integer>());
   assert (isBalanced(tree));
   System.out.println(isBalanced(tree));
   tree = new BinaryTreeNode<>();
   tree.setLeft(new BinaryTreeNode<Integer>());
   tree.getLeft().setLeft(new BinaryTreeNode<Integer>());
   assert (!isBalanced(tree));
   System.out.println(isBalanced(tree));
 }
コード例 #2
0
 public static void main(String[] args) {
   // 3
   // 2 5
   // 1 4 6
   BinaryTreeNode<Integer> tree = new BinaryTreeNode<>(3);
   tree.setLeft(new BinaryTreeNode<>(2));
   tree.getLeft().setLeft(new BinaryTreeNode<>(1));
   tree.setRight(new BinaryTreeNode<>(5));
   tree.getRight().setLeft(new BinaryTreeNode<>(4));
   tree.getRight().setRight(new BinaryTreeNode<>(6));
   // should output true.
   assert isBinaryTreeBST(tree);
   System.out.println(isBinaryTreeBST(tree));
   // 10
   // 2 5
   // 1 4 6
   tree.setData(10);
   // should output false.
   assert !isBinaryTreeBST(tree);
   System.out.println(isBinaryTreeBST(tree));
   // should output true.
   assert isBinaryTreeBST(null);
   System.out.println(isBinaryTreeBST(null));
 }