|
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 | + |
| 6 | +[](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. |
0 commit comments