def check_black_height(self, ivtree): sentinel = tu.sentinel(ivtree) node = tu.root(ivtree) height_stack = [] node_stack = [] # -1 = left, +1 = right direction_stack = [] while node is not sentinel or len(node_stack) != 0: while node is not sentinel: node_stack.append(node) direction_stack.append(-1) node = nu.left(node) height_stack.append(1) while direction_stack[-1] == 1: node = node_stack.pop() direction_stack.pop() black = nu.black(node) right_height = height_stack.pop() left_height = height_stack.pop() self.assertEqual(right_height, left_height) new_height = right_height + 1 if black else right_height if len(node_stack) == 0: # terminating and returning the black height of the root return new_height height_stack.append(new_height) node = nu.right(node_stack[-1]) direction_stack[-1] = 1
def check_child_color(self, ivtree): stack = [] sentinel = tu.sentinel(ivtree) node = tu.root(ivtree) while node is not sentinel or len(stack) > 0: while node is not sentinel: stack.append(node) node = nu.left(node) node = stack.pop() if not nu.black(node): self.assertTrue(nu.black(nu.left(node))) self.assertTrue(nu.black(nu.right(node))) node = nu.left(node)
def check_augmentation(self, tree): sentinel = tu.sentinel(tree) node = tu.root(tree) node_stack = [] while node is not sentinel or len(node_stack) > 0: while node is not sentinel: node_stack.append(node) node = nu.left(node) node = node_stack.pop() aug_end = nu.max_end(node) if nu.left(node) is not sentinel: left_aug_end = nu.aug_end(nu.left(node)) if left_aug_end > aug_end: aug_end = left_aug_end if nu.right(node) is not sentinel: right_aug_end = nu.aug_end(nu.right(node)) if right_aug_end > aug_end: aug_end = right_aug_end self.assertEqual(aug_end, nu.aug_end(node)) node = nu.right(node)
def test_bad_augmentation(self): tree = self.create_custom_tree() root = tu.root(tree) node2 = nu.left(root) nu.set_aug_end(node2, 4) self.check_augmentation(tree)
def test_bad_black_height(self): tree = self.create_custom_tree() root = tu.root(tree) node3 = nu.right(root) nu.set_black(node3, False) self.check_black_height(tree)
def test_bad_child_color(self): tree = self.create_custom_tree() root = tu.root(tree) node4 = nu.left(nu.left(root)) nu.set_black(node4, False) self.check_child_color(tree)
def test_bad_root_color(self): tree = self.create_custom_tree() root = tu.root(tree) nu.set_black(root, False) self.check_root_color(tree)
def check_root_color(self, ivtree): root = tu.root(ivtree) self.assertTrue(nu.black(root))