0% found this document useful (0 votes)
19 views34 pages

Dsa24 9

The document discusses AVL trees, which are self-balancing binary search trees. It covers the concept of balance factors in AVL trees, causes of unbalance during insertion and deletion, and that rebalancing is needed to maintain balance after such operations.

Uploaded by

parmarnupur44
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)
19 views34 pages

Dsa24 9

The document discusses AVL trees, which are self-balancing binary search trees. It covers the concept of balance factors in AVL trees, causes of unbalance during insertion and deletion, and that rebalancing is needed to maintain balance after such operations.

Uploaded by

parmarnupur44
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/ 34

Lecture 9

AVL Trees
Review of tree concepts
§ Basic tree concepts: general tree Hard to implement

§ Binary tree Extension of linked list

§ Convert a general tree into a binary tree


§ 3 commonly used DF traversal strategies for binary
trees
Preorder, inorder and postorder

§ Binary Search Tree More than sorted

§ Complexity of operations in binary search trees


There is a potential O(log n) but no guarantee
3

Transforming a general tree to a binary tree


A

B
A
C

B C D E
F D

F G H G E

I H

I J K J

K
4

Binary Search Trees


n A binary search tree (BST) is a binary tree with its
nodes arranged in such way so as to maintain a
special ordering: the key of the element stored in the
root is greater than the keys of any elements in its
left subtree and less than or equal to the keys of any
elements in its right subtree; so are its subtrees.

A binary
search tree
³
5

Properties of BSTs: Min and Max


§ The smallest (largest) node: always at the left
(right) leaf of its left-subtree (right-subtree)
Type min(Node<Type> *p) { Type max(Node<Type> *p) {
if (p->llink != NULL) { if (p->rlink != NULL) {
return min(p->llink); return max(p->rlink);
} else { } else {
return p->data; return p->data;
} }
} }
6

Properties of BSTs: to order list


nBST traversal: the inorder traversal on a BST
produces an ordered list void inorder(Node<Type> *p) {
if (p != NULL) {
inorder(p->llink);
cout << p->data << " ";
inorder(p->rlink);
}
}
BST represents
an order array
In-order
traversal of
BST

12 18 20 23 35 44 52
7

Practice
Insert the numbers in the following sequence: 89, 27, 65, 34, 12, 90, 76, 38

89

27 90

12 65

34 76

38

Delete 65
8

Practice
Insert the numbers in the following sequence: 89, 27, 65, 34, 12, 90, 76, 38

89 89 89

27 90 27 90 27 90

12 65 12 38 12 76

34 76 34 76 34

38 38

Delete 65
9

Ideal computational complexity of BST

§ Insertion: O(log2n)
§ Deletion: O(log2n) Only if the tree is well
balanced.
§ Search: O(log2n)

Can you guarantee?

n 10 100 1000 10,000 100,000 1,000,000 1,000,000,000


log2n 3 7 10 13 17 20 30
10

Properties of BSTs: multiplicity


52 12

44 18

35 20

23 Search for 52 23
Search for 22
20 35

18 44

12 52
23
23
20 35
44
18 44 18

12 52 12 20 35 52

These are all BSTs with the exactly same data.


11

Balanced Trees
§ Problems of BST: the time complexity of
search, inserting and deleting with a BST can
vary between O(log2n) and O(n).
§ Unbalanced BST could cause the complexity to
be O(n) in the worst case, so we may get no
benefits (even worse for some operations) by
using BST compared to linear data structures.
§ The problem becomes how to make a BST to be
balanced.
12

Main content of this lecture


§ The concept of balanced BST
§ The concept of AVL tree
§ Balance factor of each node in an AVL tree
§ Causes of unbalance
§ Rebalancing an AVL tree
§ Insertion and deletion operations in an AVL tree
§ Implementation of an AVL tree
§ Complexity of AVL tree operations
13

Example: Unbalanced & Balanced Trees


Key Point
They contain the
Ordered array is
same set of data
unique but the
but in different ways
ways of ordering in
of linking data.
BST can be many.

Absolute
balance is
How to measure balance? impossible!
14

Measurement of balance with BST


HL: the height of left subtree
HR: the height of right
subtree

Balance factor: BF = HR - HL,


i.e. the height of right
subtree minus the height of
left subtree
15

How to make a BST to be balanced?


The main idea: keep a
BST to be balanced all the
time whenever adding a
node to or deleting a node
from the tree.

The method: rebalancing.

Rebalance a BST if adding or


deleting causes unbalance.
Rebalanced tree are still BST.
The data are still stored in the
same nodes. The only difference
is the data are linked in a
different way.
16

AVL Trees
§ An AVL tree (or height-balanced BST) is a
binary search tree such that
§ The heights of the left and right subtrees of the root
differ by at most one, i.e. |BF| =|HR-HL| £ 1.
§ each subtree of the tree is an AVL tree.
§ AVL tree is named after G. M. Adelson-Velskii
and E. M. Landis.

An AVL tree is “nearly balanced”


Even though is called balanced
17

Balance factor in an AVL tree

Any node in an AVL tree can only be:


• Left Higher (LH) if BF=-1 (HL>HR)
• Right higher (RH) if BF=1 (HL<HR)
• Even high (EH) if BF=0 (HL=HR)
18

Causes of unbalance
§ A (nearly) balanced tree can become unbalanced due to
insertion and deletion operations on the tree:
§ Insert a node to the left subtree of a LH tree
§ Insert a node to the right subtree of a RH tree

Left of left Right of left Right of right Left of right

insert a node
19

Causes of unbalance
§ Delete a node from the left subtree of a RH tree
§ Delete a node from the right subtree of a LH tree
Left of left Right of left Right of right Left of right

delete a node
20

Cases of unbalanced BST


§ All unbalanced trees are caused by one of these
four cases if they were balanced before an
insertion or a deletion:
§ Case 1: left of left
§ Case 2: right of right
§ Case 3: right of left
§ Case 4: left of right
alancing
Reb
21

Unbalanced Trees: Case 1 &2


Case 1: left of left – left-higher subtree on a left-higher tree

Case 2: right of right – right-higher subtree on a right-higher tree

Check from the new inserted node upward


until find unbalanced node or the root
22

Unbalanced Trees: Case 3 & 4


Case 3: right of left – right-higher subtree on a left-higher tree

Case 4: left of right – left-higher subtree on a right-higher tree

Check from the new inserted node and upper


23

Rebalancing a tree by rotating nodes


n An unbalanced tree can be rebalanced by rotating the unbalanced
node based on the causes:
Ø Left of left: a single right rotation at the the unbalanced node
Ø Right of right: a single left rotation at the the unbalanced node
Ø Right of left: double rotation - left rotate the left subtree of the
unbalanced node and then right rotate the tree of the node
Ø Left of right: double rotation - right rotate the right subtree of the
unbalanced node and then left rotate the tree of the node

For each new insertion or deletion, you just


need to rebalance once.
24

Single Rotation (right) for left of left


18 20

Node* rightRotate (Node* root)


{
tempPtr = root->left;
root->left = tempPtr->right;
tempPtr->right = root;
return tempPtr;
}
12 18

Node* rightRotate (Node* root)


{
tempPtr = root->left;
root->left = tempPtr->right;
tempPtr->right = root;
return tempPtr;
}
25

Single Rotation (left) for right of right


18 12

Node* leftRotate (Node* root) {


tempPtr = root->right;
root->right = tempPtr-> left;
tempPtr->left = root;
return tempPtr;
}
20 14

Node* leftRotate (Node* root) {


tempPtr = root->right;
root->right = tempPtr-> left;
tempPtr->left = root;
return tempPtr;
}
26

Double Rotation (right of left)


Unbalanced node

(a). Do left rotation between 4 and 8 on


subtree and then do right rotation
between 12 and 8.
Unbalanced node

(b). Do left rotation between 12 and 14 on


subtree and then do right rotation between
18 and 14.
Unbalanced node

Node* leftBalance (Node* root) {


leftTree = root->left;
root->left = leftRotate(leftTree);
return rightRotate(root);
}
27

Double Rotation (left of right)


(a). Do right rotation between 44 and 18 on
subtree and then do left rotation between 12
and 18.

(b). Do right rotation between 44 and 23 on


subtree and then do left rotation between 18
and 23.

Node* rightBalance (Node* root) {


rightTree = root->right;
root->right = rightRotate(rightTree);
return leftRotate(root);
}
28

Insertion: insert a node to an AVL tree


§ Firstly, search the tree and find the place where the new item is to
be inserted.
§ If the key of the item exists in the AVL tree, insertion fails;
otherwise,
§ search must end at an empty subtree; insert the item there (new
item becomes a leaf).
§ After inserting the new item in the tree
§ Recalculating balance factor by tracing up from the new inserted
node until either find a node that is unbalanced or reach the tree
root.
§ If an unbalance is found, rebalance the tree/subtree of that node
by calling rotation functions
§ Obviously only the branch that contains the new item can be
affected.
29

Insertion Only the affected subtree


can be rebalanced

RH RH
RH

RH EH RH
EH
EH EH Right of right
EH RH
EH
Turn left
EH
EH

AVL tree before and after inserting 90


RH RH RH

EH RH EH RH EH EH

Left of right
EH EH EH
LH
Turn right
EH then turn left

AVL tree before and after inserting 75


30

Insertion
RH EH
RH

EH EH
EH RH
EH RH

EH EH LH EH LH EH RH
Right of right
EH
EH EH LH RH
Turn left
EH EH EH
EH EH EH EH
EH EH
RH

EH

AVL tree before and after inserting 95

You only have to make rotations whenever you find


a node on which the balance criteria is violated.
31

Deletion from AVL Trees


§ Four cases
§ Case 1: The node to be deleted is a leaf
§ Case 2: The node to be deleted has no right child
§ Case 3: The node to be deleted has no left child
§ Case 4: The node to be deleted has a left child and a
right child (much harder to deal with)

https://www.cs.usfca.edu/~galles/visualization/AVLtree.html
32

AVL tree ADT implementation

§ AVL node

template <class TYPE>


struct NODE { bal
TYPE data; data
NODE *left;
NODE *right;
int bal;
};

Read Code AVL_ADT.h


33

Complexity of AVL tree operations


§ If an AVL tree has n nodes, the search, insertion,
deletion take O(log n) time. Thus, an AVL tree provides
high efficiency for all the common data-storage
operations.

Operations Unordered Ordered Unordered AVL Tree


array array Linked list
Search O(n) O(log n) O(n) O(log n)
Insert O(1) O(n) O(1) O(log n)
Delete O(n) O(n) O(n) O(log n)

n 10 100 1000 10,000 100,000 1,000,000 1,000,000,000


log2n 3 7 10 13 17 20 30
34

Readings
§ Read textbook Chapter 11
§ Read the code provided for you:
§ avlTree.h is easy to read but no deletion so can’t be
fully used for any applications.
§ AVL_ADT.h is a full implementation of AVL tree
structure.
§ I will explain the code for binary tree, BST, AVL tree at
PASS meeting next Monday.

You might also like