08-BST and AVL Trees
08-BST and AVL Trees
2
What is a binary tree? (cont.)
Property 2: a unique path exists from the root to every
other node
3
Some terminology
❏The successor nodes of a node are called its children
❏The predecessor node of a node is called its parent
❏The "beginning" node is called the root (has no parent)
❏A node without children is called a leaf
4
Some terminology (cont’d)
❏Nodes are organized in levels (indexed from 0).
6
Why is h important?
❏Tree operations (e.g., insert, delete, retrieve etc.) are
typically expressed in terms of h.
7
8
How to search a binary tree?
(1) Start at the root
(2) Search the tree level
by level, until you find
the element you are
searching for or you reach
a leaf.
9
Binary Search Trees (BSTs)
❏ Binary Search Tree Property:
The value stored at
a node is greater than
the value stored at its
left child and less than
the value stored at its
right child
10
Binary Search Trees (BSTs)
❏In a BST, the value stored at the
root of a subtree is greater than
any value in its left subtree and
less than any value in its right
subtree!
11
Binary Search Trees (BSTs)
❏Where is the smallest element?
Ans: leftmost element
12
How to search a binary search tree?
(1) Start at the root
(2) Compare the value of the
item you are searching for
with the value stored at the
root
(3) If the values are equal,
then item found; otherwise, if
it is a leaf node, then not
found
13
How to search a binary search tree?
(4) If it is less than the value
stored at the root, then search
the left subtree
(5) If it is greater than the value
stored at the root, then search
the right subtree
(6) Repeat steps 2-6 for the
root of the subtree chosen in
the previous step 4 or 5
14
How to search a binary search tree?
15
Function Retrieve Item
16
Function Insert Item
Use the
binary
search tree
property to
insert the
new item at
the correct
place
17
Does the order of inserting elements into a tree matter?
Yes,
certain
orders
might
produce
very
unbalanc
ed trees!
18
Does the order of inserting elements into a
tree matter? (cont’d)
❏Unbalanced trees are not desirable because search time
increases!
❏Advanced tree structures, such as red-black trees,
guarantee balanced trees.
19
Function Delete Item
❏First, find the item; then, delete it
❏Binary search tree property must be preserved!!
❏We need to consider three different cases:
(1) Deleting a leaf
(2) Deleting a node with only one child
(3) Deleting a node with two children
20
Skewed BST
❏If a tree which is dominated by
left child node or right child
node, is said to be a Skewed
Binary Tree.
21
Limitation of BST
❏The average search time for a binary search tree is
directly proportional to its height: O(h). Most of the
operation average case time is O(log n).
❏BST’s are not guaranteed to be balanced. It may be
skewed tree also.
❏For skewed BST, the average search time becomes
O(n). So, it is working like an linear array.
❏To improve average search time and make BST
balanced, AVL trees are used.
22
AVL Tree
❏AVL tree is a height balanced tree.
❏It is a self-balancing binary search tree.
❏It was invented by Adelson-Velskii and Landis.
❏AVL trees have a faster retrieval.
❏It takes O(logn) time for insertion and deletion
operation.
❏In AVL tree, difference between heights of left and
right subtree cannot be more than one for all nodes.
23
AVL Tree
❏Balance Factor of node is:
❏ Height of left subtree – Height of Right subtree
❏Balance Factor is calculated for every node of AVL
tree.
❏At every node, height of left and right subtree can
differ by no more than 1.
❏For AVL tree, the possible values of balance factor
are -1, 0, 1
❏Balance Factor of leaf nodes is 0 (zero).
24
Example
Every AVL Tree is a binary search tree but all the
Binary Search Tree need not to be AVL trees.
25
Finding Balance Factor
26
Height of AVL Tree
❏By the definition of complete trees, any complete
binary search tree is an AVL tree
❏Thus, an upper bound on the number of nodes in an
AVL tree of height h a perfect binary tree with 2 h + 1 – 1
nodes.
❏What is a lower bound?
27
Height of AVL Tree
❏Let F(h) be the fewest number of nodes in a tree of
height h.
28
Height of AVL Tree
29
Imbalance
After an insertion, when the balance factor of node A
is –2 or 2, the node A is one of the following four
imbalance types
1.LL: new node is in the left subtree of the left subtree
of A
2.LR: new node is in the right subtree of the left
subtree of A
3.RR: new node is in the right subtree of the right
subtree of A
4.RL: new node is in the left subtree of the right
subtree of A
30
AVL Tree Example
❏Insert 14, 17, 11, 7, 53, 4, 13 into an empty AVL tree
31
AVL Tree Example
❏Insert 14, 17, 11, 7, 53, 4, 13 into an empty AVL tree
32
Types of Rotation
Rotation- To switch children and parents among two or
three adjacent nodes to restore balance of a tree.
33