-
Notifications
You must be signed in to change notification settings - Fork 13
/
print.go
68 lines (62 loc) · 1.3 KB
/
print.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
// 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
import (
"fmt"
)
// to control the print routine
type branch int
const (
root branch = iota
left branch = iota
right branch = iota
)
// print an ASCII graphic representation of the tree
func (tree *Tree) Print(printData bool) int {
return printTree(tree.root, "", root, printData)
}
// internal print - returns the maximum depth of the tree
func printTree(tree *Node, prefix string, br branch, printData bool) int {
if nil == tree {
return 0
}
rd := 0
ld := 0
if nil != tree.right {
t := " "
if left == br {
t = "| "
}
rd = printTree(tree.right, prefix+t, right, printData)
}
switch br {
case root:
fmt.Printf("%s|------+ ", prefix)
case left:
fmt.Printf("%s\\------+ ", prefix)
case right:
fmt.Printf("%s/------+ ", prefix)
}
up := interface{}(nil)
if nil != tree.up {
up = tree.up.key
}
if printData {
fmt.Printf("%v → '%v' up: %v\n", tree.key, tree.value, up)
} else {
fmt.Printf("%v up: %v\n", tree.key, up)
}
if nil != tree.left {
t := " "
if right == br {
t = "| "
}
ld = printTree(tree.left, prefix+t, left, printData)
}
if rd > ld {
return 1 + rd
} else {
return 1 + ld
}
}