詳解Go語言如何實(shí)現(xiàn)二叉樹遍歷
1. 二叉樹的定義
二叉樹需滿足的條件
① 本身是有序樹
② 樹中包含的各個(gè)節(jié)點(diǎn)的長(zhǎng)度不能超過2,即只能是0、1或者2
2. 前序遍歷
前序遍歷二叉樹的順序:根——》左——》右
package main import "fmt" //定義結(jié)構(gòu)體 type Student struct { Name string Age int Score float32 left *Student //左子樹指針 right *Student //右子樹指針 } //二叉樹定義 func main() { //根節(jié)點(diǎn) var root Student root.Name = "root" root.Age = 18 root.Score = 88 //一級(jí)左子樹 var left1 Student left1.Name = "left1" left1.Age = 20 left1.Score = 80 root.left = &left1 //一級(jí)右子樹 var right1 Student right1.Name = "right1" right1.Age = 22 right1.Score = 100 root.right = &right1 //二級(jí)左子樹 var left2 Student left2.Name = "left2" left2.Age = 25 left2.Score = 90 left1.left = &left2 //調(diào)用遍歷函數(shù) Req(&root) } //遞歸算法遍歷整個(gè)二叉樹 func Req(tmp *Student) { for tmp == nil { return } fmt.Println(tmp) //遍歷左子樹 Req(tmp.left) //遍歷右子樹 Req(tmp.right) }
輸出結(jié)果如下
&{root 18 88 0xc0000c0480 0xc0000c04b0}
&{left1 20 80 0xc0000c04e0 <nil>}
&{left2 25 90 <nil> <nil>}
&{right1 22 100 <nil> <nil>}
3. 中序遍歷
中序遍歷:左——》根——》右
package main import "fmt" //定義結(jié)構(gòu)體 type Student struct { Name string Age int Score float32 left *Student //左子樹指針 right *Student //右子樹指針 } //二叉樹定義 func main() { //根節(jié)點(diǎn) var root Student root.Name = "root" root.Age = 18 root.Score = 88 //一級(jí)左子樹 var left1 Student left1.Name = "left1" left1.Age = 20 left1.Score = 80 root.left = &left1 //一級(jí)右子樹 var right1 Student right1.Name = "right1" right1.Age = 22 right1.Score = 100 root.right = &right1 //二級(jí)左子樹 var left2 Student left2.Name = "left2" left2.Age = 25 left2.Score = 90 left1.left = &left2 //調(diào)用遍歷函數(shù) Req(&root) } //遞歸算法遍歷整個(gè)二叉樹 func Req(tmp *Student) { for tmp == nil { return } //遍歷左子樹 Req(tmp.left) //輸出root節(jié)點(diǎn) fmt.Println(tmp) //遍歷右子樹 Req(tmp.right) }
輸出結(jié)果如下
&{left2 25 90 <nil> <nil>}
&{left1 20 80 0xc000114510 <nil>}
&{root 18 88 0xc0001144b0 0xc0001144e0}
&{right1 22 100 <nil> <nil>}
4. 后序遍歷
后序遍歷:左——》右——》根
package main import "fmt" //定義結(jié)構(gòu)體 type Student struct { Name string Age int Score float32 left *Student //左子樹指針 right *Student //右子樹指針 } //二叉樹定義 func main() { //根節(jié)點(diǎn) var root Student root.Name = "root" root.Age = 18 root.Score = 88 //一級(jí)左子樹 var left1 Student left1.Name = "left1" left1.Age = 20 left1.Score = 80 root.left = &left1 //一級(jí)右子樹 var right1 Student right1.Name = "right1" right1.Age = 22 right1.Score = 100 root.right = &right1 //二級(jí)左子樹 var left2 Student left2.Name = "left2" left2.Age = 25 left2.Score = 90 left1.left = &left2 //調(diào)用遍歷函數(shù) Req(&root) } //遞歸算法遍歷整個(gè)二叉樹 func Req(tmp *Student) { for tmp == nil { return } //遍歷左子樹 Req(tmp.left) //遍歷右子樹 Req(tmp.right) //輸出root節(jié)點(diǎn) fmt.Println(tmp) }
輸出結(jié)果如下
&{left2 25 90 <nil> <nil>}
&{left1 20 80 0xc0000c04e0 <nil>}
&{right1 22 100 <nil> <nil>}
&{root 18 88 0xc0000c0480 0xc0000c04b0}
到此這篇關(guān)于詳解Go語言如何實(shí)現(xiàn)二叉樹遍歷的文章就介紹到這了,更多相關(guān)Go語言二叉樹遍歷內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Golang中Set類型的實(shí)現(xiàn)方法示例詳解
這篇文章主要給大家介紹了關(guān)于Golang中Set類型實(shí)現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09Golang使用http協(xié)議實(shí)現(xiàn)心跳檢測(cè)程序過程詳解
這篇文章主要介紹了Golang使用http協(xié)議實(shí)現(xiàn)心跳檢測(cè)程序過程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-03-03Golang實(shí)現(xiàn)驗(yàn)證一個(gè)字符串是否為URL
在實(shí)際開發(fā)過程中,有時(shí)候會(huì)遇到?URL?的校驗(yàn)問題,Go?語言中有哪些方法去驗(yàn)證一個(gè)字符串是否滿足?URL?格式呢?本文就來和大家詳細(xì)講講2023-04-04源碼分析Go語言中g(shù)ofmt實(shí)現(xiàn)原理
gofmt?是?Go?語言官方提供的一個(gè)工具,用于自動(dòng)格式化?Go?源代碼,使其符合?Go?語言的官方編碼風(fēng)格,本文給大家源碼詳細(xì)分析了Go語言中g(shù)ofmt實(shí)現(xiàn)原理,并通過圖文和代碼講解的非常詳細(xì),需要的朋友可以參考下2024-03-03使用Go語言創(chuàng)建error的幾種方式小結(jié)
Go語言函數(shù)(或方法)是支持多個(gè)返回值的,因此在Go語言的編程哲學(xué)中,函數(shù)的返回值的最后一個(gè)通常都是error類型,所以本文給大家介紹了使用Go語言創(chuàng)建error的幾種方式小結(jié),文中通過代碼示例講解的非常詳細(xì),需要的朋友可以參考下2024-01-01Golang?errgroup?設(shè)計(jì)及實(shí)現(xiàn)原理解析
這篇文章主要為大家介紹了Golang?errgroup?設(shè)計(jì)及實(shí)現(xiàn)原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08