詳解Go語(yǔ)言如何實(shí)現(xiàn)二叉樹(shù)遍歷
1. 二叉樹(shù)的定義
二叉樹(shù)需滿足的條件
① 本身是有序樹(shù)
② 樹(shù)中包含的各個(gè)節(jié)點(diǎn)的長(zhǎng)度不能超過(guò)2,即只能是0、1或者2

2. 前序遍歷
前序遍歷二叉樹(shù)的順序:根——》左——》右
package main
import "fmt"
//定義結(jié)構(gòu)體
type Student struct {
Name string
Age int
Score float32
left *Student //左子樹(shù)指針
right *Student //右子樹(shù)指針
}
//二叉樹(shù)定義
func main() {
//根節(jié)點(diǎn)
var root Student
root.Name = "root"
root.Age = 18
root.Score = 88
//一級(jí)左子樹(shù)
var left1 Student
left1.Name = "left1"
left1.Age = 20
left1.Score = 80
root.left = &left1
//一級(jí)右子樹(shù)
var right1 Student
right1.Name = "right1"
right1.Age = 22
right1.Score = 100
root.right = &right1
//二級(jí)左子樹(shù)
var left2 Student
left2.Name = "left2"
left2.Age = 25
left2.Score = 90
left1.left = &left2
//調(diào)用遍歷函數(shù)
Req(&root)
}
//遞歸算法遍歷整個(gè)二叉樹(shù)
func Req(tmp *Student) {
for tmp == nil {
return
}
fmt.Println(tmp)
//遍歷左子樹(shù)
Req(tmp.left)
//遍歷右子樹(shù)
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 //左子樹(shù)指針
right *Student //右子樹(shù)指針
}
//二叉樹(shù)定義
func main() {
//根節(jié)點(diǎn)
var root Student
root.Name = "root"
root.Age = 18
root.Score = 88
//一級(jí)左子樹(shù)
var left1 Student
left1.Name = "left1"
left1.Age = 20
left1.Score = 80
root.left = &left1
//一級(jí)右子樹(shù)
var right1 Student
right1.Name = "right1"
right1.Age = 22
right1.Score = 100
root.right = &right1
//二級(jí)左子樹(shù)
var left2 Student
left2.Name = "left2"
left2.Age = 25
left2.Score = 90
left1.left = &left2
//調(diào)用遍歷函數(shù)
Req(&root)
}
//遞歸算法遍歷整個(gè)二叉樹(shù)
func Req(tmp *Student) {
for tmp == nil {
return
}
//遍歷左子樹(shù)
Req(tmp.left)
//輸出root節(jié)點(diǎn)
fmt.Println(tmp)
//遍歷右子樹(shù)
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 //左子樹(shù)指針
right *Student //右子樹(shù)指針
}
//二叉樹(shù)定義
func main() {
//根節(jié)點(diǎn)
var root Student
root.Name = "root"
root.Age = 18
root.Score = 88
//一級(jí)左子樹(shù)
var left1 Student
left1.Name = "left1"
left1.Age = 20
left1.Score = 80
root.left = &left1
//一級(jí)右子樹(shù)
var right1 Student
right1.Name = "right1"
right1.Age = 22
right1.Score = 100
root.right = &right1
//二級(jí)左子樹(shù)
var left2 Student
left2.Name = "left2"
left2.Age = 25
left2.Score = 90
left1.left = &left2
//調(diào)用遍歷函數(shù)
Req(&root)
}
//遞歸算法遍歷整個(gè)二叉樹(shù)
func Req(tmp *Student) {
for tmp == nil {
return
}
//遍歷左子樹(shù)
Req(tmp.left)
//遍歷右子樹(shù)
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語(yǔ)言如何實(shí)現(xiàn)二叉樹(shù)遍歷的文章就介紹到這了,更多相關(guān)Go語(yǔ)言二叉樹(shù)遍歷內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Golang使用http協(xié)議實(shí)現(xiàn)心跳檢測(cè)程序過(guò)程詳解
這篇文章主要介紹了Golang使用http協(xié)議實(shí)現(xiàn)心跳檢測(cè)程序過(guò)程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-03-03
初學(xué)Go必備的vscode插件及最常用快捷鍵和代碼自動(dòng)補(bǔ)全
這篇文章主要給大家介紹了關(guān)于初學(xué)vscode寫(xiě)Go必備的vscode插件及最常用快捷鍵和代碼自動(dòng)補(bǔ)全的相關(guān)資料,由于vscode是開(kāi)源免費(fèi)的,而且開(kāi)發(fā)支持vscode的插件相對(duì)比較容易,更新速度也很快,需要的朋友可以參考下2023-07-07
golang變量uint、int大小溢出后的結(jié)果方式
在Go語(yǔ)言中,變量的大小溢出后,`uint`類(lèi)型會(huì)回繞到最小值,而`int`類(lèi)型會(huì)回繞到最大值的相反數(shù),例如,`uint8`溢出后會(huì)變成0,`int64`溢出后會(huì)變成最小的負(fù)數(shù)2024-12-12
詳解在Go語(yǔ)言單元測(cè)試中如何解決Redis存儲(chǔ)依賴(lài)問(wèn)題
在編寫(xiě)單元測(cè)試時(shí),除了?MySQL?這個(gè)外部存儲(chǔ)依賴(lài),Redis?應(yīng)該是另一個(gè)最為常見(jiàn)的外部存儲(chǔ)依賴(lài)了,本文就來(lái)講解下如何解決?Redis?外部依賴(lài),文章通過(guò)代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08
Go標(biāo)準(zhǔn)庫(kù)http?server優(yōu)雅啟動(dòng)深入理解
這篇文章主要介紹了Go標(biāo)準(zhǔn)庫(kù)http?server優(yōu)雅啟動(dòng)深入理解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
Go 請(qǐng)求兔子識(shí)別接口實(shí)現(xiàn)流程示例詳解
這篇文章主要為大家介紹了Go 請(qǐng)求兔子識(shí)別接口實(shí)現(xiàn)流程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04

