I have a representation of a (binary) tree as a table in PostgreSQL. Each row is a node of the tree and each row has a parent column and a color.
The nodes at he leaves of the tree have a color (their color column is not null and can be either green or red).
My goal is to color the whole tree. With these rules:
- If a parent has one child, then the parent's color is the child's color.
- If a parent has two children and both have the same color then the parent color is its children color.
- If a parent has two children and they have different color, then the parent color is gray.
How can I write a recursive query in PostgreSQL for this algorithm?
Example of a tree:
CREATE TEMPORARY TABLE family (
node text
, parent text
, color text
);
INSERT INTO family (parent, node, color)
VALUES
(null , 'A' , null)
, ('A' , 'AB' , null)
, ('A' , 'AC' , null)
, ('AB' , 'ABD' , null)
, ('ABD', 'ABDE', 'green')
, ('AC' , 'ACF' , null)
, ('AC' , 'ACG' , null)
, ('ACF', 'ACFH', 'red')
, ('ACF', 'ACFI', 'green')
, ('ACG', 'ACGJ', 'red')
, ('ACG', 'ACGK', 'red')
;
This tree, fully colored: