mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
pkg/adt: README initial commit
Signed-off-by: Gyuho Lee <leegyuho@amazon.com>
This commit is contained in:
parent
6917c495e8
commit
19d69d2563
40
pkg/adt/README.md
Normal file
40
pkg/adt/README.md
Normal file
@ -0,0 +1,40 @@
|
||||
|
||||
## Red-Black Tree
|
||||
|
||||
*"Introduction to Algorithms" (Cormen et al, 3rd ed.), Chapter 13*
|
||||
|
||||
1. Every node is either red or black.
|
||||
2. The root is black.
|
||||
3. Every leaf (NIL) is black.
|
||||
4. If a node is red, then both its children are black.
|
||||
5. For each node, all simple paths from the node to descendant leaves contain the
|
||||
same number of black nodes.
|
||||
|
||||
For example,
|
||||
|
||||
```go
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"go.etcd.io/etcd/pkg/adt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ivt := adt.NewIntervalTree()
|
||||
ivt.Insert(NewInt64Interval(510, 511), 0)
|
||||
ivt.Insert(NewInt64Interval(82, 83), 0)
|
||||
ivt.Insert(NewInt64Interval(830, 831), 0)
|
||||
...
|
||||
```
|
||||
|
||||
After inserting the values `510`, `82`, `830`, `11`, `383`, `647`, `899`, `261`, `410`, `514`, `815`, `888`, `972`, `238`, `292`, `953`.
|
||||
|
||||

|
||||
|
||||
Deleting the node `514` should not trigger any rebalancing:
|
||||
|
||||

|
||||
|
||||
Deleting the node `11` (TODO)
|
||||
|
||||
Try yourself at https://www.cs.usfca.edu/~galles/visualization/RedBlack.html.
|
BIN
pkg/adt/img/red-black-tree-01-insertion.png
Normal file
BIN
pkg/adt/img/red-black-tree-01-insertion.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 178 KiB |
BIN
pkg/adt/img/red-black-tree-02-delete-514.png
Normal file
BIN
pkg/adt/img/red-black-tree-02-delete-514.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 151 KiB |
Loading…
x
Reference in New Issue
Block a user