Skip to content

Commit 04663dc

Browse files
authored
Merge pull request #36 from Alokzh/feat/redesign-avl-implementation-v2
Redesign AVL Tree v2: Complete AVL Tree Implementation with Minor Fixes
2 parents bb5cac8 + c5b7e12 commit 04663dc

File tree

10 files changed

+1207
-634
lines changed

10 files changed

+1207
-634
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-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

.github/workflows/runTests.yml

Lines changed: 0 additions & 43 deletions
This file was deleted.

.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 : 'ContainersAVLTree',
5+
#directory : 'src',
6+
#platforms : [ #pharo ]
7+
}
8+
],
9+
#testing : {
10+
#coverage : {
11+
#packages : [ 'Containers-AVL-Tree' ]
12+
}
13+
}
14+
}

README.md

Lines changed: 78 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,93 @@
1-
# AVL
1+
# Containers-AVL-Tree
22

3-
[![CI](https://github.com/pharo-containers/AVL/actions/workflows/runTests.yml/badge.svg)](https://github.com/pharo-containers/AVL/actions/workflows/runTests.yml)
3+
A self-balancing AVL Tree implementation providing guaranteed O(log n) performance for all operations. Features automatic rebalancing, range queries, and full Collection protocol compliance.
44

5-
AVL Tree migration from Roassal 3. A work in progress since it should be packaged to follow project conventions.
5+
![Pharo Version](https://img.shields.io/badge/Pharo-10+-blue)
6+
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE)
67

7-
## How to install it
8+
## What is an AVL Tree?
89

9-
To install `AVL`, 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):
10+
An AVL Tree is a self-balancing binary search tree where the height difference between left and right subtrees of any node is at most 1. This guarantees O(log n) worst-case performance for all operations, unlike regular BSTs which can degrade to O(n).
1011

11-
```st
12+
To install `Container-AVL`, 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
1215
Metacello new
1316
baseline: 'ContainersAVLTree';
14-
repository: 'github://pharo-containers/Container-AVL:main';
17+
repository: 'github://pharo-containers/Container-AVL/src';
1518
load
1619
```
1720

1821
## How to depend on it
1922

20-
If you want to add a dependency on `linear-models` to your project, include the following lines into your baseline method:
23+
```smalltalk
24+
spec
25+
baseline: 'ContainersAVLTree'
26+
with: [ spec repository: 'github://pharo-containers/Container-AVL/src' ].
27+
```
28+
29+
## Why use Containers-AVL-Tree?
30+
31+
AVL Trees maintain sorted data with guaranteed efficient operations, perfect for applications requiring consistent performance regardless of input order.
32+
33+
### Key Benefits
34+
- **Guaranteed Performance**: O(log n) worst-case for all operations
35+
- **Self-Balancing**: Automatic rebalancing through rotations
36+
- **Ordered Iteration**: Automatic sorted traversal
37+
- **Range Queries**: Efficient retrieval of value ranges
38+
39+
## Basic Usage
2140

22-
```st
23-
spec
24-
baseline: 'ContainersAVLTree'
25-
with: [ spec repository: 'github://pharo-containers/Container-AVL' ].
41+
```smalltalk
42+
"Create and populate an AVL Tree"
43+
tree := CTAVLTree new.
44+
tree addAll: #(50 30 70 20 40 60 80).
45+
46+
"Search operations"
47+
tree includes: 30. "=> true"
48+
tree findMin. "=> 20"
49+
tree findMax. "=> 80"
50+
51+
"Range queries"
52+
tree elementsFrom: 35 to: 65. "=> #(40 50 60)"
53+
54+
"Tree automatically stays balanced"
55+
tree validate. "=> true"
56+
tree height. "=> 3 (logarithmic height)"
2657
```
2758

59+
## Real-World Use Case
60+
61+
```smalltalk
62+
"Order book for trading system - needs guaranteed fast operations"
63+
orderBook := CTAVLTree new.
64+
orderBook addAll: #(100.50 100.75 100.25 101.00 99.75).
65+
66+
"Fast operations regardless of market conditions"
67+
bestPrice := orderBook findMax. "=> 101.00"
68+
competitivePrices := orderBook elementsGreaterThan: 100.40.
69+
"=> #(100.50 100.75 101.00)"
70+
71+
"Remove filled orders - tree rebalances automatically"
72+
orderBook remove: 101.00.
73+
orderBook validate. "=> still perfectly balanced"
74+
```
75+
76+
## Performance Advantage
77+
78+
AVL Trees excel with sorted or nearly-sorted data where regular BSTs fail:
79+
80+
```smalltalk
81+
"Worst case for regular BST: sorted input"
82+
sortedData := 1 to: 10000.
83+
84+
avlTree := CTAVLTree new.
85+
avlTree addAll: sortedData.
86+
avlTree height. "=> ~14 (logarithmic)"
87+
88+
"Regular BST would have height 10000 (linear)!"
89+
```
90+
91+
## Contributing
92+
93+
This is part of the Pharo Containers project. Feel free to contribute by implementing additional methods, improving tests, or enhancing documentation.
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"
2-
I am a baseline
2+
I represent Baseline of AVL Tree
33
"
44
Class {
55
#name : 'BaselineOfContainersAVLTree',
@@ -10,9 +10,9 @@ Class {
1010

1111
{ #category : 'baselines' }
1212
BaselineOfContainersAVLTree >> baseline: spec [
13-
1413
<baseline>
15-
spec for: #common do: [ "Packages"
14+
spec for: #common do: [
15+
"Packages"
1616
spec package: 'Containers-AVL-Tree'.
1717
spec
1818
package: 'Containers-AVL-Tree-Tests'
@@ -21,11 +21,11 @@ BaselineOfContainersAVLTree >> baseline: spec [
2121
package: 'Containers-AVL-Tree-Inspector'
2222
with: [ spec requires: #( 'Containers-AVL-Tree' ) ].
2323

24-
"Define groups"
24+
"Groups"
2525
spec
2626
group: 'Core'
2727
with: #( 'Containers-AVL-Tree' 'Containers-AVL-Tree-Inspector' );
2828
group: 'Tests' with: #( 'Containers-AVL-Tree-Tests' ).
2929

3030
spec group: 'default' with: #( 'Core' 'Tests' ) ]
31-
]
31+
]

0 commit comments

Comments
 (0)