public/subjects/btreeinsertdata/README.md

61 lines
931 B
Markdown
Raw Permalink Normal View History

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
import (
2020-02-25 12:02:16 +00:00
"fmt"
"piscine"
)
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
$ go run .
2019-04-24 17:06:54 +00:00
1
4
5
7
$
2019-04-24 17:06:54 +00:00
```