Java實現(xiàn)的二叉樹常用操作【前序建樹,前中后遞歸非遞歸遍歷及層序遍歷】
本文實例講述了Java實現(xiàn)的二叉樹常用操作。分享給大家供大家參考,具體如下:
import java.util.ArrayDeque; import java.util.Queue; import java.util.Stack; //二叉樹的建樹,前中后 遞歸非遞歸遍歷 層序遍歷 //Node節(jié)點 class Node { int element; Node left; Node right; public Node() { } public Node(int element) { this.element = element; } } // BinaryTree public class Tree { // creat tree from array public static Node creatTree(int[] data, int i) { if (i >= data.length || data[i] == -1) return null; Node temp = new Node(data[i]); temp.left = creatTree(data, i * 2 + 1); temp.right = creatTree(data, i * 2 + 2); return temp; } // pre前序遍歷遞歸 public static void pre(Node temp) { if (temp == null) return; System.out.print(temp.element + " "); pre(temp.left); pre(temp.right); } // mid中序遍歷遞歸 public static void mid(Node temp) { if (temp == null) return; mid(temp.left); System.out.print(temp.element + " "); mid(temp.right); } // last后序遍歷遞歸 public static void last(Node temp) { if (temp == null) return; last(temp.left); last(temp.right); System.out.print(temp.element + " "); } // pre1前序遍歷非遞歸 public static void pre1(Node temp) { Stack<Node> stack = new Stack<>(); while (temp != null || !stack.isEmpty()) { while (temp != null) { stack.push(temp); System.out.print(temp.element + " "); temp = temp.left; } if (!stack.isEmpty()) { temp = stack.pop().right; } } } // mid1中序遍歷非遞歸 public static void mid1(Node temp) { Stack<Node> stack = new Stack<>(); while (temp != null || !stack.isEmpty()) { while (temp != null) { stack.push(temp); temp = temp.left; } if (!stack.isEmpty()) { temp = stack.pop(); System.out.print(temp.element + " "); temp = temp.right; } } } // last1后序遍歷非遞歸 public static void last1(Node temp) { Stack<Node> stack = new Stack<>(); Stack<Node> stack2 = new Stack<>(); while (temp != null || !stack.isEmpty()) { while (temp != null) { stack.push(temp); stack2.push(temp); temp = temp.right; } if (!stack.isEmpty()) { temp = stack.pop().left; } } while (!stack2.isEmpty()) System.out.print(stack2.pop().element + " "); } // ceng層序遍歷 public static void ceng(Node temp) { if (temp == null) return; Queue<Node> queue = new ArrayDeque<>(); queue.offer(temp); while (!queue.isEmpty()) { temp = queue.poll(); System.out.print(temp.element + " "); if (temp.left != null) queue.offer(temp.left); if (temp.right != null) queue.offer(temp.right); } } // Demo public static void main(String[] args) { int[] array = { 1, 2, 3, 4, 5, 6, 7, -1, -1, 10, -1, -1, 13 }; Node tree = creatTree(array, 0); System.out.println("腳本之家測試結(jié)果:"); pre(tree); System.out.println(); pre1(tree); System.out.println(); mid(tree); System.out.println(); mid1(tree); System.out.println(); last(tree); System.out.println(); last1(tree); System.out.println(); ceng(tree); } }
運行結(jié)果:
更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
相關(guān)文章
java中將一個實體類復(fù)制到另一個實體類的3種方法示例
這篇文章主要給大家介紹了關(guān)于java中將一個實體類復(fù)制到另一個實體類的3種方法,所謂實體類就是一個擁有Set和Get方法的類,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-07-07面試官:Java中new Object()到底占用幾個字節(jié)
這篇文章主要介紹了面試官:Java中new Object()到底占用幾個字節(jié),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02成功解決IDEA2020 Plugins 連不上、打不開的方法
這篇文章主要介紹了成功解決IDEA2020 Plugins 連不上、打不開的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Spring聲明式事務(wù)和@Aspect的攔截順序問題的解決
本篇文章主要介紹了Spring聲明式事務(wù)和@Aspect的攔截順序問題的解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05Idea 2020.2 創(chuàng)建web、Spring項目的教程圖解
這篇文章主要介紹了Idea 2020.2 創(chuàng)建web、Spring項目的教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08Springboot實現(xiàn)Shiro整合JWT的示例代碼
這篇文章主要介紹了Springboot實現(xiàn)Shiro整合JWT的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12