java實(shí)現(xiàn)二叉樹遍歷的三種方式
本文實(shí)例為大家分享了java實(shí)現(xiàn)二叉樹遍歷的具體代碼,供大家參考,具體內(nèi)容如下
二叉樹如下:
遍歷結(jié)果如下:
以下是實(shí)現(xiàn)代碼:
package binTree; import java.util.Stack; /** * @author bin.zhang * @version 2017年8月29日 上午10:22:01 */ public class BinTreeTraversal { public static void main(String[] args) { System.out.print("前序:"); Traversal.preOrder(); Traversal.preOrderRecursion(Traversal.createBinTree()); System.out.print("中序:"); Traversal.inOrder(); Traversal.inOrderRecursion(Traversal.createBinTree()); System.out.print("后序:"); Traversal.postOrder(); Traversal.postOrderRecursion(Traversal.createBinTree()); } } /** * 節(jié)點(diǎn)數(shù)據(jù)結(jié)構(gòu) * * @author bin.zhang * @version 2017年8月30日 上午11:49:38 */ class BinTreeNode { BinTreeNode() { } BinTreeNode(char data, int flag, BinTreeNode lchild, BinTreeNode rchild) { this.data = data; this.flag = flag; this.lchild = lchild; this.rchild = rchild; } char data; int flag; BinTreeNode lchild, rchild; } class Traversal { /** * 創(chuàng)建一棵二叉樹 * * @author bin.zhang * @return 根節(jié)點(diǎn) */ public static BinTreeNode createBinTree() { BinTreeNode R3 = new BinTreeNode('F', 0, null, null); BinTreeNode L2 = new BinTreeNode('D', 0, null, null); BinTreeNode R2 = new BinTreeNode('E', 0, null, R3); BinTreeNode L1 = new BinTreeNode('B', 0, L2, R2); BinTreeNode R1 = new BinTreeNode('C', 0, null, null); BinTreeNode T = new BinTreeNode('A', 0, L1, R1); return T; } // 前序 public static void preOrder() { BinTreeNode p = createBinTree(); Stack<BinTreeNode> stack = new Stack<BinTreeNode>(); while (p != null || !stack.empty()) { if (p != null) { System.out.print(p.data); stack.push(p); p = p.lchild; } else { p = stack.pop(); p = p.rchild; } } System.out.println(); } // 前序遞歸 public static void preOrderRecursion(BinTreeNode top) { if (top != null) { System.out.println(top.data); preOrderRecursion(top.lchild); preOrderRecursion(top.rchild); } } // 中序 public static void inOrder() { BinTreeNode p = createBinTree(); Stack<BinTreeNode> stack = new Stack<BinTreeNode>(); while (p != null || !stack.empty()) { if (p != null) { stack.push(p); p = p.lchild; } else { p = stack.pop(); System.out.print(p.data); p = p.rchild; } } System.out.println(); } // 中序遞歸 public static void inOrderRecursion(BinTreeNode top) { if (top != null) { inOrderRecursion(top.lchild); System.out.println(top.data); inOrderRecursion(top.rchild); } } // 后序 public static void postOrder() { BinTreeNode p = createBinTree(); Stack<BinTreeNode> stack = new Stack<BinTreeNode>(); // 初始化棧 int mark = 1; // 轉(zhuǎn)向標(biāo)志 while (p != null || !stack.empty()) { // 遍歷 if (p != null && mark != 0) { stack.push(p); p = p.lchild; }// 轉(zhuǎn)向左子樹 else { p = stack.pop(); p.flag++; // 退棧 if (p.flag == 1) { stack.push(p); p = p.rchild; mark = 1; } // 轉(zhuǎn)向右子樹 else if (p.flag == 2 && !stack.empty()) { // 輸出結(jié)點(diǎn) System.out.print(p.data); mark = 0; } else if (p.flag == 2 && stack.empty()) { // 輸出根結(jié)點(diǎn)并退出 System.out.print(p.data); break; } } // if-else } // while System.out.println(); } // 后序遞歸 public static void postOrderRecursion(BinTreeNode top) { if (top != null) { postOrderRecursion(top.lchild); postOrderRecursion(top.rchild); System.out.println(top.data); } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 圖解二叉樹的三種遍歷方式及java實(shí)現(xiàn)代碼
- 圖解紅黑樹及Java進(jìn)行紅黑二叉樹遍歷的方法
- java實(shí)現(xiàn)二叉樹的創(chuàng)建及5種遍歷方法(總結(jié))
- Java實(shí)現(xiàn)二叉樹的深度優(yōu)先遍歷和廣度優(yōu)先遍歷算法示例
- Java中二叉樹數(shù)據(jù)結(jié)構(gòu)的實(shí)現(xiàn)示例
- Java實(shí)現(xiàn)求二叉樹的深度和寬度
- Java實(shí)現(xiàn)打印二叉樹所有路徑的方法
- java使用歸并刪除法刪除二叉樹中節(jié)點(diǎn)的方法
- JAVA 實(shí)現(xiàn)二叉樹(鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu))
- 一篇文章徹底弄懂Java中二叉樹
相關(guān)文章
Automapper實(shí)現(xiàn)自動(dòng)映射的實(shí)例代碼
這篇文章主要介紹了Automapper實(shí)現(xiàn)自動(dòng)映射的實(shí)例代碼,需要的朋友可以參考下2017-09-09關(guān)于dubbo的RPC和RESTful性能及對(duì)比
這篇文章主要介紹了關(guān)于dubbo的RPC和RESTful性能及對(duì)比,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12Java實(shí)現(xiàn)開箱即用的redis分布式鎖
這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)開箱即用的基于redis的分布式鎖,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以收藏一下2022-12-12java集合collection接口與子接口及實(shí)現(xiàn)類
這篇文章主要介紹了java集合collection接口與子接口及實(shí)現(xiàn)類,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-07-07一文看懂springboot實(shí)現(xiàn)短信服務(wù)功能
項(xiàng)目中的短信服務(wù)基本上上都會(huì)用到,簡(jiǎn)單的注冊(cè)驗(yàn)證碼,消息通知等等都會(huì)用到。這篇文章主要介紹了springboot 實(shí)現(xiàn)短信服務(wù)功能,需要的朋友可以參考下2019-10-10如何優(yōu)雅的拋出Spring Boot注解的異常詳解
這篇文章主要給大家介紹了關(guān)于如何優(yōu)雅的拋出Spring Boot注解的異常的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12詳解SpringBoot下文件上傳與下載的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot下文件上傳與下載的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05