Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def is_correct_rb_tree(tree):
- if tree.root is None:
- return True
- if tree.root.color is not Colors.black:
- return False
- if black_height(tree.root) == 0:
- return False
- return check_children(tree.root)
- def check_children(node):
- if node is None:
- return True
- if node.color is Colors.red:
- color_left = Colors.black
- if node.left is not None:
- color_left = node.left.color
- color_right = Colors.black
- if node.right is not None:
- color_right = node.right.color
- return color_left is Colors.black and color_right is Colors.black and check_children(node.left) and check_children(node.right)
- else:
- return check_children(node.left) and check_children(node.right)
- def black_height(node):
- if node is None:
- return 1
- left_height = black_height(node.left)
- if left_height is 0:
- return 0
- right_height = black_height(node.right)
- if right_height is 0:
- return 0
- if left_height != right_height:
- return 0
- else:
- val = left_height
- if node.color is Colors.black:
- val += 1
- return val
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement