题目描述
判断一棵树是否是搜索二叉树、判断一棵树是否是完全二叉树
什么是二叉查找树?
二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 它的左、右子树也分别为二叉排序树。
什么是完全二叉树?
完全二叉树是效率很高的数据结构,完全二叉树是由满二叉树而引出来的。对于深度为K的,有n个结点的二叉树,当且仅当其每一个结点都与深度为K的满二叉树中编号从1至n的结点一一对应时称之为完全二叉树。
代码实现
package class_04;
import java.util.LinkedList;
import java.util.Queue;
/**
* 判断一棵树是否是搜索二叉树、判断一棵树是否是完全二叉树
* @author Administrator
*
*/
public class Code_07_IsBSTAndCBT {
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}
/**
* 判断一个数是否为二叉搜索树 -- 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值
* @param head
* @return
*/
//中序遍历的方法实现
public static Node prev=null;
public static boolean isBST(Node root)
{
if(root != null)
{
if(!isBST(root.left))
return false;
if(prev != null && root.value < prev.value)
return false;
prev = root;
if(!isBST(root.right))
return false;
}
return true;
}
// public static boolean isBST(Node head) {
// if (head == null) {
// return true;
// }
// boolean res = true;
// Node pre = null;
// Node cur1 = head;
// Node cur2 = null;
// while (cur1 != null) {
// cur2 = cur1.left;
// if (cur2 != null) {
// while (cur2.right != null && cur2.right != cur1) {
// cur2 = cur2.right;
// }
// if (cur2.right == null) {
// cur2.right = cur1;
// cur1 = cur1.left;
// continue;
// } else {
// cur2.right = null;
// }
// }
// if (pre != null && pre.value > cur1.value) {
// res = false;
// }
// pre = cur1;
// cur1 = cur1.right;
// }
// return res;
// }
/**
* 完全二叉树
*/
public static boolean isCBT(Node head) {
if (head == null) {
return true;
}
Queue<Node> queue = new LinkedList<Node>();
boolean leaf = false;//
Node left = null;
Node right = null;
queue.offer(head);
while (!queue.isEmpty()) {
head = queue.poll();
left = head.left;
right = head.right;
if ((leaf && (left != null || right != null)) || (left == null && right != null)) {
return false;
}
if (left != null) {
queue.offer(left);
}
if (right != null) {
queue.offer(right);
}
if(left==null || right ==null){
leaf=true;
}
}
return true;
}
// for test -- print tree
public static void printTree(Node head) {
System.out.println("Binary Tree:");
printInOrder(head, 0, "H", 17);
System.out.println();
}
public static void printInOrder(Node head, int height, String to, int len) {
if (head == null) {
return;
}
printInOrder(head.right, height + 1, "v", len);
String val = to + head.value + to;
int lenM = val.length();
int lenL = (len - lenM) / 2;
int lenR = len - lenM - lenL;
val = getSpace(lenL) + val + getSpace(lenR);
System.out.println(getSpace(height * len) + val);
printInOrder(head.left, height + 1, "^", len);
}
public static String getSpace(int num) {
String space = " ";
StringBuffer buf = new StringBuffer("");
for (int i = 0; i < num; i++) {
buf.append(space);
}
return buf.toString();
}
public static void main(String[] args) {
Node head = new Node(4);
head.left = new Node(2);
head.right = new Node(6);
head.left.left = new Node(1);
head.left.right = new Node(3);
head.right.left = new Node(5);
// printTree(head);
System.out.println(isBST(head));
// System.out.println(isCBT(head));
}
}