summaryrefslogtreecommitdiff
path: root/src/03/README.md
blob: d5f23059b44c53713289b65c6bf7bc2c17a275d1 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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:b)
        /    \          -->         /    \
    (15:2)    (30:2)           (15:b)    (30:b)
    /    \        \            /   \         \
(10:1) (17:1)     (35:1)  (10:r) (17:r)      (35:r)

* perform pre order traversal
* assign colour of Red/Black node based on height of each AVL node

Step 1:
          (20:b)

Step 2:
          (20:b)
          /
      (15:b)

Step 3:
          (20:b)
          /
      (15:b)
      /
  (10:r)

Step 4:
          (20:b)
          /
      (15:b)
      /   \
  (10:r) (17:r)

Step 5:
          (20:b)
          /     \
      (15:b)    (30:b)
      /   \
  (10:r) (17:r)

Step 6:
          (20:b)
          /    \
      (15:b)    (30:b)
      /   \         \
  (10:r) (17:r)      (35:r)
```

Illustrate that via AVL single rotation, any binary search tree T1 can be
transformed into another search tree T2 (with the same items).

Left rotation:

```plaintext
  (10)                  (20)
    \                  /    \
    (20)      ->    (10)    (30)
      \
      (30)
```

Right rotation:

```plaintext
     (30)            (20)
     /              /   \
   (20)     -->   (10)  (30)
   /
(10)
```

Left-Right rotation:

```plaintext
   (30)         (20)
   /           /    \
(10)     -> (10)    (30)
   \
   (20)
```

Right-Left rotation:

```plaintext
(10)             (20)
    \            /  \
    (30)  --> (10)  (30)
   /
(20)
```

Give an algorithm to perform this transformation using O(N log N) rotation on average.

See `./avl_tree.c`.