示例#1
0
 def __init__(self, size):  # initialize with size input
     self.size = size
     self.evenNotOdd = self.size % 2 == 0  # check even or odd to determine sequence
     self.towers = [Stack(), Stack(), Stack()]  # three towers
     for i in range(size, 0, -1):
         self.towers[0].push(i)  # add initial towers to tower A
     self.moveCount = 0  # set move count to 0
示例#2
0
def buildParseTree(fpexp):
    fplist = fpexp.split()
    pStack = Stack()
    eTree = BinaryTree('')
    pStack.push(eTree)
    currentTree = eTree
    for i in fplist:
        if i == '(':
            currentTree.insertLeft('')
            pStack.push(currentTree)
            currentTree = currentTree.getLeftChild()
        elif i not in ['+', '-', '*', '/', ')', '^']:
            currentTree.setRootVal(i)
            parent = pStack.pop()
            currentTree = parent
        elif i in ['+', '-', '*', '/', '^']:
            currentTree.setRootVal(i)
            currentTree.insertRight('')
            pStack.push(currentTree)
            currentTree = currentTree.getRightChild()
        elif i == ')':
            currentTree = pStack.pop()
        else:
            raise ValueError
    if eTree.getRootVal() == '':
        eTree.setRootVal(eTree.getLeftChild().getRootVal())
        eTree.insertLeft(None)
    return eTree
示例#3
0
 def drawVertical(self):  # draw entire three towers vertically
     temp = [Stack(), Stack(), Stack()
             ]  # temporary list to hold values when popping for output
     for i in range(self.size, 0, -1):
         for t in range(len(self.towers)):
             self.drawTowerVertical(t, temp,
                                    i)  # call the single row drawing method
         print()
     print('#' * (self.size * 9))  # draw the base
     for t in range(len(temp)):
         while not temp[t].isEmpty():
             self.towers[t].push(
                 temp[t].pop())  # set towers back using temporary list
示例#4
0
from tkinter import *
from Structures import Stack

root = Tk()
root.title("Stack")
root.geometry("300x300")

mstack = Stack.Stack()

show = StringVar()
entry = StringVar()


def add():
    mstack.push(addEntry.get())


def showS():
    show.set(mstack)


addButton = Button(root, text="добавить элемент", command=add)
addButton.place(x=0, y=0, width=150, height=50)

addEntry = Entry(root)
addEntry.place(x=150, y=0, width=150, height=50)
showButton = Button(root, text="показать стэк", command=showS)
showButton.place(x=0, y=50, width=150, height=50)


def fromFile(mstackl):
示例#5
0
# Name: Kevin Cui
# Date: 2020-02-28
# Description: Decimal to Binary

from Structures import Stack

number = int(input()) # Take input decimal number
digits = Stack() # init stack

while number > 0:
    digits.push(number%2) # Push binary values to stack
    number //= 2

ans = ''
while not digits.isEmpty():
    ans += str(digits.pop()) # Append digits to string

print(ans) # Output
示例#6
0
    def operators(oper, var):
        mainRoot = parseTree  #główny trzon
        derTree = BinaryTree('')
        if var == mainRoot.getRootVal():
            derTree.setRootVal('1')
            return derTree
        elif '-' + var == mainRoot.getRootVal():
            derTree.setRootVal('-1')
            return derTree
        elif mainRoot.getRootVal() not in ['+', '-', '*', '/', '^']:
            derTree.setRootVal('0')
            return derTree

        StackTree = Stack()  #stos do przechowywania poddrzew
        lenOper = len(oper)
        for i in range(
                lenOper
        ):  #przechodzimy po każdym korzeniu od tyłu bo zaczynamy od "najgłębszych" operacji

            derTree = BinaryTree('')
            root = oper[lenOper - i - 1].getRootVal()
            lChild = oper[lenOper - i - 1].getLeftChild().getRootVal()
            rChild = oper[lenOper - i - 1].getRightChild().getRootVal()

            if root == '+' or root == '-':
                derTree.setRootVal(root)
                if var == lChild:
                    derTree.insertLeft('1')
                elif var not in lChild and lChild not in [
                        '+', '-', '*', '^', '/'
                ]:
                    derTree.insertLeft('0')
                elif lChild == 'sin' + var:
                    derTree.insertLeft('cosx')
                elif lChild == 'cos' + var:
                    derTree.insertLeft('-sinx')
                elif lChild == 'tg' + var:
                    derTree.insertLeft('1/(cosx*cosx)')
                elif lChild == 'ctg' + var:
                    derTree.insertLeft('-1/(sinx*sinx)')
                elif lChild in ['+', '-', '^', '*']:
                    derTree.insertLeftTree(
                        StackTree.pop()
                    )  #jeśli lewe dziecko jest operatorem tzn, że już zróżniczkowałem i należy poprzedni korzeń podpiąć do aktualnego

                if var == rChild:  #analogicznie
                    derTree.insertRight('1')
                elif var not in rChild and rChild not in [
                        '+', '-', '*', '^', '/'
                ]:
                    derTree.insertRight('0')
                elif rChild == 'sin' + var:
                    derTree.insertRight('cosx')
                elif rChild == 'cos' + var:
                    derTree.insertRight('-sinx')
                elif rChild == 'tg' + var:
                    derTree.insertRight('1/(cosx*cosx)')
                elif rChild == 'ctg' + var:
                    derTree.insertRight('-1/(sinx*sinx)')
                elif rChild in ['+', '-', '^', '*']:
                    derTree.insertRightTree(StackTree.pop())

                StackTree.push(derTree)

            if root == '^':
                if lChild != var and rChild != var and lChild != '-' + var and rChild != var:
                    derTree.setRootVal(None)
                elif rChild == '2':
                    derTree.setRootVal(lChild)
                elif rChild == var:
                    derTree.setRootVal(lChild + '^' + var + ' * ln' + lChild)
                elif lChild == var and int(rChild) > 0:
                    dim = int(rChild) - 1
                    derTree.setRootVal('^')
                    derTree.insertLeft(var)
                    derTree.insertRight(dim)
                elif lChild == var and int(rChild) < 0:
                    dim = int(rChild) - 1
                    derTree.setRootVal('^')
                    derTree.insertLeft('-' + var)
                    derTree.insertRight(dim)
                elif lChild == '-' + var and int(rChild) > 0:
                    dim = int(rChild) - 1
                    derTree.setRootVal('^')
                    derTree.insertLeft(lChild)
                    derTree.insertRight(dim)
                elif lChild == '-' + var and int(rChild) < 0:
                    dim = int(rChild) - 1
                    derTree.setRootVal('^')
                    derTree.insertLeft('-' + lChild)
                    derTree.insertRight(dim)

                StackTree.push(derTree)

            if root == '*':
                if var in lChild and rChild not in ['+', '-', '*', '^', '/']:
                    if lChild == 'sin' + var:
                        derTree.setRootVal('*')
                        derTree.insertLeft('cosx')
                    if lChild == 'cos' + var:
                        derTree.setRootVal('*')
                        derTree.setRootVal('-sinx')
                    if lChild == 'tg' + var:
                        derTree.setRootVal('*')
                        derTree.setRootVal('1/(cosx*cosx)')
                    if lChild == 'ctg' + var:
                        derTree.setRootVal('*')
                        derTree.setRootVal('-1/(sinx*sinx)')
                    if var == lChild:
                        derTree.setRootVal(rChild)
                    else:
                        derTree.insertRight(rChild)

                elif var in rChild and lChild not in ['+', '-', '*', '^', '/']:
                    if rChild == 'sin' + var:
                        derTree.setRootVal('*')
                        derTree.insertRight('cosx')
                    if rChild == 'cos' + var:
                        derTree.setRootVal('*')
                        derTree.insertRight('-sinx')
                    if rChild == 'tg' + var:
                        derTree.setRootVal('*')
                        derTree.insertRight('1/(cosx*cosx)')
                    if rChild == 'ctg' + var:
                        derTree.setRootVal('*')
                        derTree.insertRight('-1/(sinx*sinx)')
                    if var == rChild:
                        derTree.setRootVal(lChild)
                    else:
                        derTree.insertLeft(lChild)

                elif lChild == '-' + var and rChild not in [
                        '+', '-', '*', '^', '/'
                ]:
                    derTree.setRootVal('-' + rChild)
                elif rChild == '-' + var and lChild not in [
                        '+', '-', '*', '^', '/'
                ]:
                    derTree.setRootVal('-' + lChild)
                if lChild in ['^', '*'
                              ] and rChild not in ['+', '-', '*', '^', '/']:
                    derTree.setRootVal('*')
                    derTree.insertRight(rChild)
                    derTree.insertLeftTree(StackTree.pop())
                if rChild in ['^', '*'
                              ] and lChild not in ['+', '-', '*', '^', '/']:
                    derTree.setRootVal('*')
                    derTree.insertLeft(lChild)
                    derTree.insertRightTree(StackTree.pop())
                if lChild in ['^', '*'] and rChild in ['^', '*']:
                    derTree.insertLeftTree(StackTree.pop())
                    derTree.insertRightTree(StackTree.pop())
                StackTree.push(derTree)
        return derTree
示例#7
0
 def drawHorizontal(self):
     temp = [Stack(), Stack(), Stack()
             ]  # temporary list to hold values when popping for output
     for t in range(len(self.towers)):
         self.drawTowerHorizontal(t, temp)  # draw each tower
示例#8
0
def graph_dfs(problem):
    return graphSearch(problem, Stack())
示例#9
0
def dfs(problem):
    return treeSearch(problem, Stack())