-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathredblacktreeextended.go
114 lines (98 loc) · 2.97 KB
/
redblacktreeextended.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package redblacktreeextended
import (
"fmt"
rbt "github.com/emirpasic/gods/v2/trees/redblacktree"
)
// RedBlackTreeExtended to demonstrate how to extend a RedBlackTree to include new functions
type RedBlackTreeExtended[K comparable, V any] struct {
*rbt.Tree[K, V]
}
// GetMin gets the min value and flag if found
func (tree *RedBlackTreeExtended[K, V]) GetMin() (value V, found bool) {
node, found := tree.getMinFromNode(tree.Root)
if found {
return node.Value, found
}
return value, false
}
// GetMax gets the max value and flag if found
func (tree *RedBlackTreeExtended[K, V]) GetMax() (value V, found bool) {
node, found := tree.getMaxFromNode(tree.Root)
if found {
return node.Value, found
}
return value, false
}
// RemoveMin removes the min value and flag if found
func (tree *RedBlackTreeExtended[K, V]) RemoveMin() (value V, deleted bool) {
node, found := tree.getMinFromNode(tree.Root)
if found {
tree.Remove(node.Key)
return node.Value, found
}
return value, false
}
// RemoveMax removes the max value and flag if found
func (tree *RedBlackTreeExtended[K, V]) RemoveMax() (value V, deleted bool) {
node, found := tree.getMaxFromNode(tree.Root)
if found {
tree.Remove(node.Key)
return node.Value, found
}
return value, false
}
func (tree *RedBlackTreeExtended[K, V]) getMinFromNode(node *rbt.Node[K, V]) (foundNode *rbt.Node[K, V], found bool) {
if node == nil {
return nil, false
}
if node.Left == nil {
return node, true
}
return tree.getMinFromNode(node.Left)
}
func (tree *RedBlackTreeExtended[K, V]) getMaxFromNode(node *rbt.Node[K, V]) (foundNode *rbt.Node[K, V], found bool) {
if node == nil {
return nil, false
}
if node.Right == nil {
return node, true
}
return tree.getMaxFromNode(node.Right)
}
func print(tree *RedBlackTreeExtended[int, string]) {
max, _ := tree.GetMax()
min, _ := tree.GetMin()
fmt.Printf("Value for max key: %v \n", max)
fmt.Printf("Value for min key: %v \n", min)
fmt.Println(tree)
}
// RedBlackTreeExtendedExample main method on how to use the custom red-black tree above
func main() {
tree := RedBlackTreeExtended[int, string]{rbt.New[int, string]()}
tree.Put(1, "a") // 1->x (in order)
tree.Put(2, "b") // 1->x, 2->b (in order)
tree.Put(3, "c") // 1->x, 2->b, 3->c (in order)
tree.Put(4, "d") // 1->x, 2->b, 3->c, 4->d (in order)
tree.Put(5, "e") // 1->x, 2->b, 3->c, 4->d, 5->e (in order)
print(&tree)
// Value for max key: e
// Value for min key: a
// RedBlackTree
// │ ┌── 5
// │ ┌── 4
// │ │ └── 3
// └── 2
// └── 1
tree.RemoveMin() // 2->b, 3->c, 4->d, 5->e (in order)
tree.RemoveMax() // 2->b, 3->c, 4->d (in order)
tree.RemoveMin() // 3->c, 4->d (in order)
tree.RemoveMax() // 3->c (in order)
print(&tree)
// Value for max key: c
// Value for min key: c
// RedBlackTree
// └── 3
}