-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.go
69 lines (58 loc) · 1.17 KB
/
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
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
package main
import "fmt"
const count int = 10
type Node struct {
Left *Node
Right *Node
ch byte
weight int
}
func (n *Node) Display(space int) {
// Base case
if n == nil {
return
}
// Increase distance between levels
space += count
// Process right child first
n.Right.Display(space)
// Print current node after space
// count
fmt.Printf("\n")
for i := count; i < space; i++ {
fmt.Printf(" ")
}
if n.ch == 0 {
fmt.Printf("%d\n", n.weight)
} else {
fmt.Printf("\"%s\"-%d\n", string(n.ch), n.weight)
}
// Process left child
n.Left.Display(space)
}
// val=0 if we go left, val=1 if we go right
func (n *Node) Find(b byte, path *[]uint8, isRoot bool, val uint8) bool {
if n == nil {
return false
}
if isRoot {
if(n.Left == nil && n.Right == nil) {
// single-noded tree => one repeating character in whole file
*path = append(*path, 0)
return true
}
} else {
*path = append(*path, val)
}
if n.ch == b {
return true
}
if n.Left.Find(b, path, false, 0) || n.Right.Find(b, path, false, 1) {
return true
}
*path = (*path)[:len(*path)-1]
return false
}
func (n *Node)isLeaf() bool {
return n.Left == nil && n.Right == nil
}