In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "Go language how to achieve binary tree traversal", the content of the explanation is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "Go language how to achieve binary tree traversal" bar!
1. The definition of binary tree
Conditions to be satisfied by binary tree
① itself is an ordered tree.
The length of each node contained in the ② tree cannot exceed 2, that is, it can only be 0, 1, or 2
two。 Preorder traversal
The order in which the preorder traverses the binary tree: root-"left-right"
Package mainimport "fmt" / / definition structure type Student struct {Name string Age int Score float32 left * Student / / left subtree pointer right * Student / / right subtree pointer} / / binary tree definition func main () {/ / Root node var root Student root.Name = "root" root.Age = 18 root.Score = 88 / / first-level left subtree var left1 Student left1.Name = "left1" left1.Age = 20 left1.Score = 80 root.left = & left1 / / first-level right subtree var right1 Student right1.Name = "right1" right1.Age = 22 right1.Score = 100root.right = & right1 / / second-level left subtree Var left2 Student left2.Name = "left2" left2.Age = 25 left2.Score = 90 left1.left = & left2 / / call the traversal function Req (& root)} / / Recursive algorithm traverses the entire binary tree func Req (tmp * Student) {for tmp = = nil {return} fmt.Println (tmp) / / traverse the left subtree Req (tmp.left) / / traverse the right subtree Req (tmp.right)}
The output is as follows
& {root 18 88 0xc0000c0480 0xc0000c04b0}
& {left1 20 80 0xc0000c04e0}
& {left2 25 90}
& {right1 22100}
3. Mid-order traversal
Traversal in the middle order: left-root-right
Package mainimport "fmt" / / definition structure type Student struct {Name string Age int Score float32 left * Student / / left subtree pointer right * Student / / right subtree pointer} / / binary tree definition func main () {/ / Root node var root Student root.Name = "root" root.Age = 18 root.Score = 88 / / first-level left subtree var left1 Student left1.Name = "left1" left1.Age = 20 left1.Score = 80 root.left = & left1 / / first-level right subtree var right1 Student right1.Name = "right1" right1.Age = 22 right1.Score = 100root.right = & right1 / / second-level left subtree Var left2 Student left2.Name = "left2" left2.Age = 25 left2.Score = 90 left1.left = & left2 / / call the traversal function Req (& root)} / / Recursive algorithm traverses the entire binary tree func Req (tmp * Student) {for tmp = = nil {return} / / traverses the left subtree Req (tmp.left) / / output root node fmt.Println (tmp) / / traverse the right subtree Req (tmp.right)}
The output is as follows
& {left2 25 90}
& {left1 20 80 0xc000114510}
& {root 18 88 0xc0001144b0 0xc0001144e0}
& {right1 22100}
4. Post-order traversal
Traversal in post-order: left-right-root
Package mainimport "fmt" / / definition structure type Student struct {Name string Age int Score float32 left * Student / / left subtree pointer right * Student / / right subtree pointer} / / binary tree definition func main () {/ / Root node var root Student root.Name = "root" root.Age = 18 root.Score = 88 / / first-level left subtree var left1 Student left1.Name = "left1" left1.Age = 20 left1.Score = 80 root.left = & left1 / / first-level right subtree var right1 Student right1.Name = "right1" right1.Age = 22 right1.Score = 100root.right = & right1 / / second-level left subtree Var left2 Student left2.Name = "left2" left2.Age = 25 left2.Score = 90 left1.left = & left2 / / call the traversal function Req (& root)} / / Recursive algorithm traverses the entire binary tree func Req (tmp * Student) {for tmp = = nil {return} / / traverses the left subtree Req (tmp.left) / / traversing the right subtree Req (tmp.right) / / output root node fmt.Println (tmp)}
The output is as follows
& {left2 25 90}
& {left1 20 80 0xc0000c04e0}
& {right1 22100}
& {root 18 88 0xc0000c0480 0xc0000c04b0}
Thank you for your reading, the above is the content of "how to achieve binary tree traversal in Go language". After the study of this article, I believe you have a deeper understanding of how to achieve binary tree traversal in Go language, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.