Esempio n. 1
0
 def test_init(self):
     stack = Stack()
     self.assertEqual(stack.data, [])
     stack = Stack(5)
     self.assertEqual(stack.data, [5])
     stack = Stack([1, 2, 3])
     self.assertEqual(stack.data, [1, 2, 3])
Esempio n. 2
0
 def test_size(self):
     stack = Stack()
     self.assertEqual(stack.size(), 0)
     stack.push(1)
     self.assertEqual(stack.size(), 1)
     stack.push(2)
     self.assertEqual(stack.size(), 2)
Esempio n. 3
0
    def test_peek(self):
        stack = Stack()
        self.assertRaises(Exception, stack.peek)

        stack.push(5)
        stack.push(4)
        self.assertEqual(stack.peek(), 4)
        self.assertEqual(stack.size(), 2)
        stack.pop()
        self.assertEqual(stack.peek(), 5)
        self.assertEqual(stack.size(), 1)
Esempio n. 4
0
 def test_push(self):
     stack = Stack()
     stack.push(5)
     self.assertEqual(stack.data, [5])
     self.assertEqual(stack.size(), 1)
     stack.push(5)
     self.assertEqual(stack.data, [5, 5])
     self.assertEqual(stack.size(), 2)
Esempio n. 5
0
def pre_order_iterative(node):
    if node == None:
        return
    stack1 = Stack()
    stack1.push(node)
    while not stack1.is_empty():
        temp = stack1.pop()
        print temp,
        if temp.right:
            stack1.push(temp.right)
        if temp.left:
            stack1.push(temp.left)
Esempio n. 6
0
def size_of_tree_stack(node):
    if node == None:
        return
    stack1 = Stack()
    stack1.push(node)
    size = 0
    while not stack1.is_empty():
        temp = stack1.pop()
        size += 1
        if temp.left:
            stack1.push(temp.left)
        if temp.right:
            stack1.push(temp.right)
    return size
def in_order_iterative(node):
    if node == None:
        return
    stack1 = Stack()
    #stack1.push(node)
    curr = node
    done = False
    while not done:
        if curr != None:
            stack1.push(curr)
            curr = curr.left
        else:
            if not stack1.is_empty():
                curr = stack1.pop()
                print curr,
                curr = curr.right
            else:
                done = True
Esempio n. 8
0
def bst_or_not(node):
    if node == None:
        return
    stack1 = Stack()
    stack1.push(node)
    #import ipdb; ipdb.set_trace()
    while not stack1.is_empty():
        temp = stack1.pop()
        if not temp.left is None:
            if temp.left.data < temp.data :
                stack1.push(temp.left)
            else:
                return False
        if not temp.right is None:
            if temp.right.data > temp.data :
                stack1.push(temp.right)
            else:
                return False
    return True
Esempio n. 9
0
def has_path_sum_pair(node, sum):
    if node == None:
        return
    stack1 = Stack()
    lista = [] 
    stack1.push(node)
    lista.append(node.data)
    while not stack1.is_empty():
        temp = stack1.pop()
        if not temp.left is None:
            #if temp.left.data < temp.data :
            stack1.push(temp.left)
            lista.append(temp.left.data)
            
        if not temp.right is None:
            stack1.push(temp.right)
            lista.append(temp.right.data)

    for i in range(len(lista)):
        if sum-lista[i] in lista:
            return True
    return False
Esempio n. 10
0
def print_linked_list_in_reverse(start):
    if(start == None):
        return

    stack =  Stack()
    temp = start
    while(temp != None):
        stack.push(temp)
        temp = temp.next

    while(not stack.is_empty()):
        print stack.pop(),
Esempio n. 11
0
 def test_is_empty(self):
     stack = Stack()
     self.assertTrue(stack.is_empty())
     stack.push(1)
     self.assertFalse(stack.is_empty())
def post_order_iterative_two_stack(node):
    if node == None:
        return
    stack1 = Stack()
    stack2 = Stack()
    stack1.push(node)
    while not stack1.is_empty():
        curr = stack1.pop()
        stack2.push(curr)
        if curr.left:
            stack1.push(curr.left)
        if curr.right:
            stack1.push(curr.right)
    while not stack2.is_empty():
        temp = stack2.pop()
        print temp,
def post_order_iterative_one_stack(node):
    if node == None:
        return
    stack1 = Stack()
    stack1.push(node)
    prev = None
    while not stack1.is_empty():
        curr = stack1.top()
        if (prev == None or prev.right == curr or prev.left ==curr):
            # go down the Tree
            if curr.left != None:
                stack1.push(curr.left)
            elif curr.right != None:
                stack1.push(curr.right)
            else:
                print stack1.pop()
        elif curr.left == prev:
            if curr.right != None:
                stack1.push(curr.right)
            else:
                print stack1.pop()

        elif curr.right == prev:
            print stack1.pop()
        prev = curr