{"id":26124442,"url":"https://github.com/dnaeon/go-binarytree","last_synced_at":"2025-04-13T14:43:06.835Z","repository":{"id":61625530,"uuid":"540830441","full_name":"dnaeon/go-binarytree","owner":"dnaeon","description":"A simple Binary Tree implementation in Go","archived":false,"fork":false,"pushed_at":"2022-09-27T16:47:06.000Z","size":71,"stargazers_count":27,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"v1","last_synced_at":"2025-03-27T05:41:37.398Z","etag":null,"topics":["binarytree","go","golang"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dnaeon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-09-24T13:00:04.000Z","updated_at":"2025-01-27T17:21:34.000Z","dependencies_parsed_at":"2022-10-19T18:45:25.571Z","dependency_job_id":null,"html_url":"https://github.com/dnaeon/go-binarytree","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dnaeon%2Fgo-binarytree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dnaeon%2Fgo-binarytree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dnaeon%2Fgo-binarytree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dnaeon%2Fgo-binarytree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dnaeon","download_url":"https://codeload.github.com/dnaeon/go-binarytree/tar.gz/refs/heads/v1","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248731769,"owners_count":21152838,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["binarytree","go","golang"],"created_at":"2025-03-10T16:08:22.022Z","updated_at":"2025-04-13T14:43:06.801Z","avatar_url":"https://github.com/dnaeon.png","language":"Go","readme":"# go-binarytree\n\n[![Build Status](https://github.com/dnaeon/go-binarytree/actions/workflows/test.yaml/badge.svg)](https://github.com/dnaeon/go-binarytree/actions/workflows/test.yaml/badge.svg)\n[![Go Reference](https://pkg.go.dev/badge/gopkg.in/dnaeon/go-binarytree.v1.svg)](https://pkg.go.dev/gopkg.in/dnaeon/go-binarytree.v1)\n[![Go Report Card](https://goreportcard.com/badge/gopkg.in/dnaeon/go-binarytree.v1)](https://goreportcard.com/report/gopkg.in/dnaeon/go-binarytree.v1)\n[![codecov](https://codecov.io/gh/dnaeon/go-binarytree/branch/v1/graph/badge.svg)](https://codecov.io/gh/dnaeon/go-binarytree)\n\nA simple, generic implementation of [Binary\nTrees](https://en.wikipedia.org/wiki/Binary_tree) in Go.\n\n![Example Binary Tree](./images/binarytree.svg)\n\n## Installation\n\nInstall `go-binarytree` by executing the following command.\n\n``` shell\ngo get -v gopkg.in/dnaeon/go-binarytree.v1\n```\n\n## Usage\n\nThe following example builds a simple binary tree with 7 nodes, and\nperforms _in-_, _pre-_, _post-_ and _level-order_ walking of the tree\n(error handling is omitted for simplicity).\n\n``` go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"gopkg.in/dnaeon/go-binarytree.v1\"\n)\n\nfunc main() {\n\troot := binarytree.NewNode(10)\n\tfive := root.InsertLeft(5)\n\ttwenty := root.InsertRight(20)\n\tfive.InsertLeft(9)\n\tfive.InsertRight(18)\n\ttwenty.InsertLeft(3)\n\ttwenty.InsertRight(7)\n\n\tfmt.Printf(\"height of tree: %d\\n\", root.Height())\n\tfmt.Printf(\"size of the tree: %d\\n\", root.Size())\n\tfmt.Printf(\"tree is balanced: %t\\n\", root.IsBalancedTree())\n\tfmt.Printf(\"tree is complete: %t\\n\", root.IsCompleteTree())\n\tfmt.Printf(\"tree is perfect: %t\\n\", root.IsPerfectTree())\n\n\t// Function to be called while walking the tree, which simply\n\t// prints the values of each visited node\n\twalkFunc := func(n *binarytree.Node[int]) error {\n\t\tfmt.Printf(\"%d \", n.Value)\n\t\treturn nil\n\t}\n\n\tfmt.Printf(\"in-order values: \")\n\troot.WalkInOrder(walkFunc)\n\tfmt.Println()\n\n\tfmt.Printf(\"pre-order values: \")\n\troot.WalkPreOrder(walkFunc)\n\tfmt.Println()\n\n\tfmt.Printf(\"post-orer values: \")\n\troot.WalkPostOrder(walkFunc)\n\tfmt.Println()\n\n\tfmt.Printf(\"level-order values: \")\n\troot.WalkLevelOrder(walkFunc)\n\tfmt.Println()\n}\n```\n\nRunning above example produces the following output.\n\n``` shell\nheight of tree: 2\nsize of the tree: 7\ntree is balanced: true\ntree is complete: true\ntree is perfect: true\nin-order values: 9 5 18 10 3 20 7 \npre-order values: 10 5 9 18 20 3 7 \npost-orer values: 9 18 5 3 7 20 10 \nlevel-order values: 10 5 20 9 18 3 7 \n```\n\nThe following example generates the [Dot\nrepresentation](https://en.wikipedia.org/wiki/DOT_(graph_description_language))\nof the binary tree and prints it to the standard output.\n\n``` go\npackage main\n\nimport (\n\t\"os\"\n\n\t\"gopkg.in/dnaeon/go-binarytree.v1\"\n)\n\nfunc main() {\n\troot := binarytree.NewNode(10)\n\tfive := root.InsertLeft(5)\n\ttwenty := root.InsertRight(20)\n\tfive.InsertLeft(9)\n\tfive.InsertRight(18)\n\ttwenty.InsertLeft(3)\n\ttwenty.InsertRight(7)\n\n\troot.WriteDot(os.Stdout)\n}\n```\n\nRunning above example produces an output similar to this one.\n\n``` shell\ndigraph {\n        node [color=lightblue fillcolor=lightblue fontcolor=black shape=record style=\"filled, rounded\"]\n        824634441792 [label=\"\u003cl\u003e|\u003cv\u003e 10|\u003cr\u003e\" ]\n        824634441792:l -\u003e 824634441856:v\n        824634441792:r -\u003e 824634441920:v\n        824634441856 [label=\"\u003cl\u003e|\u003cv\u003e 5|\u003cr\u003e\" ]\n        824634441856:l -\u003e 824634441984:v\n        824634441856:r -\u003e 824634442048:v\n        824634441984 [label=\"\u003cl\u003e|\u003cv\u003e 9|\u003cr\u003e\" ]\n        824634442048 [label=\"\u003cl\u003e|\u003cv\u003e 18|\u003cr\u003e\" ]\n        824634441920 [label=\"\u003cl\u003e|\u003cv\u003e 20|\u003cr\u003e\" ]\n        824634441920:l -\u003e 824634442112:v\n        824634441920:r -\u003e 824634442176:v\n        824634442112 [label=\"\u003cl\u003e|\u003cv\u003e 3|\u003cr\u003e\" ]\n        824634442176 [label=\"\u003cl\u003e|\u003cv\u003e 7|\u003cr\u003e\" ]\n}\n```\n\nThe generated representation can be rendered using\n[graphviz](https://graphviz.org/), e.g.\n\n``` shell\ndot -Tsvg /path/to/file.dot -o /tmp/to/file.svg\n```\n\nWhen building a binary tree with user-defined types such as structs,\nmake sure that you also implement the\n[fmt.Stringer](https://pkg.go.dev/fmt#Stringer) interface for your\ntype, so that Dot generation works properly.\n\nMake sure to check the included [test cases](./binarytree_test.go) for\nadditional examples.\n\n## Tests\n\nRun the tests.\n\n``` shell\nmake test\n```\n\n## License\n\n`go-binarytree` is Open Source and licensed under the [BSD\nLicense](http://opensource.org/licenses/BSD-2-Clause).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdnaeon%2Fgo-binarytree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdnaeon%2Fgo-binarytree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdnaeon%2Fgo-binarytree/lists"}