2019-04-24 17:17:07 +00:00
|
|
|
## btreeinsertdata
|
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:34:09 +00:00
|
|
|
Write a function that inserts new data in a `binary search tree` following the special properties of a `binary search trees`.
|
2019-10-09 21:42:35 +00:00
|
|
|
|
|
|
|
The nodes must be defined as follows :
|
2019-04-24 17:06:54 +00:00
|
|
|
|
2021-06-08 12:34:09 +00:00
|
|
|
### Notions
|
|
|
|
|
|
|
|
- [binary search trees](https://en.wikipedia.org/wiki/Binary_search_tree)
|
|
|
|
|
2019-04-24 17:17:07 +00:00
|
|
|
### Expected function
|
2019-04-24 17:06:54 +00:00
|
|
|
|
|
|
|
```go
|
|
|
|
type TreeNode struct {
|
2020-02-26 11:34:59 +00:00
|
|
|
Left, Right, Parent *TreeNode
|
|
|
|
Data string
|
2019-04-24 17:06:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BTreeInsertData(root *TreeNode, data string) *TreeNode {
|
|
|
|
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
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
|
|
|
|
|
2019-06-21 11:11:31 +00:00
|
|
|
import (
|
2020-02-25 12:02:16 +00:00
|
|
|
"fmt"
|
2021-05-02 15:18:17 +00:00
|
|
|
"piscine"
|
2019-06-21 11:11:31 +00:00
|
|
|
)
|
|
|
|
|
2019-04-24 17:06:54 +00:00
|
|
|
func main() {
|
2020-02-25 12:02:16 +00:00
|
|
|
root := &piscine.TreeNode{Data: "4"}
|
|
|
|
piscine.BTreeInsertData(root, "1")
|
|
|
|
piscine.BTreeInsertData(root, "7")
|
|
|
|
piscine.BTreeInsertData(root, "5")
|
|
|
|
fmt.Println(root.Left.Data)
|
|
|
|
fmt.Println(root.Data)
|
|
|
|
fmt.Println(root.Right.Left.Data)
|
|
|
|
fmt.Println(root.Right.Data)
|
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
|
|
|
|
4
|
|
|
|
5
|
|
|
|
7
|
2021-04-28 09:47:34 +00:00
|
|
|
$
|
2019-04-24 17:06:54 +00:00
|
|
|
```
|