Java實(shí)現(xiàn)的二叉樹常用操作【前序建樹,前中后遞歸非遞歸遍歷及層序遍歷】
本文實(shí)例講述了Java實(shí)現(xiàn)的二叉樹常用操作。分享給大家供大家參考,具體如下:
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Stack;
//二叉樹的建樹,前中后 遞歸非遞歸遍歷 層序遍歷
//Node節(jié)點(diǎn)
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("腳本之家測(cè)試結(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);
}
}
運(yùn)行結(jié)果:

更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
- Java 數(shù)據(jù)結(jié)構(gòu)中二叉樹前中后序遍歷非遞歸的具體實(shí)現(xiàn)詳解
- Java二叉樹的四種遍歷(遞歸與非遞歸)
- java非遞歸實(shí)現(xiàn)之二叉樹的前中后序遍歷詳解
- 通俗易懂講解C語言與Java中二叉樹的三種非遞歸遍歷方式
- java棧實(shí)現(xiàn)二叉樹的非遞歸遍歷的示例代碼
- Java二叉樹的四種遍歷(遞歸和非遞歸)
- JAVA二叉樹的幾種遍歷(遞歸,非遞歸)實(shí)現(xiàn)
- java二叉樹的幾種遍歷遞歸與非遞歸實(shí)現(xiàn)代碼
- java二叉樹的非遞歸遍歷
- Java開發(fā)深入分析講解二叉樹的遞歸和非遞歸遍歷方法
相關(guān)文章
解決IDEA springboot"spring-boot-maven-plugin"報(bào)紅問題
這篇文章主要介紹了解決IDEA springboot"spring-boot-maven-plugin"報(bào)紅問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
Java中的static關(guān)鍵字用法總結(jié)
這篇文章主要介紹了Java中的static關(guān)鍵字用法總結(jié),static是Java50個(gè)關(guān)鍵字之一,static關(guān)鍵字可以用來修飾代碼塊表示靜態(tài)代碼塊,修飾成員變量表示全局靜態(tài)成員變量,修飾方法表示靜態(tài)方法,需要的朋友可以參考下2023-11-11
Mybatis使用foreach批量更新數(shù)據(jù)報(bào)無效字符錯(cuò)誤問題
這篇文章主要介紹了Mybatis使用foreach批量更新數(shù)據(jù)報(bào)無效字符錯(cuò)誤問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
解決springcloud 配置gateway 出現(xiàn)錯(cuò)誤的問題
今天給大家分享springcloud 配置gateway 出現(xiàn)錯(cuò)誤的問題,其實(shí)解決方法很簡(jiǎn)單,只需要降低springcloud版本,改成Hoxton.SR5就好了,再次改成Hoxton.SR12,也不報(bào)錯(cuò)了,下面給大家展示下,感興趣的朋友一起看看吧2021-11-11
一文帶你掌握J(rèn)ava?Future模式的靈活應(yīng)用
Future模式,簡(jiǎn)單來說,就是一種能夠管理異步操作的方式,它可以讓咱們的程序在執(zhí)行一個(gè)耗時(shí)任務(wù)的同時(shí),還能繼續(xù)做其他事情,下面我們就來看看Future模式的具體應(yīng)用吧2024-01-01
Java命令設(shè)計(jì)模式優(yōu)雅解耦命令和執(zhí)行提高代碼可維護(hù)性
本文介紹了Java命令設(shè)計(jì)模式,它將命令請(qǐng)求封裝成對(duì)象,以達(dá)到解耦命令請(qǐng)求和執(zhí)行者的目的,從而提高代碼可維護(hù)性。本文詳細(xì)闡述了該模式的設(shè)計(jì)原則、實(shí)現(xiàn)方法和優(yōu)缺點(diǎn),并提供了實(shí)際應(yīng)用場(chǎng)景和代碼示例,幫助讀者深入理解和應(yīng)用該模式2023-04-04
SpringBoot整合Security權(quán)限控制登錄首頁
這篇文章主要為大家介紹了SpringBoot整合Security權(quán)限控制登錄首頁示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Java實(shí)現(xiàn)的漢語拼音工具類完整實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)的漢語拼音工具類,結(jié)合完整實(shí)例形式分析了java基于pinyin4j包實(shí)現(xiàn)編碼轉(zhuǎn)換的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11

