JAVA二叉樹的基本操作
記錄二叉樹的基本操作DEMO
1、創(chuàng)建一個(gè)二叉樹類
這里約束了泛型只能為實(shí)現(xiàn)了Comparable這個(gè)接口的類型。
/**
* @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é)點(diǎn)
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("[先序遍歷]當(dāng)前遍歷到的元素:" + 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("[中序遍歷]當(dāng)前遍歷到的元素:" + 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ù)遍歷]當(dāng)前遍歷到的元素:" + this.data + "\t查找的元素:" + data + "\t" + (this.data.compareTo(data) == 0 ? "查找到元素:" + data : ""));
if (this.data.compareTo(data) == 0) {
return data;
}
return target;
}
//根據(jù)值刪除節(jié)點(diǎn)
public boolean deleteNode(T data) {
System.out.println("[節(jié)點(diǎn)刪除]當(dāng)前遍歷到的父節(jié)點(diǎn):" + this.data + "\t" + "匹配的節(jié)點(diǎn)數(shù)據(jù):" + data);
//判斷左子樹是否匹配
if (this.lChild != null && (this.lChild.data.compareTo(data) == 0)) {
System.out.println("[節(jié)點(diǎn)刪除]當(dāng)前遍歷到的父節(jié)點(diǎn):" + this.data + "\t" + "匹配的節(jié)點(diǎn)數(shù)據(jù):" + data + "\t節(jié)點(diǎn)刪除成功!");
this.lChild = null;
return true;
} else if (this.rChild != null && (this.rChild.data.compareTo(data) == 0)) {
System.out.println("[節(jié)點(diǎn)刪除]當(dāng)前遍歷到的父節(jié)點(diǎn):" + this.data + "\t" + "匹配的節(jié)點(diǎn)數(shù)據(jù):" + data + "\t節(jié)點(diǎn)刪除成功!");
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;
}
}
到此這篇關(guān)于JAVA二叉樹的基本操作DEMO的文章就介紹到這了,更多相關(guān)JAVA二叉樹內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring中的@EnableWebSecurity注解詳解
這篇文章主要介紹了Spring中的@EnableWebSecurity注解詳解,EnableWebSecurity注解是個(gè)組合注解,它的注解中,又使用了@EnableGlobalAuthentication注解,需要的朋友可以參考下2023-12-12
java前后端加密解密crypto-js的實(shí)現(xiàn)
這篇文章主要介紹了java前后端加密解密crypto-js的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
Java設(shè)計(jì)模式之工廠模式實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Java設(shè)計(jì)模式之工廠模式實(shí)現(xiàn)方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了工廠模式的分類、原理、實(shí)現(xiàn)方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-12-12
springboot中JSONObject遍歷并替換部分json值
這篇文章主要介紹了springboot中JSONObject遍歷并替換部分json值,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
SpringBoot通過@MatrixVariable進(jìn)行傳參詳解
這篇文章主要介紹了SpringBoot使用@MatrixVariable傳參,文章圍繞@MatrixVariable展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06
Java多線程中的Exchanger應(yīng)用簡(jiǎn)析
這篇文章主要介紹了Java多線程中的Exchanger應(yīng)用簡(jiǎn)析,Exchanger提供了一個(gè)同步點(diǎn)exchange方法,兩個(gè)線程調(diào)用exchange方法時(shí),無論調(diào)用時(shí)間先后,兩個(gè)線程會(huì)互相等到線程到達(dá)exchange方法調(diào)用點(diǎn),此時(shí)兩個(gè)線程可以交換數(shù)據(jù),將本線程產(chǎn)出數(shù)據(jù)傳遞給對(duì)方,需要的朋友可以參考下2023-12-12

