forked from emirpasic/gods
-
Notifications
You must be signed in to change notification settings - Fork 6
/
redblacktreeextended.go
114 lines (99 loc) · 3.04 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/ugurcsen/gods-generic/trees/redblacktree"
)
// RedBlackTreeExtended to demonstrate how to extend a RedBlackTree to include new functions
type RedBlackTreeExtended[K comparable, T any] struct {
*rbt.Tree[K, T]
}
// GetMin gets the min value and flag if found
func (tree *RedBlackTreeExtended[K, T]) GetMin() (value interface{}, found bool) {
node, found := tree.getMinFromNode(tree.Root)
if node != nil {
return node.Value, found
}
return nil, false
}
// GetMax gets the max value and flag if found
func (tree *RedBlackTreeExtended[K, T]) GetMax() (value interface{}, found bool) {
node, found := tree.getMaxFromNode(tree.Root)
if node != nil {
return node.Value, found
}
return nil, false
}
// RemoveMin removes the min value and flag if found
func (tree *RedBlackTreeExtended[K, T]) RemoveMin() (value interface{}, deleted bool) {
node, found := tree.getMinFromNode(tree.Root)
if found {
tree.Remove(node.Key)
return node.Value, found
}
return nil, false
}
// RemoveMax removes the max value and flag if found
func (tree *RedBlackTreeExtended[K, T]) RemoveMax() (value T, deleted bool) {
node, found := tree.getMaxFromNode(tree.Root)
if found {
tree.Remove(node.Key)
return node.Value, found
}
var empty T
return empty, false
}
func (tree *RedBlackTreeExtended[K, T]) getMinFromNode(node *rbt.Node[K, T]) (foundNode *rbt.Node[K, T], found bool) {
if node == nil {
return nil, false
}
if node.Left == nil {
return node, true
}
return tree.getMinFromNode(node.Left)
}
func (tree *RedBlackTreeExtended[K, T]) getMaxFromNode(node *rbt.Node[K, T]) (foundNode *rbt.Node[K, T], 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.NewWithNumberComparator[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
}