Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize binary Tree traversal in Go language

Shulou Source: shulou.com Published: 2022-05-31 20:48:11 09月12日 Update

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!

Tags: Subtrees pointers nodes outputs languages functions algorithms structures results recursion learning content order that is ideas situations articles more conditions knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Information macOS Linux Apple