Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Red-black tree mystery

Red-black tree mystery

More Decks by 梅澤威志 / UMEZAWA Takeshi

Other Decks in Science

Transcript

  1. What is a binary search tree? • A binary tree

    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
  2. What is a self-balanced trees? • A balanced tree is

    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
  3. What is a red-black tree? • A kind of self-balanced

    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
  4. Properties of red-black trees In addition to the properties of

    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
  5. 3 3 1 2 2 “Mysterious” properties of red-black trees

    • 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
  6. Another kind of self-balanced search tree: 2-3-4 tree (1) •

    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
  7. Another kind of self-balanced search tree: 2-3-4 tree (2) •

    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
  8. Translation from a 2-3-4 tree to a red-black tree (1)

    • 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
  9. Translation from a 2-3-4 tree to a red-black tree (2)

    • 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
  10. Translation from a 2-3-4 tree to a red-black tree (3)

    • 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
  11. Translation from a 2-3-4 tree to a red-black tree (4)

    • 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
  12. Translation from a 2-3-4 tree to a red-black tree (5)

    I K V X S G Z W M P D A B C H L J E R Y O U T N Q F 14
  13. Simpler version: 2-3 tree and left-leaning red-black tree • Differences

    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
  14. Conclusion • Red-black trees are isometric to 2-3-4 trees, an

    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