-
Notifications
You must be signed in to change notification settings - Fork 2
/
tree.go
41 lines (34 loc) · 968 Bytes
/
tree.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
package splaytree
// SplayTree defines the splay tree type.
type SplayTree struct {
root *node
}
// Compile time test if SplayTree fully implements Interface.
var _ Interface = (*SplayTree)(nil)
// Clear all elements from the tree.
func (tree *SplayTree) Clear() {
tree.root = nil
}
// Count the number of elements in the tree.
func (tree *SplayTree) Count() int {
return tree.root.count()
}
// Height computes the height of the tree.
// This is the number of steps from
// the root to the farthest element.
// An empty tree has height -1.
func (tree *SplayTree) Height() int {
return -1 + tree.root.height()
}
// NonEmpty tests if the tree contains at least one element.
func (tree *SplayTree) NonEmpty() bool {
return tree.root != nil
}
// Root gives the element which is currently at the root
// of the tree. If the tree is empty then nil is returned.
func (tree *SplayTree) Root() Item {
if tree.root == nil {
return nil
}
return tree.root.item
}