java 二叉查找樹(shù)實(shí)例代碼
java 二叉查找樹(shù)實(shí)例代碼
1.左邊<中間<右邊
2.前序遍歷 左中右
3.中序遍歷 中左右
4.后序遍歷 左右中
public class BinaryTree {
// 二叉樹(shù)的根節(jié)點(diǎn)
public TreeNode rootNode ;
// 記錄搜索深度
public int count;
/**
* 利用傳入一個(gè)數(shù)組來(lái)建立二叉樹(shù)
*/
public BinaryTree(int[] data) {
for (int i = 0; i < data. length; i++) {
addNodeToTree(data[i]);
}
}
/**
* 將指定的值加入到二叉樹(shù)中適當(dāng)?shù)墓?jié)點(diǎn)
*/
private void addNodeToTree(int value) {
TreeNode currentNode = rootNode;
// 建立樹(shù)根
if (rootNode == null) {
rootNode = new TreeNode(value);
return;
}
// 建立二叉樹(shù)
while (true) {
// 新增的value比節(jié)點(diǎn)的value小,則在左子樹(shù)
if (value < currentNode.value ) {
if (currentNode.leftNode == null) {
currentNode.leftNode = new TreeNode(value);
return;
} else {
currentNode = currentNode.leftNode;
}
} else { // 新增的value比節(jié)點(diǎn)的value大,在右子樹(shù)
if (currentNode.rightNode == null) {
currentNode. rightNode = new TreeNode(value);
return;
} else {
currentNode = currentNode. rightNode;
}
}
}
}
/**
* 中序遍歷(左子樹(shù) -樹(shù)根- 右子樹(shù))
*/
public void inOrder(TreeNode node) {
if (node != null) {
inOrder(node. leftNode);
System. out.print("[" + node.value + "]");
inOrder(node. rightNode);
}
}
/**
* 前序遍歷(樹(shù)根 -左子樹(shù)- 右子樹(shù))
*/
public void preOrder(TreeNode node) {
if (node != null) {
System. out.print("[" + node.value + "]");
preOrder(node. leftNode);
preOrder(node. rightNode);
}
}
/**
* 后序遍歷(左子樹(shù) -右子樹(shù)- 樹(shù)根)
*/
public void postOrder(TreeNode node) {
if (node != null) {
postOrder(node. leftNode);
postOrder(node. rightNode);
System. out.print("[" + node.value + "]");
}
}
/**
* 從二叉樹(shù)中查找指定value
*/
public boolean findTree(TreeNode node, int value) {
if (node == null) {
System. out.println("共搜索" + count + "次");
return false;
} else if (node.value == value) {
System. out.println("共搜索" + count + "次");
return true;
} else if (value < node.value) {
count++;
return findTree(node.leftNode , value);
} else {
count++;
return findTree(node.rightNode , value);
}
}
/**
* 利用中序遍歷進(jìn)行排序
*/
public void sort() {
this.inOrder(rootNode );
}
class TreeNode {
int value ;
TreeNode leftNode;
TreeNode rightNode;
public TreeNode(int value) {
this.value = value;
this.leftNode = null;
this.rightNode = null;
}
}
public static void main(String[] args) {
int[] content = { 50, 35, 27, 45, 40, 48, 78, 56, 90 };
BinaryTree tree = new BinaryTree(content);
System. out.println("前序遍歷:" );
tree.preOrder(tree. rootNode);
System. out.println("\n中序遍歷:" );
tree.inOrder(tree. rootNode);
System. out.println("\n后序遍歷:" );
tree.postOrder(tree. rootNode);
System. out.println("\n\n開(kāi)始搜索:" );
boolean isFind = tree.findTree(tree.rootNode, 48);
System. out.println("是否搜索到" + 48 + ":" + isFind);
System. out.println("\n進(jìn)行排序:" );
tree.sort();
}
}
前序遍歷:
[50][35][27][45][40][48][78][56][90]
中序遍歷:
[27][35][40][45][48][50][56][78][90]
后序遍歷:
[27][40][48][45][35][56][90][78][50]
開(kāi)始搜索:
共搜索3次
是否搜索到48:true
進(jìn)行排序:
[27][35][40][45][48][50][56][78][90]
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- java二叉查找樹(shù)的實(shí)現(xiàn)代碼
- 詳解Java二叉排序樹(shù)
- Java的二叉樹(shù)排序以及遍歷文件展示文本格式的文件樹(shù)
- Java中二叉樹(shù)數(shù)據(jù)結(jié)構(gòu)的實(shí)現(xiàn)示例
- 圖解紅黑樹(shù)及Java進(jìn)行紅黑二叉樹(shù)遍歷的方法
- java使用歸并刪除法刪除二叉樹(shù)中節(jié)點(diǎn)的方法
- Java實(shí)現(xiàn)求二叉樹(shù)的深度和寬度
- JAVA 實(shí)現(xiàn)二叉樹(shù)(鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu))
- Java 實(shí)現(xiàn)二叉搜索樹(shù)的查找、插入、刪除、遍歷
- java實(shí)現(xiàn)二叉樹(shù)的創(chuàng)建及5種遍歷方法(總結(jié))
- 圖解二叉樹(shù)的三種遍歷方式及java實(shí)現(xiàn)代碼
- Java基于二叉查找樹(shù)實(shí)現(xiàn)排序功能示例
相關(guān)文章
如何使用Mockito調(diào)用靜態(tài)方法和void方法
這篇文章主要介紹了如何使用Mockito調(diào)用靜態(tài)方法和void方法的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
基于Eclipce配置Spring Boot過(guò)程圖解
這篇文章主要介紹了基于Eclipce配置Spring Boot過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
SpringBoot+Prometheus+Grafana實(shí)現(xiàn)應(yīng)用監(jiān)控和報(bào)警的詳細(xì)步驟
這篇文章主要介紹了SpringBoot+Prometheus+Grafana實(shí)現(xiàn)應(yīng)用監(jiān)控和報(bào)警的詳細(xì)步驟,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
SpringCloud中的Feign服務(wù)間的調(diào)用詳解
這篇文章主要介紹了SpringCloud中的Feign服務(wù)間的調(diào)用詳解,Feign 是一個(gè)聲明式的 REST 客戶端,它能讓 REST 調(diào)用更加簡(jiǎn)單,Feign 供了 HTTP 請(qǐng)求的模板,通過(guò)編寫簡(jiǎn)單的接口和插入注解,就可以定義好 HTTP 請(qǐng)求的參數(shù)、格式、地址等信息,需要的朋友可以參考下2024-01-01
SpringMVC對(duì)日期類型的轉(zhuǎn)換示例
本篇文章主要介紹了SpringMVC對(duì)日期類型的轉(zhuǎn)換示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
詳解如何使用IntelliJ IDEA新建一個(gè)Servlet項(xiàng)目
這篇文章主要介紹了詳解如何使用IntelliJ IDEA新建一個(gè)Servlet項(xiàng)目,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
springboot登陸頁(yè)面圖片驗(yàn)證碼簡(jiǎn)單的web項(xiàng)目實(shí)現(xiàn)
這篇文章主要介紹了springboot登陸頁(yè)面圖片驗(yàn)證碼簡(jiǎn)單的web項(xiàng)目實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04

