java二叉排序树
二叉排序树又称二叉查找树。它或者是一颗空树,或者是具有如下性质的二叉树:
1.如果左子树不空,那么左子树上的所有节点均小于它的根节点的值;
2.如果右子树不空,那么右子树上的所有节点均大于它的根节点的值;
3.左右字树也分别是二叉排序树。
关于二叉排序树的建立和遍历的代码实现如下:
class Node{
public int data;
public Node left;
public Node right;
public Node(int data){
this.data = data;
this.left = null;
this.right = null;
}
} public class BinaryTree{ private Node root;
public BinaryTree(){
root = null;
} //将date插入到排序二叉树中
public void insert(int data){
Node newNode = new Node(data);
if(root == null)
root = newNode;
else{
Node current = root;
Node parent;
while (true) { //寻找插入的位置
parent = current;
if(data < current.data){
current = current.left;
if(current == null){
parent.left = newNode;
return;
}
}
else{
current = current.right;
if(current == null){
parent.right = newNode;
return;
}
}
}
}
}
//输入数值,构建二叉树
public void buildTree(int[] data){
for (int i = 0; i<data.length; i++) {
insert(data[i]);
}
}
//中序遍历方法递归实现
public void inOrder(Node localRoot){
if (localRoot != null) {
inOrder(localRoot.left);
System.out.print(localRoot.data + " ");
inOrder(localRoot.right);
}
}
public void inOrder(){
this.inOrder(this.root);
}
//先序遍历方法递归实现
public void preOrder(Node localRoot){
if (localRoot != null) {
System.out.print(localRoot.data + " ");
preOrder(localRoot.left);
preOrder(localRoot.right);
}
}
public void preOrder(){
this.preOrder(this.root);
}
//后序遍历方法递归实现
public void postOrder(Node localRoot){
if (localRoot != null) {
postOrder(localRoot.left);
postOrder(localRoot.right);
System.out.print(localRoot.data + " ");
}
}
public void postOrder(){
this.postOrder(this.root);
} //层次遍历(利用一个队列实现)
public static void levelOrder(Node localRoot){
if(localRoot == null)
return;
List<Node> queue = new LinkedList<Node>();
queue.add(localRoot);
while(!queue.isEmpty()){
Node temp = queue.poll(0);
System.out.print(temp.data + " ");
if(temp.left != null){
queue.add(temp.left);
}
if(temp.right != null){
queue.add(temp.right);
}
}
System.out.println();
}
public void levelOrder(){
this.levelOrder(this.root);
}
}
java二叉排序树的更多相关文章
- Java二叉排序树(转)
一.二叉排序树定义 1.二叉排序树的定义 二叉排序树(Binary Sort Tree)又称二叉查找(搜索)树(Binary Search Tree).其定义为:二叉排序树或者是空树,或者是满足如下性 ...
- 算法8 五大查找之:二叉排序树(BSTree)
上一篇总结了索引查找,这一篇要总结的是二叉排序树,又称为二叉搜索树(BSTree) . 构造一棵二叉排序树的目的,其实并不是为了排序,而是为了提高查找和插入删除的效率. 什么是二叉排序树呢?二叉排序树 ...
- 算法08 五大查找之:二叉排序树(BSTree)
上一篇总结了索引查找,这一篇要总结的是二叉排序树(Binary Sort Tree),又称为二叉查找树(Binary Search Tree) ,即BSTree. 构造一棵二叉排序树的目的,其实并不是 ...
- (转)查找算法:二叉排序树(BSTree)
二叉排序树(Binary Sort Tree),又称为二叉查找树(Binary Search Tree) ,即BSTree. 构造一棵二叉排序树的目的,其实并不是为了排序,而是为了提高查找和插入删除的 ...
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- Java 7之集合类型 - 二叉排序树、平衡树、红黑树---转
http://blog.csdn.net/mazhimazh/article/details/19961017 为了理解 TreeMap 的底层实现,必须先介绍排序二叉树和平衡二叉树,然后继续介绍红黑 ...
- 【Java】 大话数据结构(11) 查找算法(2)(二叉排序树/二叉搜索树)
本文根据<大话数据结构>一书,实现了Java版的二叉排序树/二叉搜索树. 二叉排序树介绍 在上篇博客中,顺序表的插入和删除效率还可以,但查找效率很低:而有序线性表中,可以使用折半.插值.斐 ...
- 二叉排序树的理解和实现(Java)
二叉排序树的定义和性质 二叉排序树又称二叉排序树.它或者是一个空树,或者是一个具有下列性质的二叉树: 若它的左子树不空,则左子树上所有节点的值均小于它的根结构的值 若它的右子树不空,则右子树上所有结点 ...
- 【LeetCode-面试算法经典-Java实现】【109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)】
[109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 ...
随机推荐
- hdu6078 Wavel Sequence dp+二维树状数组
//#pragma comment(linker, "/STACK:102400000,102400000") /** 题目:hdu6078 Wavel Sequence 链接:h ...
- Shoot the Bullet ZOJ - 3229 有源汇有上下界的最大流
/** zoj提交评判不了,所以不知道代码正不正确.思路是应该没问题的.如果有不对的地方,请多指教. 题目:Shoot the Bullet ZOJ - 3229 链接:https://vjudge. ...
- splay旋转模板
splay旋转模板 void rotate(int x) { int y=f[x],z=son(x);f[x]=f[y]; if (f[x]) t[f[x]][son(y)]=x; t[y][z]=t ...
- SSH初体验系列--Hibernate--2--crud操作
Ok,今天比较详细的学习一下hibernate的C(create).R(read).U(update).D(delete) 相关api... 前言 Session: 是Hibernate持久化操作的基 ...
- Xcode下开发c静态库for ios CPU架构 静态库合并
新建一个Cocoa Touch Static Library工程 1,先在工程左侧删除“工程名Tests”下的文件与文件夹(从内往外删,最后删除"工程名Tests文件夹") :D ...
- datagrid后台分页js
$(function () { gridbind(); bindData(); }); //表格绑定function gridbind() { $('#dg').datagrid({ title: ' ...
- (转)java位运算
转自:http://aijuans.iteye.com/blog/1850655 Java 位运算(移位.位与.或.异或.非) public class Test { public static ...
- js转译html标签
api返回的接口数据是这样的"<div>测试数据</div>" js拼接html时会过滤掉<div></div> 所以需要对< ...
- mui 单页面下拉刷新
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 修改了JS代码,刷新网页后,加载的JS还是原来旧的?
本地修改JS脚本后,刷新网页看一下修改后的执行效果,结果调试显示加载的JS还是原来旧的,反复刷新均无效,郁闷! 解决办法:清理一下浏览器缓存(长经验了!) Ctrl+Shift+Del 清除G ...