博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java与算法(11)
阅读量:2441 次
发布时间:2019-05-10

本文共 3336 字,大约阅读时间需要 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
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
queue = new LinkedList
(); 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) { return null; } Node root = new Node(array[end]); int less = -1; int more = end; for(int i=start;i
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); }}

转载地址:http://ivcqb.baihongyu.com/

你可能感兴趣的文章
资料模型
查看>>
html容器标签_HTML容器标签
查看>>
valueof方法_Number valueOf()方法
查看>>
Number toFixed()方法
查看>>
seal report_对象seal()方法
查看>>
next. js_如何分析Next.js应用程序捆绑包
查看>>
golang数据结构_Go数据结构:图形
查看>>
String trimEnd()方法
查看>>
字符串的replace方法_字符串replace()方法
查看>>
字符串indexof方法_字符串indexOf()方法
查看>>
react 服务器端渲染_使用React进行服务器端渲染
查看>>
数据结构堆栈 内存堆栈_数据结构:堆栈
查看>>
instanceof运算符_JavaScript instanceof运算符
查看>>
macos 安装mysql_如何在macOS上安装MySQL
查看>>
javascript运算符_JavaScript赋值运算符
查看>>
在Vue.js中使用Tailwind
查看>>
express静态服务_使用Express服务静态资产
查看>>
Vue.js计算属性
查看>>
叶节点到根节点的路径_节点路径模块
查看>>
express json_使用Express发送JSON响应
查看>>