Skip to content

Commit ab64067

Browse files
authored
Merge pull request #1 from Alokzh/feature/red-black-tree-implementation
Implemented complete Red Black Tree with complete functionalities
2 parents cec47c7 + 92a4834 commit ab64067

File tree

14 files changed

+1752
-1
lines changed

14 files changed

+1752
-1
lines changed

.github/workflows/CI.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: CI
2+
env:
3+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4+
on:
5+
push:
6+
branches: [main]
7+
pull_request:
8+
branches: [main]
9+
workflow_dispatch:
10+
jobs:
11+
build:
12+
strategy:
13+
matrix:
14+
os: [macos-latest, ubuntu-latest, windows-latest]
15+
smalltalk: [Pharo64-14, Pharo64-13, Pharo64-12, Pharo64-11, Pharo64-10]
16+
runs-on: ${{ matrix.os }}
17+
name: ${{ matrix.smalltalk }} on ${{ matrix.os }}
18+
steps:
19+
- uses: actions/checkout@v3
20+
- name: Setup SmalltalkCI
21+
uses: hpi-swa/setup-smalltalkCI@v1
22+
with:
23+
smalltalk-version: ${{ matrix.smalltalk }}
24+
- name: Load and Test
25+
run: smalltalkci -s ${{ matrix.smalltalk }}
26+
shell: bash
27+
timeout-minutes: 15

.project

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
'srcDirectory' : 'src'
3+
}

.smalltalk.ston

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
SmalltalkCISpec {
2+
#loading : [
3+
SCIMetacelloLoadSpec {
4+
#baseline : 'ContainersRedBlackTree',
5+
#directory : 'src',
6+
#platforms : [ #pharo ]
7+
}
8+
],
9+
#testing : {
10+
#coverage : {
11+
#packages : [ 'Containers-RedBlackTree' ]
12+
}
13+
}
14+
}

README.md

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,108 @@
1-
# Containers-RedBlackTree
1+
# Containers-RedBlackTree
2+
3+
A self-balancing Red-Black Tree implementation providing guaranteed O(log n) performance for all operations. Features automatic rebalancing, range queries, and full Collection protocol compliance.
4+
5+
![Pharo Version](https://img.shields.io/badge/Pharo-10+-blue)
6+
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE)
7+
8+
## What is a Red-Black Tree?
9+
10+
A Red-Black Tree is a self-balancing binary search tree where each node has a color (red or black) and maintains specific balance properties. This guarantees O(log n) worst-case performance for all operations, with excellent practical performance due to simpler rebalancing than AVL trees.
11+
12+
To install `Container-RedBlackTree`, go to the Playground (Ctrl+OW) in your [Pharo](https://pharo.org/) image and execute the following Metacello script (select it and press Do-it button or Ctrl+D):
13+
14+
```smalltalk
15+
Metacello new
16+
baseline: 'ContainersRedBlackTree';
17+
repository: 'github://pharo-containers/Container-RedBlackTree/src';
18+
load
19+
```
20+
21+
## How to depend on it
22+
23+
```smalltalk
24+
spec
25+
baseline: 'ContainersRedBlackTree'
26+
with: [ spec repository: 'github://pharo-containers/Container-RedBlackTree/src' ].
27+
```
28+
29+
## Why use Containers-RedBlackTree?
30+
31+
Red-Black Trees provide excellent balance between insertion performance and search efficiency, making them ideal for applications requiring frequent insertions with consistent lookup performance.
32+
33+
### Key Benefits
34+
35+
- **Guaranteed Performance**: O(log n) worst-case for all operations
36+
- **Efficient Insertions**: Simpler rebalancing than AVL trees
37+
- **Ordered Iteration**: Automatic sorted traversal
38+
- **Range Queries**: Efficient retrieval of value ranges
39+
- **Memory Efficient**: Lower overhead than other balanced trees
40+
41+
## Red-Black Tree Properties
42+
43+
1. Every node is either red or black
44+
2. The root is always black
45+
3. All nil nodes are black
46+
4. Red nodes cannot have red children
47+
5. Every path from root to nil contains the same number of black nodes
48+
49+
## Basic Usage
50+
51+
```smalltalk
52+
"Create and populate a Red-Black Tree"
53+
tree := CTRedBlackTree new.
54+
tree addAll: #(50 30 70 20 40 60 80).
55+
56+
"Search operations"
57+
tree includes: 30. "=> true"
58+
tree findMin. "=> 20"
59+
tree findMax. "=> 80"
60+
61+
"Range queries"
62+
tree elementsFrom: 35 to: 65. "=> #(40 50 60)"
63+
64+
"Tree automatically stays balanced"
65+
tree validate. "=> true"
66+
tree height. "=> 3 or 4 (guaranteed O(log n))"
67+
```
68+
69+
## Real-World Use Case
70+
71+
```smalltalk
72+
"Event scheduling system - needs fast insertions and range queries"
73+
scheduler := CTRedBlackTree new.
74+
scheduler addAll: #(900 1030 1200 1330 1500 1630).
75+
76+
"Fast operations for busy schedules"
77+
nextMeeting := scheduler successorOf: 1100. "=> 1200"
78+
morningMeetings := scheduler elementsLessThan: 1200.
79+
"=> #(900 1030)"
80+
81+
"Add urgent meeting - tree rebalances automatically"
82+
scheduler add: 1045.
83+
scheduler validate. "=> still perfectly balanced"
84+
```
85+
86+
## Performance Advantage
87+
88+
Red-Black Trees excel with mixed workloads of insertions and lookups:
89+
90+
```smalltalk
91+
database := CTRedBlackTree new.
92+
93+
"Fast bulk loading"
94+
1 to: 10000 do: [ :i | database add: i ].
95+
database height. "=> ~14 (logarithmic)"
96+
97+
"Efficient range queries"
98+
recentRecords := database elementsFrom: 9500 to: 10000.
99+
"=> returns 501 elements efficiently"
100+
101+
"Fast predecessor/successor operations"
102+
database predecessorOf: 5000. "=> 4999"
103+
database successorOf: 5000. "=> 5001"
104+
```
105+
106+
## Contributing
107+
108+
This is part of the Pharo Containers project. Feel free to contribute by implementing additional methods, improving tests, or enhancing documentation.

src/.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
#format : #tonel
3+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"
2+
I represent the baseline for the Red-Black Tree container.
3+
"
4+
Class {
5+
#name : 'BaselineOfContainersRedBlackTree',
6+
#superclass : 'BaselineOf',
7+
#category : 'BaselineOfContainersRedBlackTree',
8+
#package : 'BaselineOfContainersRedBlackTree'
9+
}
10+
11+
{ #category : 'baselines' }
12+
BaselineOfContainersRedBlackTree >> baseline: spec [
13+
14+
<baseline>
15+
spec for: #common do: [
16+
spec package: 'Containers-RedBlackTree'.
17+
spec
18+
package: 'Containers-RedBlackTree-Tests'
19+
with: [ spec requires: #( 'Containers-RedBlackTree' ) ] ]
20+
]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Package { #name : 'BaselineOfContainersRedBlackTree' }

0 commit comments

Comments
 (0)