Debian Codegraph @

examples/redblacktree/redblacktree.go

raw symbols SCIP
1 // Copyright (c) 2015, Emir Pasic. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package main
6
7 import (
8 "fmt"
9
10 rbt "github.com/emirpasic/gods/v2/trees/redblacktree"
11 )
12
13 // RedBlackTreeExample to demonstrate basic usage of RedBlackTree
14 func main() {
15 tree := rbt.New[int, string]() // empty(keys are of type int)
16
17 tree.Put(1, "x") // 1->x
18 tree.Put(2, "b") // 1->x, 2->b (in order)
19 tree.Put(1, "a") // 1->a, 2->b (in order, replacement)
20 tree.Put(3, "c") // 1->a, 2->b, 3->c (in order)
21 tree.Put(4, "d") // 1->a, 2->b, 3->c, 4->d (in order)
22 tree.Put(5, "e") // 1->a, 2->b, 3->c, 4->d, 5->e (in order)
23 tree.Put(6, "f") // 1->a, 2->b, 3->c, 4->d, 5->e, 6->f (in order)
24
25 fmt.Println(tree)
26 //
27 // RedBlackTree
28 // │ ┌── 6
29 // │ ┌── 5
30 // │ ┌── 4
31 // │ │ └── 3
32 // └── 2
33 // └── 1
34
35 _ = tree.Values() // []interface {}{"a", "b", "c", "d", "e", "f"} (in order)
36 _ = tree.Keys() // []interface {}{1, 2, 3, 4, 5, 6} (in order)
37
38 tree.Remove(2) // 1->a, 3->c, 4->d, 5->e, 6->f (in order)
39 fmt.Println(tree)
40 //
41 // RedBlackTree
42 // │ ┌── 6
43 // │ ┌── 5
44 // └── 4
45 // │ ┌── 3
46 // └── 1
47
48 tree.Clear() // empty
49 tree.Empty() // true
50 tree.Size() // 0
51 }