def test_popping_from_stack_removes_item(): stack = Stack() stack.push(1) assert_equal(stack.empty(), False) assert_equal(stack.pop(), 1) assert_equal(stack.empty(), True)
def main(): print "\ncheck stack" stack = Stack(1, 2, 34, 5) for x in range(0, 5): stack.push(x) print stack for x in range(0, 15): print "".join(["size=", str(len(stack)), " cur_node=", str(stack.pop())]) print "\ncheck queue" queue = Queue(1, 2, 34, 5) for x in range(0, 5): queue.enter(x) print stack for x in range(0, 15): print "".join(["size=", str(len(queue)), " cur_node=", str(queue.exit())]) print "\ncheck BSTree" tree = BSTree(1, 2, 34, 5) print tree print tree.find(10) print tree.find(5) print tree.max() print tree.min() print tree.successor(34) print tree.successor(5) print tree.predecessor(1) print tree.predecessor(2)
def test_popping_from_stack_returns_last_item(): stack = Stack() stack.push(1) stack.push(2) stack.push(3) assert_equal(stack.pop(), 3)
def midorder(self, f): """ B树中序遍历 :param f: :return: """ result = [] stack = Stack() cur_node = self.__root if cur_node.is_leaf: return map(f, cur_node.keys) while True: if cur_node.is_leaf: # 到叶节点了,开始把叶节点的所有关键字都遍历掉 result.extend(map(f, cur_node.keys)) # 开始从栈中取元素,遍历下一个节点叶节点 if stack.empty(): return result cur_node, i = stack.pop() result.append(f(cur_node.keys[i])) if i < len(cur_node) - 1: stack.push((cur_node, i + 1)) cur_node = cur_node.childs[i + 1] else: stack.push((cur_node, 0)) cur_node = cur_node.childs[0] return result
class MinStack: def __init__(self): self.mins = Stack() self.items = Stack() def push(self, item): min = self.mins.peek() if min == None or item < min: self.mins.push(item) return self.items.push(item) def pop(self): item = self.items.pop() if item == self.mins.peek(): self.mins.pop() return item def min(self): return self.mins.peek()
def sort_stack(stack): sorted_stack = stack stack = Stack() while not sorted_stack.is_empty(): stack.push(sorted_stack.pop()) while not stack.is_empty(): tmp = stack.pop() while not sorted_stack.is_empty() and sorted_stack.peek() < tmp: stack.push(sorted_stack.pop()) sorted_stack.push(tmp)
def remove_even(self): tstack = Stack() # empty the stack and store even numbers in # another, temporary stack (in reverse order) while not self.empty: popped = self.pop() if popped % 2 == 1: tstack.push(popped) # move the data from temporary stack back to original while not tstack.empty: self.push(tstack.pop())
def preorder(self, f): result = [] stack = Stack(self.__root) while True: cur_node = stack.pop() # 栈中没有元素的时候就表示所有的元素都已经遍历完了 if cur_node is None: break result.append(f(cur_node.value)) if cur_node.left is not None: stack.push(cur_node.left) if cur_node.right is not None: stack.push(cur_node.right) return result
class TestStack(unittest.TestCase): def setUp(self): # Create stacks for the tests to use self.new = Stack() self.empty = Stack() self.empty.push('hi') self.empty.pop() # Don't add in ascending or descending order self.non_empty = Stack() self.non_empty.push(5) self.non_empty.push(2) self.non_empty.push(7) self.non_empty.push(2) def test_length(self): self.assertEqual(len(self.new), 0) self.assertEqual(len(self.empty), 0) self.assertEqual(len(self.non_empty), 4) def test_is_empty(self): self.assertTrue(self.new.is_empty()) self.assertTrue(self.empty.is_empty()) self.assertFalse(self.non_empty.is_empty()) def test_lifo_order(self): self.assertEqual(self.non_empty.pop(), 2) self.assertEqual(self.non_empty.pop(), 7) self.assertEqual(self.non_empty.pop(), 2) self.assertEqual(self.non_empty.pop(), 5) def test_access_to_empty(self): with self.assertRaises(AssertionError): self.new.top() with self.assertRaises(AssertionError): self.empty.top() with self.assertRaises(AssertionError): self.new.pop() with self.assertRaises(AssertionError): self.empty.pop() def test_membership(self): self.assertFalse(2 in self.new) self.assertFalse(2 in self.empty) self.assertTrue(2 in self.non_empty) self.assertTrue(5 in self.non_empty) self.assertTrue(7 in self.non_empty)
def midorder(self, f): result = [] stack = Stack(self.__root) cur_node = self.__root.left # 第一个阶段首先把所有树左边的节点放进栈里,这个时候并不遍历 # 第二个阶段的时候由于左节点遍历了之后,再遍历右节点 while not stack.empty() or cur_node is not None: # 第二个判断条件比较重要,因为如果根节点没有左子树,这个时候栈就是空的,会直接退出循环 if cur_node is not None: stack.push(cur_node) cur_node = cur_node.left else: cur_node = stack.pop() result.append(f(cur_node.value)) cur_node = cur_node.right return result
def base_converter(dec_number, base): digits = "0123456789ABCDEF" s = Stack() n = dec_number while n > 0: number = n % base if number > 9: s.push(digits[number]) else: s.push(n % base) n = n // base number = s.size() string = '' for i in range(number): string = string + str(s.pop()) return string
def midorder(self, f): """ 中序遍历 :param f:访问一个节点的时候要对节点进行处理的函数 :return: """ result = [] stack = Stack(self.__root) cur_node = self.__root.left # 第一个阶段首先把所有树左边的节点放进栈里,这个时候并不遍历 # 第二个阶段的时候由于左节点遍历了之后,再遍历右节点 while not stack.empty() or cur_node is not self.Nil: # 第二个判断条件比较重要,因为如果根节点没有左子树,这个时候栈就是空的,会直接退出循环 if cur_node is not self.Nil: stack.push(cur_node) cur_node = cur_node.left else: cur_node = stack.pop() result.append(f(cur_node)) cur_node = cur_node.right return result
def dfs(self, gray_func, black_func): """ 图的深度遍历 :param gray_func: :param black_func: :return: """ gray_list = [] black_list = [] # 初始化 for key in self.__dict.iterkeys(): key.start_time = None key.end_time = None key.set_white() # 开始遍历 counter = 0 for key in self.__dict.iterkeys(): if key.is_white(): dfs_stack = Stack() key.set_gray() key.start_time = counter counter += 1 dfs_stack.push(key) while not dfs_stack.empty(): cur_node = dfs_stack.pop() gray_list.append(gray_func(key)) for end_node in self.__dict[cur_node]: if end_node.is_white(): end_node.set_gray() end_node.start_time = counter counter += 1 dfs_stack.push(end_node) cur_node.set_black() black_list.append(black_func(cur_node)) cur_node.end_time = counter counter += 1 return gray_list, black_list
class SetOfStacks: def __init__(self, capacity): self.capacity = capacity self.stacks = [Stack()] self.active_stack_index = 0 self.active_stack = self.stacks[0] def update_active_stack_next(self): if self.active_stack.size < self.capacity: return self.active_stack = Stack() self.stacks.append(self.active_stack) self.active_stack_index += 1 def update_active_stack_prev(self): if self.active_stack.size > 0: return if self.active_stack_index == 0: return self.active_stack_index -= 1 self.active_stack = self.stacks[self.active_stack_index] self.update_active_stack_prev() def push(self, data): self.update_active_stack_next() self.active_stack.push(data) return data def pop(self): self.update_active_stack_prev() return self.active_stack.pop() def pop_at(self, index): stack = self.stacks[index] if stack: return stack.pop()
def test_popping_from_empty_stack_raises_custom_error_message(): stack = Stack() assert_equal(stack.pop(), "Error: Stack is empty")