目录
- 记录二叉树的基本操作DEMO
- 1、创建一个二叉树类
- 2、然后创建二叉树的节点
记录二叉树的基本操作DEMO
1、创建一个二叉树类
这里约束了泛型只能为实现了Comparable这个接口的类型。
public class BinaryTree<T extends Comparable> {
//树根
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;
public class BinaryTreeNode<T extends Comparable> {
T data;
BinaryTreeNode lChild;
BinaryTreeNode rChild;
public BinaryTreeNode(T data) {
this.data = 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 = 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二叉树的基本操作DEMO的文章就介绍到这了,更多相关JAVA二叉树内容请搜索编程界以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程界!