-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary-tree.go
185 lines (149 loc) · 3.56 KB
/
binary-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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package karytree
const (
left = iota
right = iota
)
// Binary creates a binary karytree.Node
func Binary[T comparable](key T) Node[T] {
return NewNode(key)
}
// SetLeft sets the left child.
func (k *Node[T]) SetLeft(other *Node[T]) {
k.SetNthChild(left, other)
}
// SetRight sets the left child.
func (k *Node[T]) SetRight(other *Node[T]) {
k.SetNthChild(right, other)
}
// Left gets the left child
func (k *Node[T]) Left() *Node[T] {
return k.NthChild(left)
}
// Right gets the right child
func (k *Node[T]) Right() *Node[T] {
return k.NthChild(right)
}
// InorderIterative is a channel-based iterative implementation of an preorder traversal.
func InorderIterative[T comparable](root *Node[T], quit <-chan struct{}) <-chan *Node[T] {
nChan := make(chan *Node[T])
go func() {
defer close(nChan)
stack := []*Node[T]{}
var curr *Node[T]
curr = root
for {
for curr != nil {
stack = append(stack, curr)
curr = curr.Left()
}
if len(stack) == 0 {
break
}
stack, curr = stack[:len(stack)-1], stack[len(stack)-1]
select {
case <-quit:
return
case nChan <- curr:
}
curr = curr.Right()
}
}()
return nChan
}
// PreorderIterative is a channel-based iterative implementation of an preorder traversal.
func PreorderIterative[T comparable](root *Node[T], quit <-chan struct{}) <-chan *Node[T] {
nChan := make(chan *Node[T])
go func() {
defer close(nChan)
stack := []*Node[T]{}
var curr *Node[T]
curr = root
for {
select {
case <-quit:
return
case nChan <- curr:
}
left := curr.Left()
if left != nil {
right := curr.Right()
if right != nil {
stack = append(stack, right)
}
curr = left
continue
}
if len(stack) == 0 {
break
}
stack, curr = stack[:len(stack)-1], stack[len(stack)-1]
continue
}
}()
return nChan
}
// PostorderIterative is a channel-based iterative implementation of an preorder traversal.
func PostorderIterative[T comparable](root *Node[T], quit <-chan struct{}) <-chan *Node[T] {
nChan := make(chan *Node[T])
go func() {
defer close(nChan)
stack1 := []*Node[T]{}
stack2 := []*Node[T]{}
var curr *Node[T]
stack1 = append(stack1, root)
for len(stack1) != 0 {
stack1, curr = stack1[:len(stack1)-1], stack1[len(stack1)-1]
stack2 = append(stack2, curr)
left := curr.Left()
if left != nil {
stack1 = append(stack1, left)
}
right := curr.Right()
if right != nil {
stack1 = append(stack1, right)
}
}
for len(stack2) != 0 {
stack2, curr = stack2[:len(stack2)-1], stack2[len(stack2)-1]
select {
case <-quit:
return
case nChan <- curr:
}
}
}()
return nChan
}
// InorderRecursive is a recursive inorder traversal with visitors
func InorderRecursive[T comparable](root *Node[T], f func(*Node[T])) {
inorder(root, f)
}
func inorder[T comparable](root *Node[T], f func(*Node[T])) {
if root != nil {
inorder(root.Left(), f)
f(root)
inorder(root.Right(), f)
}
}
// PreorderRecursive is a recursive inorder traversal with visitors
func PreorderRecursive[T comparable](root *Node[T], f func(*Node[T])) {
preorder(root, f)
}
func preorder[T comparable](root *Node[T], f func(*Node[T])) {
if root != nil {
f(root)
preorder(root.Left(), f)
preorder(root.Right(), f)
}
}
// PostorderRecursive is a recursive inorder traversal with visitors
func PostorderRecursive[T comparable](root *Node[T], f func(*Node[T])) {
postorder(root, f)
}
func postorder[T comparable](root *Node[T], f func(*Node[T])) {
if root != nil {
postorder(root.Left(), f)
postorder(root.Right(), f)
f(root)
}
}