-
Notifications
You must be signed in to change notification settings - Fork 2
/
find.go
44 lines (41 loc) · 895 Bytes
/
find.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
package splaytree
// Min gives the smallest element in the tree.
// If the tree is empty then nil is returned.
func (tree *SplayTree) Min() Item {
node := tree.root
if node == nil {
return nil
}
for node.left != nil {
node = node.left
}
item := node.item
tree.splay(item)
return item
}
// Max gives the largest element in the tree.
// If the tree is empty then nil is returned.
func (tree *SplayTree) Max() Item {
node := tree.root
if node == nil {
return nil
}
for node.right != nil {
node = node.right
}
item := node.item
tree.splay(item)
return item
}
// Lookup an item and return the found item.
// If the tree is empty then nil is returned.
func (tree *SplayTree) Lookup(item Item) Item {
if item == nil || tree.root == nil {
return nil
}
tree.splay(item)
if item.Less(tree.root.item) || tree.root.item.Less(item) {
return nil
}
return tree.root.item
}