-
Notifications
You must be signed in to change notification settings - Fork 13
/
iterator.go
75 lines (67 loc) · 1.37 KB
/
iterator.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
// Copyright (c) 2014-2016 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package avl
import ()
// return the node with the lowest key value
func (tree *Tree) First() *Node {
return tree.root.first()
}
// internal: lowest node in a sub-tree
func (tree *Node) first() *Node {
if nil == tree {
return nil
}
for nil != tree.left {
tree = tree.left
}
return tree
}
// return the node with the highest key value
func (tree *Tree) Last() *Node {
return tree.root.last()
}
// internal: highest node in a sub-tree
func (tree *Node) last() *Node {
if nil == tree {
return nil
}
for nil != tree.right {
tree = tree.right
}
return tree
}
// given a node, return the node with the next highest key value or
// nil if no more nodes.
func (tree *Node) Next() *Node {
if nil == tree.right {
key := tree.key
for {
tree = tree.up
if nil == tree {
return nil
}
if 1 == tree.key.Compare(key) { // tree.key > key
return tree
}
}
}
return tree.right.first()
}
// given a node, return the node with the lowest key value or nil if
// no more nodes
func (tree *Node) Prev() *Node {
if nil == tree.left {
key := tree.key
for {
tree = tree.up
if nil == tree {
return nil
}
if -1 == tree.key.Compare(key) { // tree.key < key
return tree
}
}
}
return tree.left.last()
}