Your professor is right in the sense that a root of zero will not free the whole tree. In languages like C, if you make the root=nullwhole tree still in memory.
But in Java, as @radai said, the garbage collector will clear it for you if you no longer have references to tree nodes. So, in the context of Java, make root=nullwill work.
In any case, if you need to clear every node of the tree (which, for example, is necessary in C / C ++), you can use the pos-order algorithm. Of course, you will need to adapt the algorithm to your variable names and structures (or classes if you want to apply them in Java).
void clearTree(treenode *node) {
if (node != NULL) {
clearTree( node->leftChild );
clearTree( node->rightChild );
delete( node );
}
}
source
share