Answer the question
In order to leave comments, you need to log in
Why doesn't a pointer change another pointer in golang?
func (t *Tree) Delete(value int) bool {
if t.Search(value) {
currentNode := t.root
for currentNode != nil {
if value == currentNode.value {
if currentNode.left == nil && currentNode.right == nil {
currentNode = nil
return true
}
} else if value < currentNode.value {
currentNode = currentNode.left
} else {
currentNode = currentNode.right
}
}
}
return false
}
Answer the question
In order to leave comments, you need to log in
currentNode is a variable that exists only in this function. It's a pointer, you assign a value to it, and then you assign nil to it. From this, the previously assigned object will not change, because you are only changing the pointer to this object.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question