java遍歷樹結構數據_Java數據結構——二叉樹的遍歷(匯總)

二叉樹的遍歷分為深度優先遍歷(DFS)和廣度優先遍歷(BFS)

DFS遍歷主要有:

前序遍歷

中序遍歷

后序遍歷

一、遞歸實現DFS

Node.java:

public class Node {

private Object data;

Node richild;

Node lechild;

public Object getData() {

return data;

}

public void setData(Object data) {

this.data = data;

}

public Node getRichild() {

return richild;

}

public void setRichild(Node richild) {

this.richild = richild;

}

public Node getLechild() {

return lechild;

}

public void setLechild(Node lechild) {

this.lechild = lechild;

}

public Node(Object data, Node lechild, Node richild) {

super();

this.data = data;

this.richild = richild;

this.lechild = lechild;

}

public Node() {

super();

}

}

遞歸遍歷:

public class BTree {

private static Node root;

//構造樹

public static void init() {

Node node1 = new Node("A", null, null);

Node node2 = new Node("B", node1, null);

Node node3 = new Node("C", null, null);

Node node4 = new Node("D", node2, node3);

Node node5 = new Node("E", null, null);

Node node6 = new Node("F", null, node5);

Node node7 = new Node("G", node4, node6);

root = node7;

}

//訪問節點

public static void visited(Node n) {

System.out.print(n.getData() + " ");

}

//前序遍歷

public static void preOrder(Node n) {

if (n != null) {

visited(n);

preOrder(n.getLechild());

preOrder(n.getRichild());

}

}

//中序遍歷

public static void inOrder(Node n) {

if (n != null) {

inOrder(n.getLechild());

visited(n);

inOrder(n.getRichild());

}

}

//后序遍歷

public static void postOrder(Node n) {

if (n != null) {

postOrder(n.getLechild());

postOrder(n.getRichild());

visited(n);

}

}

public static void main(String[] args) {

init();

System.out.print("遞歸前序:");

preOrder(root);

System.out.println();

System.out.print("遞歸中序:");

inOrder(root);

System.out.println();

System.out.print("遞歸后序:");

postOrder(root);

System.out.println();

}

}

二、非遞歸實現DFS(依靠棧)

//前序遍歷

public static void preOrder(Node n) {

System.out.print("非遞歸前序:");

Stack stack = new Stack<>();

int index = 0;

while (n != null || index > 0) {

while (n != null) {

System.out.print(n.getData() + " ");

stack.push(n);

index++;

n = n.getLechild();

}

n = stack.pop();

index--;

n = n.getRichild();

}

}

//中序遍歷

public static void inOrder(Node n) {

System.out.print("非遞歸中序:");

Stack stack = new Stack<>();

int index = 0;

while (n != null || index > 0) {

while (n != null) {

stack.push(n);

index++;

n = n.getLechild();

}

n = stack.pop();

System.out.print(n.getData() + " ");

index--;

n = n.getRichild();

}

}

//后序遍歷

public static void postOrder(Node n) {

System.out.print("非遞歸后序:");

Stack stack = new Stack<>();

int index = 0;

Node lastVisited = null;

while (n != null || index > 0) {

while (n != null) {

stack.push(n);

index++;

n = n.getLechild();

}

n = stack.peek();

if (n.getRichild() == null || n.getRichild() == lastVisited) {

System.out.print(n.getData() + " ");

lastVisited = n;

index--;

stack.pop();

n = null;

} else {

n = n.getRichild();

}

}

}

三、實現層序遍歷(依靠隊列)

public static void LevenOrder(Node root) {

if (root == null) {

return;

}

Queue queue = new LinkedList<>();

queue.add(root);

Node temp = null;

while (!queue.isEmpty()) {

temp = queue.poll();

visited(temp);

if (temp.getLeChild() != null) {

queue.add(temp.getLeChild());

}

if (temp.getRChild() != null) {

queue.add(temp.getChild());

}

}

}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/396318.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/396318.shtml
英文地址,請注明出處:http://en.pswp.cn/news/396318.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

vue 移動端頭像裁剪_使用vue-cropper裁剪正方形上傳頭像-阿里云開發者社區

引用方式在組件內使用import { VueCropper } from vue-croppercomponents: {VueCropper,},main.js里面使用import VueCropper from vue-cropperVue.use(VueCropper)基本使用方法ref"cropper":img"option.img":autoCrop"true":fixedNumber"[…

規則引擎 設計 git_引擎蓋下的Git

規則引擎 設計 gitby Wassim Chegham由Wassim Chegham 引擎蓋下的Git (Git under the hood) Let’s explore some common Git commands, and dive into its internals to see what Git does when you run them.讓我們探索一些常見的Git命令&#xff0c;并深入了解其內部&#…

練習題之死鎖

public class PrintMain {public static String obj1"obj1";public static String obj2"obj2";public static void main(String[] args) {new Thread(new Runnable() {public void run() {System.out.println(new Date().toString "LockA開始執行&qu…

啟用或禁用對 Exchange Server 中的郵箱的 POP3 或 IMAP4 訪問

https://docs.microsoft.com/zh-cn/Exchange/clients/pop3-and-imap4/configure-mailbox-access?viewexchserver-2019 記錄下轉載于:https://www.cnblogs.com/amoy9812/p/9875426.html

java有什么壓力_編程語言的心智負擔!你學編程得有多大的壓力快來測試一下...

很多編程語言對比的文章&#xff0c;總喜歡比較各種編程語言的性能、語法、IO模型。本文將從心智負擔這個角度去比較下不同的編程語言和技術。內存越界如&#xff1a;C語言、C(C with class)C/C可以直接操作內存&#xff0c;但編程必須要面對內存越界問題。發生內存越界后&…

什么叫有效物理網卡_如何區分虛擬網卡和物理網卡?-阿里云開發者社區

一、什么是物理網卡和虛擬網卡&#xff1f;圖示如下&#xff1a;紅色部分包含VMWare的為虛擬網卡。通常&#xff0c;我們部署VMWare虛擬機、VMSphere虛擬集群、XenCenter虛擬集群是都會涉及虛擬網卡。二、辨別物理網卡和虛擬網卡的應用場景場景一&#xff1a;一般部署虛擬集群的…

算法復雜度的表示法_用簡單的英語算法:時間復雜度和Big-O表示法

算法復雜度的表示法by Michael Olorunnisola通過Michael Olorunnisola 用簡單的英語算法&#xff1a;時間復雜度和Big-O表示法 (Algorithms in plain English: time complexity and Big-O notation) Every good developer has time on their mind. They want to give their us…

Android Studio 開始運行錯誤

/********************************************************************************* Android Studio 開始運行錯誤* 說明&#xff1a;* 打開Android Studio就拋出這個錯誤。* * 2017-4-1 深圳 南…

IOS 計步器

這篇博客介紹的是當前比較流行的“計步器”-只是簡單的知識點 計步器的實現在IOS8開始進行了改變。 但是我會對之前之后的都進行簡單介紹。 IOS 8 - // // ViewController.m // CX 計步器 // // Created by ma c on 16/4/12. // Copyright © 2016年 bjsxt. All rights…

vue學習之二ECMAScript6標準

一、ECMAScript6標準簡述 ECMAScript 6.0&#xff08;以下簡稱 ES6&#xff09;是 JavaScript 語言的下一代標準&#xff0c;已經在 2015 年 6 月正式發布了。它的目標&#xff0c;是使得 JavaScript 語言可以用來編寫復雜的大型應用程序&#xff0c;成為企業級開發語言。 1.1E…

抖音吸粉_抖音吸粉5大實用方法首次分享!輕松實現粉絲10000+

抖音&#xff0c;是一款可以拍短視頻的音樂創意短視頻社交軟件&#xff0c;該軟件于2016年9月上線&#xff0c;是一個專注年輕人音樂短視頻社區。用戶可以通過這款軟件選擇歌曲&#xff0c;拍攝音樂短視頻&#xff0c;形成自己的作品。抖音APP僅推出半年&#xff0c;用戶量就突…

mapper mysql 主鍵_實現通用mapper主鍵策略兼容mysql和oracle

【原創文章&#xff0c;轉載請注明原文章地址&#xff0c;謝謝&#xff01;】1.直接用官方提供的注解方法是無法達到兼容效果的2.跟蹤源碼看看是否有其他方法3.這里有個genSql&#xff0c;可以看一下這個類4.創建一個自定義的處理類實現GenSql(代碼中是我實際項目中用到的策略&…

權限分配界面 純手工 僅用到bootstrap的架構 以及 c標簽

<div class"form-group"> <div class"row"> <label class"col-sm-2 control-label">配置權限</label> <div class"col-sm-10"> <c:forEach var"m" items…

數據管理與數據庫 大學課程_根據數據顯示的50種最佳免費在線大學課程

數據管理與數據庫 大學課程When I launched Class Central back in November 2011, there were around 18 or so free online courses, and almost all of them were from Stanford.當我在2011年11月推出Class Central時&#xff0c;大約有18項免費在線課程&#xff0c;幾乎所有…

每天一個linux命令(12):more命令

more命令&#xff0c;功能類似 cat &#xff0c;cat命令是整個文件的內容從上到下顯示在屏幕上。 more會以一頁一頁的顯示方便使用者逐頁閱讀&#xff0c;而最基本的指令就是按空白鍵&#xff08;space&#xff09;就往下一頁顯示&#xff0c;按 b 鍵就會往回&#xff08;back&…

java 面試題 由淺入深_面試官由淺入深的面試套路

閱讀文本大概需要3分鐘。從上圖看來面試官面試是有套路的&#xff0c;一不小心就一直被套路。0x01&#xff1a;Thread面試官&#xff1a;創建線程有哪幾種方式&#xff1f;應聘者&#xff1a;繼承Thread類、實現Runable接口、使用j.u.c中的線程池面試官&#xff1a;繼承Thread類…

怎么用centos7運行c語言程序_centos如何編譯c語言代碼

centos如何編譯c語言代碼,文件,選項,作用,鏈接,程序 centos如何編譯c語言代碼 易采站長站,站長之家為您整理了centos如何編譯c語言代碼的相關內容。 編譯c,c++代碼 安裝gcc 1、使用如下命令查詢 centos 官方gcc的所有包:yum -list gcc* 可安裝的軟件包gcc.x86_64gcc-c++.x86…

第四篇:基本數據類型及用法(1)

字符串&#xff08;str型&#xff09; -可以做加法&#xff0c;乘法 乘法例&#xff1a; n1"alex" n2n1*3 print(n2) #結果&#xff1a;alexalexalex -首字母大寫: capitalize() -所有字母變小寫: casefold()、lower() #casefold更牛&#xff0c;很多未知的對應關系也…

Android Studio 錯誤集

錯誤列表與解決方案: 1.Android studio Gradle project sync failed Android studio 構建項目出錯 Error:Unable to start the daemon process: could not reserve enough space for object heap.Please assign more memory to Gradle in the projects gradle.properties file.…

需求簡報_代碼簡報:我如何通過做自己喜歡的事情來獲得頂級技術實習

需求簡報Here are three stories we published this week that are worth your time:這是我們本周發布的三個值得您關注的故事&#xff1a; How I landed a top-tier tech internship by doing something I love: 7 minute read 我如何通過做自己喜歡的事情獲得一流的技術實習…