blob: 32ada6cec64ba17e4156c5aad35c9aaaef8597cc (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
Illustrate that the nodes of any AVL tree T can be
colored "red" and "black" so that T becomes a
red-black tree.
```plaintext
AVL Tree Red-Black Tree
(20:3) (20:r)
/ \ --> / \
(15:2) (30:2) (15:b) (30:b)
/ \ \ / \ \
(10:1) (17:1) (35:1) (10:r) (17:r) (35:r)
* perform in order traversal
* add node to red/black tree
* assign colour of Red/Black node based on height of AVL node
Step 1:
(20:r)
Step 2:
(20:r)
/
(15:b)
Step 3:
(20:r)
/ \
(15:b) (30:b)
Step 4:
(20:r)
/ \
(15:b) (30:b)
/
(10:r)
Step 5:
(20:r)
/ \
(15:b) (30:b)
/ \
(10:r) (17:r)
Step 6:
(20:r)
/ \
(15:b) (30:b)
/ \ \
(10:r) (17:r) (35:r)
```
```c
RBTree *avl_tree_to_rb_tree(AVLTree *t) {
if (!t)
return NULL;
RBTree *r = rb_tree_initialize_with(t->value, t->height % 2 == 0 ? black : red);
r->left = avl_tree_to_rb_tree(t->left);
r->right = avl_tree_to_rb_tree(t->right);
return r;
}
```
|