red black trees
TRANSCRIPT
![Page 1: Red black trees](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55a9398f1a28ab3e0a8b4851/html5/thumbnails/1.jpg)
Submitted to:
Miss.Komal Bashir
Submitted by:
Maira shahnawaz
Uneza rehman
Amna siddiqui
![Page 2: Red black trees](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55a9398f1a28ab3e0a8b4851/html5/thumbnails/2.jpg)
Binary Search Tree (BST) is a good
data structure and It supports:
Insertion
Deletion
Search
![Page 3: Red black trees](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55a9398f1a28ab3e0a8b4851/html5/thumbnails/3.jpg)
Red-Black Tree is one of the balanced binary search
tree
A red-black tree is a binary search tree with one
extra attribute for each node: the colour, which is
either red or black
![Page 4: Red black trees](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55a9398f1a28ab3e0a8b4851/html5/thumbnails/4.jpg)
Invented 1972
Invented by Rudolf Bayer
Time complexity in big (o) notation:
Average Worst case
Space O(n) O(n)
Search O(log n) O(log n)
Insert O(log n) O(log n)
Delete O(log n) O(log n)
![Page 5: Red black trees](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55a9398f1a28ab3e0a8b4851/html5/thumbnails/5.jpg)
That's really your decision, but I've found that red black
trees are best suited to largely random data that has
occasional degenerate runs, and searches have no
locality of reference. This takes full advantage of the
minimal work that red black trees perform to maintain
balance compared to AVL trees and still allows for
speedy searches.
![Page 6: Red black trees](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55a9398f1a28ab3e0a8b4851/html5/thumbnails/6.jpg)
A binary search tree is a red-black tree if:
Every node is either red or black
The root is black
Every leaf (NIIL) is black
A red node’s children are black
![Page 7: Red black trees](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55a9398f1a28ab3e0a8b4851/html5/thumbnails/7.jpg)
Every path from a node x to an external node must
contain the same number of black nodes = black-
height(x)
![Page 8: Red black trees](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55a9398f1a28ab3e0a8b4851/html5/thumbnails/8.jpg)
![Page 9: Red black trees](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55a9398f1a28ab3e0a8b4851/html5/thumbnails/9.jpg)
Red Black Tree
![Page 10: Red black trees](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55a9398f1a28ab3e0a8b4851/html5/thumbnails/10.jpg)
Since red-black tree is a balanced BST, it supports
Search
Insertion
Deletion
![Page 11: Red black trees](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55a9398f1a28ab3e0a8b4851/html5/thumbnails/11.jpg)
A new item is always inserted as a leaf in the tree
If we color a leaf black, we will create a longer path
of black nodes (violating property 4)
Therefore, a new item must be colored red [unless it
is the root]
if the parent is colored black, we are done
![Page 12: Red black trees](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55a9398f1a28ab3e0a8b4851/html5/thumbnails/12.jpg)
If the parent is red, we will have consecutive red
nodes (violating property 3)
We must adjust the tree to ensure property three,
without introducing a violation to property 4
The operations are rotations and color changes.
![Page 13: Red black trees](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55a9398f1a28ab3e0a8b4851/html5/thumbnails/13.jpg)
Sibling of the parent is black [adopt the convention
that null references are black]
Inserted node is an outside grandchild
A single rotation between the parent and the
grandparent, with appropriate color changes,
restores property 3
![Page 14: Red black trees](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55a9398f1a28ab3e0a8b4851/html5/thumbnails/14.jpg)
![Page 15: Red black trees](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55a9398f1a28ab3e0a8b4851/html5/thumbnails/15.jpg)
Notice that before insertion of node X, there was
one black node from G to each of A, B, and C, and
two black nodes from G to each of D and E
After the rotation and recoloring, notice that the
number of black nodes on each of those paths
remains unchanged
Property 3 has been restored
![Page 16: Red black trees](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55a9398f1a28ab3e0a8b4851/html5/thumbnails/16.jpg)
Sibling of the parent is red
Neither single nor double rotations work, since both
result in (possibly) consecutive red nodes
Case 2
![Page 17: Red black trees](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55a9398f1a28ab3e0a8b4851/html5/thumbnails/17.jpg)
![Page 18: Red black trees](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55a9398f1a28ab3e0a8b4851/html5/thumbnails/18.jpg)
This fixes property 3 for this sub tree
What happens if the parent of this sub tree is also
red?
We could percolate this procedure up toward the
root until we no longer have two consecutive re
nodes, or we reach the root
The advantage over AVL trees has disappeared
![Page 19: Red black trees](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55a9398f1a28ab3e0a8b4851/html5/thumbnails/19.jpg)
![Page 20: Red black trees](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55a9398f1a28ab3e0a8b4851/html5/thumbnails/20.jpg)
Color flip
![Page 21: Red black trees](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55a9398f1a28ab3e0a8b4851/html5/thumbnails/21.jpg)
Single Rotation
![Page 22: Red black trees](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55a9398f1a28ab3e0a8b4851/html5/thumbnails/22.jpg)
Recall that in deleting from a binary search tree, the
only nodes which are actually removed are leaves or
nodes with exactly one child
Nodes with two children are never removed. Their
contents are just replaced
Deletions
![Page 23: Red black trees](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55a9398f1a28ab3e0a8b4851/html5/thumbnails/23.jpg)
If the node to be deleted is red, there is no problem -
- just delete the node
If the node to be deleted is black, its removal will
violate property 4
The solution is to ensure that any node to be deleted
is red
![Page 24: Red black trees](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55a9398f1a28ab3e0a8b4851/html5/thumbnails/24.jpg)
Remove 9
![Page 25: Red black trees](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55a9398f1a28ab3e0a8b4851/html5/thumbnails/25.jpg)
Remove 8:
![Page 26: Red black trees](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55a9398f1a28ab3e0a8b4851/html5/thumbnails/26.jpg)
Remove 7:
![Page 27: Red black trees](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55a9398f1a28ab3e0a8b4851/html5/thumbnails/27.jpg)
Although you may never need to implement your own
set or map classes, thanks to their common built-in
support, understanding how these data structures work
should help you better assess the performance of your
applications and give you more insight into what
structure is right for a given task. For more practice with
these concepts, check out these problems from the Top
Coder archive that involve trees: