java 完全二叉樹的構(gòu)建與四種遍歷方法示例
本來就是基礎(chǔ)知識(shí),不能丟的太干凈,今天竟然花了那么長的時(shí)間才寫出來,記一下。
有如下的一顆完全二叉樹:

先序遍歷結(jié)果應(yīng)該為:1 2 4 5 3 6 7
中序遍歷結(jié)果應(yīng)該為:4 2 5 1 6 3 7
后序遍歷結(jié)果應(yīng)該為:4 5 2 6 7 3 1
層序遍歷結(jié)果應(yīng)該為:1 2 3 4 5 6 7
二叉樹的先序遍歷、中序遍歷、后序遍歷其實(shí)都是一樣的,都是執(zhí)行遞歸操作。
我這記錄一下層次遍歷吧:層次遍歷需要用到隊(duì)列,先入隊(duì)在出隊(duì),每次出隊(duì)的元素檢查是其是否有左右孩子,有則將其加入隊(duì)列,由于利用隊(duì)列的先進(jìn)先出原理,進(jìn)行層次遍歷。
下面記錄下完整代碼(Java實(shí)現(xiàn)),包括幾種遍歷方法:
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
/**
* 定義二叉樹節(jié)點(diǎn)元素
* @author bubble
*
*/
class Node {
public Node leftchild;
public Node rightchild;
public int data;
public Node(int data) {
this.data = data;
}
}
public class TestBinTree {
/**
* 將一個(gè)arry數(shù)組構(gòu)建成一個(gè)完全二叉樹
* @param arr 需要構(gòu)建的數(shù)組
* @return 二叉樹的根節(jié)點(diǎn)
*/
public Node initBinTree(int[] arr) {
if(arr.length == 1) {
return new Node(arr[0]);
}
List<Node> nodeList = new ArrayList<>();
for(int i = 0; i < arr.length; i++) {
nodeList.add(new Node(arr[i]));
}
int temp = 0;
while(temp <= (arr.length - 2) / 2) { //注意這里,數(shù)組的下標(biāo)是從零開始的
if(2 * temp + 1 < arr.length) {
nodeList.get(temp).leftchild = nodeList.get(2 * temp + 1);
}
if(2 * temp + 2 < arr.length) {
nodeList.get(temp).rightchild = nodeList.get(2 * temp + 2);
}
temp++;
}
return nodeList.get(0);
}
/**
* 層序遍歷二叉樹,,并分層打印
* @param root 二叉樹根節(jié)點(diǎn)
*
*/
public void trivalBinTree(Node root) {
Queue<Node> nodeQueue = new ArrayDeque<>();
nodeQueue.add(root);
Node temp = null;
int currentLevel = 1; //記錄當(dāng)前層需要打印的節(jié)點(diǎn)的數(shù)量
int nextLevel = 0;//記錄下一層需要打印的節(jié)點(diǎn)的數(shù)量
while ((temp = nodeQueue.poll()) != null) {
if (temp.leftchild != null) {
nodeQueue.add(temp.leftchild);
nextLevel++;
}
if (temp.rightchild != null) {
nodeQueue.add(temp.rightchild);
nextLevel++;
}
System.out.print(temp.data + " ");
currentLevel--;
if(currentLevel == 0) {
System.out.println();
currentLevel = nextLevel;
nextLevel = 0;
}
}
}
/**
* 先序遍歷
* @param root 二叉樹根節(jié)點(diǎn)
*/
public void preTrival(Node root) {
if(root == null) {
return;
}
System.out.print(root.data + " ");
preTrival(root.leftchild);
preTrival(root.rightchild);
}
/**
* 中序遍歷
* @param root 二叉樹根節(jié)點(diǎn)
*/
public void midTrival(Node root) {
if(root == null) {
return;
}
midTrival(root.leftchild);
System.out.print(root.data + " ");
midTrival(root.rightchild);
}
/**
* 后序遍歷
* @param root 二叉樹根節(jié)點(diǎn)
*/
public void afterTrival(Node root) {
if(root == null) {
return;
}
afterTrival(root.leftchild);
afterTrival(root.rightchild);
System.out.print(root.data + " ");
}
public static void main(String[] args) {
TestBinTree btree = new TestBinTree();
int[] arr = new int[] {1,2,3,4,5,6,7};
Node root = btree.initBinTree(arr);
System.out.println("層序遍歷(分層打印):");
btree.trivalBinTree(root);
System.out.println("\n先序遍歷:");
btree.preTrival(root);
System.out.println("\n中序遍歷:");
btree.midTrival(root);
System.out.println("\n后序遍歷:");
btree.afterTrival(root);
}
}
遍歷結(jié)果:

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java二叉搜索樹基礎(chǔ)原理與實(shí)現(xiàn)方法詳解
- java實(shí)現(xiàn) 二叉搜索樹功能
- Java創(chuàng)建二叉搜索樹,實(shí)現(xiàn)搜索,插入,刪除的操作實(shí)例
- Java 實(shí)現(xiàn)二叉搜索樹的查找、插入、刪除、遍歷
- 圖解紅黑樹及Java進(jìn)行紅黑二叉樹遍歷的方法
- 圖解二叉樹的三種遍歷方式及java實(shí)現(xiàn)代碼
- java實(shí)現(xiàn)二叉樹的創(chuàng)建及5種遍歷方法(總結(jié))
- Java實(shí)現(xiàn)二叉樹的深度優(yōu)先遍歷和廣度優(yōu)先遍歷算法示例
- Java實(shí)現(xiàn)的二叉樹常用操作【前序建樹,前中后遞歸非遞歸遍歷及層序遍歷】
- Java的二叉樹排序以及遍歷文件展示文本格式的文件樹
- Java中二叉樹的建立和各種遍歷實(shí)例代碼
- java實(shí)現(xiàn)按層遍歷二叉樹
- Java二叉搜索樹遍歷操作詳解【前序、中序、后序、層次、廣度優(yōu)先遍歷】
相關(guān)文章
mybatis-plus用insertBatchSomeColumn方法批量新增指定字段
mybatisPlus底層的新增方法是一條一條的新增的,下面這篇文章主要給大家介紹了關(guān)于mybatis-plus用insertBatchSomeColumn方法批量新增指定字段的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
Springboot自動(dòng)配置與@Configuration配置類詳解
這篇文章主要介紹了SpringBoot中的@Configuration與自動(dòng)配置,在進(jìn)行項(xiàng)目編寫前,我們還需要知道一個(gè)東西,就是SpringBoot對(duì)我們的SpringMVC還做了哪些配置,包括如何擴(kuò)展,如何定制,只有把這些都搞清楚了,我們?cè)谥笫褂貌艜?huì)更加得心應(yīng)手2022-07-07
SpringBoot+ECharts是如何實(shí)現(xiàn)數(shù)據(jù)可視化的
今天帶大家學(xué)習(xí)的是關(guān)于Java的相關(guān)知識(shí),文章圍繞著SpringBoot+ECharts怎么實(shí)現(xiàn)數(shù)據(jù)可視化展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06
Springboot利于第三方服務(wù)進(jìn)行ip定位獲取省份城市
本文主要介紹了Springboot利于第三方服務(wù)進(jìn)行ip定位獲取省份城市,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
java計(jì)算值所占的百分比,結(jié)果為100%問題
這篇文章主要介紹了java計(jì)算值所占的百分比,結(jié)果為100%問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
實(shí)例解決Java異常之OutOfMemoryError的問題
在本篇文章中,我們給大家分享了關(guān)于解決Java異常之OutOfMemoryError的問題的方法,有此需要的朋友們學(xué)習(xí)下。2019-02-02
關(guān)于JDK+Tomcat+eclipse+MyEclipse的配置方法,看這篇夠了
關(guān)于JDK+Tomcat+eclipse+MyEclipse的配置問題,很多朋友都搞不太明白,網(wǎng)上一搜配置方法多種哪種最精簡呢,今天小編給大家分享一篇文章幫助大家快速掌握J(rèn)DK Tomcat eclipse MyEclipse配置技巧,需要的朋友參考下吧2021-06-06
Spring SpringMVC,Spring整合MyBatis 事務(wù)配置的詳細(xì)流程
這篇文章給大家介紹SSM整合詳細(xì)流程步驟 Spring SpringMVC,Spring整合MyBatis 事務(wù)配置,本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-10-10

