Exemple #1
0
 def next(self):
     tempNode=Node()
     if self.__tree__ == None:
         return False
     if self.__tree__.getRoot() == None:
         return False
     if self.__isFirst:
         self.__isFirst = False
         if not self.__tree__.getRoot().getLeft() == None:
             self.__currentNode__ = self.getLeftMostChild(self.__tree__.getRoot().getLeft())
         else:
             self.__currentNode__ = self.__tree__.getRoot()
     else:
         if not self.__currentNode__.getRight() == None:
             self.__currentNode__=self.getLeftMostChild(self.__currentNode__.getRight())
         elif self.__currentNode__.getParent() == None:
             return False
         elif self.__currentNode__ == self.__currentNode__.getParent().getLeft():
             self.__currentNode__ = self.__currentNode__.getParent()
         elif self.__currentNode__ == self.__currentNode__.getParent().getRight():
             tempNode=self.__currentNode__
             while not tempNode == None:
                 if tempNode == tempNode.getParent().getLeft():
                     self.__currentNode__ = tempNode.getParent()
                     return self.__currentNode__
                 else:
                     tempNode = tempNode.getParent()
             else:
                 return False
     return self.__currentNode__
Exemple #2
0
class TreeIterator:
    def __init__(self):
        self.__tree__ = Tree()
        self.__currentNode__ = Node()
        self.__isFirst = True

    def setTree(self,tree):
        self.__tree__=tree

    def next(self):
        tempNode=Node()
        if self.__tree__ == None:
            return False
        if self.__tree__.getRoot() == None:
            return False
        if self.__isFirst:
            self.__isFirst = False
            if not self.__tree__.getRoot().getLeft() == None:
                self.__currentNode__ = self.getLeftMostChild(self.__tree__.getRoot().getLeft())
            else:
                self.__currentNode__ = self.__tree__.getRoot()
        else:
            if not self.__currentNode__.getRight() == None:
                self.__currentNode__=self.getLeftMostChild(self.__currentNode__.getRight())
            elif self.__currentNode__.getParent() == None:
                return False
            elif self.__currentNode__ == self.__currentNode__.getParent().getLeft():
                self.__currentNode__ = self.__currentNode__.getParent()
            elif self.__currentNode__ == self.__currentNode__.getParent().getRight():
                tempNode=self.__currentNode__
                while not tempNode == None:
                    if tempNode == tempNode.getParent().getLeft():
                        self.__currentNode__ = tempNode.getParent()
                        return self.__currentNode__
                    else:
                        tempNode = tempNode.getParent()
                else:
                    return False
        return self.__currentNode__

    def getLeftMostChild(self,node):
        if not node.getLeft() == None:
            return self.getLeftMostChild(node.getLeft())
        else:
            return node