0

Implementation of the algorithm in Wiki article has a drawback. If the tree consists from only one root then nothing happen. The tree is not modified. How to fix this issue? In C++ it is possible to set root pointer to null, but what to do in python?

9
  • 1
    if the tree has only the root, then the root is a leaf, so you delete it. If you have an object with a reference to the root, you can set it to nothing. Commented May 9, 2014 at 20:33
  • 2
    @msh How are empty trees represented in your code? Because that is the result: An empty tree Commented May 9, 2014 at 20:36
  • 1
    Python has None if you are trying to perform something similar to null in c++ Commented May 9, 2014 at 20:36
  • 1
    This is a continuation of a previous question, and the OP is looking to replace the value with None, but for all names that have that instance assigned to them - this is not possible in Python. Commented May 9, 2014 at 20:40
  • 1
    @Lattyware, that is useful info. Commented May 9, 2014 at 20:42

1 Answer 1

2

This is a continuation of a previous question, and the OP is looking to replace the value with None, but for all names that have that instance assigned to them - this is not possible in Python.

The answer is there is no way to delete the value in this case due to the way Python is designed - you would have to implement an object manager of some kind, store it in a container and access it through that, or change your design not to rely on mutation.

Another question worth asking - is this a case that is likely to happen? It may be possible (and desirable) just to define this as a limitation where the root node can't be destroyed. I can't think of a case where you would want that functionality.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.