JAVA二叉樹的基本操作
更新時間:2021年08月26日 08:52:35 作者:JackHui007
這篇文章主要介紹了JAVA二叉樹的基本操作DEMO,想要詳情了解的小伙伴請接著看下文吧
記錄二叉樹的基本操作DEMO
1、創(chuàng)建一個二叉樹類
這里約束了泛型只能為實現(xiàn)了Comparable這個接口的類型。
/**
* @author JackHui
* @version BinaryTree.java, 2020年03月05日 12:45
*/
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、然后創(chuàng)建二叉樹的節(jié)點
package binarytree;
/**
* @author JackHui
* @version BinaryTreeNode.java, 2020年03月06日 10:24
*/
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("[后續(xù)遍歷]當前遍歷到的元素:" + this.data + "\t查找的元素:" + data + "\t" + (this.data.compareTo(data) == 0 ? "查找到元素:" + data : ""));
if (this.data.compareTo(data) == 0) {
return data;
}
return target;
}
//根據(jù)值刪除節(jié)點
public boolean deleteNode(T data) {
System.out.println("[節(jié)點刪除]當前遍歷到的父節(jié)點:" + this.data + "\t" + "匹配的節(jié)點數(shù)據(jù):" + data);
//判斷左子樹是否匹配
if (this.lChild != null && (this.lChild.data.compareTo(data) == 0)) {
System.out.println("[節(jié)點刪除]當前遍歷到的父節(jié)點:" + this.data + "\t" + "匹配的節(jié)點數(shù)據(jù):" + data + "\t節(jié)點刪除成功!");
this.lChild = null;
return true;
} else if (this.rChild != null && (this.rChild.data.compareTo(data) == 0)) {
System.out.println("[節(jié)點刪除]當前遍歷到的父節(jié)點:" + this.data + "\t" + "匹配的節(jié)點數(shù)據(jù):" + data + "\t節(jié)點刪除成功!");
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二叉樹內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring中的@EnableWebSecurity注解詳解
這篇文章主要介紹了Spring中的@EnableWebSecurity注解詳解,EnableWebSecurity注解是個組合注解,它的注解中,又使用了@EnableGlobalAuthentication注解,需要的朋友可以參考下2023-12-12
springboot中JSONObject遍歷并替換部分json值
這篇文章主要介紹了springboot中JSONObject遍歷并替換部分json值,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11
SpringBoot通過@MatrixVariable進行傳參詳解
這篇文章主要介紹了SpringBoot使用@MatrixVariable傳參,文章圍繞@MatrixVariable展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-06-06

