二叉排序树又称二叉查找树。它或者是一颗空树,或者是具有如下性质的二叉树:

  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二叉排序树的更多相关文章

  1. Java二叉排序树(转)

    一.二叉排序树定义 1.二叉排序树的定义 二叉排序树(Binary Sort Tree)又称二叉查找(搜索)树(Binary Search Tree).其定义为:二叉排序树或者是空树,或者是满足如下性 ...

  2. 算法8 五大查找之:二叉排序树(BSTree)

    上一篇总结了索引查找,这一篇要总结的是二叉排序树,又称为二叉搜索树(BSTree) . 构造一棵二叉排序树的目的,其实并不是为了排序,而是为了提高查找和插入删除的效率. 什么是二叉排序树呢?二叉排序树 ...

  3. 算法08 五大查找之:二叉排序树(BSTree)

    上一篇总结了索引查找,这一篇要总结的是二叉排序树(Binary Sort Tree),又称为二叉查找树(Binary Search Tree) ,即BSTree. 构造一棵二叉排序树的目的,其实并不是 ...

  4. (转)查找算法:二叉排序树(BSTree)

    二叉排序树(Binary Sort Tree),又称为二叉查找树(Binary Search Tree) ,即BSTree. 构造一棵二叉排序树的目的,其实并不是为了排序,而是为了提高查找和插入删除的 ...

  5. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  6. Java 7之集合类型 - 二叉排序树、平衡树、红黑树---转

    http://blog.csdn.net/mazhimazh/article/details/19961017 为了理解 TreeMap 的底层实现,必须先介绍排序二叉树和平衡二叉树,然后继续介绍红黑 ...

  7. 【Java】 大话数据结构(11) 查找算法(2)(二叉排序树/二叉搜索树)

    本文根据<大话数据结构>一书,实现了Java版的二叉排序树/二叉搜索树. 二叉排序树介绍 在上篇博客中,顺序表的插入和删除效率还可以,但查找效率很低:而有序线性表中,可以使用折半.插值.斐 ...

  8. 二叉排序树的理解和实现(Java)

    二叉排序树的定义和性质 二叉排序树又称二叉排序树.它或者是一个空树,或者是一个具有下列性质的二叉树: 若它的左子树不空,则左子树上所有节点的值均小于它的根结构的值 若它的右子树不空,则右子树上所有结点 ...

  9. 【LeetCode-面试算法经典-Java实现】【109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)】

    [109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 ...

随机推荐

  1. python_集合_笔记

    集合 特性: a.确定性(元素必须可以hash) b.互异性(去重) c.无序性(集合中的元素没有先后之分) 集合关系测试 交集 & jihe1.intersection(jihe2) 差集 ...

  2. Swift-8-枚举

    // Playground - noun: a place where people can play import UIKit // 枚举语法 enum SomeEnumeration { // e ...

  3. ApexSql Log 2016破解版&补丁

    绿色破解版: http://download.csdn.net/detail/gsyifan/9316993 官网: https://www.apexsql.com/sql_tools_log.asp ...

  4. 华为OJ平台试题 —— 数组:输入n个整数,输出当中最小的k个

    输入n个整数.输出当中最小的k个: 代码: /*  * 输入n个整数,输出当中最小的k个.  * 输入说明:1.输入两个整数:2.输入一个整数数组  * 输出说明:输出一个整数数组  */ <p ...

  5. 高质量JavaScript代码

    才华横溢的Stoyan Stefanov,在他写的由O’Reilly初版的新书<JavaScript Patterns>(JavaScript模式)中,我想要是为我们的读者贡献其摘要,那会 ...

  6. word页眉与页脚详解

    1.如何隔离封面等不需要插入页码的页面: 首先插入分节符下一页(一定是分节符),再在下一页(即要开始插入页码的页面)选择视图-->页眉和页脚-->设置为取消链接到前一页.设置页码格式为起始 ...

  7. PHP 代码规范

    FIG制定的 PHP 规范,简称 PSR,是 PHP 开发的事实标准.FIG 是 Framework Interoperability Group (框架可互用小组) 的缩写,由几位开源框架的开发者成 ...

  8. python 之 多线程

    一.多线程(具体可参照博文多进程---->http://www.cnblogs.com/work115/p/5621789.html) 1.函数式实现多线程 2.类实现多线程 3.多线程之线程锁 ...

  9. boost::interprocess::managed_shared_memory(2)(std::string)

    #include <iostream> #include <boost/interprocess/managed_shared_memory.hpp> #include < ...

  10. c#的小技巧

    很多.net的使用小技巧,总是要自己记下来的,给自己. 一:时间格式话中H和h的区别 DateTime.ToString("yyyy-MM-dd HH:mm:ss");//转化成2 ...