網站首頁 編程語言 正文
前言:
樹可以有許多不同的形狀,并且它們可以在每個節點允許的子節點數量或它們在節點內組織數據值的方式上有所不同。 而在其中最常用的樹之一是二叉樹。 二叉樹是一棵樹,其中每個節點最多可以有兩個孩子。 一個孩子被識別為左孩子,另一個孩子被識別為右孩子。
二叉樹是一種數據結構,在每個節點下面最多存在兩個其他節點。即一個節點要么連接至一個、兩個節點或不連接其他節點。
樹形結構的深度(也被稱作高度)則被定義為根節點為根節點至某個節點間的最長路徑,而節點的深度則表示當當前節點至樹根節點間的邊數。二叉樹有許多不同的形狀和大小。 形狀取決于節點的數量和節點的鏈接方式。
下圖說明了由九個節點組成的二叉樹的三種不同形狀:
Go 語言實現二叉樹
定義二叉樹的結構
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()
函數通過遞歸方式訪問二叉樹的全部節點。
創建二叉樹
利用create()
函數利用隨機整數填寫二叉樹:
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
函數:
- 第一個 if 語句在插入時如果是空樹,就把插入的節點設置為根節點,并創建為?
&Tree{nil, v, nil}
- 第二個 if 語句確定插入值是否已在二叉樹中存在。若存在,函數將直接返回且不執行任何操作
- 第三個 if 語句檢查插入的值位于當前節點的左孩子節點還是右孩子節點,并插入到相應的位置。
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) }
運行結果:
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
原文鏈接:https://blog.51cto.com/yuzhou1su/5278948
相關推薦
- 2022-09-02 useEffect中不能使用async原理詳解_React
- 2022-05-17 Spring boot 集成Redis客戶端Lettuce,導致服務線程數不斷增加
- 2022-06-10 ASP.NET?Core使用AutoMapper組件_實用技巧
- 2022-12-09 ReactQuery系列之數據轉換示例詳解_React
- 2022-03-22 nginx中一個請求的count計數跟蹤淺析_nginx
- 2022-08-16 Golang輕量級IoC容器安裝使用示例_Golang
- 2024-01-09 poi操作Excel給列設置下拉菜單(數據驗證)
- 2022-05-11 C#?中使用Stopwatch計時器實現暫停計時繼續計時功能_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支