torres
06/26/2020, 1:13 PMfun main(args: Array<String>) {
var tree = Node("A", null, null)
val q: Queue<Node> = LinkedList()
q.add(tree)
tree = q.remove() // line 5, remove element in q and assign to tree so tree so should size 0
println(tree.data) // line 6, why does this still print "A" though?
}
// given
class Node {
var data: String
var left: Node? = null
var right: Node? = null
constructor(data: String) {
this.data = data
}
constructor(data: String, left: Node?, right: Node?) {
this.data = data
this.left = left
this.right = right
}
}diesieben07
06/26/2020, 1:15 PMTree object. You put it into the queue and then get it back out, but it's still the same Node objecttorres
06/26/2020, 1:17 PMtree = q.remove() // line 5diesieben07
06/26/2020, 1:18 PMtree. So what it does is change the list contentsdiesieben07
06/26/2020, 1:18 PMtreeMichael de Kaste
06/26/2020, 1:19 PMMichael de Kaste
06/26/2020, 1:21 PMq.remove() removes a node from it's queue, but returns it for you. and you assigned that into your var treetorres
06/26/2020, 1:21 PMtree is declared as var so it should take the content of q.remove()torres
06/26/2020, 1:21 PMMichael de Kaste
06/26/2020, 1:22 PMtorres
06/26/2020, 1:22 PMtree shoudl have no content?Michael de Kaste
06/26/2020, 1:22 PMMichael de Kaste
06/26/2020, 1:22 PMq.remove() statement doestorres
06/26/2020, 1:23 PMtorres
06/26/2020, 1:23 PMtorres
06/26/2020, 1:23 PMreturn removeFirst();torres
06/26/2020, 1:23 PM