-
Notifications
You must be signed in to change notification settings - Fork 13
/
setup.go
81 lines (68 loc) · 1.53 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
81
// SPDX-License-Identifier: ISC
// Copyright (c) 2014-2020 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package avl
// Tree - type to hold the root node of a tree
type Tree struct {
root *Node
count int
}
// New - create an initially empty tree
func New() *Tree {
return &Tree{
root: nil,
count: 0,
}
}
// IsEmpty - true if tree contains some data
func (tree *Tree) IsEmpty() bool {
return nil == tree.root
}
// Count - 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 - 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
}
// Key - read the key from a node item
func (p *Node) Key() Item {
return p.key
}
// Value - read the value from a node item
func (p *Node) Value() interface{} {
return p.value
}
// Parent - return parent node of a node
func (p *Node) Parent() *Node {
return p.up
}
// Depth - 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
}