Java实现二叉搜索树的添加,前序、后序、中序及层序遍历,求树的节点数,求树的最大值、最小值,查找等操作
什么也不说了,直接上代码。
首先是节点类,大家都懂得
/**
* 二叉树的节点类
*
* @author HeYufan
*
* @param <T>
*/
class Node<T extends Comparable<? super T>>
{
/**
* 节点储存的值
*/
private T data;
/**
* 左子节点
*/
private Node<T> leftNode;
/**
* 右子节点
*/
private Node<T> rightNode; public Node()
{
this(null);
} public Node(T data)
{
this.data = data;
this.leftNode = null;
this.rightNode = null;
} /**
* @return data
*/
public T getData()
{
return data;
} /**
* @param data
* 要设置的 data
*/
public void setData(T data)
{
this.data = data;
} /**
* @return leftNode
*/
public Node<T> getLeftNode()
{
return leftNode;
} /**
* @param leftNode
* 要设置的 leftNode
*/
public void setLeftNode(Node<T> leftNode)
{
this.leftNode = leftNode;
} /**
* @return rightNode
*/
public Node<T> getRightNode()
{
return rightNode;
} /**
* @param rightNode
* 要设置的 rightNode
*/
public void setRightNode(Node<T> rightNode)
{
this.rightNode = rightNode;
} }
然后是二叉搜索树的实现
/**
*
*/
package dataStructure.tree.binarytree; import java.util.LinkedList;
import java.util.Queue; /**
* 二叉排序树
*
* @author HeYufan
*
*/
public class BinaryTree<T extends Comparable<? super T>>
{
// 二叉树的根节点
private Node<T> root;
// 二叉树的节点总数
private int size; public BinaryTree()
{
this.root = null;
this.size = 0;
} // 根据一个节点生成二叉树(该节点作为根节点)
public BinaryTree(Node<T> root)
{
this.root = root;
this.size = 1;
} /**
* 插入
*
* @param value
* 要插入二叉树的值
*/
public void insert(T value)
{
root = insert(this.root, value);
} /**
* 二叉树的节点总数
*
* @return
*/
public int size()
{
return this.size;
} /**
* 二叉树是否为空树
*
* @return
*/
public boolean isEmpty()
{
return this.size == 0;
} /**
* 检测二叉树的节点是否包含value
*
* @param value
* @return
*/
public boolean isContain(T value)
{
return isContain(root, value);
} /**
* 递归比较node节点的值与value
*
* @param node
* @param value
* @return
*/
private boolean isContain(Node<T> node, T value)
{
// 如果指定节点为空,说明当前二叉树不包含value,返回false
if(node == null)
{
return false;
} // node节点的值与value比较的结果
int result = node.getData().compareTo(value);
// 如果result等于0,说明node节点的值与value值相同,说明二叉搜索树包含value,返回true
if(result == 0)
{
return true;
}
// 如果result小于0,说明node节点的值小于value,继续递归比较node的右子节点与value
else if(result < 0)
{
return isContain(node.getRightNode(), value);
}
// 如果result大于0,说明node节点的值大于value,继续递归比较node的左子节点与value
else
{
return isContain(node.getLeftNode(), value);
}
} /**
* 获取二叉树中的最小值
*
* @return
*/
public T getMin()
{
// 如果二叉树为空,则返回null
if(isEmpty())
{
return null;
}
return getMin(root).getData();
} /**
* 根据二叉搜索树的性质,我们可以知道一颗二叉搜索树的最小值一定是这棵树最左边的节点
*
* @param node
* 要搜索的根节点
* @return
*/
private Node<T> getMin(Node<T> node)
{
Node<T> leftNode = node.getLeftNode();
// 如果当前节点的左节点为空,说明这个节点的值就是这棵树最小的值
if(leftNode == null)
{
return node;
}
else
{
// 否则,递归遍历当前节点的左子树
return getMin(leftNode);
}
} /**
* 获取二叉树中的最大值(不采用递归方式,递归方式类似getMin)
*
* @return
*/
public T getMax()
{
// 如果当前二叉树为空,返回null
if(isEmpty())
{
return null;
}
Node<T> rightNode = this.root;
// 判断当前节点(从root节点开始)是否有右子节点
// 如果没有,说明当前节点的值就是该二叉搜索树的做大值,当前循环结束
// 否则,让rightNode指向当前节点的右子节点,继续while循环
while (rightNode.getRightNode() != null)
{
rightNode = rightNode.getRightNode();
}
return rightNode.getData();
} /**
* 将值value插入到以node为根节点的二叉树下
*
* @param node
* 目标子树的根节点
* @param value
* 要插入的值
* @return 这里返回的node其实就是更新后的根节点
*/
private Node<T> insert(Node<T> node, T value)
{
// 如果要插入的目标根节点为null,则这个节点就是要插入值的地方
if(node == null)
{
node = new Node<T>(value);
this.size++;
return node;
}
// 0表示value等于node的值;-1表示value小于node的值;1表示value大于node的值
int result = value.compareTo(node.getData());
if(result < 0)
{
// 当value小于node的值,则将value插入到node节点的左子树中,此时value的目标节点就是node.getLeftNode()
node.setLeftNode(insert(node.getLeftNode(), value));
}
else if(result > 0)
{
// 当value大于node的值,则将value插入到node节点的右子树中,此时value的目标节点就是node.getRightNode()
node.setRightNode(insert(node.getRightNode(), value));
}
else
{
// 如果result = 0,说明插入的值已经存在了,可以选择更新值或者什么也不做
}
return node;
} /**
* 先序遍历
*/
public void preorder()
{
preorder(root);
} /**
* 后序遍历
*/
public void postorder()
{
postorder(root);
} /**
* 中序遍历
*/
public void inorder()
{
inorder(root);
} /**
* 层序遍历
*/
public void level()
{
if(root == null)
return;
Queue<Node<T>> queue = new LinkedList<Node<T>>();
queue.add(root);
Node<T> node = null;
while (queue.size() != 0)
{
node = queue.poll();
System.out.println(node.getData());
if(node.getLeftNode() != null)
queue.add(node.getLeftNode());
if(node.getRightNode() != null)
queue.add(node.getRightNode());
}
} /**
* 先序遍历:遍历以node为根节点的子树 先打印节点的值,再遍历节点的左子树,最后遍历节点的右子树
*
* @param node
* 要遍历的子树的根节点
*/
private void preorder(Node<T> node)
{
if(node == null)
{
return;
}
else
{
// 打印节点的值
System.out.println(node.getData());
// 递归遍历该节点的左子树
preorder(node.getLeftNode());
// 递归遍历该节点的右子树
preorder(node.getRightNode());
}
} /**
* 后序遍历:遍历以node为根节点的子树;先遍历节点的左子树,再遍历节点的右子树,最后打印节点的值
*
* @param node
* 要遍历的子树的根节点
*/
private void postorder(Node<T> node)
{
if(node == null)
{
return;
}
else
{
// 递归遍历该节点的左子树
postorder(node.getLeftNode());
// 递归遍历该节点的右子树
postorder(node.getRightNode());
// 打印节点的值
System.out.println(node.getData());
}
} /**
* 中序遍历:遍历以node为根节点的子树;先遍历左子树,再打印节点的值,最后遍历右子树
*
* @param node
*/
private void inorder(Node<T> node)
{
if(node == null)
{
return;
}
else
{
// 递归遍历该节点的左子树
inorder(node.getLeftNode());
// 打印节点的值
System.out.println(node.getData());
// 递归遍历该节点的右子树
inorder(node.getRightNode());
}
}
}
测试:
/**
*
*/
package dataStructure.test; import dataStructure.tree.binarytree.BinaryTree; /**
* @author Administrator
*
*/
public class TestBinaryTree
{
public static void main(String[] args)
{
BinaryTree<Integer> tree = new BinaryTree<Integer>();
tree.insert(5);
tree.insert(3);
tree.insert(2);
tree.insert(7);
tree.insert(8);
tree.insert(4);
tree.insert(1);
tree.insert(4);
tree.insert(1);
System.out.println("是否包含1:" + tree.isContain(1));
System.out.println("是否包含10:" + tree.isContain(10));
System.out.println("树的节点数:" + tree.size());
System.out.println("树的最大值:" + tree.getMax());
System.out.println("树的最小值:" + tree.getMin());
System.out.println("先序遍历:");
tree.preorder();
System.out.println("后序遍历:");
tree.postorder();
System.out.println("中序遍历:");
tree.inorder();
System.out.println("层序遍历:");
tree.level(); }
}
树的结构:
输出结果:
是否包含1:true
是否包含10:false
树的节点数:7
树的最大值:8
树的最小值:1
先序遍历:
5
3
2
1
4
7
8
后序遍历:
1
2
4
3
8
7
5
中序遍历:
1
2
3
4
5
7
8
层序遍历:
5
3
7
2
4
8
1
Java实现二叉搜索树的添加,前序、后序、中序及层序遍历,求树的节点数,求树的最大值、最小值,查找等操作的更多相关文章
- Java实现二叉搜索树
原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11406176.html 尝试一下用Java实现二叉搜索树/二叉查找树,记录自己的学习历程. 1 ...
- Java实现二叉搜索树的插入、删除
前置知识 二叉树的结构 public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode() { } TreeNode( ...
- Java数据结构——二叉搜索树
定义二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若 ...
- 【剑指offer】面试题24:二叉搜索树的兴许前序遍历序列
分析: 前序: 根 左 右 后序: 左 由 根 二叉搜索树: 左 < 根 < 右 那么这就非常明显了. def ifpost(postArray, start, end): #one or ...
- Java创建二叉搜索树,实现搜索,插入,删除操作
Java实现的二叉搜索树,并实现对该树的搜索,插入,删除操作(合并删除,复制删除) 首先我们要有一个编码的思路,大致如下: 1.查找:根据二叉搜索树的数据特点,我们可以根据节点的值得比较来实现查找,查 ...
- L2-004. 这是二叉搜索树吗?(前序转后序递归)
L2-004. 这是二叉搜索树吗? 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 一棵二叉搜索树可被递归地定义为具有下列性质的 ...
- Java实现二叉搜索树及相关操作
package com.tree; import com.tree.BitNode; /** * * 二叉搜索树:一个节点的左子节点的关键字小于这个节点.右子节点的关键字大于或等于这个父节点 * * ...
- 剑指offer-面试题54-二叉搜索树的第k大节点-中序遍历
/* 题目: 求二叉搜索树的第k大节点. */ /* 思路: 中序遍历. */ #include<iostream> #include<cstring> #include< ...
- Java对二叉搜索树进行插入、查找、遍历、最大值和最小值的操作
1.首先,须要一个节点对象的类.这些对象包括数据.数据代表存储的内容,并且还有指向节点的两个子节点的引用 class Node { public int iData; public double dD ...
随机推荐
- PHP函数十进制、二进制、八进制和十六进制转换
PHP函数篇详解十进制.二进制.八进制和十六进制互相转换函数说明,主要掌握各进制转换的方法,以应用于实际开发. 一,十进制(decimal system)转换函数说明 1,十进制转二进制 decbin ...
- ## GridView 布局:item设置的高度和宽度不起作用、自动适配列数、添加Header和Footer ##
一.item设置的高度和宽度不起作用 转自:http://www.cnblogs.com/0616--ataozhijia/p/6031875.html [Android Pro] listView和 ...
- RBF径向基神经网络——乳腺癌医学诊断建模
案例描述 近年来疾病早期诊断越来越受到医学专家的重视,从而产生了各种疾病诊断的新方法.乳癌最早的表现是患乳出现单发的.无痛性并呈进行性生长的小肿块.肿块位于外上象限最多见,其次是乳头.乳晕区和内上象限 ...
- hdu 5493 Queue(线段树)
Problem Description N people numbered to N are waiting in a bank for service. They all stand in a qu ...
- Spring的AOP2
本文是<AOP 那点事儿>的续集. 在上篇中,我们从写死代码,到使用代理:从编程式 Spring AOP 到声明式 Spring AOP.一切都朝着简单实用主义的方向在发展.沿着 Spri ...
- kettle工具同步数据乱码-Linux下乱码问题二
将写好的kettle工程部署到Linux下后,同步的数据都成了乱码,幸运的是数据库有备份. 下面就说一下,kettle工程如何同步两端编码格式都是utf8的数据库. 我们只需要更改kettle数据库连 ...
- c# 高斯模糊
using System; using System.Collections.Generic; using System.Text; using System.Drawing; using Syste ...
- RMAN-format变量及configuration配置项
一.FORMAT字符串替代变量使用FORMAT参数时可使用的各种替换变量,如下:%c:备份片的拷贝数(从1开始编号):%d:数据库名称:%D:位于该月中的天数 (DD):%M:位于该年中的月份 (MM ...
- mysql1主多从配置
mysql一主多从的配置: 其实1主多从的配置与一主一从配置非常相似,现在主要讲讲一主多从的大概配置方法. 一, 1,master端开启binlog日志,并且设置server id=1. 2.重启服务 ...
- Illustrated C#学习笔记(一)
迄今为止最容易看懂的一本C#入门图书,的确是,很不错的一本书,继续读下去,并做好相关笔记吧. Chapter 1 C#和.NET框架 主要讲述了一些.NET框架下的一些不明觉厉的名词如CLR,CLI. ...