Trees: Balanced Search
Trees: Balanced Search
com
Balanced Search
trees
(Balanced Search Tree)
•AVL tree
•2-3 and 2-3-4 trees
•splay tree
•Red Black Tree
•B-tree
one
2nd
BALANCED TREE
BALANCED TREE
4
5
6
AVL TREES
- The property of the AVL tree is that the height difference between the right
subtree and the left subtree is at most one node that is. This rule applies to
all nodes of the tree.
hL+1 or hL-one
hL
TL TR
8
AVL Trees
- AVL trees are balanced binary search trees.
- left = height(left subtree), and
sagh=height(right subtree) if HUNTINGTree
could be.
- if equal 0 0 one 9 0
- If left saz one
red numbers balance
- If more than right - one
is a factor.
60 6 - one
6 0
one 4 0 4
one5 7 - one 10 2nd 9 one
0 4 0 one 8 0 0 one 5 8 0
9 0
07 9 0
AVL Tree AVL Tree
Not an AVL Tree
6 6 0
6 one
- one
one4 9 2nd 04
2nd 4 7 - 2nd
90
0 one 7 one3 8 - one
0 one 5 0
- one
80 0 one 9 0
x {
int value;
key
left int height;
right
height i'm hunting left;
i'm hunting right;
}
11th
AVL TREES
- This
bad uptime is eliminated by creating an
AVL tree data model.
AVL Trees
HUNTINGTree
- So how?
04
9 0
- Lucky:
- call duration O(h) = O(log n)
- unlucky
- Adding and deleting operations can cause the tree to
become unstable.
0 Add 3
6 6 one
one
5 7 7
- one
2nd 5 - one
0 4 0 one 4
9 9 0
03
AVL Tree
No longer an AVL tree
14
4
one 9 0
- Idea: After adding the new node
one. Get to the root by correcting the balance factor.
0 3
Balancing: Example
Add 3 rotation
60 6 2nd 6 0
one 5 7 - one
2nd
5 7 - one
0 4 7 - one
0 4 9 0 one4 9 0 0 3 5 0 9 0
AVL 0 3
AVL
Not AVL
L R
A B NS D
A B NS D
- There are 4 different situations:
L
rotation
2nd P
P
0 or 1 L R A
R
B
A B NS D
NS D
tree after insertion
Tree after LL Correction
one4 20 0 4
one 20 0 2nd 4 0 20 0 0 3 20 0
0 3 0 3 3
one 0 2nd 4 0
at the beginning
0 2nd 0 2nd LL Fix
AVL Tree
your pivot after it's done
After adding 2 determination AVL tree
state of the tree
The type of imbalance
balance factor determination
straighten to root
advance
• LL Imbalance:
– P(4)'Fame bf NS 2nd
– L(3)'Fame bf NS 0 or 1
20
0 4 20 0 0 4 4
one 20 0
20 0 3 10 0
one
0 3 5 0 3
one 50
03 50 0 2nd 5 0 20 0
at the beginning
0 2nd 0 2nd
AVL Tree your pivot LL Fix
After adding 2 determination after it's done
state of the tree AVL tree
The type of imbalance
balance factor determination
root by straightening
• LL Imbalance:
move forward
– P(4)'Fame bfNS 2nd
– L(3)'Fame bfNS 0 or 1
21
30 0 30 0 30 -one 0 20 40 0
at the beginning
AVL Tree 40 0 40 0 RR Correction
after it's done
After adding 40 your pivot
AVL tree
state of the tree determination
0 4 20 0 0 4 20 -one 0 10 30 -one
0 15 30 0 0 15 30 -one 04 15 0 40 0
at the beginning
40 0
AVL Tree
After adding 40 RR Correction
state of the tree after it's done
AVL tree
Proceed to the root by correcting the The type of imbalance
balance factor and set 10 as the pivot determination
set
• RR Imbalance:
– P(10) - bf = -2nd
– R(20) - bf = 0 or -1
24
1. Rotation
2nd
P P 2. Rotation
LR
L 2nd R LR 2nd R
- one
L P
one L
LR R
A NS D B2 NS D A B1 B2
A
B1 B2 B1
NS D
tree after insertion 1. Tree after rotation After LR Fix
• LR Imbalance: When added to the right of the left subtree of P (to the LR tree)
– P - bf = 2nd
- L - bf = -one
– Correction: Rotate around L & P 2 times
25
one
0 3 70 03 7 one 03 5 0 20 0
1. Rotation 2. Rotation RL
P - 2nd P - 2nd
2nd R one P R
2nd RL
L L
R L
one
RL
A A C1 C1 C2 D
B D B
A B
C1 C2 C2 D
tree after insertion 1. After rotation After RL Fix
at the beginning 17 0
RL Fix
AVL Tree
after adding 17 after it's done
state of the tree AVL tree
balance factor
straighten to root The type of imbalance
advance and pivot 10 determination
set as
• RL Imbalance:
– P(10) - bf = -2nd
– R(20) - bf = one
28
AVL TREES
AVL TREES
AVL TREES
- Function 3. Add Function for AVL Tree
- public AvlAgac insert(AvlAgac p, int x) {
- if (p == null)
-{ p = new AvlAgac(); p.data = x; }
- else
-{ if (x <p.data)
- {
- p.left = insert(p.left, x);if (height(p.left)-
- height(p.right) == 2)if (x < p.left.data)
-
- p = singlerotateright(p);
- else p = doublerotateright(p);
- }
31
AVL TREES
- else if (x > p.data)
- { p.right = insert(p.right, x);
- if (height(p.left) - height(p.right) == -2)if (x >
- p.right.data)
- p = singlerotateleft(p);
- else p = doublerotateleft(p); }
-
- p.height = max(height(p.left), height(p.right)) + 1; return p;
-
- }
- int max(int a, int b)
- { if (a > b) return a;
- else return b;
- }
32
AVL TREES
- AvlAgac singlerotateleft(AvlAgac p) {
-
- AvlAgac lc = p.left;
- p.left = lc.right;
- lc.right = p;
- p.height = max(height(p.left), height(p.right)) + 1; lc.height =
- max(height(lc.left), lc.height) + 1;return lc;
-
- }
33
AVL TREES
- AvlAgacdoublerotateright(AvlAgac p)
- {
- p.left = singlerotateleft(p.left);return
- singlerotateright(p); }
-
34
AVL TREES
- AvlAgac singlerotateleft(AvlAgac p) {
-
- AvlAgac rc = p.right;
- p.right = rc.left;
- rc.left = p;
- p.height = max(height(p.left), height(p.right))+1; rc.height =
- max(height(rc.right), rc.height)+ 1;return rc;
-
- }
35
AVL TREES
- AvlAgacdoublerotateleft(AvlAgac p)
- {
- p.right = singlerotateright(p.right);return
- singlerotateleft(p); }
-
36
AVL TREES
- Sample:
- 46, added
37
AVL TREES
- Sample:
-
-
AVL TREES
- Example:Create a 1,2,3,4,5,6,7,8,9,10 tree and balance it
step by step.
39
one Delete(20)
Turn
10 one 10 2nd
10 4 - one
0 4 20 0 04 0 4 0 3 10 one
0 3 5 0 0 3 50 0 3 5 0 0 5
advance
LL Imbalance:
– P(10)'Fame bfNS 2nd
– L(4)'Fame bfNS 0 or 1
41
4 2nd
- one
20 0 - one 4 - one 4 0 4 10 0
one
5 0 50 From LR Fix
5 0
then AVL Tree
at the beginning 20 after deletion 10 as pivot
AVL Tree state of the tree determination
advance
LR Imbalance:
– P(10) - bf = 2nd
– L(4) - bf = -one
42
5 20
- one
one
5 20
- one
one
3
one 7 30 -one
7 0 15
- one
one 3 - one 30 -one
2nd 0 4
one 0 8 0 40
2nd 0 4
one 0 8 0 40
0 one
0 one 10 after deletion
Initial AVL Tree 10 and 15(smallest element
in right subtree) swapped
and 15 deleted.
balance factor
root by straightening
move forward
43
5
one 20
- 2nd
5
one 30 0
one 3 7 30 -one 3
one - one 7 20 0 40 0
- one
2nd 0 4
one 2nd 0 4
one 0 8
0 8 0 40
one 5 30 0 one 3 15 0
0 one
Setting 15 as the pivot Adjusted AVL tree
of imbalance
set type
LL Imbalance:
– P(15) - bf = 2nd
– L(5) - bf = 0 or 1
45
- 19 deleted (The largest node on the left or the smallest node on the right is made the root.
or
46
58
-7 40 10
- 14
47
58,7,40
- 10 14
48
Deletion Example
- Sample: Add 7 to the tree and balance it.
- Delete the value 20 from the tree (The smallest node on the right will be taken)
49
Homework
•AVL tree
•2-3 and 2-3-4 trees
•splay tree
•Red Black Tree
•B-tree
52
53
Splay Trees
- It is intuitive.
- If X has been accessed once, it will be accessed once more.
- After X is reached, the "splaying" operation is performed.
- X moves to the root.
54
Sample
- Here, besides the tree being balanced, 12 can be
accessed in O(1) time.
- Active (recently accessed) active when moving nodes
towards rootnodes that are not rooted out.
Root Root
15 After find(12) 12
2nd
6 18 Splay main idea: 6 15
using rotation
one
3 12 12Bring the root . 3 9 14 18
9 14
after 12 calls
starting tree
55
G G G G
P P P P P P
x x x x x x
1. zig 2. zig-zig 3. zig-zag 4. zag-zig 5. zag-zag 6. zag
56
Splay Woodworking
-When X is reached, one of 6 rotations is
executed:
- Single Rotation (X has P but not G)
- zig, zag
15
6
Zig-Right
6 18 15
3
3 12 12 18
15 6 3
Zig-Right Zig-Right
6 18 one 6
3 15
4 15
3 12 one 4 12 18
one 4 12 18
- The “zag” operation is the only rotation operation as in the AVL tree. For
6 15
Zag-Left
3 15 6 18
12 18 3 12
60
15 Zag-Left 20 Zag-Left 30
15 30
6 20 20 40
6 17 25 40 15 25
17 30
6 17
25 40
61
15 Zag-Left 15 Zig-Right 12
6 18 12 18 6 15
12 3 10 14 18
3 6 14
10 14 3 10
62
6 20 6 15 20
17
6 16 18 30
17 30 16 20
18 30
16 18
63
80
80 40
70 85
70 85 30 70
60 75 50 80
40 75
50 65 45 60 75 85
30 50
55 65
40 55 45 60
55 65
30 45 zig-zig
zig-zig
(a) (b) (NS)
64
40 40
60
30 70 30 60 40 70
50 70
50 80 30 50 65 80
45 60 75 85 45 55 65 80 85
45 55 75
55 65 75 85
zig-zag
zag
(a) (b) (NS)
65
- Splaying can be applied not only after Search but also after
value operations such as Add/Delete.
- Add X: Move X to root after X has been added to the leaf node (BST
operation).
- Delete X: Search X and move it to root. Delete the X in the root and move
the largest element in the left subtree or the smallest element in the right
root to the root.
- Find X: If X is not found, move the leaf node where the search
ended to the root.
66
Homework
Homework