mirror of https://github.com/01-edu/public.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
1.3 KiB
74 lines
1.3 KiB
5 years ago
|
## btreesearchitem
|
||
6 years ago
|
|
||
6 years ago
|
### Instructions
|
||
6 years ago
|
|
||
5 years ago
|
Write a function that searches for a node with a data element equal to `elem`and that returns that node.
|
||
6 years ago
|
|
||
6 years ago
|
### Expected function
|
||
6 years ago
|
|
||
|
```go
|
||
5 years ago
|
func BTreeSearchItem(root *TreeNode, elem string) *TreeNode {
|
||
6 years ago
|
|
||
|
}
|
||
|
```
|
||
|
|
||
6 years ago
|
### Usage
|
||
6 years ago
|
|
||
5 years ago
|
Here is a possible program to test your function :
|
||
6 years ago
|
|
||
|
```go
|
||
|
package main
|
||
|
|
||
|
import (
|
||
5 years ago
|
"fmt"
|
||
|
piscine ".."
|
||
6 years ago
|
)
|
||
|
|
||
|
func main() {
|
||
5 years ago
|
root := &piscine.TreeNode{Data: "4"}
|
||
|
piscine.BTreeInsertData(root, "1")
|
||
|
piscine.BTreeInsertData(root, "7")
|
||
|
piscine.BTreeInsertData(root, "5")
|
||
|
selected := piscine.BTreeSearchItem(root, "7")
|
||
6 years ago
|
fmt.Print("Item selected -> ")
|
||
|
if selected != nil {
|
||
|
fmt.Println(selected.Data)
|
||
|
} else {
|
||
|
fmt.Println("nil")
|
||
|
}
|
||
|
|
||
|
fmt.Print("Parent of selected item -> ")
|
||
|
if selected.Parent != nil {
|
||
|
fmt.Println(selected.Parent.Data)
|
||
|
} else {
|
||
|
fmt.Println("nil")
|
||
|
}
|
||
|
|
||
|
fmt.Print("Left child of selected item -> ")
|
||
|
if selected.Left != nil {
|
||
|
fmt.Println(selected.Left.Data)
|
||
|
} else {
|
||
|
fmt.Println("nil")
|
||
|
}
|
||
|
|
||
|
fmt.Print("Right child of selected item -> ")
|
||
|
if selected.Right != nil {
|
||
|
fmt.Println(selected.Right.Data)
|
||
|
} else {
|
||
|
fmt.Println("nil")
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
And its output :
|
||
|
|
||
|
```console
|
||
5 years ago
|
student@ubuntu:~/[[ROOT]]/test$ go build
|
||
|
student@ubuntu:~/[[ROOT]]/test$ ./test
|
||
6 years ago
|
Item selected -> 7
|
||
|
Parent of selected item -> 4
|
||
|
Left child of selected item -> 5
|
||
|
Right child of selected item -> nil
|
||
5 years ago
|
student@ubuntu:~/[[ROOT]]/test$
|
||
6 years ago
|
```
|