Java与算法(11)

Java与算法(11)

1.通过给定一个有序数组生成一棵平衡二叉搜索树
public class BuildAVL {
	public static class Node {
		public int value;
		public Node left;
		public Node right;

		public Node(int data) {
			this.value = data;
		}
	}
	
	public static Node build(int[] array)  {
		if (array==null) {
			return null;
		}
		
		return buildtree(array, 0,array.length-1);
	}
	public static Node buildtree(int[] array,int start,int end) {
		if (start>end) {
			return null;
		}
		
		int mid = (start+end)/2;
		Node root = new Node(array[mid]);
		root.left = buildtree(array, start, mid-1);
		root.right = buildtree(array, mid+1, end);
		
		return root;
	}
	public static void print(Node root) {
		if (root==null) {
			return;
		}
		print(root.left);
		System.out.print(root.value+"  ");
		print(root.right);
	}
	public static void main(String[] args) {
			int[] a = {1,4,9,15,26,33,45,59,62,79};
			Node root = build(a);
			print(root);
	}

}
2.判断一棵树是否为二叉搜索树和完全二叉树
public class isBSTandCBT {
	List<Integer> list = new LinkedList<>();
	public static class Node {
		int data;
		Node left;
		Node right;
		public Node(int data) {
			// TODO Auto-generated constructor stub
			this.data = data;
			left = null;
			right = null;
		}
	}
	
	
	public void toArrry(Node root) {
		if (root==null) {
			return;
		}
		
		toArrry(root.left);
		list.add(root.data);
		toArrry(root.right);
	}
	public boolean isBST() {
		for(int i=0;i<list.size()-1;i++) {
			if (list.get(i+1)<list.get(i)) {
				return false;
			}
		}
		return true;
	}
	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);
			} else {
				leaf = true;
			}
		}
		return true;
	}
	
	

	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);
		isBSTandCBT isBSTandCBT  = new isBSTandCBT();
		isBSTandCBT.toArrry(head);
		System.out.println(isBSTandCBT.isBST());
		System.out.println(isCBT(head));
	}

}
3.给定一个数组,判断是否可能为一棵二叉搜索树后序遍历的结果
public class BSTArray {
	
	public static class Node {
		int data;
		Node left;
		Node right;
		public Node(int data) {
			// TODO Auto-generated constructor stub
			this.data = data;
			left = null;
			right = null;
		}
	}
	
	
	
	public boolean isPostArray(int[]  array) {
		if (array==null && array.length==0) {
			return false;
		}
		
		return isBSTArray(array, 0,array.length-1);
	}
	
	
	public boolean isBSTArray(int[] array,int start,int end) {
		if (start==end) {
			return true;
		}
		int less = -1;
		int more = end;
		for(int i=start;i<end;i++) {
			if (array[i]<array[end]) {
				less= i;
			} else {
				more=more==end?i:more;
			}
		}
		
		if (less==-1 || more==end) {
			return isBSTArray(array, start, end-1);
		}
		if (less!=more-1) {
			return false;
		}
	return isBSTArray(array, start, less) && isBSTArray(array, more, end-1);
		
	}
	
	
	public Node arrayToBst(int[] array) {
		return toBST(array,0,array.length-1);
	}
	
	
	public Node toBST(int[] array,int start,int end) {
		if (start>end) {
			return null;
		}
		Node root = new Node(array[end]);
		int less = -1;
		int more = end;
		for(int i=start;i<end;i++) {
			if (array[end]>array[i]) {
				less=i;
			}  else {
				more= more==end?i:more;
			}
		}
		
		root.left=toBST(array, start, less);
		root.right = toBST(array, more, end-1);
		return root;
		
	}
	public void prinf(Node root) {
		if (root==null) {
			return ;
		}
		prinf(root.left);
		System.out.print(root.data+" ");
		prinf(root.right);
	}
	
	public static void main(String[] args) {
			BSTArray bstArray = new BSTArray();
			int[] a={1,4,3,7,27,10,5};
			System.out.println(bstArray.isPostArray(a));
			Node root = bstArray.arrayToBst(a);
			bstArray.prinf(root);
	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值