duanbai1974 2019-03-10 23:58
浏览 9

无法更新方法内部的指针的值

I'm implementing an AVLTreeMap in Go but having some problems with values passed in as pointers. I'm attempting to update the value of a node pointer but its value won't persist after the function.

In the function func (tree *AVLTreeMap) put(key int, value string). I am able to modify the root node of the tree and can print it in the main. However, when I pass that same root to the function func (node *Node) insertByKey(key int, value string) right after, the value won't update and only the value from put() persists.

package main

import (
    "bufio"
    "fmt"
    "os"
    "strconv"
    "strings"
    "io"
)

/* NODE */
type Node struct {
    key     int
    value   string
    left    *Node
    right   *Node
    height  int
}


/* TREE */
type AVLTreeMap struct {
    root *Node
}

func (tree *AVLTreeMap) get(key int) string {
    value := tree.root.lookupByKey(key)
    if value == "" {
        return "Not Found"
    }

    return value
}

func (node *Node) inOrder() {
    fmt.Println(node)
    if (node != nil) {
        fmt.Println(node.key)

        node.left.inOrder()
        node.inOrder()
        node.right.inOrder()
    }
}

func (node *Node) lookupByKey(key int) string {

    if (node == nil) {                  // check for nullptr
        return ""

    } else if (node.key == key) {       // key matching
        return node.value

    } else if (key < node.left.key) {   // recur left
        return node.left.lookupByKey(key)

    } else if (key > node.right.key) {  // recur right
        return node.right.lookupByKey(key)
    } 

    return ""
}

func (node *Node) insertByKey(key int, value string)  {

    if (node == nil) {                  // check for nullptr
        node = &Node{ key: key, value: value,}

    } else if (key < node.key) {   // recur left
        node.left.insertByKey(key, value)

    } else if (key > node.key) {  // recur right
        node.right.insertByKey(key, value)
    }
}

func (tree *AVLTreeMap) put(key int, value string) {
    //fmt.Println(tree.root)
    tree.root = &Node{ key: 1, value: "2",}
    tree.root.insertByKey(key, value)
}

func (tree *AVLTreeMap) remove(key int) {
    return
}

func (tree *AVLTreeMap) levelOrder() {
    return
}

func (tree *AVLTreeMap) rotateLeft(curr *Node) *Node {
    return curr
}

func (tree *AVLTreeMap) rotateRight(curr *Node) *Node {
    return curr
}

func (tree *AVLTreeMap) getHeight(curr *Node) int {
    return 1
}

func (tree *AVLTreeMap) getBalance(curr *Node) int {
    return 1
}

/* UTILS */
func checkError(err error) {
    if err != nil {
        panic(err)
    }
}

func strToInt(val string) int {
    if val, err := strconv.Atoi(val); err == nil {
        return val   
    }

    return 0
}

func readLine(reader *bufio.Reader) string {
    str, _, err := reader.ReadLine()
    if err == io.EOF {
        return ""
    }

    return strings.TrimRight(string(str), "
")
}

func getCommand(command string, tree *AVLTreeMap) {

    vals := strings.Split(command, " ")
    var (
        key int
        val string
    )

    if len(vals) == 1 {         // level order
        tree.levelOrder()

    } else if len(vals) == 3 {  // put
        key, val = strToInt(vals[1]), vals[2]
        tree.put(key, val)

    } else if len(vals) == 2 {  // get
        //key = strToInt(vals[1])
        //fmt.Println(tree.get(key)) 
    }
}

func main() {
    reader := bufio.NewReaderSize(os.Stdin, 16 * 1024 * 1024)

    // get num commands
    numCommands := strToInt(readLine(reader))
    fmt.Println(numCommands)

    // initialize new TreeMap
    tree := AVLTreeMap{}
    //tree.root = &Node{ key: 1, value: "z", }

    for {
        val := readLine(reader)
        if val == "" {
            break;
        } else {

            // run commands on tree
            getCommand(val, &tree)
        }
    }
    fmt.Println(tree.root)
    //tree.root.inOrder()
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 数学建模,尽量用matlab回答,论文格式
    • ¥15 昨天挂载了一下u盘,然后拔了
    • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
    • ¥20 易康econgnition精度验证
    • ¥15 msix packaging tool打包问题
    • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能