zoukankan      html  css  js  c++  java
  • 如何判断是搜索二叉树与完全二叉树

    1、搜索二叉树的定义:

    它是一棵空树,或者是具有下列性质的二叉树:若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 它的左、右子树也分别为二叉排序树。搜索二叉树可以方便的查找树中的最大值或最小值。

    如何判断一棵二叉树是否是搜索二叉树?

    :只需要判断该二叉树的中序遍历是否从小到大有序,即升序。

    2、完全二叉树:

    简单来说,就是一颗二叉树,假设有k(k > 1)层,那么k层,从左到右依次有点结点,但不一定是满的。1~k-1层,每一层都是满的。

    (1)所有的叶结点都出现在第k层或k-l层(层次最大的两层)

    (2)对任一结点,如果其右子树的最大层次为L,则其左子树的最大层次为L或L+l。

    如何判断一颗二叉树是否为完全二叉树?

    答:按层遍历,从上至下,从左至右。(1)如果一个节点,有右子树,而无左子树,一定不是完全二叉树。 (2)如果遍历过程中出现了,一个结点有左子树,而无右子树,或者这个结点无左子树,也无右子树。那么后面遍历出现的结点一定都是叶子结点。否则不是完全二叉树。

    代码入下:

    package class_04;
    
    import java.util.LinkedList;
    import java.util.Queue;
    
    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;
    		}
    	}
    
    	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 l = null;
    		Node r = null;
    		queue.offer(head);
    		while (!queue.isEmpty()) {
    			head = queue.poll();   // 弹出当前结点
    			l = head.left;         // 左孩子
    			r = head.right;        // 右孩子
    			if ((leaf && (l != null || r != null)) || (l == null && r != null)) {
    				return false;
    			}
    			if (l != null) {
    				queue.offer(l);
    			}
    			if (r != null) {
    				queue.offer(r);
    			} 
    			if(l == null || r == 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));
    
    	}
    }
  • 相关阅读:
    Levmar 配置
    函数前加static与不加static的区别
    关于形如--error LNK2005: xxx 已经在 msvcrtd.lib ( MSVCR90D.dll ) 中定义--的问题分析解决
    Latex 算法Algorithm
    matlab 曲线拟合
    生成eps图形
    libSVM在matlab下的使用安装
    spring 事务管理笔记
    集合框架
    小程序 滑动弹窗阻止主页面滑动
  • 原文地址:https://www.cnblogs.com/horken/p/10706128.html
Copyright © 2011-2022 走看看