Java二叉排序树(转)
一、二叉排序树定义
1.二叉排序树的定义
二叉排序树(Binary Sort Tree)又称二叉查找(搜索)树(Binary Search Tree)。其定义为:二叉排序树或者是空树,或者是满足如下性质的二叉树:
①若它的左子树非空,则左子树上所有结点的值均小于根结点的值;
②若它的右子树非空,则右子树上所有结点的值均大于根结点的值;
③左、右子树本身又各是一棵二叉排序树。
上述性质简称二叉排序树性质(BST性质),故二叉排序树实际上是满足BST性质的二叉树。
2.二叉排序树的性质
按中序遍历二叉排序树,所得到的中序遍历序列是一个递增有序序列。
3.二叉排序树的插入
在二叉排序树中插入新结点,要保证插入后的二叉树仍符合二叉排序树的定义。
插入过程:
若二叉排序树为空,则待插入结点*S作为根结点插入到空树中;
当非空时,将待插结点关键字S->key和树根关键字t->key进行比较,若s->key = t->key,则无须插入,若s->key< t->key,则插入到根的左子树中,若s->key> t->key,则插入到根的右子树中。而子树中的插入过程和在树中的插入过程相同,如此进行下去,直到把结点*s作为一个新的树叶插入到二叉排序树中,或者直到发现树已有相同关键字的结点为止。
4.二叉排序树的查找
假定二叉排序树的根结点指针为 root ,给定的关键字值为 K ,则查找算法可描述为:
① 置初值: q = root ;
② 如果 K = q -> key ,则查找成功,算法结束;
③ 否则,如果 K < q -> key ,而且 q 的左子树非空,则将 q 的左子树根送 q ,转步骤②;否则,查找失败,结束算法;
④ 否则,如果 K > q -> key ,而且 q 的右子树非空,则将 q 的右子树根送 q ,转步骤②;否则,查找失败,算法结束。
5.二叉排序树的删除
假设被删结点是*p,其双亲是*f,不失一般性,设*p是*f的左孩子,下面分三种情况讨论:
⑴ 若结点*p是叶子结点,则只需修改其双亲结点*f的指针即可。
⑵ 若结点*p只有左子树PL或者只有右子树PR,则只要使PL或PR 成为其双亲结点的左子树即可。
⑶ 若结点*p的左、右子树均非空,先找到*p的中序前趋(或后继)结点*s(注意*s是*p的左子树中的最右下的结点,它的右链域为空),然后有两种做法:① 令*p的左子树直接链到*p的双亲结点*f的左链上,而*p的右子树链到*p的中序前趋结点*s的右链上。② 以*p的中序前趋结点*s代替*p(即把*s的数据复制到*p中),将*s的左子树链到*s的双亲结点*q的左(或右)链上。
6、二叉树的遍历
二叉树的遍历有三种方式,如下:
(1)前序遍历(DLR),首先访问根结点,然后遍历左子树,最后遍历右子树。简记根-左-右。
(2)中序遍历(LDR),首先遍历左子树,然后访问根结点,最后遍历右子树。简记左-根-右。
(3)后序遍历(LRD),首先遍历左子树,然后遍历右子树,最后访问根结点。简记左-右-根。
二、代码编写
1、树节点类的定义
- package com.lin;
- /**
- * 功能概要:
- *
- * @author linbingwen
- * @since 2015年8月29日
- */
- public class TreeNode {
- public Integer data;
- /*该节点的父节点*/
- public TreeNode parent;
- /*该节点的左子节点*/
- public TreeNode left;
- /*该节点的右子节点*/
- public TreeNode right;
- public TreeNode(Integer data) {
- this.data = data;
- }
- @Override
- public String toString() {
- return "TreeNode [data=" + data + "]";
- }
- }
2、二叉排序树的定义
- package com.lin;
- /**
- * 功能概要:排序/平衡二叉树
- *
- * @author linbingwen
- * @since 2015年8月29日
- */
- public class SearchTree {
- public TreeNode root;
- public long size;
- /**
- * 往树中加节点
- * @author linbingwen
- * @since 2015年8月29日
- * @param data
- * @return Boolean 插入成功返回true
- */
- public Boolean addTreeNode(Integer data) {
- if (null == root) {
- root = new TreeNode(data);
- System.out.println("数据成功插入到平衡二叉树中");
- return true;
- }
- TreeNode treeNode = new TreeNode(data);// 即将被插入的数据
- TreeNode currentNode = root;
- TreeNode parentNode;
- while (true) {
- parentNode = currentNode;// 保存父节点
- // 插入的数据比父节点小
- if (currentNode.data > data) {
- currentNode = currentNode.left;
- // 当前父节点的左子节点为空
- if (null == currentNode) {
- parentNode.left = treeNode;
- treeNode.parent = parentNode;
- System.out.println("数据成功插入到二叉查找树中");
- size++;
- return true;
- }
- // 插入的数据比父节点大
- } else if (currentNode.data < data) {
- currentNode = currentNode.right;
- // 当前父节点的右子节点为空
- if (null == currentNode) {
- parentNode.right = treeNode;
- treeNode.parent = parentNode;
- System.out.println("数据成功插入到二叉查找树中");
- size++;
- return true;
- }
- } else {
- System.out.println("输入数据与节点的数据相同");
- return false;
- }
- }
- }
- /**
- * 查找数据
- * @author linbingwen
- * @since 2015年8月29日
- * @param data
- * @return TreeNode
- */
- public TreeNode findTreeNode(Integer data){
- if(null == root){
- return null;
- }
- TreeNode current = root;
- while(current != null){
- if(current.data > data){
- current = current.left;
- }else if(current.data < data){
- current = current.right;
- }else {
- return current;
- }
- }
- return null;
- }
- }
这里暂时只放了一个增加和查找的方法
3、前、中、后遍历
- package com.lin;
- import java.util.Stack;
- /**
- * 功能概要:
- *
- * @author linbingwen
- * @since 2015年8月29日
- */
- public class TreeOrder {
- /**
- * 递归实现前序遍历
- * @author linbingwen
- * @since 2015年8月29日
- * @param treeNode
- */
- public static void preOrderMethodOne(TreeNode treeNode) {
- if (null != treeNode) {
- System.out.print(treeNode.data + " ");
- if (null != treeNode.left) {
- preOrderMethodOne(treeNode.left);
- }
- if (null != treeNode.right) {
- preOrderMethodOne(treeNode.right);
- }
- }
- }
- /**
- * 循环实现前序遍历
- * @author linbingwen
- * @since 2015年8月29日
- * @param treeNode
- */
- public static void preOrderMethodTwo(TreeNode treeNode) {
- if (null != treeNode) {
- Stack<TreeNode> stack = new Stack<TreeNode>();
- stack.push(treeNode);
- while (!stack.isEmpty()) {
- TreeNode tempNode = stack.pop();
- System.out.print(tempNode.data + " ");
- // 右子节点不为null,先把右子节点放进去
- if (null != tempNode.right) {
- stack.push(tempNode.right);
- }
- // 放完右子节点放左子节点,下次先取
- if (null != tempNode.left) {
- stack.push(tempNode.left);
- }
- }
- }
- }
- /**
- * 递归实现中序遍历
- * @author linbingwen
- * @since 2015年8月29日
- * @param treeNode
- */
- public static void medOrderMethodOne(TreeNode treeNode){
- if (null != treeNode) {
- if (null != treeNode.left) {
- medOrderMethodOne(treeNode.left);
- }
- System.out.print(treeNode.data + " ");
- if (null != treeNode.right) {
- medOrderMethodOne(treeNode.right);
- }
- }
- }
- /**
- * 循环实现中序遍历
- * @author linbingwen
- * @since 2015年8月29日
- * @param treeNode
- */
- public static void medOrderMethodTwo(TreeNode treeNode){
- Stack<TreeNode> stack = new Stack<TreeNode>();
- TreeNode current = treeNode;
- while (current != null || !stack.isEmpty()) {
- while(current != null) {
- stack.push(current);
- current = current.left;
- }
- if (!stack.isEmpty()) {
- current = stack.pop();
- System.out.print(current.data+" ");
- current = current.right;
- }
- }
- }
- /**
- * 递归实现后序遍历
- * @author linbingwen
- * @since 2015年8月29日
- * @param treeNode
- */
- public static void postOrderMethodOne(TreeNode treeNode){
- if (null != treeNode) {
- if (null != treeNode.left) {
- postOrderMethodOne(treeNode.left);
- }
- if (null != treeNode.right) {
- postOrderMethodOne(treeNode.right);
- }
- System.out.print(treeNode.data + " ");
- }
- }
- /**
- * 循环实现后序遍历
- * @author linbingwen
- * @since 2015年8月29日
- * @param treeNode
- */
- public static void postOrderMethodTwo(TreeNode treeNode){
- if (null != treeNode) {
- Stack<TreeNode> stack = new Stack<TreeNode>();
- TreeNode current = treeNode;
- TreeNode rightNode = null;
- while(current != null || !stack.isEmpty()) {
- while(current != null) {
- stack.push(current);
- current = current.left;
- }
- current = stack.pop();
- while (current != null && (current.right == null ||current.right == rightNode)) {
- System.out.print(current.data + " ");
- rightNode = current;
- if (stack.isEmpty()){
- System.out.println();
- return;
- }
- current = stack.pop();
- }
- stack.push(current);
- current = current.right;
- }
- }
- }
- }
4、使用方法
- package com.lin;
- /**
- * 功能概要:
- *
- * @author linbingwen
- * @since 2015年8月29日
- */
- public class SearchTreeTest {
- /**
- * @author linbingwen
- * @since 2015年8月29日
- * @param args
- */
- public static void main(String[] args) {
- SearchTree tree = new SearchTree();
- tree.addTreeNode(50);
- tree.addTreeNode(80);
- tree.addTreeNode(20);
- tree.addTreeNode(60);
- tree.addTreeNode(10);
- tree.addTreeNode(30);
- tree.addTreeNode(70);
- tree.addTreeNode(90);
- tree.addTreeNode(100);
- tree.addTreeNode(40);
- System.out.println("=============================="+"采用递归的前序遍历开始"+"==============================");
- TreeOrder.preOrderMethodOne(tree.root);
- System.out.println();
- System.out.println("=============================="+"采用循环的前序遍历开始"+"==============================");
- TreeOrder.preOrderMethodTwo(tree.root);
- System.out.println();
- System.out.println("=============================="+"采用递归的后序遍历开始"+"==============================");
- TreeOrder.postOrderMethodOne(tree.root);
- System.out.println();
- System.out.println("=============================="+"采用循环的后序遍历开始"+"==============================");
- TreeOrder.postOrderMethodTwo(tree.root);
- System.out.println();
- System.out.println("=============================="+"采用递归的中序遍历开始"+"==============================");
- TreeOrder.medOrderMethodOne(tree.root);
- System.out.println();
- System.out.println("=============================="+"采用循环的中序遍历开始"+"==============================");
- TreeOrder.medOrderMethodTwo(tree.root);
- }
- }
输出结果:
同样,进行查找过程如下:
- TreeNode node = tree.findTreeNode(100);
- System.out.println(node);
结果是正确的
http://blog.csdn.net/evankaka/article/details/48088241
Java二叉排序树(转)的更多相关文章
- java二叉排序树
二叉排序树又称二叉查找树.它或者是一颗空树,或者是具有如下性质的二叉树: 1.如果左子树不空,那么左子树上的所有节点均小于它的根节点的值: 2.如果右子树不空,那么右子树上的所有节点均大于它的根节点的 ...
- 算法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实现][全部题目文件夹索引] 原题 ...
随机推荐
- 60s 经济学探奇
理解经济学 什么是经济学.对于学习金融的同学,一定会给你搬出一大堆定义.例证.学派.说经济学是一门研究研究价值的生产.流通.分配.消费的规律的理论. 非常高大上的感觉,可是对于我这样没有什么金融学理论 ...
- 魔棒工具--RegionGrow算法简介
原地址:http://www.cnblogs.com/easymind223/archive/2012/07/04/2576964.html ps里面的魔棒工具非常好用,是图像处理中非常常用的一个工具 ...
- Mod in math
An Introduction to Modular Math When we divide two integers we will have an equation that looks like ...
- WebService的相关使用
近期公司项目使用WebService ,这里简单做个总结. 事实上详细使用细节有些情况下须要改,还须要看实际情况,须要与server联调,详细沟通. 比方公司连接,非要把envelope.dotNet ...
- SmartDraw2008破解过程总结
SmartDraw2008破解过程总结作者:chszs 原创转载请保留作者名. 按下列步骤完毕,保证能够支持中文. 一.所需软件:1)SmartDraw2008安装软件:2)SmartDraw200 ...
- java多线程:ReentrantReadWriteLock读写锁使用
Lock比传统的线程模型synchronized更多的面向对象的方式.锁和生活似,应该是一个对象.两个线程运行的代码片段要实现同步相互排斥的效果.它们必须用同一个Lock对象. 读写锁:分为读锁和写锁 ...
- C#语言实现ArcGIS数据源重置之Set Data Source功能
1.须要:依据选择的Mxd路径和目标数据源路径进行重置数据源.此处以(.Mdb为例): 主要利用到的接口: (1)IMapDocument (2)IMapControl2 (3)IWor ...
- C语言文件操作之fgets()
来说一说fgets(..)函数. 原型 char * fgets(char * s, int n,FILE *stream); 參数: s: 字符型指针, ...
- Android开发之使用URL訪问网络资源
Android开发之使用URL訪问网络资源 URL (UniformResource Locator)对象代表统一资源定位器,它是指向互联网"资源"的指针. 资源能够是简单的文件或 ...
- NEU月赛Segment Balls(线段树)
问题 D: Segment Balls 时间限制: 1 Sec 内存限制: 128 MB 提交: 253 解决: 37 题目描述 Small K has recently earn money i ...