2019-06-27 08:55:29 +00:00
|
|
|
## btreeapplypostorder
|
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
|
|
|
|
2021-06-08 12:55:01 +00:00
|
|
|
Write a function that applies a given function `f`, to each element in the tree using a postorder 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
|
2019-06-27 13:42:34 +00:00
|
|
|
func BTreeApplyPostorder(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 (
|
2020-02-26 09:46:18 +00:00
|
|
|
"fmt"
|
2021-05-02 15:18:17 +00:00
|
|
|
"piscine"
|
2019-04-24 17:06:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2020-02-26 09:46:18 +00:00
|
|
|
root := &piscine.TreeNode{Data: "4"}
|
|
|
|
piscine.BTreeInsertData(root, "1")
|
|
|
|
piscine.BTreeInsertData(root, "7")
|
|
|
|
piscine.BTreeInsertData(root, "5")
|
|
|
|
piscine.BTreeApplyPostorder(root, fmt.Println)
|
2019-04-24 17:06:54 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
And its output :
|
|
|
|
|
|
|
|
```console
|
2021-04-28 09:47:34 +00:00
|
|
|
$ go run .
|
2019-04-24 17:06:54 +00:00
|
|
|
1
|
|
|
|
5
|
|
|
|
7
|
|
|
|
4
|
2021-04-28 09:47:34 +00:00
|
|
|
$
|
2019-04-24 17:06:54 +00:00
|
|
|
```
|