Professional Documents
Culture Documents
Data Structures - Week #6: Special Trees
Data Structures - Week #6: Special Trees
Special Trees
Outline
• Adelson-Velskii-Landis (AVL) Trees
• Splay Trees
• B-Trees
AVL Trees
2
Week #1
Definition
An AVL tree is a BST with the following
balance condition:
for each node in the BST, the height of left
and right sub-trees can differ by at most 1,
or
hh
− ≤1. NL NR
3
Week #1
Rotations
Definition:
• Rotation is the operation performed
on a BST to restore its AVL property
lost as a result of an insert operation.
• We consider the node α whose new
balance violates the AVL condition.
4
Week #1
Rotation
• Violation of AVL condition
• The AVL condition violation may occur in four cases:
– Insertion into left subtree of the left child (L/L)
– Insertion into right subtree of the left child (R/L)
– Insertion into left subtree of the right child (L/R)
– Insertion into right subtree of the right child (R/R)
• The outside cases 1 and 4 (i.e., L/L and R/R) are fixed by a
single rotation.
• The other cases (i.e., R/L and L/R) need two rotations called
double rotation to get fixed.
• These are fundamental operations in balanced-tree algorithms.
5
Week #1
α ≡ k1 node
March 10, 2021 Borahan Tümer, Ph.D. 11
Double Rotation (R/L)
α≡
6
Week #1
Double Rotation
(R/L) α ≡ k1 node
k1
Height 1 k1 Height
k2 k2
k3
2
Z
k3 X B C Z Height 3
X BC
The symmetric case (L/R) is handled similarly left as an exercise to you! March 10,
Animation 48
48
7
Week #1
48
16
March 10, 2021 Borahan Tümer, Ph.D. 15
48
16
24
8
Week #1
48
1 -1
R/L
Dbl. Rot.
16
24
24
16
48
20
9
Week #1
Constructing an AVL Tree – Animation
20
8
48
16 24
24
16
48
20
20
8
16 24
24
16
48
20
8 12
16 24
20 L/L 2 0
8
24
Sngl. Rot. 8 16
1 0 OK
20
48
-1 0
OK
12
8
32
16 24
16
8
24
20 48 32
12
8 32 54 8
16 24
16
24
20 48 32
54
12
8 32 54 728
16 24
16
24
20 48 32
72
54
12
March 10, 2021 Borahan Tümer, Ph.D. 24
12
Week #1
8
16 24 0 2 R/R
Sngl. Rot.
32 54 72 8
16
24
20 48 0 1 OK
OK 72
32
54
-1 0
12
8 32 54 72 8
16 24
16
18
48
72
12 18
24 54 20 32
13
Week #1
8 32 54 72 8 13
16 24
18
48
16
24 54 1 0
OK
L/R 20 32 0 -1
Dbl.Rot. OK
12
18
21
72
OK
8 32 54 72 18 16
16 24
24
96
48
12 18 96
20
32 54
8
72
14
Week #1
8 32 54 72 18 16
16 24
24
96
48
-1 1
96
12 18
20 R/R 72
32 Sngl. Rot. -1 0
54 OK
8
March 10, 2021 Borahan Tümer, Ph.D. 29
8 32 54 72 18 16
16 24
24
48 96
17
12 18 54
20 96
32
72
8
15
Week #1
Splay Trees
16
Week #1
Splaying
• The underlying idea of splaying is to move a
deep node accessed upwards to the root,
assuming that it will be accessed in the near
future again.
• While doing this, other deep nodes are also
carried up to smaller depth levels, making the
average depth of nodes closer to O(log2n).
17
Week #1
Splaying
• Splaying is similar to bottom-up AVL
rotations
• If a node X is the child of the root R, – then
we rotate only X and R, and this is the last rotation
performed.
else consider X, its parent P and grandparent G.
Two cases and their symmetries to consider
Zig-zag case, and
Zig-zig case.
G X
P Height h G
P
Height h+1
D
X A B C D Height h+2
A
BC
This is the same operation as an AVL double rotation in an R/L violation. March 10,
18
Week #1
LC(P): left child of node P
RC(P): right child of node
P
Zig-zig case
X
P Height h+1
D as is
Height h+2
C B
X LC(G)
Height h Height h+3
D C
G P
A
B
G
A
as is LC(P)
Animated Example
10
2 11
14
39
5 6
Initial BST
8
Animated
Example 10
2 11
Grandparent
1439
5
Node with 6 accessed! G stands for
Zig-zig 8 7
P stands for
case P Parent
X denotes the
G
X 6 current node
Animated Example
10
2 11
G
14 Node with 6 accessed!
39
X 7
5
P
8
6
Zig-zag case
20
Week #1
Animated Example
10
Zig-zig
2 11
G
case
5 9
3
7
7
13 5
1 8 P
8
4
9 Node with 6
X
6 accessed!
Animated Example
R
10
X
4
6 11
2 9
4 59
13
accessed!
2 8
7
13
5 Node with 6
7 8
21
Week #1
4
10
59
13
Animated 11
Example 7
22
Week #1
23
Week #1
B-Trees
• A B-tree is a rooted tree with the following
properties:
• Every node x has the following fields: – n[x], the
number of keys currently stored in x. – the n[x] keys
themselves, in non-decreasing order, so that
key1[x] ≤ key2[x] ≤ ... ≤ keyn[x][x] ,
– leaf[x], a boolean value, true if x is a leaf. March 10, 2021
B-Trees
• Each internal node has n[x]+1 pointers,
c1[x],..., cn[x]+1[x], to its children. Leaf nodes
have no children, hence no pointers!
• The keys separate the ranges of keys stored in
each subtree: if kiis any key stored in the
subtree with root ci[x], then
k1 ≤ key1[x] ≤ k2 ≤ key2[x] ≤ ... ≤ keyn[x][x] ≤ kn[x]+1 .
• All leaves have the same depth, h, equal to the
tree’s height.
March 10, 2021 Borahan Tümer, Ph.D. 48
24
Week #1
B-Trees
• There are lower and upper bounds on the
number of keys a node may contain. These
bounds can be expressed in terms of a fixed
integer t ≥ 2 called the minimum degree of the
B-Tree.
– Lower limits
• All nodes but the root has at least t-1 keys.
• Every internal node but the root has at least t children.
• A non-empty tree’s root must have at least one key.
B-Trees
– Upper limits
• Every node can contain at most 2t-1 keys.
• Every internal node can have at most 2t children. •
A node is defined to be full if it has exactly 2t-1 keys. •
For
a B-tree of minimum degree t ≥ 2 and n nodes
≤n
ht
log +
1 2
25
Week #1
• B-tree search
• B-tree insert
• B-tree removal
26
Week #1
March 10, 2021 Borahan Tümer, Ph.D. 53 March 10, 2021 Borahan Tümer, Ph.D. 54
27
Week #1
March 10, 2021 Borahan Tümer, Ph.D. 55
Search in B-Trees
• Similar to search in BSTs with the exception
that instead of a binary, a multi-way (n[x]+1-
way) decision is made.
21 53 77
34 43 49 58 65 71 82 88 93
7 13 17
keyi(x) ci(x)
March 10, 2021 Borahan Tümer, Ph.D. 56
28
Week #1
Search in B-Trees
B-tree-Search(x, k)
{ i=1;
while (i ≤ n[x] and k > keyi[x]) i++;
if (i ≤ n[x] and k = keyi[x]) // if key found return
(x,i);
if (leaf[x]) // if key not found at a leaf return NULL;
else {DISK_READ(ci[x]); // if key < keyi[x] return
B-tree-Search(ci[x],k);}
}
Insertion in B-Trees
• Insertion into a B-tree is more complicated than that
into a BST, since the creation of a new node to place
the new key may violate the B-tree property of the
tree.
• Instead, the key is put into a leaf node x if it is not
full.
• If full, a split is performed, which splits a full node
(with 2t-1 keys) at its median key, keyt[x], into two
nodes with t-1 keys each.
• keyt[x] moves up into the parent of x and identifies
the split point of the two new trees.
Insertion in B-Trees
• A single-pass insertion starts at the root
traversing down to the leaf into which the key
is to be inserted.
• On the path down, all full nodes are split
including a full leaf that also guarantees a
parent with an available position for the
median key of a full node to be placed.
7 13 17
34 43 49 58 65 71 82 88 93
949799
146
March 10, 2021 Borahan Tümer, Ph.D. 60
30
Week #1
Insertion in B-Trees:
Example 69 inserted... 53
21 77
March 10, 2021 Borahan Tümer, Ph.D. 61
7 13 17
34 43 49 58 65 71 82 88 93
Insertion in
949799 B-Trees:
146
9 1112 141516 181920 272932 404142 454748 505152 Example
545557 606163 676870 737476 787981 848586 899092
69 inserted... 53
21 77
65
146
9 1112 141516 181920 272932 404142 454748 505152
7 13 17
34 43 49 58 71 82 88 93
949799
31
Week #1
Insertion in B-Trees:
Example 69 inserted... 53
21 77
65
34 43 49 58 71 82 88 93
68
949799
7 13 17
9 1112 141516 181920 272932 404142 737476 787981 848586 899092
146 454748 505152 545557 606163
Insertion in B-Trees:B-tree-Insert
B-tree-Insert(T,k)
{ r=root[T];
if (n[r] == 2t-1) {
s=malloc(new-B-tree-node);
root[T]=s;
leaf[s]=false;
n[s]=0;
c1[s]=r;
B-tree-Split-Child(s,1,r);
B-tree-Insert-Nonfull(s,k); }
else B-tree-Insert-Nonfull(r,k);
}
March 10, 2021 Borahan Tümer, Ph.D. 64
32
Week #1
Insertion in B-Trees:B-tree-Split-Child
B-tree-Split-Child(x,i,y)
{ z=malloc(new-B-tree-node);
leaf[z]=leaf[y];
n[z]=t-1;
for (j = 1; j < t) keyj[z]=keyj+t[y]; A
if (!leaf[y])
⎭⎬⎫
for (j = 1; j <= t;j++) cj[z]=cj+t[y]; B
n[y]=t-1;
⎭⎬⎫
⎭⎬⎫
for (j=n[x]; j>=i; j--) keyj+1[x]=keyj[x]; E
⎭⎬⎫
keyi[x]=keyt[y]; n[x]++; F
DISK_WRITE(y);
DISK_WRITE(z); ⎭⎬⎫
DISK_WRITE(x); }
March 10, 2021 Borahan Tümer, Ph.D. 65 B-tree-Split-Child:
Example
t=3 ...
21 53 77 y
x
A z
...
... 71 76
55 58 65 71 76
B
33
Week #1
B-tree-Split-Child: Example
x C
t=3
77
21 53 65
E z
y F D
... ......
55 58 71 76
B-tree-Split-Child:
Example x
t=3
77z
21 53 65 y
... ......
55 58 71 76
34
Week #1
Insertion in B-Trees:B-tree-Insert
Nonfull B-tree-Insert-Nonfull(x,k)
{ i=n[x];
if (leaf[x]) {
while (i≥1 and k < keyi[x]) B-tree-Split-Child(x,i, ci[x]);
{keyi+1[x]=keyi[x]; i--;} keyi+1[x]=k; if x is a leaf
n[x]++; then place key in x;
DISK_WRITE(x); write x on disk;
else find the node (root of subtree) key goes to;
}
read node from disk;
else { if node full
while (i≥1 and k < keyi[x]) i--; split node at key’s
i++; position;
DISK_READ(ci[x]); recursive call with
if (n[ci[x]]==2t-1) { node split and key;
35
Week #1
36
Week #1
37
Week #1
949799
7 13 17
9 1112 141516 181920 272932 404142 737476 787981 848586 899092
146 454748 505152 545557 606163
21 77
65
949799
7 13 17 146
34 43 49 58 71 82 88 93 9 1112 141516 181920 272932 404142 454748 505152
Pred. Of 21Case 1
38
Week #1
20 77
65
Case 2a
949799
7 13 17
34 43 49 58 71 82 88 93 68 Succ. of 65
146
9 1112 141516 1819 272932 404142 454748 505152 B-Trees: Example
545557 606163 67 7376 787981 848586 899092 6970
Removal in
65 removed... 53 69 to remove...
Case 2b recursively followed by
case 3a
node of 68 and
recursively delete 69!
34 43 49 58 71 82 88 93 69
7 13 17
20 77
67
949799
34 43 49 58 71 82 88 93
7 13 17
20 77
67
6870 949799