public/subjects/btreeapplypreorder/README.md

51 lines
721 B
Markdown
Raw Permalink Normal View History

2019-06-27 08:55:29 +00:00
## btreeapplypreorder
2019-04-24 17:06:54 +00:00
2019-04-24 17:17:07 +00:00
### Instructions
2019-04-24 17:06:54 +00:00
Write a function that applies a given function `f` to each element in the tree using a preorder walk.
### Notions
- [Tree Traversal](https://en.wikipedia.org/wiki/Tree_traversal)
2019-04-24 17:06:54 +00:00
2019-04-24 17:17:07 +00:00
### Expected function
2019-04-24 17:06:54 +00:00
```go
func BTreeApplyPreorder(root *TreeNode, f func(...interface{}) (int, error)) {
2019-04-24 17:06:54 +00:00
}
```
2019-04-24 17:17:07 +00:00
### Usage
2019-04-24 17:06:54 +00:00
2020-02-25 12:02:16 +00:00
Here is a possible program to test your function :
2019-04-24 17:06:54 +00:00
```go
package main
import (
"fmt"
"piscine"
2019-04-24 17:06:54 +00:00
)
func main() {
root := &piscine.TreeNode{Data: "4"}
piscine.BTreeInsertData(root, "1")
piscine.BTreeInsertData(root, "7")
piscine.BTreeInsertData(root, "5")
piscine.BTreeApplyPreorder(root, fmt.Println)
2019-04-24 17:06:54 +00:00
}
```
And its output :
```console
$ go run .
2019-04-24 17:06:54 +00:00
4
1
7
5
$
2019-04-24 17:06:54 +00:00
```