Skip to content

Commit 3e59cee

Browse files
committed
rearrange param and receiver
1 parent 25a1408 commit 3e59cee

File tree

3 files changed

+31
-39
lines changed

3 files changed

+31
-39
lines changed

comparer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (t *Tree[T]) compare(a, b T) int {
8080
}
8181
}
8282

83-
// cmpCovers, returns true if a cmpCovers b.
83+
// cmpCovers, returns true if a covers b.
8484
//
8585
// =================================================================|
8686
// | visualization | ll | rr | lr | rl | description |

helpers.go

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,38 @@ const (
1616

1717
// traverse the BST in some order, call the visitor function for each node.
1818
// Prematurely stop traversion if visitor function returns false.
19-
func (n *node[T]) traverse(order traverseOrder, depth int, visitFn func(n *node[T], depth int) bool) bool {
19+
func (t *Tree[T]) traverse(n *node[T], order traverseOrder, depth int, visitFn func(n *node[T], depth int) bool) bool {
2020
if n == nil {
2121
return true
2222
}
2323

2424
switch order {
2525
case inorder:
2626
// left, do-it, right
27-
if !n.left.traverse(order, depth+1, visitFn) {
27+
if !t.traverse(n.left, order, depth+1, visitFn) {
2828
return false
2929
}
3030

3131
if !visitFn(n, depth) {
3232
return false
3333
}
3434

35-
if !n.right.traverse(order, depth+1, visitFn) {
35+
if !t.traverse(n.right, order, depth+1, visitFn) {
3636
return false
3737
}
3838

3939
return true
4040
case reverse:
4141
// right, do-it, left
42-
if !n.right.traverse(order, depth+1, visitFn) {
42+
if !t.traverse(n.right, order, depth+1, visitFn) {
4343
return false
4444
}
4545

4646
if !visitFn(n, depth) {
4747
return false
4848
}
4949

50-
if !n.left.traverse(order, depth+1, visitFn) {
50+
if !t.traverse(n.left, order, depth+1, visitFn) {
5151
return false
5252
}
5353

@@ -100,7 +100,7 @@ func (t Tree[T]) Fprint(w io.Writer) error {
100100
// init map
101101
pcm.pcMap = make(map[*node[T]][]*node[T])
102102

103-
pcm = t.root.buildParentChildsMap(pcm, &t)
103+
pcm = t.buildParentChildsMap(t.root, pcm)
104104

105105
if len(pcm.pcMap) == 0 {
106106
return nil
@@ -111,14 +111,14 @@ func (t Tree[T]) Fprint(w io.Writer) error {
111111
return err
112112
}
113113

114-
// start recursion with root and empty padding
115-
return walkAndStringify(w, pcm, nil, "")
114+
// start recursion with nil parent and empty padding
115+
return t.hierarchyStringify(w, nil, pcm, "")
116116
}
117117

118-
func walkAndStringify[T any](w io.Writer, pcm parentChildsMap[T], parent *node[T], pad string) error {
118+
func (t *Tree[T]) hierarchyStringify(w io.Writer, n *node[T], pcm parentChildsMap[T], pad string) error {
119119
// the prefix (pad + glyphe) is already printed on the line on upper level
120-
if parent != nil {
121-
if _, err := fmt.Fprintf(w, "%v\n", parent.item); err != nil {
120+
if n != nil {
121+
if _, err := fmt.Fprintf(w, "%v\n", n.item); err != nil {
122122
return err
123123
}
124124
}
@@ -127,22 +127,23 @@ func walkAndStringify[T any](w io.Writer, pcm parentChildsMap[T], parent *node[T
127127
spacer := "│ "
128128

129129
// dereference child-slice for clearer code
130-
childs := pcm.pcMap[parent]
130+
childs := pcm.pcMap[n]
131131

132132
// for all childs do, but ...
133-
for i := range childs {
133+
for i, child := range childs {
134134
// ... treat last child special
135135
if i == len(childs)-1 {
136136
glyphe = "└─ "
137137
spacer = " "
138138
}
139+
139140
// print prefix for next item
140141
if _, err := fmt.Fprint(w, pad+glyphe); err != nil {
141142
return err
142143
}
143144

144145
// recdescent down
145-
if err := walkAndStringify(w, pcm, childs[i], pad+spacer); err != nil {
146+
if err := t.hierarchyStringify(w, child, pcm, pad+spacer); err != nil {
146147
return err
147148
}
148149
}
@@ -180,13 +181,15 @@ func (t Tree[T]) FprintBST(w io.Writer) error {
180181
}
181182

182183
// start recursion with empty padding
183-
return t.root.preorderStringify(w, "")
184+
return t.binarytreeStringify(w, t.root, "")
184185
}
185186

186-
// preorderStringify, traverse the tree, stringify the nodes in preorder
187-
func (n *node[T]) preorderStringify(w io.Writer, pad string) error {
187+
// binarytreeStringify, traverse the tree, stringify the nodes in preorder
188+
func (t *Tree[T]) binarytreeStringify(w io.Writer, n *node[T], pad string) error {
188189
// stringify this node
189-
if _, err := fmt.Fprintf(w, "%v [prio:%.4g] [%p|l:%p|r:%p]\n", n.item, float64(n.prio)/math.MaxUint32, n, n.left, n.right); err != nil {
190+
_, err := fmt.Fprintf(w, "%v [prio:%.4g] [%p|l:%p|r:%p]\n",
191+
n.item, float64(n.prio)/math.MaxUint32, n, n.left, n.right)
192+
if err != nil {
190193
return err
191194
}
192195

@@ -206,7 +209,7 @@ func (n *node[T]) preorderStringify(w io.Writer, pad string) error {
206209
if _, err := fmt.Fprint(w, pad+glyphe); err != nil {
207210
return err
208211
}
209-
if err := n.left.preorderStringify(w, pad+spacer); err != nil {
212+
if err := t.binarytreeStringify(w, n.left, pad+spacer); err != nil {
210213
return err
211214
}
212215
}
@@ -218,7 +221,7 @@ func (n *node[T]) preorderStringify(w io.Writer, pad string) error {
218221
if _, err := fmt.Fprint(w, pad+glyphe); err != nil {
219222
return err
220223
}
221-
if err := n.right.preorderStringify(w, pad+spacer); err != nil {
224+
if err := t.binarytreeStringify(w, n.right, pad+spacer); err != nil {
222225
return err
223226
}
224227
}
@@ -248,27 +251,23 @@ type parentChildsMap[T any] struct {
248251
}
249252

250253
// buildParentChildsMap, in-order traversal
251-
//
252-
// The parameter t is needed to access the compare function.
253-
func (n *node[T]) buildParentChildsMap(pcm parentChildsMap[T], t *Tree[T]) parentChildsMap[T] {
254+
func (t *Tree[T]) buildParentChildsMap(n *node[T], pcm parentChildsMap[T]) parentChildsMap[T] {
254255
if n == nil {
255256
return pcm
256257
}
257258

258259
// in-order traversal, left tree
259-
pcm = n.left.buildParentChildsMap(pcm, t)
260+
pcm = t.buildParentChildsMap(n.left, pcm)
260261

261262
// detect parent-child-mapping for this node
262-
pcm = n.pcmForNode(pcm, t)
263+
pcm = t.pcmForNode(n, pcm)
263264

264265
// in-order traversal, right tree
265-
return n.right.buildParentChildsMap(pcm, t)
266+
return t.buildParentChildsMap(n.right, pcm)
266267
}
267268

268269
// pcmForNode, find parent in stack, remove items from stack, put this item on stack.
269-
//
270-
// The parameter t is needed to access the compare function.
271-
func (n *node[T]) pcmForNode(pcm parentChildsMap[T], t *Tree[T]) parentChildsMap[T] {
270+
func (t *Tree[T]) pcmForNode(n *node[T], pcm parentChildsMap[T]) parentChildsMap[T] {
272271
// if this item is covered by a prev item on stack
273272
for j := len(pcm.stack) - 1; j >= 0; j-- {
274273

@@ -307,7 +306,7 @@ func (t Tree[T]) Statistics() (size int, maxDepth int, average, deviation float6
307306
depths := make(map[int]int)
308307

309308
// get the depths, sum up the size
310-
t.root.traverse(inorder, 0, func(n *node[T], depth int) bool {
309+
t.traverse(t.root, inorder, 0, func(n *node[T], depth int) bool {
311310
depths[depth] += 1
312311
size += 1
313312
return true
@@ -388,7 +387,7 @@ func (t Tree[T]) Visit(start, stop T, visitFn func(item T) bool) {
388387

389388
span := (&t).join(mid1, (&t).join(l, mid2, true), true)
390389

391-
span.traverse(order, 0, func(n *node[T], _ int) bool {
390+
t.traverse(span, order, 0, func(n *node[T], _ int) bool {
392391
return visitFn(n.item)
393392
})
394393
}

treap.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ func NewTree[T any](cmp func(a, b T) (ll, rr, lr, rl int), items ...T) Tree[T] {
5656
}
5757

5858
// makeNode, create new node with item and random priority.
59-
// The parameter t is needed to access the compare function.
6059
func (t *Tree[T]) makeNode(item T) *node[T] {
6160
n := new(node[T])
6261
n.item = item
@@ -467,8 +466,6 @@ func (t Tree[T]) Covers(item T) []T {
467466
}
468467

469468
// covers rec-descent
470-
//
471-
// The parameter t is needed to access the compare function.
472469
func (t *Tree[T]) covers(n *node[T], item T) (result []T) {
473470
if n == nil {
474471
return
@@ -641,8 +638,6 @@ func (t Tree[T]) PrecededBy(item T) []T {
641638
}
642639

643640
// precededBy rec-desent
644-
//
645-
// The parameter t is needed to access the compare function.
646641
func (t *Tree[T]) precededBy(n *node[T], item T) (result []T) {
647642
if n == nil {
648643
return
@@ -705,8 +700,6 @@ func (t *Tree[T]) join(n, m *node[T], immutable bool) *node[T] {
705700

706701
// recalc the augmented fields in treap node after each creation/modification with values in descendants.
707702
// Only one level deeper must be considered. The treap datastructure is very easy to augment.
708-
//
709-
// The parameter t is needed to access the compare function.
710703
func (t *Tree[T]) recalc(n *node[T]) {
711704
if n == nil {
712705
return

0 commit comments

Comments
 (0)