with the property “for each node, the value labeled on the node is greater than the values labeled on left descendants and is less than the values labeled on right descendants” A x < A A < x 5 2 8 9 1 4 3 3
a tree with the property “for each node, left and right subtrees differ in height by no more than 1” • A self-balanced tree is a (search) tree including a mechanism to keep them as balanced as possible during operations • Operations = typically adding/removing nodes, possibly searching/iterating nodes • Balanced search trees have good search performance. Not a balanced tree 4
binary search tree widely used • Good balance of search performance and modification performance. • Some programing language standards have ordered (sorted) associative arrays. They are usually implemented by using red-black trees • std::map (C++), TreeMap (Java), … 5
normal binary search trees: • The root node is black <- OK! • All leaf (nil) nodes are black <- OK! • All other nodes are red or black <- OK! • … 10 8 2 9 1 20 17 5 6
• All parent nodes of red nodes are black <- huh…? • For each leaf node, the number of black nodes on the path from the root to the leaf are same <- what!? • Why are these properties introduced? 10 8 2 9 1 20 17 5 7
All nodes have 1, 2 or 3 value(s) and 2, 3 or 4 child nodes. • If a node has 1 value, the node has 2 child nodes, and so on. • A node which has N child nodes is called N-node. • For each node, the values in the nodes and its descendants are ordered. x < A A B A < x < B B < x A 3-node and its descendants 8
For each leaf (nil) nodes, the number of nodes on the path from the root to the leaf are same. • Note: we don’t discuss the way to keep these properties. L R C F H O D E G I J K M N P Q A B U Y S T V W X Z (Nil nodes are omitted) 9
• 2-3-4 trees are not practical • Because its operation has many special cases and difficult to implement. • Tring to translate 2-3-4 trees to red-black trees • Because binary trees are easier to operate. 10
• A 2-node is translated to 1 black node. • A 4-node is translated to 1 black node and 2 red nodes. • A leaf (nil) node is translated to a black leaf (nil) node. X A Y A Y X X A B Y Z B A Y X W C C W Z 11
• A 3-node is translated to 1 black node and 1 red node. The red node can be left or right child of the black node. X A B Y Z B A Z Y X or B A Z Y X 12
• The resulting red-black tree has following properties: • All parent nodes of red nodes are black. • For each leaf node, the number of black nodes on the path from the root to the leaf are same. • Because black nodes are mapped one-to-one with 2-3-4 tree nodes. 13
from 2-3-4 tree and normal red-black tree: • All nodes have 1 or 2 value(s) and 2 or 3 child nodes in 2-3 trees • Every red node is always left child of a black node in left-leaning red-black trees. • “Left-leaning” means that the left subtree is taller than the right subtree. X A B Y Z B A Z Y X 15
easy-to-understand tree data structure. • We can understand the properties and operations of red-black trees more easily by keeping the fact above in mind. 16