Go?數(shù)據(jù)結(jié)構(gòu)之二叉樹詳情
前言:
樹可以有許多不同的形狀,并且它們可以在每個節(jié)點允許的子節(jié)點數(shù)量或它們在節(jié)點內(nèi)組織數(shù)據(jù)值的方式上有所不同。 而在其中最常用的樹之一是二叉樹。 二叉樹是一棵樹,其中每個節(jié)點最多可以有兩個孩子。 一個孩子被識別為左孩子,另一個孩子被識別為右孩子。
二叉樹是一種數(shù)據(jù)結(jié)構(gòu),在每個節(jié)點下面最多存在兩個其他節(jié)點。即一個節(jié)點要么連接至一個、兩個節(jié)點或不連接其他節(jié)點。
樹形結(jié)構(gòu)的深度(也被稱作高度)則被定義為根節(jié)點為根節(jié)點至某個節(jié)點間的最長路徑,而節(jié)點的深度則表示當(dāng)當(dāng)前節(jié)點至樹根節(jié)點間的邊數(shù)。二叉樹有許多不同的形狀和大小。 形狀取決于節(jié)點的數(shù)量和節(jié)點的鏈接方式。
下圖說明了由九個節(jié)點組成的二叉樹的三種不同形狀:

Go 語言實現(xiàn)二叉樹
定義二叉樹的結(jié)構(gòu)
package main
import (
"fmt"
"math/rand"
"time"
)
type Tree struct {
Left *Tree
Value int
Right *Tree
}二叉樹遍歷
func traverse(t *Tree) {
if t == nil {
return
}
traverse(t.Left)
fmt.Print(t.Value, " ")
traverse(t.Right)
}traverse()函數(shù)通過遞歸方式訪問二叉樹的全部節(jié)點。
創(chuàng)建二叉樹
利用create()函數(shù)利用隨機整數(shù)填寫二叉樹:
func create(n int) *Tree {
var t *Tree
rand.Seed(time.Now().Unix())
for i := 0; i < 2 * n; i++ {
temp := rand.Intn(n * 2)
t = insert(t, temp)
}
return t
}插入值
insert 函數(shù):
- 第一個 if 語句在插入時如果是空樹,就把插入的節(jié)點設(shè)置為根節(jié)點,并創(chuàng)建為
&Tree{nil, v, nil} - 第二個 if 語句確定插入值是否已在二叉樹中存在。若存在,函數(shù)將直接返回且不執(zhí)行任何操作
- 第三個 if 語句檢查插入的值位于當(dāng)前節(jié)點的左孩子節(jié)點還是右孩子節(jié)點,并插入到相應(yīng)的位置。
func insert(t *Tree, v int) *Tree {
if t == nil {
return &Tree{nil, v, nil}
}
if v == t.Value {
return t
}
if v < t.Value {
t.Left = insert(t.Left, v)
return t
}
t.Right = insert(t.Right, v)
return t
}測試
package main
import (
"fmt"
"math/rand"
"time"
)
type Tree struct {
Left *Tree
Value int
Right *Tree
}
func traverse(t *Tree) {
if t == nil {
return
}
traverse(t.Left)
fmt.Print(t.Value, " ")
traverse(t.Right)
}
func create(n int) *Tree {
var t *Tree
rand.Seed(time.Now().Unix())
for i := 0; i < 2*n; i++ {
temp := rand.Intn(n * 2)
t = insert(t, temp)
}
return t
}
func insert(t *Tree, v int) *Tree {
if t == nil {
return &Tree{nil, v, nil}
}
if v == t.Value {
return t
}
if v < t.Value {
t.Left = insert(t.Left, v)
return t
}
t.Right = insert(t.Right, v)
return t
}
func main() {
tree := create(10)
fmt.Println("The value of the root of the tree is", tree.Value)
traverse(tree)
fmt.Println()
tree = insert(tree, -10)
tree = insert(tree, -2)
traverse(tree)
fmt.Println()
fmt.Println("The value of the boot of the the tree is", tree.Value)
}運行結(jié)果:
The value of the root of the tree is 18
0 1 4 5 6 8 9 10 12 14 15 18 19
-10 -2 0 1 4 5 6 8 9 10 12 14 15 18 19
The value of the boot of the the tree is 18
到此這篇關(guān)于 Go 數(shù)據(jù)結(jié)構(gòu)之二叉樹詳情的文章就介紹到這了,更多相關(guān) Go 二叉樹內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Go常用標(biāo)準(zhǔn)庫之fmt的簡介與使用詳解
fmt 是 Go 語言中的一個常用標(biāo)準(zhǔn)庫,它用于格式化輸入和輸出數(shù)據(jù),這篇文章主要為大家介紹了fmt的基本使用,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10
Go空結(jié)構(gòu)體struct{}的作用是什么
本文主要介紹了Go空結(jié)構(gòu)體struct{}的作用是什么,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
Go語言的Windows下環(huán)境配置以及簡單的程序結(jié)構(gòu)講解
這篇文章主要介紹了Go語言的Windows下環(huán)境配置以及簡單的程序結(jié)構(gòu)講解,從編程語言約定俗成的hellow world開始,需要的朋友可以參考下2015-10-10
詳解Go語言RESTful JSON API創(chuàng)建
這篇文章主要介紹了詳解Go語言RESTful JSON API創(chuàng)建,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05

