https://github.com/dnaeon/go-binarytree
A simple Binary Tree implementation in Go
https://github.com/dnaeon/go-binarytree
binarytree go golang
Last synced: 9 months ago
JSON representation
A simple Binary Tree implementation in Go
- Host: GitHub
- URL: https://github.com/dnaeon/go-binarytree
- Owner: dnaeon
- License: other
- Created: 2022-09-24T13:00:04.000Z (over 3 years ago)
- Default Branch: v1
- Last Pushed: 2022-09-27T16:47:06.000Z (over 3 years ago)
- Last Synced: 2025-03-27T05:41:37.398Z (10 months ago)
- Topics: binarytree, go, golang
- Language: Go
- Homepage:
- Size: 69.3 KB
- Stars: 27
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-binarytree
[](https://github.com/dnaeon/go-binarytree/actions/workflows/test.yaml/badge.svg)
[](https://pkg.go.dev/gopkg.in/dnaeon/go-binarytree.v1)
[](https://goreportcard.com/report/gopkg.in/dnaeon/go-binarytree.v1)
[](https://codecov.io/gh/dnaeon/go-binarytree)
A simple, generic implementation of [Binary
Trees](https://en.wikipedia.org/wiki/Binary_tree) in Go.

## Installation
Install `go-binarytree` by executing the following command.
``` shell
go get -v gopkg.in/dnaeon/go-binarytree.v1
```
## Usage
The following example builds a simple binary tree with 7 nodes, and
performs _in-_, _pre-_, _post-_ and _level-order_ walking of the tree
(error handling is omitted for simplicity).
``` go
package main
import (
"fmt"
"gopkg.in/dnaeon/go-binarytree.v1"
)
func main() {
root := binarytree.NewNode(10)
five := root.InsertLeft(5)
twenty := root.InsertRight(20)
five.InsertLeft(9)
five.InsertRight(18)
twenty.InsertLeft(3)
twenty.InsertRight(7)
fmt.Printf("height of tree: %d\n", root.Height())
fmt.Printf("size of the tree: %d\n", root.Size())
fmt.Printf("tree is balanced: %t\n", root.IsBalancedTree())
fmt.Printf("tree is complete: %t\n", root.IsCompleteTree())
fmt.Printf("tree is perfect: %t\n", root.IsPerfectTree())
// Function to be called while walking the tree, which simply
// prints the values of each visited node
walkFunc := func(n *binarytree.Node[int]) error {
fmt.Printf("%d ", n.Value)
return nil
}
fmt.Printf("in-order values: ")
root.WalkInOrder(walkFunc)
fmt.Println()
fmt.Printf("pre-order values: ")
root.WalkPreOrder(walkFunc)
fmt.Println()
fmt.Printf("post-orer values: ")
root.WalkPostOrder(walkFunc)
fmt.Println()
fmt.Printf("level-order values: ")
root.WalkLevelOrder(walkFunc)
fmt.Println()
}
```
Running above example produces the following output.
``` shell
height of tree: 2
size of the tree: 7
tree is balanced: true
tree is complete: true
tree is perfect: true
in-order values: 9 5 18 10 3 20 7
pre-order values: 10 5 9 18 20 3 7
post-orer values: 9 18 5 3 7 20 10
level-order values: 10 5 20 9 18 3 7
```
The following example generates the [Dot
representation](https://en.wikipedia.org/wiki/DOT_(graph_description_language))
of the binary tree and prints it to the standard output.
``` go
package main
import (
"os"
"gopkg.in/dnaeon/go-binarytree.v1"
)
func main() {
root := binarytree.NewNode(10)
five := root.InsertLeft(5)
twenty := root.InsertRight(20)
five.InsertLeft(9)
five.InsertRight(18)
twenty.InsertLeft(3)
twenty.InsertRight(7)
root.WriteDot(os.Stdout)
}
```
Running above example produces an output similar to this one.
``` shell
digraph {
node [color=lightblue fillcolor=lightblue fontcolor=black shape=record style="filled, rounded"]
824634441792 [label="| 10|" ]
824634441792:l -> 824634441856:v
824634441792:r -> 824634441920:v
824634441856 [label="| 5|" ]
824634441856:l -> 824634441984:v
824634441856:r -> 824634442048:v
824634441984 [label="| 9|" ]
824634442048 [label="| 18|" ]
824634441920 [label="| 20|" ]
824634441920:l -> 824634442112:v
824634441920:r -> 824634442176:v
824634442112 [label="| 3|" ]
824634442176 [label="| 7|" ]
}
```
The generated representation can be rendered using
[graphviz](https://graphviz.org/), e.g.
``` shell
dot -Tsvg /path/to/file.dot -o /tmp/to/file.svg
```
When building a binary tree with user-defined types such as structs,
make sure that you also implement the
[fmt.Stringer](https://pkg.go.dev/fmt#Stringer) interface for your
type, so that Dot generation works properly.
Make sure to check the included [test cases](./binarytree_test.go) for
additional examples.
## Tests
Run the tests.
``` shell
make test
```
## License
`go-binarytree` is Open Source and licensed under the [BSD
License](http://opensource.org/licenses/BSD-2-Clause).