55[ ![ Go Report Card] ( https://goreportcard.com/badge/gopkg.in/dnaeon/go-binarytree.v1 )] ( https://goreportcard.com/report/gopkg.in/dnaeon/go-binarytree.v1 )
66[ ![ codecov] ( https://codecov.io/gh/dnaeon/go-binarytree/branch/v1/graph/badge.svg )] ( https://codecov.io/gh/dnaeon/go-binarytree )
77
8- A simple implementation of [ Binary Trees] ( https://en.wikipedia.org/wiki/Binary_tree ) in Go.
8+ A simple, generic implementation of [ Binary
9+ Trees] ( https://en.wikipedia.org/wiki/Binary_tree ) in Go.
910
1011![ Example Binary Tree] ( ./images/binarytree.svg )
1112
@@ -19,7 +20,134 @@ go get -v gopkg.in/dnaeon/go-binarytree.v1
1920
2021## Usage
2122
22- See the included [ test cases] ( ./binarytree_test.go ) for some examples.
23+ The following example builds a simple binary tree with 7 nodes, and
24+ performs _ in-_ , _ pre-_ , _ post-_ and _ level-order_ walking of the tree
25+ (error handling is omitted for simplicity).
26+
27+ ``` go
28+ package main
29+
30+ import (
31+ " fmt"
32+
33+ " gopkg.in/dnaeon/go-binarytree.v1"
34+ )
35+
36+ func main () {
37+ root := binarytree.NewNode (10 )
38+ five := root.InsertLeft (5 )
39+ twenty := root.InsertRight (20 )
40+ five.InsertLeft (9 )
41+ five.InsertRight (18 )
42+ twenty.InsertLeft (3 )
43+ twenty.InsertRight (7 )
44+
45+ fmt.Printf (" height of tree: %d \n " , root.Height ())
46+ fmt.Printf (" size of the tree: %d \n " , root.Size ())
47+ fmt.Printf (" tree is balanced: %t \n " , root.IsBalancedTree ())
48+ fmt.Printf (" tree is complete: %t \n " , root.IsCompleteTree ())
49+ fmt.Printf (" tree is perfect: %t \n " , root.IsPerfectTree ())
50+
51+ // Function to be called while walking the tree, which simply
52+ // prints the values of each visited node
53+ walkFunc := func (n *binarytree.Node [int ]) error {
54+ fmt.Printf (" %d " , n.Value )
55+ return nil
56+ }
57+
58+ fmt.Printf (" in-order values: " )
59+ root.WalkInOrder (walkFunc)
60+ fmt.Println ()
61+
62+ fmt.Printf (" pre-order values: " )
63+ root.WalkPreOrder (walkFunc)
64+ fmt.Println ()
65+
66+ fmt.Printf (" post-orer values: " )
67+ root.WalkPostOrder (walkFunc)
68+ fmt.Println ()
69+
70+ fmt.Printf (" level-order values: " )
71+ root.WalkLevelOrder (walkFunc)
72+ fmt.Println ()
73+ }
74+ ```
75+
76+ Running above example produces the following output.
77+
78+ ``` shell
79+ height of tree: 2
80+ size of the tree: 7
81+ tree is balanced: true
82+ tree is complete: true
83+ tree is perfect: true
84+ in-order values: 9 5 18 10 3 20 7
85+ pre-order values: 10 5 9 18 20 3 7
86+ post-orer values: 9 18 5 3 7 20 10
87+ level-order values: 10 5 20 9 18 3 7
88+ ```
89+
90+ The following example generates the [ Dot
91+ representation] ( https://en.wikipedia.org/wiki/DOT_(graph_description_language) )
92+ of the binary tree and prints it to the standard output.
93+
94+ ``` go
95+ package main
96+
97+ import (
98+ " os"
99+
100+ " gopkg.in/dnaeon/go-binarytree.v1"
101+ )
102+
103+ func main () {
104+ root := binarytree.NewNode (10 )
105+ five := root.InsertLeft (5 )
106+ twenty := root.InsertRight (20 )
107+ five.InsertLeft (9 )
108+ five.InsertRight (18 )
109+ twenty.InsertLeft (3 )
110+ twenty.InsertRight (7 )
111+
112+ root.WriteDot (os.Stdout )
113+ }
114+ ```
115+
116+ Running above example produces an output similar to this one.
117+
118+ ``` shell
119+ digraph {
120+ node [color= lightblue fillcolor= lightblue fontcolor= black shape= record style= " filled, rounded" ]
121+ 824634441792 [label= " <l>|<v> 10|<r>" ]
122+ 824634441792:l -> 824634441856:v
123+ 824634441792:r -> 824634441920:v
124+ 824634441856 [label= " <l>|<v> 5|<r>" ]
125+ 824634441856:l -> 824634441984:v
126+ 824634441856:r -> 824634442048:v
127+ 824634441984 [label= " <l>|<v> 9|<r>" ]
128+ 824634442048 [label= " <l>|<v> 18|<r>" ]
129+ 824634441920 [label= " <l>|<v> 20|<r>" ]
130+ 824634441920:l -> 824634442112:v
131+ 824634441920:r -> 824634442176:v
132+ 824634442112 [label= " <l>|<v> 3|<r>" ]
133+ 824634442176 [label= " <l>|<v> 7|<r>" ]
134+ }
135+ ```
136+
137+ The generated representation can be rendered using
138+ [ graphviz] ( https://graphviz.org/ ) , e.g.
139+
140+ ``` shell
141+ dot -Tsvg /path/to/file.dot -o /tmp/to/file.svg
142+ ```
143+
144+ When building a binary tree with user-defined types such as structs,
145+ make sure that you also implement the
146+ [ fmt.Stringer] ( https://pkg.go.dev/fmt#Stringer ) interface for your
147+ type, so that Dot generation works properly.
148+
149+ Make sure to check the included [ test cases] ( ./binarytree_test.go ) for
150+ additional examples.
23151
24152## Tests
25153
0 commit comments