-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsset.go
153 lines (131 loc) · 2.96 KB
/
sset.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package sset
const (
red color = false // This must be the default.
black color = true
)
type color bool
// Node must be implemented by nodes of the set.
type Node interface {
// GetNodeInfo returns the node's set metadata.
// It is usually implemented by embedding NodeInfo.
GetNodeInfo() *NodeInfo
// Cmp returns -1, if z < nd, 0 if z == nd, 1 if z > nd.
Cmp(nd Node) int
// SetValue updates the value to that of nd.
SetValue(nd Node)
}
// NodeInfo holds a node's set metadata.
type NodeInfo struct {
left, right Node
color color
}
func (info *NodeInfo) GetNodeInfo() *NodeInfo {
return info
}
func nodeInfo(h Node) *NodeInfo {
if h == nil {
return nil
}
return h.GetNodeInfo()
}
// SortedSet holds a set of nodes.
type SortedSet struct {
root Node
}
// Get returns a Node with the same value as nd,
// or nil if none was found.
func (set *SortedSet) Get(nd Node) Node {
if set.root == nil {
return nil
}
x := set.root
for {
cmp := nd.Cmp(x)
if cmp == 0 {
return x
}
info := x.GetNodeInfo()
if cmp < 0 {
x = info.left
} else {
x = info.right
}
if x == nil {
return nil
}
}
}
// Len returns the number of elements in the set.
func (set *SortedSet) Len() int {
return nodeLen(set.root)
}
func nodeLen(h Node) int {
if h == nil {
return 0
}
info := h.GetNodeInfo()
return 1 + nodeLen(info.left) + nodeLen(info.right)
}
// Insert adds the given node to the set. If a node
// already exists with the same value, it will be
// changed to nd's value.
func (set *SortedSet) Insert(nd Node) {
info := nd.GetNodeInfo()
if info.left != nil || info.right != nil {
panic("Nodes to be inserted should be nude, but weren't.")
}
set.root = insert(set.root, nd)
set.root.GetNodeInfo().color = black
}
func insert(h Node, in Node) Node {
if h == nil {
return in
}
hinfo := h.GetNodeInfo()
l, r := nodeInfo(hinfo.left), nodeInfo(hinfo.right)
if l != nil && r != nil && l.color == red && r.color == red {
colorFlip(hinfo, l, r)
}
if cmp := in.Cmp(h); cmp == 0 {
h.SetValue(in)
} else if cmp < 0 {
hinfo.left = insert(hinfo.left, in)
l = nodeInfo(hinfo.left)
} else {
hinfo.right = insert(hinfo.right, in)
r = nodeInfo(hinfo.right)
}
if r != nil && r.color == red && !(l != nil && l.color == red) {
h = rotateLeft(h, hinfo, r)
hinfo = h.GetNodeInfo()
l, r = nodeInfo(hinfo.left), nodeInfo(hinfo.right)
}
if l != nil && l.left != nil {
ll := l.left.GetNodeInfo()
if l.color == red && ll.color == red {
h = rotateRight(h, hinfo, l)
}
}
return h
}
func rotateLeft(h Node, hinfo, rinfo *NodeInfo) Node {
x := hinfo.right
hinfo.right = rinfo.left
rinfo.left = h
rinfo.color = hinfo.color
hinfo.color = red
return x
}
func rotateRight(h Node, hinfo, linfo *NodeInfo) Node {
x := hinfo.left
hinfo.left = linfo.right
linfo.right = h
linfo.color = hinfo.color
hinfo.color = red
return x
}
func colorFlip(hinfo, l, r *NodeInfo) {
hinfo.color = !hinfo.color
l.color = !l.color
r.color = !r.color
}