コード例 #1
0
def postOrderTraversial(root):
    buffer = Stack()
    arraylist = []
    node = root
    buffer.push(node)
    node = node.leftChild
    while(node != None):
        buffer.push(node)
        node = node.leftChild

    while(buffer.isEmpty() is False):
        node = buffer.peek()
        if(node.rightChild != None and node.rightChild.visited is False): # We have to set a visited flag that make the
            node = node.rightChild                                        # nodes will not visit multiple times.
            while(node != None):
                buffer.push(node)
                parent = node
                node = node.leftChild

            if(parent.rightChild == None):
                arraylist.append(parent.key)
                parent.visited = True
                buffer.pop()
        else:
            node.visited = True                                          # Set visited flags
            arraylist.append(node.key)
            buffer.pop()
    return arraylist
コード例 #2
0
def inOrderTraversial(root):
    arraylist = []
    buffer = Stack()
    node = root
#    buffer.push(node)
#    node = node.leftChild
#    while(node != None):
#        buffer.push(node)
#        node = node.leftChild
#
#    while(buffer.isEmpty() is False):
#        node = buffer.pop()
#        arraylist.append(node.key)
#        if(node.rightChild != None):
#            node = node.rightChild
#            while(node != None):
#                buffer.push(node)
#               node = node.leftChild
#
#    return arraylist
#
    while (buffer.isEmpty() is False or node != None):
        if (node != None):
            buffer.push(node)
            node = node.leftChild
        else:
            node = buffer.pop()
            arraylist.append(node.key)
            node = node.rightChild

    return arraylist
コード例 #3
0
ファイル: P4_5.py プロジェクト: sunnyyants/crackingPython
def checkBST(root):
    list = []
    stack = Stack()
    node = root
#    stack.push(node)
    while(stack.isEmpty() is False or node != None):
        if(node != None):
            stack.push(node)
            node = node.leftChild
        else:
            node = stack.pop()
            list.append(node.key)
            node = node.rightChild
    print list
    return checkOrder(list)