0% found this document useful (0 votes)
6 views45 pages

Hafta.

The document covers AVL Trees and Splay Trees, focusing on their definitions, properties, and operations. AVL Trees are balanced binary search trees that maintain a height difference of at most 1 between subtrees, ensuring O(log2n) access time. Splay Trees allow for efficient access by moving recently accessed nodes closer to the root, optimizing future accesses despite potential O(n) worst-case scenarios.

Uploaded by

brkykybl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views45 pages

Hafta.

The document covers AVL Trees and Splay Trees, focusing on their definitions, properties, and operations. AVL Trees are balanced binary search trees that maintain a height difference of at most 1 between subtrees, ensuring O(log2n) access time. Splay Trees allow for efficient access by moving recently accessed nodes closer to the root, optimizing future accesses despite potential O(n) worst-case scenarios.

Uploaded by

brkykybl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

CSE201-DATA STRUCTURES

Special Trees

1
CONTENT OF THIS WEEK

 Adelson-Velskii-Landis (AVL) Trees


 Splay Trees

2
AVL TREES

3
MOTIVATION FOR AVL TREES

 Accessing a node in a BST takes O(log2n) in


average.
 But in the worst caseO(n)
 Is there a way to guarantee a worst-case access
time of O(log2n) per node or can we find a way to
guarantee a BST depth of O(log2n)?
 A: AVL Trees

4
DEFINITION

An AVL tree is a balanced binary search tree

Balance condition:
for each node in the BST, the height of left
and right sub-trees can differ by at most 1, or

hN L  hN R  1.
5
REMARKS ON BALANCE CONDITION

 Balance condition must be easy to maintain:


 This is the reason, for example, for the balance
condition’s not being as follows: the height of left and
right sub-trees of each node have the same height.
 It ensures the depth of the BST is O(log2n).
 The height information is stored as an additional
field in BTNodeType.

6
STRUCTURE OF AN AVL TREE

struct BTNodeType {
infoType *data;
unsigned int height;
struct BTNodeType *left;
struct BTNodeType *right;
}

7
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.

8
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.
 Animation
https://yongdanielliang.github.io/animation/web/AVLTree.html

9
CONSTRUCTING AN AVL TREE – ANIMATION

48

48

10
CONSTRUCTING AN AVL TREE – ANIMATION

48 16

48

16

11
CONSTRUCTING AN AVL TREE – ANIMATION

48 16 24

48

16

24

12
CONSTRUCTING AN AVL TREE – ANIMATION

48 16 24

48
1
-1
16 R/L
Dbl. Rot.
24

13
CONSTRUCTING AN AVL TREE – ANIMATION

48 16 24 20

24

16 48

20

14
CONSTRUCTING AN AVL TREE – ANIMATION

48 16 24 20 8

24

16 48

8 20

15
CONSTRUCTING AN AVL TREE – ANIMATION

48 16 24 20 8 12

24

16 48

8 20

12

16
CONSTRUCTING AN AVL TREE – ANIMATION

48 16 24 20 8 12

24
L/L 2
Sngl. Rot. 0
16 48
1 0
OK
8 20
-1 0
OK
12

17
CONSTRUCTING AN AVL TREE – ANIMATION

48 16 24 20 8 12 32

16

8 24

12 20 48

32

18
CONSTRUCTING AN AVL TREE – ANIMATION

48 16 24 20 8 12 32 54

16

8 24

12 20 48

32 54

19
CONSTRUCTING AN AVL TREE – ANIMATION

48 16 24 20 8 12 32 54 72

16

8 24

12 20 48

32 54

72

20
CONSTRUCTING AN AVL TREE – ANIMATION

48 16 24 20 8 12 32 54 72

16

8 24
0 2 R/R
Sngl. Rot.
12 20 48
0 1
OK
32 54
-1 0
OK
72

21
CONSTRUCTING AN AVL TREE – ANIMATION

48 16 24 20 8 12 32 54 72 18

16

8 48

12 24 54

20 32 72

18

22
CONSTRUCTING AN AVL TREE – ANIMATION

48 16 24 20 8 12 32 54 72 18

16
1 3

8 48
L/R 2 1
Dbl.Rot. 24 OK
12 54
1 0
OK
20 32 72
0 -1
OK
18

23
CONSTRUCTING AN AVL TREE – ANIMATION

48 16 24 20 8 12 32 54 72 18 96

24

16 48

8 20 32 54

12 18 72

96

24
CONSTRUCTING AN AVL TREE – ANIMATION

48 16 24 20 8 12 32 54 72 18 96

24

16 48

8 20 32 54
-1 1

12 18 72
R/R -1 0
Sngl. OK
Rot. 96

25
CONSTRUCTING AN AVL TREE – ANIMATION

48 16 24 20 8 12 32 54 72 18 96

24

16 48

8 20 32 54
-1 1

12 18 72
R/R -1 0
Sngl. OK
Rot. 96

26
CONSTRUCTING AN AVL TREE – ANIMATION

48 16 24 20 8 12 32 54 72 18 96 64 17 60 98 68 84 36 30

24

16 48

8 20 32 72

12 18 54 96

27
EXERCISE

 Insert 1,2,3,4,5,6,7 one by one to an initially


empty AVL tree.

28
EXERCISE

 Insert 10, 20, 30, 40, 25, 15, 60 one by one to an


initially empty AVL tree.

29
SPLAY TREES

30
MOTIVATION FOR SPLAY TREES

 We are looking for a data structure where, even though


some worst case (O(n)) accesses may be possible, m
consecutive tree operations starting from an empty tree
(inserts, finds and/or removals) take O(m*log2n).
 Here, the main idea is to assume that, O(n) accesses are
not bad as long as they occur relatively infrequently.
 Hence, we are looking for modifications of a BST per
tree operation that attempts to minimize O(n) accesses.

31
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).

32
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.

33
ZIG-ZAG CASE

G X

P D P Height 1 G

A X A B Height 2 C D

Height 3
B C

This is the same operation as an AVL double rotation in an R/L violation.

34
ZIG-ZIG CASE
LC(P): left child of node P
RC(P): right child of node P

G Height 1 X

P D A Height 2 P
as is

X Height 3 B G
C
LC(G)

A B Height 4 C D
as is LC(P)

35
ANIMATED EXAMPLE
10

2 11

1 4

3 9

5
Initial BST
8

36
ANIMATED EXAMPLE
10

2 11

1 4 Node with 6 accessed!


3 9

G 8

Zig-zig P 7
case
X 6

37
ANIMATED EXAMPLE

10

2 11

1 4 Node with 6 accessed!


3 G 9

P 5 Zig-zag
case
X 6

38
ANIMATED EXAMPLE

10

2 G Zig-zig 11
case
1 4 P Node with 6 accessed!
3 6 X

5 9

39
ANIMATED EXAMPLE
R
10
X
6 11

4 9

2 5 7

1 3 8

Node with 6 accessed!

40
ANIMATED EXAMPLE

4 10

2 5 9 11

1 3 7

Node with 6 accessed!

41
ANIMATED EXAMPLE

4 10

2 5 9 11

1 3 7

Delete 2 and show the result!

42
ANIMATED EXAMPLE

4 10

2 5 9 11

1 3 7

Delete 2 and show the result!


The maximum element in right subtree of 2 will be written in place of 2.
The parent of 2 must be splayed.
So splay 4!

43
EXERCISE

 Insert 9, 2, 90, 53, 4, 64, 95, 50, 35, 40 one by


one to an initially empty splay tree.

44
RECOMMENDATION

 AVL tree visualization


https://www.cs.usfca.edu/~galles/visualization/A
VLtree.html or
http://www.motleytech.net/balanced-binary-tree-
avl-tree-animation.html
 Splay tree visualization 
https://www.cs.usfca.edu/~galles/visualization/Sp
layTree.html

45

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy