In a normal binary tree search, when you delete an internal node with two nodes as children, we take the maximum element of the left subtree or the minimum of the right subtree, and move its value at that node is removed (as shown here). Then remove the node from which copiabamos the value must be less than two leaves no child nodes. Copy a value does not violate any of the red-black properties and reduces the problem of general andalusia delete to delete a node with at most one leaf child. No matter if this node is the node that originally we wanted to delete the node or the value of that copy.
Summarizing, we can assume that we remove a node with a child as much leaf (if leaf nodes only have children, take them as a child). If you remove a red node, we can simply replace it with your child, to be black. All roads to simply delete the node pass through a node less red, and both nodes, the father of the deletion and the child have to be black, so that the properties 3 (all pages, including zero, are black ) and 4 (the two sons of every red node are black) are maintained. Another simple case is when the deleted node is black and his son is red. Simply remove a black node could break the 4 properties (the two children of every red node are black) and 5 (all paths from a given node to its leaves contain the same number of black nodes), but if your child repainted in black Both properties are preserved.
The complex case is when the node that will be erased and his son are black. We started by replacing the node that will be wiped off with his son. Call this son (in his new position) N, and his brother (the new son of his father) S. In the diagrams below, we use P for the new parent of N, SL for the left child of S and SR for the new child law S (S can be shown that it can not be a leaf).
Note: some changed roles and labels of the nodes, but in each case, any label remains the same node representing the beginning of the case. All colors shown in the diagram is either assumed or if any implied by these assumptions. The target represents an unknown color (either red or black).
Compliance with these rules in a tree with n nodes, ensures a maximum of up to three rotations and O (log2n) recolor.
We will find the brother using this function:
struct node
brother (struct node n)
(
If (n n-> parent-> izdo)
Return n-> parent-> dcho
Else
Return n-> parent-> izdo
)
Note: In order to preserve the good definition of the tree, we need every leaf remains a leaf null null after all the changes (that leaves no child will not have any). If we are deleting the node has a child leaf N, it is easy to see that the property is satisfied. If on the other hand N is a leaf null is checked by the code or diagrams that in all cases the property is also satisfied.
We can perform the steps highlighted above with the following code, which replaces the function reemplazar nodo son in place of n in the tree. For better understanding of the example code in this section assume that the leaves are represented by zero real nodes instead of NULL (the insertion section of the code works with both representations).
void
elimina un hijo (struct node n)
(
/
Precondition: n is at least one child is not zero.
/
struct node child es hoja (n-> dcho) 'n-> izdo: n-> dcho
Reemplazar nodo (n, child)
If (n-> color BLACK) (
If (child-> color RED)
Child-> color BLACK
Else
Eliminar caso1 (child)
)
Free (n)
)
Note: If N is a leaf null and void do not want to represent leaf nodes as real, we can modify the algorithm by calling a first eliminar caso1 () in its parent (the node that we delete, n in the code above) and delete later. We can do this because the father is black, so it behaves the same way as a zero sheet (sometimes called leaf 'ghost'). And we can safely remove it, so that n will remain a leaf after all operations, as shown above.
If N is original and his father black, then delete the original cause parent paths passing through N and a black node unless the roads are not. Since this violates Property 5 (All paths from a given node to its leaf nodes contain the same number of black nodes), the tree must be rebalanced. There are several cases to consider.
Case 1: N is the new root. In this case, we have finished. Remove a black node of each path and the new root is black, so the properties are met. One possible implementation in the C programming language would be as follows:
void
eliminar caso1 (struct node n)
(
If (n-> parent! NULL)
Eliminar caso2 (n)
)
Note: In cases 2, 5 and 6, we assume that N is the son left his father P.
- Java World
All Java programs, other than simple console-based applications are multi-threaded, whether you like it or not. The problem is that the abstract window templates Toolkit (AWT) processes operating system (OS) events on its own thread, so that your listener methods are actually implemented on the AWT thread.
