-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.go
More file actions
92 lines (74 loc) · 2.66 KB
/
example.go
File metadata and controls
92 lines (74 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import "fmt"
func main() {
// Create a binary tree
tree := GeneralTree{}
tree.Root = &Node{Value: 1}
tree.Root.Left = &Node{Value: 2}
tree.Root.Right = &Node{Value: 3}
tree.Root.Left.Left = &Node{Value: 4}
tree.Root.Left.Right = &Node{Value: 5}
tree.Root.Right.Left = &Node{Value: 6}
tree.Root.Right.Right = &Node{Value: 7}
tree.Root.Left.Left.Left = &Node{Value: 8}
tree.Root.Left.Left.Right = &Node{Value: 9}
tree.Root.Left.Right.Left = &Node{Value: 10}
tree.Root.Left.Right.Right = &Node{Value: 11}
tree.Root.Right.Left.Left = &Node{Value: 12}
tree.Root.Right.Left.Right = &Node{Value: 13}
tree.Root.Right.Right.Left = &Node{Value: 14}
tree.Root.Right.Right.Right = &Node{Value: 15}
tree.Root.Left.Left.Left.Left = &Node{Value: 16}
tree.Root.Left.Left.Left.Right = &Node{Value: 17}
tree.Root.Left.Left.Right.Left = &Node{Value: 18}
// Print the binary tree in level order
fmt.Println("Level Order")
tree.LevelOrder()
// Print the binary tree in reverse-level order
fmt.Println("Reverse Level Order")
tree.ReverseLevelOrder()
// Print the binary tree in vertical order
fmt.Println("Vertical Order")
tree.VerticalOrder()
// Print the binary tree in reverse-vertical order
fmt.Println("Reverse Vertical Order")
tree.ReverseVerticalOrder()
// Print the binary tree in diagonal order
fmt.Println("Diagonal Order")
tree.DiagonalOrder()
// Print the binary tree in reverse-diagonal order
fmt.Println("Reverse Diagonal Order")
tree.ReverseDiagonalOrder()
// Create another tree with the creator function
tree2 := NewGeneralTree()
tree2.Root = &Node{Value: 1}
tree2.Root.Left = &Node{Value: 2}
tree2.Root.Right = &Node{Value: 3}
tree2.Root.Left.Left = &Node{Value: 4}
// Print the binary tree in level order
fmt.Println("Level Order")
tree2.LevelOrder()
// Print the depth of the tree
fmt.Println("Depth")
fmt.Println(tree2.Depth())
// Print the height of the tree
fmt.Println("Height")
fmt.Println(tree2.Height())
// Print the number of nodes in the tree
fmt.Println("Size")
fmt.Println(tree2.Size())
// Print the number of leaves in the tree
fmt.Println("Leaves")
fmt.Println(tree2.Leaves())
// Print the number of full nodes in the tree
fmt.Println("Full Nodes")
fmt.Println(tree2.FullNodes())
// Print the number of half nodes in the tree
fmt.Println("Half Nodes")
fmt.Println(tree2.HalfNodes())
// Create another tree with the helper function
tree3 := NewGeneralTreeWithNumbers(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18)
// Print the binary tree in level order
fmt.Println("Level Order")
tree3.LevelOrder()
}