-3

I have a red black tree as follows:

                8B
           /          \
         4R           10B
       /    \        /    \
    2B        6B   9B     11B       
  /   \      /  \
1B    3B    5B   7B

I want to delete 10. what will happen ?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
adam
  • 37
  • 5

1 Answers1

1

To delete '10' you need to exchange the value with either the in-order predecessor or in-order successor, (that is, for 10 either 9 or 11) and then make that the node to delete (it is possible to do either by swapping the values or by exchanging the tree positions of the nodes)

So for example say that you swap 9 and 10, once you do that you will color the sibling red (because both nephews are black) and then make the parent the working node. Your tree will then be (and sorry about any poor tree formatting, I am blind so have a very hard time ensuring things line up right), also ignore the fact that the tree order is violated that will be repaired later:

        8b
   4r      11b*
 2b  6b  9r    10b
1 3 5 7

The sibling node (6) is red so exchange the colors of the sibling and parent then rotate so that the parent is now child to the former sibling:

        4b
   2b        8r
1b  3b  6b    11b*
      5b  7b 9r   10b

Both nephew nodes (the children of 6) are black so color the sibling red and since the parent is red it will be colored black:

        4b
   2b        8b
1b  3b  6r    11b*
      5b  7b 9b   10b

Now remove 10 from the tree and re-balancing is complete: 4b 2b 8b 1b 3b 6r 11b 5b 7b 9r

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23
  • Not knowing exactly how the first graph is supposed to be connected, I'm giving it a try: 2b-left-1, 2b-right-3 then 6b-left-5, 6b-right-7 - That's how I edited it. Hope it's what you intended. – Ted Lyngmo Aug 14 '20 at 23:05