-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10959 from gyuho/adt
pkg/adt: refactor + add more test cases
- Loading branch information
Showing
18 changed files
with
480 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
|
||
## 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`. | ||
![red-black-tree-01-insertion.png](img/red-black-tree-01-insertion.png) | ||
Deleting the node `514` should not trigger any rebalancing: | ||
![red-black-tree-02-delete-514.png](img/red-black-tree-02-delete-514.png) | ||
Deleting the node `11` triggers multiple rotates for rebalancing: | ||
![red-black-tree-03-delete-11.png](img/red-black-tree-03-delete-11.png) | ||
![red-black-tree-04-delete-11.png](img/red-black-tree-04-delete-11.png) | ||
![red-black-tree-05-delete-11.png](img/red-black-tree-05-delete-11.png) | ||
![red-black-tree-06-delete-11.png](img/red-black-tree-06-delete-11.png) | ||
![red-black-tree-07-delete-11.png](img/red-black-tree-07-delete-11.png) | ||
![red-black-tree-08-delete-11.png](img/red-black-tree-08-delete-11.png) | ||
![red-black-tree-09-delete-11.png](img/red-black-tree-09-delete-11.png) | ||
Try yourself at https://www.cs.usfca.edu/~galles/visualization/RedBlack.html. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.