dsa-fall-lecture4 - mkurnosov.net · symmetric binary b-trees: data structure and maintenance...
TRANSCRIPT
![Page 1: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/1.jpg)
Лекция 4:
Красно-чёрные деревья
(Red-black trees)
Курносов Михаил Георгиевич
к.т.н. доцент Кафедры вычислительных систем
Сибирский государственный университет
телекоммуникаций и информатики
http://www.mkurnosov.net
![Page 2: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/2.jpg)
Двоичные деревья поиска
22
Двоичное дерево поиска (Binary Search Tree, BST) – это
двоичное дерево, в котором:
1) каждый узел (node) имеет не более двух дочерних узлов
(child nodes)
2) каждый узел содержит ключ (key) и значение (value)
и для него выполняются следующие условия:
� ключи всех узлов левого поддерева меньше значения
ключа родительского узла
� ключи всех узлов правого поддерева больше значения
ключа родительского узла
![Page 3: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/3.jpg)
Двоичные деревья поиска (Binary Search Trees)
3
60Волк
35Рысь
90Ягуар
8Лиса
15Барсук
180Тигр
200Лев
4000Слон
600Медведь
9 узлов, глубина (depth) = 3
Value:
Key:
Min Max
![Page 4: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/4.jpg)
Двоичные деревья поиска (Binary Search Trees)
4
1
Value
2
Value
1. Операции имеют трудоемкость
пропорциональную высоте дерева
2. В среднем случае высота дерева O(log(n))
3. В худшем случае элементы добавляются
по возрастанию (убыванию) ключей –
дерево вырождается в список длины n
bstree_add(1, value)
bstree_add(2, value)
bstree_add(3, value)
bstree_add(4, value)
3
Value
4
Value
NULL
NULL
NULL
![Page 5: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/5.jpg)
� Сбалансированное дерево поиска (self-balancing
binary search tree) – дерево поиска, в котором высота
поддеревьев любого узла различаются не более чем на
заданную константу k
� Виды сбалансированных деревьев поиска:
� АВЛ-деревья (AVL trees, лекция 3)
� Красно-черные деревья (Red-black trees)
� Splay tree
� AA tree
� …
Сбалансированные деревья поиска
5
![Page 6: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/6.jpg)
Красно-чёрные деревья (Red-black trees)
6
� Автор Rudolf Bayer
Technical University of Munich, Germany, 1972
� В работе автора дерево названо “Symmetric binary B-tree”
� В работе Р. Седжвика (1978) дано современное название –
“Red-black tree”
[1] Rudolf Bayer. Symmetric binary B-Trees: Data structure
and maintenance algorithms // Acta Informatica. – 1972. –
Vol. 1, No. 4 – pp. 290-306.
[2] Guibas L., Sedgewick R. A Dichromatic Framework for
Balanced Trees // Proc. of the 19th Annual Symposium on
Foundations of Computer Science, 1978. – pp. 8-21.
![Page 7: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/7.jpg)
Red-black tree
77
ОперацияСредний случай
(average case)
Худший случай
(worst case)
Add(key, value) O(logn) O(logn)
Lookup(key) O(logn) O(logn)
Remove(key) O(logn) O(logn)
Min O(logn) O(logn)
Max O(logn) O(logn)
Сложность по памяти: O(n)
![Page 8: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/8.jpg)
Применение красно-чёрных деревьев
8
� GNU libstdc++ (/usr/include/c++/bits)
std::map, std::multimap, std::set, std::multiset
� LLVM libc++
std::map, std::set
� Java
java.util.TreeMap, java.util.TreeSet
� Microsoft .NET 4.5 Framework Class Library
SortedDictionary, SortedSet
![Page 9: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/9.jpg)
Красно-чёрные деревья (Red-black trees)
9
� Красно-черное дерево (Red-black tree, RB-tree) –
это бинарное дерево поиска, для которого выполняются
красно-черные свойства (red-black properties):
1) каждый узел является красным или черным
2) корень дерева является черным
3) каждый лист дерева (NULL) является черным
4) у красного узла оба дочерних узла – черные
5) у любого узла все пути от него до листьев, являющихся
его потомками, содержат одинаковое количество
черных узлов
![Page 10: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/10.jpg)
1) каждый узел является красным или черным
2) корень дерева является черным
3) каждый лист дерева (NULL) является черным
4) у красного узла оба дочерних узла – черные
5) у любого узла все пути от него до листьев, являющихся его
потомками, содержат одинаковое число черных узлов
Пример красно-черного дерева
10
71
2
11
14
NULL NULL
NULL NULL
5
NULL NULL
8NULL NULL
15
Лист (leaf)
NULL
![Page 11: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/11.jpg)
Добавление элемента
11
1. Находим лист для вставки нового элемента
2. Создаем элемент и окрашиваем его в красный цвет
3. Перекрашиваем узлы и выполняем повороты
71
2
11
14
NULL NULL
NULL
5
NULL NULL
8NULL NULL
15
NULL NULL
4Нарушено свойство 4 –
красный узел должен иметь два черных дочерних узла
![Page 12: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/12.jpg)
Нарушение свойств красно-черного дерева
12
Какие свойства красно-черного дерева могут быть
нарушены после вставки нового узла (красного цвета)?
1) каждый узел является красным или черным – выполняется
2) корень дерева является черным – не выполняется
(например, при добавление первого элемента)
71
2
11
14
NULL NULL
NULL
5
NULL NULL
8NULL NULL
15
NULL NULL
4
![Page 13: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/13.jpg)
Нарушение свойств красно-черного дерева
13
Какие свойства красно-черного дерева могут быть
нарушены после вставки нового узла (красного цвета)?
71
2
11
14
NULL NULL
NULL
5
NULL NULL
8NULL NULL
15
NULL NULL
4
3) каждый лист дерева (NULL) является черным – выполняется
4) у красного узла оба дочерних узла являются черными –
не выполняется
![Page 14: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/14.jpg)
5) у любого узла все пути от него до листьев (его потомков),
содержат одинаковое число черных узлов – выполняется
(новый узел замещает черный NULL, но сам имеет два
черных дочерних NULL)
Нарушение свойств красно-черного дерева
14
Какие свойства красно-черного дерева могут быть
нарушены после вставки нового узла (красного цвета)?
71
2
11
14
NULL NULL
NULL
5
NULL NULL
8NULL NULL
15
NULL NULL
4
![Page 15: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/15.jpg)
Нарушение свойств красно-черного дерева
15
После добавления нового элемента свойства 2 и 4
могут быть нарушены
1) каждый узел является красным или черным – выполняется
2) корень дерева является черным – не выполняется
(например, при добавление первого элемента)
3) каждый лист дерева (NULL) является черным – выполняется
4) у красного узла оба дочерних узла являются черными –
не выполняется
5) у любого узла все пути от него до листьев, являющихся его
потомками, содержат одинаковое число черных узлов –
выполняется (новый узел замещает черный NULL, но сам
имеет два черных дочерних NULL)
![Page 16: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/16.jpg)
Восстановление красно-черного дерева
16
� Возможно 6 случаев, нарушающих свойства красно-черного
дерева (3 случая симметричны другим трем)
� Восстановление свойств начинаем с нового элемента
и продвигаемся вверх к корню дерева
71
2
11
14
NULL NULL
NULL
5
NULL NULL
8NULL NULL
15
NULL NULL
4
![Page 17: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/17.jpg)
Восстановление красно-черного дерева
17
Случай 1
� Узел P – это корень левого поддерева своего родителя G
� Узел z красный
� Родительский узел P узла z красный
� Узел U (дядя узла z) красный
az
P
G
NULL NULL
b c
U P нарушает свойство 4
![Page 18: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/18.jpg)
Восстановление красно-черного дерева
18
Случай 1
� Узел P – это корень левого поддерева своего родителя G
� Узел z красный
� Родительский узел P узла z красный
� Узел U (дядя узла z) красный
az
P
G
NULL NULL
b c
U
az
P
G
NULL NULL
b c
U
Перекрашиваем узлы
� P – черный
� U – черный
� G – красный
![Page 19: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/19.jpg)
Восстановление красно-черного дерева
19
Случай 2
� Узел P – это корень левого поддерева своего родителя G
� Узел z красный
� Родительский узел P узла z красный
� Узел U черный
� Узел z – правый дочерний элемент P
a z
P
G
NULL NULL
b c
U
P нарушает свойство 4
![Page 20: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/20.jpg)
Восстановление красно-черного дерева
20
Случай 2
� Узел P – это корень левого поддерева своего родителя G
� Узел z красный
� Родительский узел P узла z красный
� Узел U черный
� Узел z – правый дочерний элемент P
a z
P
G
NULL NULL
b c
U
Переходим к случаю 3
путем поворота
дерева P влево
P
z
G
b c
U
a
NULL
NULL
![Page 21: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/21.jpg)
Левый поворот дерева (left rotation)
x
y
b c
a
y
x
a b
c
Левый поворот x
(left rotation)
function LeftRotate(x)
y = x.right
x.right = y.left /* Subtree b */
if y.left != NULL then
y.left.parent = x /* Setup parent of b */
y.parent = x.parent
if x = x.parent.left then /* x is left subtree */
x.parent.left = y
else
x.parent.right = y
y.left = x
x.parent = y
end function21
![Page 22: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/22.jpg)
Восстановление красно-черного дерева
22
Случай 3
� Узел P – это корень левого поддерева своего родителя G
� Узел z красный
� Родительский узел P узла z красный
� Узел U черный
� Узел z – левый дочерний элемент P
P нарушает свойство 4
az
P
G
NULL NULL
b c
U
![Page 23: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/23.jpg)
Восстановление красно-черного дерева
23
Случай 3
� Узел P – это корень левого поддерева своего родителя G
� Узел z красный
� Родительский узел P узла z красный
� Узел U черный
� Узел z – левый дочерний элемент P
1. Перекрашиваем
вершины
o P – черный
o G – красный
2. Поворачиваем
дерево G вправо
az
P
G
NULL NULL
b c
U
a
z
P
G
NULL NULL
b c
U
![Page 24: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/24.jpg)
Правый поворот дерева (right rotation)
y
x
c a
b
x
y
b c
a
Правый поворот x
(right rotation)
function RightRotate(x)
y = x.left
x.left = y.right /* Subtree c */
if y.right != NULL then
y.right.parent = x /* Setup parent of c */
y.parent = x.parent
if x = x.parent.left then /* x is left subtree */
x.parent.left = y
else
x.parent.right = y
y.right = x
x.parent = y
end function24
![Page 25: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/25.jpg)
Восстановление красно-черного дерева
25
Случаи 4, 5 и 6 симметричны случаям 1, 2 и 3
� Узел P – это корень правого поддерева своего родителя G
� Узел z красный
� Родительский узел P узла z красный
� Узел U черный или красный
� Узел z – левый или правый дочерний элемент P
![Page 26: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/26.jpg)
Восстановление красно-черного дерева
function RBTree_Fixup(z)
while z.parent.color = RED do
if z.parent = z.parent.parent.left then
/* z in left subtree of G */
y = z.parent.parent.right; /* Uncle */
if y.color = RED then
/* Case 1 */
z.parent.color = BLACK
y.color = BLACK
z.parent.parent.color = RED
z = z.parent.parent
else
if z = z.parent.right then
/* Case 2 --> case 3 */
z = z.parent
RBTree_RotateLeft(z)
end if 26
![Page 27: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/27.jpg)
Восстановление красно-черного дерева
27
/* Case 3 */
z.parent.color = BLACK
z.parent.parent.color = RED
RBTree_RotateRight(z.parent.parent)
end if
else
/* z in right subtree of G */
/* ... */
end if
end while
root.color = BLACK
end function
![Page 28: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/28.jpg)
Удаление элемента
28
1. По заданному ключу находим элемент для удаления
2. Удаляем элемент (как в случае обычного дерева поиска)
3. Перекрашивая узлы и выполняя повороты
восстанавливаем структуру красно-черного дерева
71
2
11
14
NULL NULL
NULL
5
NULL NULL
8NULL NULL
15
NULL NULL
4
[CLRS, С. 351]
![Page 29: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/29.jpg)
Высота красно-черных деревьев
29
� Обозначим:
� h(x) – высота красно-черного дерева с корнем в узле х
� bh(x) – количество черных элементов на пути от узла x
к листу (“черная” высота дерева, black height)
71
2
11
14
NULL NULL
NULL
5
NULL NULL
8NULL NULL
15
NULL bh(NULL) = 0
![Page 30: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/30.jpg)
Лемма. Красно-черное дерево с n внутренними узлами имеет
высоту не более чем 2log(n + 1)
Доказательство
� Покажем по индукции, что любое поддерево с вершиной
в узле x содержит не менее 2bh(x) – 1 внутренних узлов
� Базис индукции: bh(x) = 0, следовательно это лист
(NULL, не содержит внутренних узлов): 20 – 1 = 0
� Индуктивное предположение: считаем, в любом поддереве
x с черной высотой < bh(x) количество внутренних узлов
2bh(x) – 1
Высота красно-черных деревьев
30
![Page 31: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/31.jpg)
Высота красно-черных деревьев
31
Лемма. Красно-черное дерево с n внутренними узлами имеет
высоту не более чем 2log(n + 1)
Доказательство (продолжение)
� Если х черный, оба узла имеют черную высоту bh(x) – 1,
если x красный, узлы имею высоту bh(x)
x
…
l
… …
r
…
bh(x)
bh(x) bh(x) – 1
� Следовательно в поддереве x число n
внутренних узлов
n ≥ 1 + (2bh(x) – 1 – 1) + (2bh(x) – 1 – 1)
n + 1 ≥ 2bh(x)
![Page 32: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/32.jpg)
Высота красно-черных деревьев
32
Лемма. Красно-черное дерево с n внутренними узлами имеет
высоту не более чем 2log(n + 1)
Доказательство (продолжение)
� По свойствам как минимум половина узлов на пути
от корня к листу черные, тогда
bh(x) ≥ h(x) / 2
� Следовательно
n + 1 ≥ 2h(x) / 2
� Логарифмируем
log(n + 1) ≥ h(x) / 2
h(x) ≤ 2log(n + 1)
![Page 33: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/33.jpg)
0
5
10
15
20
25
100
200
300
400
500
600
700
800
900
1000
1100
1200
1300
1400
1500
1600
1700
1800
1900
2000
Red-black tree vs. AVL tree
33
� Высота AVL-дерева
log� � ≤ ℎ � ≤ 1.4405 log� � + 2 − 1.3277
� Высота красно-черного дерева
ℎ � ≤ 2 log�(� + 1)
h(n)
n
AVL tree
Red-black tree
log2(n)
![Page 34: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/34.jpg)
Реализация red-black tree (rbtree)
#define COLOR_RED 0
#define COLOR_BLACK 1
struct rbtree {
int key;
char *value;
int color;
struct rbtree *parent;
struct rbtree *left;
struct rbtree *right;
};
struct rbtree EmptyNode = {0, 0, COLOR_BLACK,
NULL, NULL, NULL};
struct rbtree *NullNode = &EmptyNode;34
A
NullNode NullNode
B
NullNode NullNode
C
NullNode
EmptyNode
![Page 35: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/35.jpg)
Пример формирования дерева
int main(int argc, char **argv)
{
struct rbtree *tree = NULL;
tree = rbtree_add(tree, 10, "10");
tree = rbtree_add(tree, 5, "5");
tree = rbtree_add(tree, 3, "3");
tree = rbtree_add(tree, 11, "11");
tree = rbtree_add(tree, 12, "12");
tree = rbtree_add(tree, 6, "6");
tree = rbtree_add(tree, 8, "8");
tree = rbtree_add(tree, 9, "9");
rbtree_print(tree);
rbtree_free(tree);
return 0;
} 35
![Page 36: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/36.jpg)
Добавление узла
struct rbtree *rbtree_add(struct rbtree *root,
int key, char *value)
{
struct rbtree *node, *parent = NullNode;
/* Search leaf for new element */
for (node = root; node != NullNode &&
node != NULL; )
{
parent = node;
if (key < node->key)
node = node->left;
else if (key > node->key)
node = node->right;
else
return root;
}36
![Page 37: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/37.jpg)
Добавление узла (продолжение)
node = malloc(sizeof(*node));
if (node == NULL)
return NULL;
node->key = key;
node->value = value;
node->color = COLOR_RED;
node->parent = parent;
node->left = NullNode;
node->right = NullNode;
37
![Page 38: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/38.jpg)
Добавление узла (продолжение)
if (parent != NullNode) {
if (key < parent->key)
parent->left = node;
else
parent->right = node;
} else {
root = node;
}
return rbtree_fixup_add(root, node);
}
38
![Page 39: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/39.jpg)
Восстановление свойств после добавления
struct rbtree *rbtree_fixup_add(struct rbtree *root,
struct rbtree *node)
{
struct rbtree *uncle;
/* Current node is RED */
while (node != root &&
node->parent->color == COLOR_RED)
{
if (node->parent ==
node->parent->parent->left)
{
/* node in left tree of grandfather */
uncle = node->parent->parent->right;
39
![Page 40: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/40.jpg)
Восстановление свойств после добавления
if (uncle->color == COLOR_RED) {
/* Case 1 - uncle is RED */
node->parent->color = COLOR_BLACK;
uncle->color = COLOR_BLACK;
node->parent->parent->color = COLOR_RED;
node = node->parent->parent;
} else {
/* Cases 2 & 3 - uncle is BLACK */
if (node == node->parent->right) {
/* Reduce case 2 to case 3 */
node = node->parent;
root = rbtree_left_rotate(root,
node);
}
40
![Page 41: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/41.jpg)
Восстановление свойств после добавления
/* Case 3 */
node->parent->color = COLOR_BLACK;
node->parent->parent->color =
COLOR_RED;
root = rbtree_right_rotate(root,
node->parent->parent);
}
41
![Page 42: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/42.jpg)
Восстановление свойств после добавления
} else {
/* Node in right tree of grandfather */
uncle = node->parent->parent->left;
if (uncle->color == COLOR_RED) {
/* Uncle is RED */
node->parent->color = COLOR_BLACK;
uncle->color = COLOR_BLACK;
node->parent->parent->color =
COLOR_RED;
node = node->parent->parent;
} else {
42
![Page 43: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/43.jpg)
Восстановление свойств после добавления
/* Uncle is BLACK */
if (node == node->parent->left) {
node = node->parent;
root = rbtree_right_rotate(root,
node);
}
node->parent->color = COLOR_BLACK;
node->parent->parent->color =
COLOR_RED;
root = rbtree_left_rotate(root,
node->parent->parent);
}
}
}
root->color = COLOR_BLACK;
return root;
} 43
![Page 44: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/44.jpg)
Левый поворот (left rotate)
struct rbtree *rbtree_left_rotate(
struct rbtree *root, struct rbtree *node)
{
struct rbtree *right = node->right;
/* Create node->right link */
node->right = right->left;
if (right->left != NullNode)
right->left->parent = node;
/* Create right->parent link */
if (right != NullNode)
right->parent = node->parent;
44
![Page 45: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/45.jpg)
Левый поворот (left rotate)
if (node->parent != NullNode) {
if (node == node->parent->left)
node->parent->left = right;
else
node->parent->right = right;
} else {
root = right;
}
right->left = node;
if (node != NullNode)
node->parent = right;
return root;
}
45
![Page 46: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/46.jpg)
Правый поворот (right rotate)
struct rbtree *rbtree_right_rotate(
struct rbtree *root, struct rbtree *node)
{
struct rbtree *left = node->left;
/* Create node->left link */
node->left = left->right;
if (left->right != NullNode)
left->right->parent = node;
/* Create left->parent link */
if (left != NullNode)
left->parent = node->parent;
46
![Page 47: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/47.jpg)
Правый поворот (right rotate)
if (node->parent != NullNode) {
if (node == node->parent->right)
node->parent->right = left;
else
node->parent->left = left;
} else {
root = left;
}
left->right = node;
if (node != NullNode)
node->parent = left;
return root;
}
47
![Page 48: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/48.jpg)
Литература
48
1. Кормен Т.Х., Лейзерсон Ч.И., Ривест Р.Л., Штайн К.
Алгоритмы: построение и анализ. – 2-е изд. – М.:
Вильямс, 2005. – 1296 с. (С. 336-356)
2. Седжвик Р. Фундаментальные алгоритмы на С++.
Анализ/Структуры данных/Сортировка/Поиск. – К.:
ДиаСофт, 2001. – 688 с. (C. 545)
3. To Google: red-black tree
![Page 49: dsa-fall-lecture4 - mkurnosov.net · Symmetric binary B-Trees: Data structure and maintenance algorithms // Acta Informatica. – 1972. – Vol. 1, No. 4 – pp. 290-306. [2] Guibas](https://reader036.vdocuments.site/reader036/viewer/2022062414/5ed3e928b38510386857313f/html5/thumbnails/49.jpg)
Задание
49
� Изучить алгоритм удаления элемента из красно-черного
дерева [CLRS, С. 351]