圖解二叉樹的三種遍歷方式及java實現(xiàn)代碼
二叉樹(binary tree)是一顆樹,其中每個節(jié)點都不能有多于兩個的兒子。
1.二叉樹節(jié)點
作為圖的特殊形式,二叉樹的基本組成單元是節(jié)點與邊;作為數(shù)據(jù)結(jié)構(gòu),其基本的組成實體是二叉樹節(jié)點(binary tree node),而邊則對應(yīng)于節(jié)點之間的相互引用。
如下,給出了二叉樹節(jié)點的數(shù)據(jù)結(jié)構(gòu)圖示和相關(guān)代碼:

// 定義節(jié)點類:
private static class BinNode {
private Object element;
private BinNode lChild;// 定義指向左子樹的指針
private BinNode rChild;// 定義指向右子樹的指針
public BinNode(Object element, BinNode lChild, BinNode rChild) {
this.element = element;
this.lChild = lChild;
this.rChild = rChild;
}
}
2.遞歸遍歷
二叉樹本身并不具有天然的全局次序,故為實現(xiàn)遍歷,需通過在各節(jié)點與其孩子之間約定某種局部次序,間接地定義某種全局次序。
按慣例左兄弟優(yōu)先于右兄弟,故若將節(jié)點及其孩子分別記作V、L和R,則下圖所示,局部訪問的次序可有VLR、LVR和LRV三種選擇。根據(jù)節(jié)點V在其中的訪問次序,三種策略也相應(yīng)地分別稱作先序遍歷、中序遍歷和后序遍歷,下面將分別介紹。

2.1 先序遍歷
圖示:

代碼實現(xiàn):
/**
* 對該二叉樹進行前序遍歷 結(jié)果存儲到list中 前序遍歷
*/
public static void preOrder(BinNode node) {
list.add(node); // 先將根節(jié)點存入list
// 如果左子樹不為空繼續(xù)往左找,在遞歸調(diào)用方法的時候一直會將子樹的根存入list,這就做到了先遍歷根節(jié)點
if (node.lChild != null) {
preOrder(node.lChild);
}
// 無論走到哪一層,只要當前節(jié)點左子樹為空,那么就可以在右子樹上遍歷,保證了根左右的遍歷順序
if (node.rChild != null) {
preOrder(node.rChild);
}
}
2.2 中序遍歷
圖示:

代碼實現(xiàn):
/**
* 對該二叉樹進行中序遍歷 結(jié)果存儲到list中
*/
public static void inOrder(BinNode node) {
if (node.lChild != null) {
inOrder(node.lChild);
}
list.add(node);
if (node.rChild != null) {
inOrder(node.rChild);
}
}
2.3 后序遍歷
實例圖示:

代碼實現(xiàn):
/**
* 對該二叉樹進行后序遍歷 結(jié)果存儲到list中
*/
public static void postOrder(BinNode node) {
if (node.lChild != null) {
postOrder(node.lChild);
}
if (node.rChild != null) {
postOrder(node.rChild);
}
list.add(node);
}
附:測試相關(guān)代碼
private static BinNode root;
private static List<BinNode> list = new ArrayList<BinNode>();
public static void main(String[] args) {
init();
// TODO Auto-generated method stub
//preOrder(root);
//inOrder(root);
postOrder(root);
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i).element + " ");
}
}
// 樹的初始化:先從葉節(jié)點開始,由葉到根
public static void init() {
BinNode b = new BinNode("b", null, null);
BinNode a = new BinNode("a", null, b);
BinNode c = new BinNode("c", a, null);
BinNode e = new BinNode("e", null, null);
BinNode g = new BinNode("g", null, null);
BinNode f = new BinNode("f", e, g);
BinNode h = new BinNode("h", f, null);
BinNode d = new BinNode("d", c, h);
BinNode j = new BinNode("j", null, null);
BinNode k = new BinNode("k", j, null);
BinNode m = new BinNode("m", null, null);
BinNode o = new BinNode("o", null, null);
BinNode p = new BinNode("p", o, null);
BinNode n = new BinNode("n", m, p);
BinNode l = new BinNode("l", k, n);
root = new BinNode("i", d, l);
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot SpEL語法掃盲與查詢手冊的實現(xiàn)
這篇文章主要介紹了SpringBoot SpEL語法掃盲與查詢手冊的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
Java實現(xiàn)創(chuàng)建運行時類的對象操作示例
這篇文章主要介紹了Java實現(xiàn)創(chuàng)建運行時類的對象操作,結(jié)合實例形式分析了Java動態(tài)創(chuàng)建對象的原理與相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2018-08-08
springboot運行時新增/更新外部接口的實現(xiàn)方法
這篇文章主要介紹了springboot運行時新增/更新外部接口的實現(xiàn)方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
spring框架配置實體類復(fù)雜屬性注入xml文件過程詳解
這篇文章主要介紹了spring框架配置實體類復(fù)雜屬性注入xml文件過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09
MybatisPlus中如何調(diào)用Oracle存儲過程
這篇文章主要介紹了MybatisPlus中如何調(diào)用Oracle存儲過程的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05
MybatisPlus lambdaQueryWrapper中常用方法的使用
本文主要介紹了MybatisPlus lambdaQueryWrapper中常用方法的使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07

