-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.go
More file actions
155 lines (145 loc) · 2.73 KB
/
node.go
File metadata and controls
155 lines (145 loc) · 2.73 KB
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
154
155
package avl
import "fmt"
// Node is contained in an [AVL].
//
// T should be a pointer to return nil if the value is not found.
type Node[T any] struct {
Value T
heigth uint
left *Node[T]
right *Node[T]
}
func (n *Node[T]) String() string {
left := `.`
if n.left != nil {
left = n.left.String()
}
right := `.`
if n.right != nil {
right = n.right.String()
}
if n.right == n.left {
return fmt.Sprintf("%v", n.Value)
}
return fmt.Sprintf("{%d}%v: [%s, %s]", n.heigth, n.Value, left, right)
}
func (n *Node[T]) updateHeight() {
left := uint(0)
if n.left != nil {
left = n.left.heigth
}
right := uint(0)
if n.right != nil {
right = n.right.heigth
}
n.heigth = 1 + max(left, right)
}
func (n *Node[T]) rotateLeft() *Node[T] {
next := n.right
n.right = next.left
next.left = n
n.updateHeight()
next.updateHeight()
return next
}
func (n *Node[T]) rotateRight() *Node[T] {
next := n.left
n.left = next.right
next.right = n
n.updateHeight()
next.updateHeight()
return next
}
func (n *Node[T]) rotate(cmp CompareFunc[T]) *Node[T] {
left := 0
if n.left != nil {
left = int(n.left.heigth)
}
right := 0
if n.right != nil {
right = int(n.right.heigth)
}
switch left - right {
case 2:
l := n.left
if cmp(l.Value, n.Value) > 0 {
n.left = l.rotateLeft()
}
return n.rotateRight()
case -2:
r := n.right
if cmp(r.Value, n.Value) < 0 {
n.right = r.rotateRight()
}
return n.rotateLeft()
default:
return n
}
}
func (n *Node[T]) insert(v T, cmp CompareFunc[T]) (*Node[T], bool) {
comp := cmp(n.Value, v)
var next **Node[T]
if comp == 0 {
n.Value = v
return n, false
} else if comp > 0 {
next = &n.left
} else {
next = &n.right
}
var ok bool
if *next != nil {
*next, ok = (*next).insert(v, cmp)
} else {
ok = true
*next = newNode(v)
}
n.updateHeight()
return n.rotate(cmp), ok
}
func (n *Node[T]) delete(key T, cmp CompareFunc[T]) (*Node[T], bool) {
comp := cmp(n.Value, key)
res := n
var ok bool
if comp == 0 {
ok = true
if n.right == nil {
res = n.left
} else if n.left == nil {
res = n.right
} else {
cur := n.left
for cur.right != nil {
cur = cur.right
}
n.Value = cur.Value
n.left, _ = n.left.delete(cur.Value, cmp)
}
} else if comp < 0 {
if n.right == nil {
return nil, false
}
n.right, ok = n.right.delete(key, cmp)
} else {
if n.left == nil {
return nil, false
}
n.left, ok = n.left.delete(key, cmp)
}
if res != nil {
res.updateHeight()
return res.rotate(cmp), ok
}
return res, ok
}
// Clone the [Node].
func (n *Node[T]) Clone(clone func(T) T) *Node[T] {
node := newNode(clone(n.Value))
if n.left != nil {
node.left = n.left.Clone(clone)
}
if n.right != nil {
node.right = n.right.Clone(clone)
}
return node
}