示例#1
0
    def bst_postorder_list_nr(self):
        """
        Algorithm:
        - Create two stacks, stk1, stk2
        - stk1.push(root)
        - while stk1 is not empty:
              curr = stk1.stk_pop()
              stk2.push(curr)
              if curr has .left:
                 stk1.push(curr.left)
              if curr has right:
                 stk1.push(curr.right)
        - stk2 has all items pushed in post-order.
        """
        ret = []
        if not self.root:
            return ret
        stk1 = stack()
        stk2 = stack()
        stk1.stk_push(self.root)

        while not stk1.stk_isempty():
            curr = stk1.stk_pop()
            stk2.stk_push(curr)
            if curr.l_child:
                stk1.stk_push(curr.l_child)
            if curr.r_child:
                stk1.stk_push(curr.r_child)

        while not stk2.stk_isempty():
            ret.append(stk2.stk_pop().data)

        return ret
示例#2
0
 def test_stk_full(self):
     stk = stack(2)
     stk.stk_push(1)
     stk.stk_push(2)
     self.assertEqual(True, stk.stk_isfull())
     stk.stk_pop()
     self.assertEqual(False, stk.stk_isfull())
示例#3
0
 def test_stk_empty(self):
     stk = stack()
     self.assertEqual(0, stk.stk_get_len())
     self.assertEqual(None, stk.stk_top())
     self.assertEqual(None, stk.stk_pop())
     self.assertEqual(True, stk.stk_isempty())
     with self.assertRaises(Exception) as cm:
         stk.stk_peek(0)
     self.assertEqual(cm.exception.message, "stack::index is out of range")
示例#4
0
 def test_stk_reverse(self):
     lst = generate_random_list(9, 1, 99)
     stk = stack()
     for x in xrange(0, len(lst)):
         stk.stk_push(lst[x])
     ret = []
     while not stk.stk_isempty():
         ret.append(stk.stk_pop())
     lst.reverse()
     self.assertEqual(lst, ret)
示例#5
0
 def test_stk_size1(self):
     s = stack(1)
     s.stk_push(9)
     self.assertEqual(1, s.stk_get_len())
     self.assertEqual(9, s.stk_pop())
     self.assertEqual(None, s.stk_pop())
     s.stk_push(9)
     with self.assertRaises(Exception) as cm:
         s.stk_push(10)
     self.assertEqual(cm.exception.message, "stack::stack is full")
示例#6
0
 def bst_preorder_list_nr(self):
     ret = []
     if not self.root:
         return ret
     done = False
     stk = stack()
     trav = self.root
     while not done:
         if trav:
             ret.append(trav.data)
             stk.stk_push(trav)
             trav = trav.l_child
         else:
             if not stk.stk_isempty():
                 trav = stk.stk_pop()
                 trav = trav.r_child
             else:
                 done = True
     return ret
示例#7
0
 def bst_get_size_nr(self):
     if not self.root:
         return 0
     sz = 0
     stk = stack()
     done = False
     trav = self.root
     while not done:
         if trav:
             stk.stk_push(trav)
             trav = trav.l_child
         else:
             if not stk.stk_isempty():
                 trav = stk.stk_pop()
                 sz += 1
                 trav = trav.r_child
             else:
                 done = True
     return sz
示例#8
0
    def bst_get_next_preorder_nr(self, item):
        if not self.root:
            return None

        # first, get the node corresponding to this key
        node = self.bst_find_get_node_nr(item)
        if not node:
            return None

        # if this node has left child or right child, those
        # will be the next in pre-order traversal
        if node.l_child:
            return node.l_child
        if node.r_child:
            return node.r_child

        # now, we need to bother only about leaf nodes
        stk = stack()
        trav = self.root
        while trav:
            stk.stk_push(trav)
            if item < trav.data:
                trav = trav.l_child
            elif item > trav.data:
                trav = trav.r_child
            else:
                break

        prev = stk.stk_pop()
        while not stk.stk_isempty():
            top = stk.stk_top()
            if prev == top.l_child:
                if top.r_child:
                    return top.r_child
            prev = stk.stk_pop()

        return None
示例#9
0
 def test_stk_size0(self):
     with self.assertRaises(Exception) as cm:
         stk = stack(0)
     self.assertEqual(cm.exception.message, "stack::invalid stack size")