Which is easier to implement: 2-3-4 Tree or Red-Ebony?

In the textbook that I am studying from (Lafore), red-black trees are presented for the first time, and it does not contain pseudo-code, although the algorithms associated with it seem quite complicated and have many unique cases.

Then he presents 2-3-4 Trees that seem a lot easier for me to understand, and I would suggest that they be implemented. It includes some actual Java code, which is very clear. It seems to imply that 2-3-4 is easier to implement, and I agree with what I have seen so far.

Wikipedia, however, says the opposite ... I think this is not true:

http://en.wikipedia.org/wiki/2-3-4_tree

2-3-4 trees is an isometry of red-black trees, which means they are equivalent data structures. In other words, for every 2-3-4 trees, there is at least one red-black tree with data elements in the same order. In addition, insert and delete operations on 2-3-4 trees that cause decompositions, breaks and merges of the node are equivalent to color flips and rotations in red-black trees. An introduction to red-black trees is usually introduced by 2-3-4 trees at first, because they are conceptually simpler. 2-3-4 trees, however, it can be difficult to implement in most programming languages โ€‹โ€‹due to the large number of special cases associated with tree operations. Red-black trees are easier to apply, therefore, as a rule, they are used instead.

In particular, the part about โ€œmore special casesโ€ seems to me completely backward (I think this is RB, which has a large number of special cases, and not 2-3-4). But I'm still studying (and still honestly not quite completely wrapped my head around red-black trees), so I would like to hear other opinions.

As a support ... although I agree with most of what Laford says, I think itโ€™s interesting that he presented them in the opposite order from what Wikipedia says is common (2-3-4 to Belarus). I think that 2-3-4 will make more sense at first, since RB is much more difficult to conceptualize. Perhaps he chose this order because RB was more closely associated with the BST, which he had just completed in the previous chapter.

+4
source share
1 answer

the โ€œmore special casesโ€ part seems completely backward to me (I think itโ€™s RB, which has a lot of special cases, not 2-3-4)

RB Trees can be implemented in a dozen lines if you have pattern matching in your langugage:

data Color = R | B data Tree a = E | T Color (Tree a) a (Tree a) balance :: Color -> Tree a -> a -> Tree a -> Tree a balance B (TR (TR axb) yc ) zd = TR (TB axb) y (TB czd) balance B (TR ax (TR byc)) zd = TR (TB axb) y (TB czd) balance B ax (TR (TR byc) zd ) = TR (TB axb) y (TB czd) balance B ax (TR by (TR czd)) = TR (TB axb) y (TB czd) balance col axb = T col axb insert :: Ord a => a -> Tree a -> Tree a insert xs = TB ayb where ins E = TRE x E ins s@ (T col ayb) | x < y = balance col (ins a) yb | x > y = balance col ay (ins b) | otherwise = s T _ ayb = ins s 

This is a famous definition from Okasaki paper.

+3
source

Source: https://habr.com/ru/post/1388685/


All Articles