You are currently viewing Null can not be a value of a non-null type TypeVariable(V)

Null can not be a value of a non-null type TypeVariable(V)

I’ve made an algorithm that removes a word from a trie but I cannot figure out why I’m getting this error. Please help and make me understand where I’m lacking…

fun remove(list: List<Key>){

//searching

var currentNode = root

list.forEach { element->

if (currentNode.children[element]==null) return

currentNode = currentNode.children[element]!!

}

if (!currentNode.isWord) return

You set isTerminating to false so that the current node can be removed by the loop in the next step.

currentNode.isWord = false

This is the tricky part. Since nodes can be shared, you don’t want to carelessly remove elements that belong to another collection. If there are no other children in the current node, it means that other collections do not depend on the current node. You also check to see if the current node is a terminating node. If it is, then it belongs to another collection. As long as current satisfies these conditions, you continually backtrack through the parent property and remove the nodes.

val parent = currentNode.parent

while (currentNode.children.isEmpty() && !currentNode.isWord){

parent?.let {

it.children[currentNode.word!!] = null "Null can not be a value of a non-null type

TypeVariable(V)"

currentNode = it

}

}

}

submitted by /u/Winter-Protection-62
[link] [comments]