AVL Balancing

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Thu Nov 23 01:26:53 CET 2006


scbauer wrote:
> This is one of the errors that im getting
> Traceback (most recent call last):
>   File "<pyshell#49>", line 1, in <module>
>     t.insert(5)
>   File "/Users/stevenbauer/Desktop/AVL.py", line 68, in insert
>     stack.append(current)
> NameError: global name 'stack' is not defined

    def __init__(self):
        self.__root = None
        self.__stack=[]
        stack = self.__stack

   def addNode(self, data):
             .....
                        stack.append(current)


Some partial suggestions:
- Why do you use __? Use them only when necessary. My suggestion is to
use:
self._stack = []
- What's the purpose of stack = self.__stack ? It it does nothing
there.
- stack.append(current) can't find the stack name, because it's not
present in this scope, it's present into the __init__ only. My
suggestion is to use the instance attribute name.

You can probably fix that problem now.

Bye,
bearophile




More information about the Python-list mailing list