0% found this document useful (0 votes)
4 views

Module 3-1

The document covers the basics of Binary Search Trees (BST) and AVL Trees, detailing their properties, insertion methods, and balancing techniques through rotations. It explains the concept of balance factors and provides examples of how to maintain balance during insertions in an AVL tree. Additionally, it briefly touches on sorting methods, including comparison counting and distribution counting.
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)
4 views

Module 3-1

The document covers the basics of Binary Search Trees (BST) and AVL Trees, detailing their properties, insertion methods, and balancing techniques through rotations. It explains the concept of balance factors and provides examples of how to maintain balance during insertions in an AVL tree. Additionally, it briefly touches on sorting methods, including comparison counting and distribution counting.
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/ 49

Module 3

Basics
Binary Search Tree
• Properties of Binary Search Tree:
• The left subtree of a node contains only nodes with keys
less than the node’s key.
• The right subtree of a node contains only nodes with keys
greater than the node’s key.
• The left and right subtree each must also be a binary
search tree.
• There must be no duplicate nodes(BST may have
duplicate values with different handling approaches).
insertion
AVL
• AVL Tree was designed by Adelson-Velski and Landis in
1962. Thus, AVL represents the first letter of each
originator’s first alphabet.
• AVL tree is a self-balancing binary search tree. It
balances the height of left subtree and right subtree.
• An AVL tree is called balanced when the difference of
height is -1, 0, 1. It is also called balance factor (BF).
• BALANCE FACTOR = Height of left subtree – Height
of right subtree
How to check the height
Every leaf has balance factor 0.
Beginning at 0 from the leaves, the count will increase to
the root.
• If the difference becomes more than 1 or less than -1,
then we have to perform rotation to balance the height of
subtrees.

• There are four rotations possible in the AVL tree.


• 1) Left (LL ) rotation
• 2) Right (RR) rotation
• 3) Left Right (LR) rotation
• 4) Right Left (RL) rotation
• (1) Left (LL) rotation
• In this type of rotation, the new node is inserted to the left
subtree of the left subtree.

• In this tree, node 1 has balance factor 2. As a result, the


left rotation of node 1 makes this tree a balanced tree.
• (2) Right (RR) rotation
• In this type of rotation, the new node is inserted to the
right subtree of the right subtree.

• In this tree, node 3 has balance factor 2. As a result, the


right rotation of node 3 makes this tree a balanced tree.
• (3) Left-Right (LR) rotation
• Left right rotation is difficult to understand, so you should
concentrate first on the left rotation and then on the right
rotation.
• Suppose, the new node inserted on the right subtree of
left subtree.
• In this tree, node 3 of the unbalanced tree has a balance
factor 2, but there is no left rotation or right rotation that
node 3 can perform.
• The binary search tree property must be preserved in the
AVL tree, and rotation of node 3 violates this.
• So, we perform node 1 and 2 rotation. After rotation, node
1 is leaf of the tree and node 2 is internal node of the tree.
• After that, we perform right rotation of node 3.
• (4) Right-Left (RL) rotation
• Right-left rotation is difficult to understand, so you should
concentrate first on the right rotation and then on the left
rotation.
• Suppose, the new node inserted on the left subtree of
right subtree.
• In this tree, node 1 of the unbalanced tree has a balance
factor 2, but there is no left rotation or right rotation that
node 1 can perform.
• The binary search tree property must be preserved in the
AVL tree, and rotation of node 1 violates this.
• So, we perform node 3 and 2 rotation. After rotation, node
3 is leaf of the tree, and node 2 is internal node of the tree.
• After that, we perform right rotation of node 1.
• Working
• Input is – {50, 12, 10, 72, 54, 95, 1, 52, 98}
• Insert 50

• AVL tree insertion 50

• The node 50 is inserted as a root in an empty tree.


Input is – {50, 12, 10, 72, 54, 95, 1, 52, 98}

• Insert 12
• AVL tree insert 12
• The 12 is less than 50 (12 < 50), So it will be the left child
of 50. 50, 12, 10, 72, 54, 95, 1, 52, 98

• 12 has balance factor 0.


• 50 has balance factor 1.
• So, Height is balanced.
Input is – {50, 12, 10, 72, 54, 95, 1, 52, 98}

insert 10
AVL tree insert 10
The 10 is less than 12 (10 < 12), So it will be the left child of
12.
12 has balance factor 1.
50 has balance factor = 2 – 0 = 2.
By looking at the tree, we do right rotation.
As a result of rotation, 12 is the root. 10 is left child and 50
is right child of 12.
Input is – {50, 12, 10, 72, 54, 95, 1, 52, 98}

• Insert 72
• AVL tree insert 72
• 72 is greater than 50 (50 < 72), So it will be the right child
of 50.
• 50 has balance factor -1.
• 12 has balance factor -1.
• Insert 54 Input is – {50, 12, 10, 72, 54, 95, 1, 52, 98}
• AVL tree insert 54
• 54 is less than 72 (54 < 72), So it will
be the left child of 72.
• 72 has balance factor 1.
• 50 has balance factor -2.
• We can overcome this problem by
rotating node 50 to the left. However,
54 is already in the left position. Also,
if 54 becomes the child with 50, we’ll
have to go through another rotation.
As a result, 54 and 72 will rotate to the
right on the first rotation, whereas 50
will revolve to the left. We rotate
(Right-Left).
• As a result of rotations, 54 is the
parent of 50 and 72.
• Insert 95 Input is – {50, 12, 10, 72, 54, 95, 1, 52, 98}

• 95 is greater than 72 (95 > 72), So it will be the right child


of 72.
• 72 has balance factor -1.
• 54 has balance factor -1.
• 12 has balance factor -2. So, we perform left rotation of
12.
• Insert 1, 52 Input is – {50, 12, 10, 72, 54, 95, 1, 52, 98}
• insert 1 and 52
• 1 is less than 10 (1 < 10), So 1 will be the left child of 10.
• 52 is greater than 50 (52 > 50), So 52 will be the right
child of 50.
• 12 has balance factor 0.
• 72 has balance factor -1.
• 54 has balance factor 1.
• Insert 98 Input is – {50, 12, 10, 72, 54, 95, 1, 52, 98}
• 98 is greater than 95 (98 > 95), So 98 will be the right child of 95.
• 95 has balance factor -1.
• 54 has balance factor 0.
• 72 has balance factor -2. So, we perform left rotation of node 72.
• As a result of rotation, 95 will be the parent of 72 and 98.
HEAP AND HEAPSORT
Sorting by counting
To sort the array 62, 31, 84, 96, 19, 47 using comparison counting,
we compare each element to all others, incrementing a counter for each
element that is smaller, and then use these counts to determine the sorted
position. The sorted array is 19, 31, 47, 62, 84, 96.

Original Array Count Array (after Sorted Position


comparisons)
62 0 —
31 1 —
84 0 —
96 0 —
19 0 —
47 0 —
•62: •96:
◦31 < 62: Count = 1 ◦62 < 96: Count = 1
◦19 < 62: Count = 2 ◦31 < 96: Count = 2
◦47 < 62: Count = 3 ◦84 < 96: Count = 3
◦47 < 96: Count = 4
•31: ◦19 < 96: Count = 5
◦19 < 31: Count = 1
•19:
•84: ◦(No elements are smaller than 19)
◦62 < 84: Count = 1
•47:
◦31 < 84: Count = 2
◦62 > 47: Count = 0
◦47 < 84: Count = 3
◦31 < 47: Count = 1
◦19 < 84: Count = 4
◦19 < 47: Count = 2
Original Array Count Array Sorted Position
62 3 4
31 1 2
84 4 5
96 5 6
19 0 1
47 2 3
Comparison Counting sort
Distribution counting

You might also like