|
1 | | -# AVL |
| 1 | +# Containers-AVL-Tree |
2 | 2 |
|
3 | | -[](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. |
4 | 4 |
|
5 | | -AVL Tree migration from Roassal 3. A work in progress since it should be packaged to follow project conventions. |
| 5 | + |
| 6 | +[](LICENSE) |
6 | 7 |
|
7 | | -## How to install it |
| 8 | +## What is an AVL Tree? |
8 | 9 |
|
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). |
10 | 11 |
|
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 |
12 | 15 | Metacello new |
13 | 16 | baseline: 'ContainersAVLTree'; |
14 | | - repository: 'github://pharo-containers/Container-AVL:main'; |
| 17 | + repository: 'github://pharo-containers/Container-AVL/src'; |
15 | 18 | load |
16 | 19 | ``` |
17 | 20 |
|
18 | 21 | ## How to depend on it |
19 | 22 |
|
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 |
21 | 40 |
|
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)" |
26 | 57 | ``` |
27 | 58 |
|
| 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. |
0 commit comments