-
Notifications
You must be signed in to change notification settings - Fork 0
/
binarytree.go
154 lines (134 loc) · 3.11 KB
/
binarytree.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package datastruct
import "fmt"
func NewBinaryTree() *BinaryTree {
return new(BinaryTree)
}
func NewBinaryTreeFromNode(node *BinaryTreeNode) *BinaryTree {
bt := NewBinaryTree()
bt.Root = node
return bt
}
func NewBinaryTreeNode(value interface{}) *BinaryTreeNode {
btn := new(BinaryTreeNode)
btn.Value = value
return btn
}
func (bt *BinaryTree) InsertLeftChild(lt *BinaryTree) {
bt.Root.Left = lt.Root
}
func (bt *BinaryTree) InsertRightChild(rt *BinaryTree) {
bt.Root.Right = rt.Root
}
func (bt *BinaryTree) Left() *BinaryTree {
return NewBinaryTreeFromNode(bt.Root.Left)
}
func (bt *BinaryTree) Right() *BinaryTree {
return NewBinaryTreeFromNode(bt.Root.Right)
}
func (bt *BinaryTree) LeftNode() *BinaryTreeNode {
return bt.Root.Left
}
func (bt *BinaryTree) RightNode() *BinaryTreeNode {
return bt.Root.Right
}
func (bt *BinaryTree) DeleteLeft() {
bt.Root.Left = nil
}
func (bt *BinaryTree) DeleteRight() {
bt.Root.Right = nil
}
func (bt *BinaryTree) PreOrderTraverseRecursive(f BinaryTreeHandleFunc, e BinaryTreeErrorHandleFunc) {
tNode := bt.Root
if tNode == nil {
return
}
err := f(tNode)
if err != nil {
e(err)
}
l := NewBinaryTreeFromNode(tNode.Left)
l.PreOrderTraverseRecursive(f, e)
r := NewBinaryTreeFromNode(tNode.Right)
r.PreOrderTraverseRecursive(f, e)
}
//PreOrderTraverseStack is a stack based pre-order traversal
func (bt *BinaryTree) PreOrderTraverseStack(f BinaryTreeHandleFunc, e BinaryTreeErrorHandleFunc) {
//now we use a stack to do the traversal
var s = make([]*BinaryTreeNode, 0)
s = append(s, bt.Root)
for len(s) > 0 {
h := s[len(s)-1]
s = s[:len(s)-1]
err := f(h)
if err != nil {
e(err)
}
if h.Right != nil {
s = append(s, h.Right)
}
if h.Left != nil {
s = append(s, h.Left)
}
}
}
func (bt *BinaryTree) InOrderTraverseRecursive(f BinaryTreeHandleFunc, e BinaryTreeErrorHandleFunc) {
tNode := bt.Root
if tNode == nil {
return
}
l := NewBinaryTreeFromNode(tNode.Left)
l.InOrderTraverseRecursive(f, e)
err := f(tNode)
if err != nil {
e(err)
}
r := NewBinaryTreeFromNode(tNode.Right)
r.InOrderTraverseRecursive(f, e)
}
func (bt *BinaryTree) PostOrderTraverseRecursive(f BinaryTreeHandleFunc, e BinaryTreeErrorHandleFunc) {
tNode := bt.Root
if tNode == nil {
return
}
l := NewBinaryTreeFromNode(tNode.Left)
l.PostOrderTraverseRecursive(f, e)
r := NewBinaryTreeFromNode(tNode.Right)
r.PostOrderTraverseRecursive(f, e)
err := f(tNode)
if err != nil {
e(err)
}
}
func (bt *BinaryTree) LeverTraverse(f BinaryTreeHandleFunc, e BinaryTreeErrorHandleFunc) {
var q = make([]*BinaryTreeNode, 0)
q = append(q, bt.Root)
for len(q) > 0 {
err := f(q[0])
if err != nil {
e(err)
}
if q[0].Left != nil {
q = append(q, q[0].Left)
}
if q[0].Right != nil {
q = append(q, q[0].Right)
}
q = q[1:]
}
}
func (bt *BinaryTree) Deep() int {
if bt.Root == nil {
return 0
}
l := NewBinaryTreeFromNode(bt.Root.Left)
nLeft := l.Deep()
r := NewBinaryTreeFromNode(bt.Root.Right)
nRight := r.Deep()
if nLeft > nRight {
return nLeft + 1
}
return nRight + 1
}
func (btn *BinaryTreeNode) String() string {
return fmt.Sprintf("%v", btn.Value)
}