詳解Go語言如何實現(xiàn)二叉樹遍歷
1. 二叉樹的定義
二叉樹需滿足的條件
① 本身是有序樹
② 樹中包含的各個節(jié)點的長度不能超過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é)點 var root Student root.Name = "root" root.Age = 18 root.Score = 88 //一級左子樹 var left1 Student left1.Name = "left1" left1.Age = 20 left1.Score = 80 root.left = &left1 //一級右子樹 var right1 Student right1.Name = "right1" right1.Age = 22 right1.Score = 100 root.right = &right1 //二級左子樹 var left2 Student left2.Name = "left2" left2.Age = 25 left2.Score = 90 left1.left = &left2 //調(diào)用遍歷函數(shù) Req(&root) } //遞歸算法遍歷整個二叉樹 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é)點 var root Student root.Name = "root" root.Age = 18 root.Score = 88 //一級左子樹 var left1 Student left1.Name = "left1" left1.Age = 20 left1.Score = 80 root.left = &left1 //一級右子樹 var right1 Student right1.Name = "right1" right1.Age = 22 right1.Score = 100 root.right = &right1 //二級左子樹 var left2 Student left2.Name = "left2" left2.Age = 25 left2.Score = 90 left1.left = &left2 //調(diào)用遍歷函數(shù) Req(&root) } //遞歸算法遍歷整個二叉樹 func Req(tmp *Student) { for tmp == nil { return } //遍歷左子樹 Req(tmp.left) //輸出root節(jié)點 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é)點 var root Student root.Name = "root" root.Age = 18 root.Score = 88 //一級左子樹 var left1 Student left1.Name = "left1" left1.Age = 20 left1.Score = 80 root.left = &left1 //一級右子樹 var right1 Student right1.Name = "right1" right1.Age = 22 right1.Score = 100 root.right = &right1 //二級左子樹 var left2 Student left2.Name = "left2" left2.Age = 25 left2.Score = 90 left1.left = &left2 //調(diào)用遍歷函數(shù) Req(&root) } //遞歸算法遍歷整個二叉樹 func Req(tmp *Student) { for tmp == nil { return } //遍歷左子樹 Req(tmp.left) //遍歷右子樹 Req(tmp.right) //輸出root節(jié)點 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語言如何實現(xiàn)二叉樹遍歷的文章就介紹到這了,更多相關(guān)Go語言二叉樹遍歷內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Golang使用http協(xié)議實現(xiàn)心跳檢測程序過程詳解
這篇文章主要介紹了Golang使用http協(xié)議實現(xiàn)心跳檢測程序過程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2023-03-03使用Go語言創(chuàng)建error的幾種方式小結(jié)
Go語言函數(shù)(或方法)是支持多個返回值的,因此在Go語言的編程哲學中,函數(shù)的返回值的最后一個通常都是error類型,所以本文給大家介紹了使用Go語言創(chuàng)建error的幾種方式小結(jié),文中通過代碼示例講解的非常詳細,需要的朋友可以參考下2024-01-01Golang?errgroup?設(shè)計及實現(xiàn)原理解析
這篇文章主要為大家介紹了Golang?errgroup?設(shè)計及實現(xiàn)原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08