-
Notifications
You must be signed in to change notification settings - Fork 13
/
setup.go
80 lines (67 loc) · 1.43 KB
/
setup.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
// Copyright (c) 2014-2017 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package avl
// type to hold the root node of a tree
type Tree struct {
root *Node
count int
}
// create an initially empty tree
func New() *Tree {
return &Tree{
root: nil,
count: 0,
}
}
// true if tree contains some data
func (tree *Tree) IsEmpty() bool {
return nil == tree.root
}
// number of nodes currently in the tree
func (tree *Tree) Count() int {
return tree.count
}
// Root return the root node of the tree
func (tree *Tree) Root() *Node {
return tree.root
}
// GetChildrenByDepth will returns all children in a specific depth of a tree
func (p *Node) GetChildrenByDepth(depth uint) []*Node {
nodes := []*Node{}
if depth == 0 {
nodes = []*Node{p}
} else {
left := p.left
right := p.right
if left != nil {
nodes = append(nodes, left.GetChildrenByDepth(depth-1)...)
}
if right != nil {
nodes = append(nodes, right.GetChildrenByDepth(depth-1)...)
}
}
return nodes
}
// read the key from a node
func (p *Node) Key() item {
return p.key
}
// read the value from a node
func (p *Node) Value() interface{} {
return p.value
}
// return parent node of a node
func (p *Node) Parent() *Node {
return p.up
}
// get the depth of a node
func (p *Node) Depth() uint {
count := uint(0)
parent := p.up
for parent != nil {
count += 1
parent = parent.up
}
return count
}