-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree_test.go
75 lines (64 loc) · 1.2 KB
/
tree_test.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
package util
import (
"fmt"
"testing"
)
func TestTree(t *testing.T) {
/*
A
|-B-E
|-C
|-D
*/
b := NewNode()
e := NewNode()
e.Put("ID", "E")
f := NewNode()
f.Put("ID", "F")
b.AddChild(e)
b.AddChild(f)
b.Put("ID", "B")
c := NewNode()
c.Put("ID", "C")
d := NewNode()
d.Put("ID", "D")
root := NewNode()
root.Put("ID", "A")
root.AddChild(b)
root.AddChild(c)
root.AddChild(d)
root.Format = func(n *Node) string {
return n.Data["ID"].(string)
}
tree := Tree{
Root: root,
}
fmt.Println(tree)
fmt.Println("BFS")
tree.BFS(func(n *Node) {
fmt.Print(n.Data, " ")
})
fmt.Println("\npre order")
tree.DFSPreOrder(func(n *Node) {
fmt.Print(n.Data, " ")
})
fmt.Println("\npost order")
tree.DFSPostOrder(func(n *Node) {
fmt.Print(n.Data, " ")
})
fmt.Println("\ndepth: ", tree.Depth())
fmt.Println("degree: ", tree.Degree())
fmt.Println("height: ", tree.Height())
fmt.Println("level: ", tree.Level())
n, ok := tree.Search("ID", func(i, j interface{}) bool {
return i == j
}, "B", "E")
fmt.Println(n, ok)
fmt.Println(tree)
n = tree.Insert("ID", func(i, j interface{}) bool {
return i == j
}, "B", "E", "G")
fmt.Println("insert")
fmt.Println(n)
fmt.Println(tree)
}