JAVA二叉树的基本操作
目录
记录二叉树的基本操作DEMO
1、创建一个二叉树类
2、然后创建二叉树的节点
记录二叉树的基本操作DEMO
1、创建一个二叉树类
这里约束了泛型只能为实现了Comparable这个接口的类型。
/**
* @author JackHui
* @version BinaryTree.java, 2020年03月05日 12:45
*/
public class BinaryTree {
//树根
BinaryTreeNode root;
public boolean deleteData(T data) {
if (root.data.equals(data)) {
root = null;
return true;
}
return root.deleteNode(data);
}
public T frontSearch(T data) {
return (T) root.frontSearch(data);
}
public T midSearch(T data) {
return (T) root.midSearch(data);
}
public T rearSearch(T data) {
return (T) root.rearSearch(data);
}
public void frontEach() {
this.root.frontEach();
}
public void midEach() {
this.root.midEach();
}
public void rearEach() {
this.root.rearEach();
}
public BinaryTreeNode getRoot() {
return root;
}
public void setRoot(BinaryTreeNode root) {
this.root = root;
}
}
2、然后创建二叉树的节点
package binarytree;
/**
* @author JackHui
* @version BinaryTreeNode.java, 2020年03月06日 10:24
*/
public class BinaryTreeNode {
T data;
BinaryTreeNode lChild;
BinaryTreeNode rChild;
public BinaryTreeNode(T data) {
this.data = https://www.it610.com/article/data;
}
//先序遍历
public void frontEach() {
System.out.print(this.data +"\t");
if (lChild != null) {
lChild.frontEach();
}
if (rChild != null) {
rChild.frontEach();
}
}
//中序遍历
public void midEach() {
if (lChild != null) {
lChild.frontEach();
}
System.out.print(this.data + "\t");
if (rChild != null) {
rChild.frontEach();
}
}
//后序遍历
public void rearEach() {
if (lChild != null) {
lChild.frontEach();
}
if (rChild != null) {
rChild.frontEach();
}
System.out.print(this.data + "\t");
}
//先序查找
public T frontSearch(T data) {
T target = null;
System.out.println("[先序遍历]当前遍历到的元素:" + this.data + "\t查找的元素:" + data + "\t" + (this.data.compareTo(data) == 0 ? "查找到元素:" + data : ""));
if (this.data.compareTo(data) == 0) {
return data;
} else {
if (lChild != null && (target = (T) lChild.frontSearch(data)) != null) {
return target;
}
if (rChild != null && (target = (T) rChild.frontSearch(data)) != null) {
return target;
}
}
return target;
}
//中序查找
public T midSearch(T data) {
T target = null;
if (lChild != null && (target = (T) lChild.midSearch(data)) != null) {
return target;
}
System.out.println("[中序遍历]当前遍历到的元素:" + this.data + "\t查找的元素:" + data + "\t" + (this.data.compareTo(data) == 0 ? "查找到元素:" + data : ""));
if (this.data.compareTo(data) == 0) {
return data;
} else {
if (rChild != null && (target = (T) rChild.midSearch(data)) != null) {
return target;
}
}
return target;
}
//后序查找
public T rearSearch(T data) {
T target = null;
if (lChild != null && (target = (T) lChild.rearSearch(data)) != null) {
return target;
}
if (rChild != null && (target = (T) rChild.rearSearch(data)) != null) {
return target;
}
System.out.println("[后续遍历]当前遍历到的元素:" + this.data + "\t查找的元素:" + data + "\t" + (this.data.compareTo(data) == 0 ? "查找到元素:" + data : ""));
if (this.data.compareTo(data) == 0) {
return data;
}
return target;
}
//根据值删除节点
public boolean deleteNode(T data) {
System.out.println("[节点删除]当前遍历到的父节点:" + this.data + "\t" + "匹配的节点数据:" + data);
//判断左子树是否匹配
if (this.lChild != null && (this.lChild.data.compareTo(data) == 0)) {
System.out.println("[节点删除]当前遍历到的父节点:" + this.data + "\t" + "匹配的节点数据:" + data + "\t节点删除成功!");
this.lChild = null;
return true;
} else if (this.rChild != null && (this.rChild.data.compareTo(data) == 0)) {
System.out.println("[节点删除]当前遍历到的父节点:" + this.data + "\t" + "匹配的节点数据:" + data + "\t节点删除成功!");
this.rChild = null;
return true;
}
if (this.lChild != null && this.lChild.deleteNode(data)) {
return true;
}
if (this.rChild != null && this.rChild.deleteNode(data)) {
return true;
}
return false;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = https://www.it610.com/article/data;
}
public BinaryTreeNode getlChild() {
return lChild;
}
public void setlChild(BinaryTreeNode lChild) {
this.lChild = lChild;
}
public BinaryTreeNode getrChild() {
return rChild;
}
public void setrChild(BinaryTreeNode rChild) {
this.rChild = rChild;
}
}
【JAVA二叉树的基本操作】到此这篇关于JAVA二叉树的基本操作DEMO的文章就介绍到这了
推荐阅读
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- 【生信技能树】R语言练习题|【生信技能树】R语言练习题 - 中级
- 事件代理
- Java|Java OpenCV图像处理之SIFT角点检测详解
- java中如何实现重建二叉树
- 数组常用方法一
- 【Hadoop踩雷】Mac下安装Hadoop3以及Java版本问题
- 种树郭橐驼传(文言句式+古今异义+词类活用+通假字)
- Java|Java基础——数组
- RxJava|RxJava 在Android项目中的使用(一)