官术网_书友最值得收藏!

Trees in Go

A graph is a finite and nonempty set of vertices and edges. A directed graph is a graph whose edges have a direction associated with them. A directed acyclic graph is a directed graph with no cycles in it. A tree is a directed acyclic graph that satisfies three more principles: firstly, it has a root node: the entry point to the tree; secondly, every vertex, except the root, has one and only one entry point; and thirdly, there is a path that connects the root with each vertex and belongs to the tree.

As a result, the root is the first node of the tree. Each node can be connected to one or more nodes depending on the tree type. If each node leads to one and only one other node, then the tree is a linked list!

The most commonly used type of tree is called a binary tree because each node can have up to two children. The following figure shows a graphical representation of a binary tree's data structure:

A binary tree

The presented code will only show you how to create a binary tree and how to traverse it in order to print all of its elements as proof that Go can be used for creating a tree data structure. Therefore, it will not implement the full functionality of a binary tree, which also includes deleting a tree node and balancing a tree.

The code of tree.go will be presented in three parts.

The first part is the expected preamble as well as the definition of the node, as given here:

package main 
 
import ( 
   "fmt" 
   "math/rand" 
   "time" 
) 
type Tree struct { 
   Left  *Tree 
   Value int 
   Right *Tree 
} 

The second part contains functions that allow you to traverse a tree in order to print all of its elements, create a tree with randomly generated numbers, and insert a node into it:

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) 
         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 
} 

The second if statement of insert() checks whether a value already exists in the tree, in order to not add it again. The third if statement identifies whether the new element will be on the left or right-hand side of the current node.

The last part is the implementation of the main() function:

func main() { 
   tree := create(30) 
   traverse(tree) 
   fmt.Println() 
   fmt.Println("The value of the root of the tree is", tree.Value) 
} 

Executing tree.go will generate the following output:

$ go run tree.go
0 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 21 22 23 24 25 26 27 28 29
The value of the root of the tree is 16
Please note that as the values of the nodes of the tree are generated randomly, the output of the program will be different each time you run it. If you want to get the same elements all the time, then use a constant for the seed value in the create() function.
主站蜘蛛池模板: 高雄县| 民丰县| 大洼县| 汉川市| 连云港市| 文成县| 积石山| 泸水县| 周至县| 资中县| 乌兰察布市| 苏尼特右旗| 巧家县| 鄄城县| 台北市| 泰州市| 平阴县| 镇安县| 阳新县| 读书| 木兰县| 玛多县| 开封市| 贵德县| 赣州市| 孟津县| 邢台市| 青神县| 托克逊县| 黄浦区| 唐河县| 刚察县| 永年县| 冀州市| 富锦市| 克什克腾旗| 潢川县| 紫金县| 巫溪县| 页游| 铜梁县|