第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

BST golang 搜索功能

BST golang 搜索功能

Go
BIG陽 2022-06-01 16:06:11
你能幫忙搜索功能嗎,它總是返回零,我不明白為什么func BTreeSearchItem(root *TreeNode, elem string) *TreeNode {    if root == nil {        return nil    }    if root.Data < elem {        return BTreeSearchItem(root.Left, elem)    } else if root.Data > elem {        return BTreeSearchItem(root.Right, elem)    }    return root}試圖這樣做,但它返回 4 而不是 7func BTreeSearchItem(root *TreeNode, elem string) *TreeNode {    if root == nil {        return nil    }    if root.Data < elem {        BTreeSearchItem(root.Left, elem)    } else if root.Data > elem {        BTreeSearchItem(root.Right, elem)    }    return root}完整代碼是package mainimport "fmt"type TreeNode struct {    Left, Right, Parent *TreeNode    Data                string}func BTreeSearchItem(root *TreeNode, elem string) *TreeNode {    if root == nil {        return nil    }    if root.Data < elem {        BTreeSearchItem(root.Left, elem)    } else if root.Data > elem {        BTreeSearchItem(root.Right, elem)    }    return root}func BTreeInsertData(root *TreeNode, data string) *TreeNode {    if root == nil {        return &TreeNode{Data: data}    }    if root.Data == data {        return nil    }    if root.Data > data {        if root.Left == nil {            root.Left = &TreeNode{Data: data}        }        return BTreeInsertData(root.Left, data)    }    if root.Data < data {        if root.Right == nil {            root.Right = &TreeNode{Data: data}        }        return BTreeInsertData(root.Right, data)    }    return root}func main() {    root := &TreeNode{Data: "4"}    BTreeInsertData(root, "1")    BTreeInsertData(root, "7")    BTreeInsertData(root, "5")    selected := BTreeSearchItem(root, "7")    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")    }}搜索了互聯(lián)網(wǎng)并通過方法找到了很多,但我需要作為功能。我是編程的初學者,所以我很難理解PlayGoundLink
查看完整描述

2 回答

?
白衣非少年

TA貢獻1155條經(jīng)驗 獲得超0個贊

好的,https://play.golang.org/p/gQmdYLfAvIY此代碼有效,BTreeInsertData忘記添加Parant并且BTreeSearchItem我不匹配Right并且Left



查看完整回答
反對 回復 2022-06-01
?
一只甜甜圈

TA貢獻1836條經(jīng)驗 獲得超5個贊

我的建議是在調(diào)用函數(shù)之前檢查rootin的值。我懷疑價值觀本身已經(jīng)處于那個階段。main()BTreeSearchItemrootnil



查看完整回答
反對 回復 2022-06-01
  • 2 回答
  • 0 關(guān)注
  • 173 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號