JAVA 实现二叉树(链式存储结构)
作者:lqh 时间:2022-02-18 11:35:21
二叉树的分类(按存储结构)
树的分类(按存储结构)
顺序存储(用数组表示(静态二叉树))
链式存储
一些特别的二叉根:
完全二叉树,平衡二叉树(AVL),线索二叉树,三叉的(带父亲的指针)
二叉搜索树或者叫二叉查找树(BST)
所用二叉树如下图所示:
二叉树的Java实现(链式存储结构)
class TreeNode {
private int key = 0;
private String data = null;
private boolean isVisted = false;
private TreeNode leftChild = null;
private TreeNode rightChild = null;
public TreeNode(){
}
public TreeNode(int key, String data){
this.key = key;
this.data = data;
this.leftChild = null;
this.rightChild = null;
}
public int getKey() {
return key;
}
public void setKey(int key) {
this.key = key;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public TreeNode getLeftChild() {
return leftChild;
}
public void setLeftChild(TreeNode leftChild) {
this.leftChild = leftChild;
}
public TreeNode getRightChild() {
return rightChild;
}
public void setRightChild(TreeNode rightChild) {
this.rightChild = rightChild;
}
public boolean isVisted() {
return isVisted;
}
public void setVisted(boolean isVisted) {
this.isVisted = isVisted;
}
}
public class BinaryTree {
private TreeNode root = null;
public BinaryTree() {
root = new TreeNode(1, "rootNode(A)");
}
public void createBinTree(TreeNode root){
//手动的创建(结构如图所示)
TreeNode newNodeB = new TreeNode(2,"B");
TreeNode newNodeC = new TreeNode(3,"C");
TreeNode newNodeD = new TreeNode(4,"D");
TreeNode newNodeE = new TreeNode(5,"E");
TreeNode newNodeF = new TreeNode(6,"F");
root.setLeftChild(newNodeB);
root.setRightChild(newNodeC);
root.getLeftChild().setLeftChild(newNodeD);
root.getLeftChild().setRightChild(newNodeE);
root.getRightChild().setRightChild(newNodeF);
}
public boolean IsEmpty() {
// 判二叉树空否
return root == null;
}
public int Height() {
// 求树高度
return Height(root);
}
public int Height(TreeNode subTree) {
if (subTree == null)
return 0; //递归结束:空树高度为0
else {
int i = Height(subTree.getLeftChild());
int j = Height(subTree.getRightChild());
return (i < j) ? j + 1 : i + 1;
}
}
public int Size() {
// 求结点数
return Size(root);
}
public int Size(TreeNode subTree) {
if (subTree == null)
return 0;
else {
return 1 + Size(subTree.getLeftChild())
+ Size(subTree.getRightChild());
}
}
public TreeNode Parent(TreeNode element) {
//返回双亲结点
return (root == null || root == element) ? null : Parent(root, element);
}
public TreeNode Parent(TreeNode subTree, TreeNode element) {
if (subTree == null)
return null;
if (subTree.getLeftChild() == element
|| subTree.getRightChild() == element)
//找到, 返回父结点地址
return subTree;
TreeNode p;
//先在左子树中找,如果左子树中没有找到,才到右子树去找
if ((p = Parent(subTree.getLeftChild(), element)) != null)
//递归在左子树中搜索
return p;
else
//递归在左子树中搜索
return Parent(subTree.getRightChild(), element);
}
public TreeNode LeftChild(TreeNode element) {
//返回左子树
return (element != null) ? element.getLeftChild() : null;
}
public TreeNode RightChild(TreeNode element) {
//返回右子树
return (element != null) ? element.getRightChild() : null;
}
public TreeNode getRoot() {
//取得根结点
return root;
}
public void destroy(TreeNode subTree) {
//私有函数: 删除根为subTree的子树
if (subTree != null) {
destroy(subTree.getLeftChild()); //删除左子树
destroy(subTree.getRightChild()); //删除右子树
//delete subTree; //删除根结点
subTree = null;
}
}
public void Traverse(TreeNode subTree) {
System.out.println("key:" + subTree.getKey() + "--name:"
+ subTree.getData());
Traverse(subTree.getLeftChild());
Traverse(subTree.getRightChild());
}
public void PreOrder(TreeNode subTree) {
//先根
if (subTree != null) {
visted(subTree);
PreOrder(subTree.getLeftChild());
PreOrder(subTree.getRightChild());
}
}
public void InOrder(TreeNode subTree) {
//中根
if (subTree != null) {
InOrder(subTree.getLeftChild());
visted(subTree);
InOrder(subTree.getRightChild());
}
}
public void PostOrder(TreeNode subTree) {
//后根
if (subTree != null) {
PostOrder(subTree.getLeftChild());
PostOrder(subTree.getRightChild());
visted(subTree);
}
}
public void LevelOrder(TreeNode subTree) {
//水平遍边
}
public boolean Insert(TreeNode element){
//插入
return true;
}
public boolean Find(TreeNode element){
//查找
return true;
}
public void visted(TreeNode subTree) {
subTree.setVisted(true);
System.out.println("key:" + subTree.getKey() + "--name:"
+ subTree.getData());
}
public static void main(String[] args) {
BinaryTree bt = new BinaryTree();
bt.createBinTree(bt.root);
System.out.println("the size of the tree is " + bt.Size());
System.out.println("the height of the tree is " + bt.Height());
System.out.println("*******先根(前序)[ABDECF]遍历*****************");
bt.PreOrder(bt.root);
System.out.println("*******中根(中序)[DBEACF]遍历*****************");
bt.InOrder(bt.root);
System.out.println("*******后根(后序)[DEBFCA]遍历*****************");
bt.PostOrder(bt.root);
}
}
结果输出:
the size of the tree is 6
the height of the tree is 3
*******先根(前序)[ABDECF]遍历*****************
key:1--name:rootNode(A)
key:2--name:B
key:4--name:D
key:5--name:E
key:3--name:C
key:6--name:F
*******中根(中序)[DBEACF]遍历*****************
key:4--name:D
key:2--name:B
key:5--name:E
key:1--name:rootNode(A)
key:3--name:C
key:6--name:F
*******后根(后序)[DEBFCA]遍历*****************
key:4--name:D
key:5--name:E
key:2--name:B
key:6--name:F
key:3--name:C
key:1--name:rootNode(A)
希望本文对学习JAVA程序设计的同学有所帮助。
标签:Java,二叉树
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
springmvc用于方法鉴权的注解拦截器的解决方案代码
2022-06-02 10:30:18
![](https://img.aspxhome.com/file/2023/7/77397_0s.jpg)
SpringBoot整合Shiro框架,实现用户权限管理
2021-10-27 00:03:02
java json 省市级联实例代码
2021-11-13 18:50:35
SpringBoot加密配置文件的SQL账号密码方式
2023-08-23 08:59:42
![](https://img.aspxhome.com/file/2023/9/64019_0s.png)
flutter实现扫码枪获取数据源禁止系统键盘弹窗示例详解
2023-07-23 01:52:41
![](https://img.aspxhome.com/file/2023/0/96760_0s.webp)
SpringBoot使用过滤器、拦截器和监听器的案例代码(Springboot搭建java项目)
2022-11-21 06:08:54
图解JVM垃圾内存回收算法
2023-10-13 17:24:35
![](https://img.aspxhome.com/file/2023/4/64804_0s.jpg)
Spring Cloud Gateway不同频率限流的解决方案(每分钟,每小时,每天)
2023-01-05 13:49:34
5分钟搭建SpringCloud Eureka服务注册中心的实现
2022-07-12 05:12:42
![](https://img.aspxhome.com/file/2023/0/79850_0s.png)
spring mvc+localResizeIMG实现HTML5端图片压缩上传
2023-07-11 15:31:51
C#开发Windows UWP系列之布局面板RelativePanel
2023-04-11 12:28:29
![](https://img.aspxhome.com/file/2023/4/81134_0s.png)
JetCache 缓存框架的使用及源码解析(推荐)
2021-07-23 12:21:25
![](https://img.aspxhome.com/file/2023/2/64402_0s.png)
Java如何实现自定义异常类
2023-06-21 23:44:01
一文带你了解C#中的协变与逆变
2022-08-06 22:31:21
RocketMQ特性Broker存储事务消息实现
2022-07-10 20:42:13
Java实现计算器设计
2023-08-18 13:36:54
![](https://img.aspxhome.com/file/2023/7/79347_0s.jpg)
Spring实战之注入集合值操作示例
2023-03-04 04:02:53
初学者Android studio安装图文详解
2022-08-06 07:22:12
![](https://img.aspxhome.com/file/2023/6/80886_0s.png)
浅谈Java中ArrayList线程不安全怎么办
2023-10-02 19:24:56
使用SpringMVC的@Validated注解验证的实现
2023-09-20 19:49:55