I am trying to verify whether a binary tree is a red-black one. Some properties I need to check is that:
- Root is black
- There cannot be 2 consecutive red nodes.
- You need to add empty nodes as leaf nodes, whose color is taken as black.
- Every path from a leaf to the root contains the same number of black nodes
Somehow, my test cases are all passed but I am sure I did not implement the number 4 above.
How can I check whether any depth from root till Empty (the end) has the same number of black nodes?
I define my tree to be :
type 'a tree = Empty | T of color * 'a tree * 'a * 'a tree
My code simply matches the current tree with some bad cases and return false. Otherwise, call the recursive function on the left and right branches.
let rec good_RB t = match t with
| Empty -> true
| T (R, T (R, _, _, _), _, _)
| T (R , _, _, T (R, _, _, _)
-> false
| T (_, lft, _, rgt) -> good_RB lft && good_RB rgt
Emptycase - Lhooq