예제 #1
0
 def DFS(self):
     """Permette di restituire una lista di elementi ottenuta da una visita
     in profondita' dell'albero."""
     res = []
     stack = PilaArrayList()
     if self.root != None:
         stack.push(self.root)
     while not stack.isEmpty():
         current = stack.pop()
         res.append(current.info)
         if current.rightSon != None:
             stack.push(current.rightSon)
         if current.leftSon != None:
             stack.push(current.leftSon)
     return res
예제 #2
0
 def DFS(self):
     """Visita in profondità
     return: lista di BinaryNode.info"""
     res = []
     stack = PilaArrayList()
     if self.root is not None and self.isActive(self.root):  # TODO forse eliminare ultimo control
         stack.push(self.root)
     while not stack.isEmpty():
         current = stack.pop()
         if self.isActive(current):
             res.append(current.info)
         if current.rightSon != None:
             stack.push(current.rightSon)
         if current.leftSon != None:
             stack.push(current.leftSon)
     return res
예제 #3
0
    def stampa(self):
        """Permette di stampare l'albero. Per farlo si usa una pila di appoggio"""
        stack = PilaArrayList()
        if self.root != None:
            stack.push(
                [self.root, 0]
            )  # pila di liste di due elementi [il nodo, il livello occupato dal nodo]
        else:
            print("Empty tree!")
        while not stack.isEmpty():
            current = stack.pop()
            level = current[1]
            print("|---" * level + str(current[0].info))

            if current[0].rightSon != None:
                stack.push([current[0].rightSon, level + 1])
            if current[0].leftSon != None:
                stack.push([current[0].leftSon, level + 1])
예제 #4
0
    def stampa(self):
        """return: void. Stampa albero sfruttando pila di appoggio"""
        stack = PilaArrayList()
        if self.root is not None:
            # pila di liste di due elementi
            # [il nodo, il livello occupato dal nodo]
            stack.push([self.root, 0])
        else:
            print("Empty tree!")
        while not stack.isEmpty():
            current = stack.pop()
            level = current[1]
            print("|---" * level + str(current[0].info))

            if current[0].rightSon is not None:
                stack.push([current[0].rightSon, level + 1])
            if current[0].leftSon is not None:
                stack.push([current[0].leftSon, level + 1])
예제 #5
0
 def DFS(self):
     res = []     
     stack = PilaArrayList()
     if self.root != None:
         stack.push(self.root)
     while not stack.isEmpty():
         current = stack.pop()
         res.append(current.info)
         for i in range(len(current.sons) - 1, -1, -1):
             stack.push(current.sons[i])
     return res
예제 #6
0
def iterativeQuickSort(l, left, right, det=False):
    theStack = Stack()
    theStack.push(left)
    theStack.push(right)
    while not theStack.isEmpty():
        right = theStack.pop()
        left = theStack.pop()

        if printSwitch.dumpOperations:
            print("quickSortIter-step({},{})".format(left, right))

        if right <= left:
            continue

        mid = partition(l, left, right, det)

        theStack.push(left)
        theStack.push(mid - 1)

        theStack.push(mid + 1)
        theStack.push(right)
예제 #7
0
def iterativeQuickSort(l, left, right, det=False):
    theStack = Stack()
    theStack.push(left)
    theStack.push(right)
    while not theStack.isEmpty():
        right = theStack.pop()
        left = theStack.pop()


        if right <= left:
            continue

        mid = partition(l, left, right, det)

        theStack.push(left)
        theStack.push(mid - 1)

        theStack.push(mid + 1)
        theStack.push(right)