https://github.com/mikenye/gotrees
A generic Binary Search Tree (BST) implementation in Go, supporting both unbalanced and balanced trees (e.g., Red-Black Trees)
https://github.com/mikenye/gotrees
bst go golang rbtree red-black-tree red-black-trees
Last synced: about 2 months ago
JSON representation
A generic Binary Search Tree (BST) implementation in Go, supporting both unbalanced and balanced trees (e.g., Red-Black Trees)
- Host: GitHub
- URL: https://github.com/mikenye/gotrees
- Owner: mikenye
- License: mit
- Created: 2025-03-08T10:00:50.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-20T14:37:27.000Z (about 1 year ago)
- Last Synced: 2025-12-26T20:14:06.819Z (6 months ago)
- Topics: bst, go, golang, rbtree, red-black-tree, red-black-trees
- Language: Go
- Homepage: https://pkg.go.dev/github.com/mikenye/gotrees
- Size: 122 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gotrees - Generic Binary Search Trees in Go
[](https://goreportcard.com/report/github.com/mikenye/gotrees)
[](https://codecov.io/gh/mikenye/gotrees)
[](https://pkg.go.dev/github.com/mikenye/gotrees)
[](https://bencher.dev/perf/gotrees/plots)
## Overview
**gotrees** is a pure Go implementation of **generic binary search trees**, designed for flexibility and efficiency. It provides:
- **`bst`:** A **basic, non-self-balancing** Binary Search Tree (BST).
- **`rbtree`:** A **self-balancing Red-Black Tree** (extends `bst`).
Both implementations are **written entirely in Go** (**no Cgo**), ensuring **portability** and **easy integration** into any Go project.
## Why gotrees Exists
1. **Generic Support:** Before Go introduced generics, BSTs often relied on `interface{}`, leading to runtime type assertions and reduced type safety.
2. **Extensibility:** `bst` provides a foundation for creating custom tree structures (e.g., Red-Black Trees, AVL Trees).
3. **Learning & Exploration:** Developed to deepen understanding of balanced tree structures while prioritizing usability.
## Installation
```sh
go get github.com/mikenye/gotrees
```
## Package Overview
### **[bst - Binary Search Tree](./bst/)**
A **generic, pointer-based Binary Search Tree (BST)** that supports:
- **Ordered key-value storage** (via a user-defined comparison function).
- **Efficient traversal and search operations**.
- **Manual structure modification** (for creating custom balanced trees).
- **Extensibility** (for extending to create Red-Black Trees, AVL Trees, etc).
> ⚠️ **`bst` does not self-balance** – for a balanced BST, use `rbtree`.
### **[rbtree - Red-Black Tree](./rbtree/)**
A **self-balancing Red-Black Tree**, extending `bst`. It ensures:
- **Automatic balancing** for O(log n) performance.
- **Preservation of Red-Black Tree properties** (no consecutive red nodes, balanced black height).
- **Safe insertions and deletions without manual balancing**.
## Features
- **✅ Well documented** – Every function documented.
- **✅ 100% Go Implementation** – No Cgo dependencies.
- **✅ Fully Generic** – Supports any key and value types.
- **✅ Extensible** – `bst` can be used to build other trees.
## Limitations
- **Not Thread-Safe** – External synchronization is required for concurrent access.
- **No Duplicate Keys** – Each key must be unique.