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

Tree

Uploaded by

samarth
Copyright
© © All Rights Reserved
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Tree

Uploaded by

samarth
Copyright
© © All Rights Reserved
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
You are on page 1/ 73

Data Structures and Algorithms

Tree

Prepared By : Vaishali Koria Data Structure and Algorithms


Introduction
• It is non-linear data structure.
• A tree represents hierarchy organization
structure of corporation.
• Unlike Arrays, Linked Lists, Stack and
queues, which are linear data structures,
trees are hierarchical data structures.

Prepared By : Vaishali Koria Data Structure and Algorithms


Tree Terminologies
• A is the root node ( Does not have any
parent)
• B is parent of D & E.
• A is ancestor of D & E.
• D & E are descendants of A.
• C is sibling of B.
• Sibling : if n1 and n2 are left and right child
of n, than n1 and n2 are said to be sibling.
(nodes with same parent node)
• D & E are children of B.
• D,E,F,G,I are leaves.

Prepared By : Vaishali Koria Data Structure and Algorithms


Tree Terminologies
• Level/Depth  Number of generations from the root, root node at level 0
• The level of a node is defined recursively defined.
• If node n is the root of tree T, its level is 0
• If node n is not the root of tree T, its level is 1 + the level of its parent

Level 0
A

Level 1
B C

D
Level 2

Prepared By : Vaishali Koria Data Structure and Algorithms


Tree Terminologies
Height of tree –The height of a tree is the maximum number of levels of the tree
(starting from root).
Height of node –The height of a node is the maximum number of levels of the
tree (starting from the root to that node).

A
The height of this tree
B C is 2

Degree of a node: number of children of that node. e.g: Degree of A is 2 and Degree of
B is 1.

Prepared By : Vaishali Koria Data Structure and Algorithms


Tree Terminologies
level 0
level 1

level 2

level 3

The Depth (Level) of E is ______


The Height of the tree is _______
The Degree of node B is _______

Prepared By : Vaishali Koria Data Structure and Algorithms


Tree Types
Complete Binary Tree: Binary tree in which every level, except possibly the last, is
completely filled, and all nodes are as far left as possible.

Full Binary Tree: A full binary tree (sometimes proper binary tree or 2-tree) is a
tree in which every node other than the leaves has two children

Prepared By : Vaishali Koria Data Structure and Algorithms


Tree Types
Strict Binary Tree: A binary tree in which every node has either two or zero
number of children is called Strictly Binary Tree. Strictly binary tree is also called
as Full Binary Tree or Proper Binary Tree or 2-Tree.

Perfect Binary Tree: Binary tree in which all interior nodes have two children and
all leaves have the same depth or same level.

Prepared By : Vaishali Koria Data Structure and Algorithms


Tree Types

Prepared By : Vaishali Koria Data Structure and Algorithms


Tree Terminologies
Maximum number of nodes in a binary tree with height h is _______________

Answer : 2(h+1) -1

Prepared By : Vaishali Koria Data Structure and Algorithms


Tree Terminologies
If leaf nodes are n, then number of nodes with degree 2 is ________.

Answer : n -1

Prepared By : Vaishali Koria Data Structure and Algorithms


Sequential representation of Tree
A
IF the index of any node is i, then
Index of left child is : 2*i+1
B C Index of Right child is : 2*i+2

D
E F
Index of H is 14… what is the index of
its parent?
G H

Sequential Representation of the Tree:

Maximum size of the array to store tree is : 1(level 0)+ 2(level 1) + 4 (level 2) + 8 (level 3) = 15

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
A B C D E - F G - - - - - - H

Prepared By : Vaishali Koria Data Structure and Algorithms


Linked Representation of Tree
1000 Suppose root A is stored at memory address 1000. This can be
A represented using this linked list structure.

1010 1010 A 1016


1016 1000
B C 1010
1016
1021 B 1030 NULL C 1036
1021 1036
D 1021
E F 1036
1040 D NULL NULL E NULL
NULL F 1050
1030
G H 1030

1040 NULL G NULL


1050
1040 NULL H NULL
1050

Prepared By : Vaishali Koria Data Structure and Algorithms


Code
How to code to create different nodes?
1010 A 1016

How to code for this node struct node *create()


structure? {
struct node *p;
char x;
struct node printf("Enter data(-1 for no node):");
{ flushall();
char data; // mid field scanf("%c",&x);
struct node *lptr;
struct node *rptr; if(x=='n')
}; return NULL;
void main() p=(struct node*)malloc(sizeof(struct node));
{ p->data=x;
struct node *root =(struct node*)malloc(sizeof(struct node)); printf("Enter left child of %c:\n",x);
clrscr(); p->lptr=create();
root=create(); printf("Enter right child of %c:\n",x);
preorder(root); p->rptr=create();
} return p;
}

Prepared By : Vaishali Koria Data Structure and Algorithms


Code
struct node
void preorder(struct node *t)
{
{
char data; // mid field
if(t!=NULL)
struct node *lptr;
{
struct node *rptr;
printf(" %c",t->data);
};
preorder(t->lptr);
void main() preorder(t->rptr);
{ }
struct node *root =(struct node*)malloc(sizeof(struct node)); }
clrscr();
root=create();
preorder(root);
}
struct node *create()
{
struct node *p;
char x;
printf("Enter data(-1 for no node):");
flushall();
scanf("%c",&x);
if(x=='n’)
return NULL;
p=(struct node*)malloc(sizeof(struct node));
p->data=x;
printf("Enter left child of %c:\n",x);
p->lptr=create();
printf("Enter right child of %c:\n",x);
p->rptr=create();
return p;
}

Prepared By : Vaishali Koria Data Structure and Algorithms


Traversals
Pre-Order Traversal:

Root Left Right

Prepared By : Vaishali Koria Data Structure and Algorithms


Traversals
In-Order Traversal:

Left Root Right

Prepared By : Vaishali Koria Data Structure and Algorithms


Traversals
Post-Order Traversal:

Left Right Root

Prepared By : Vaishali Koria Data Structure and Algorithms


Example 1:
Preorder Traversal : a b c d f g e
Inorder Traversal : c b f d g a e

Draw the tree and find the postorder traversal.

Prepared By : Vaishali Koria Data Structure and Algorithms


Example 2:
Postorder Traversal : g h d i e b j f c a
Inorder Traversal : g d h b e i a f j c

Draw the tree and find the preorder traversal.

Prepared By : Vaishali Koria Data Structure and Algorithms


Traversals
void preorder(node *t) void postorder(node *t) void Inorder(node *t)
{ { {
if(t!=NULL) if(t!=NULL) if(t!=NULL)
{ { {
printf(" %d",t->data); postorder(t->left); Inorder(t->left);
preorder(t->left); postorder(t->right); printf(" %d",t->data);
preorder(t->right); printf(" %d",t->data); Inorder(t->right);
}
} } }
} }

Prepared By : Vaishali Koria Data Structure and Algorithms


Pre order Traversals
void preorder(node *t) Preorder(1000) Preorder(1010)
{
1000!=NULL ✔ 1010!=NULL ✔
if(t!=NULL)
{ Printf(‘A’) Printf(‘B’)
printf(" %d",t->data); Preorder(1010) Preorder(1021)
preorder(t->left);
Preorder(1016) Preorder(1030)
preorder(t->right);
}
} Preorder(1021)
Preorder(1016) Preorder(1030)
1021!=NULL ✔
1016!=NULL ✔ 1030!=NULL ✔
Printf(‘D’)
Printf(‘C’) Printf(‘E’) A B D G E C F H
Preorder(1040)
Preorder(NULL) Preorder(NULL)
Preorder(NULL)
Preorder(1036) Preorder(NULL)
return Preorder(1040)
Preorder(1036) 1040!=NULL ✔
Preorder(1050) return
1036!=NULL ✔ Printf(‘G’)
1050!=NULL ✔
Printf(‘F’) return
Printf(‘H’) Preorder(NULL)
Preorder(NULL)
Preorder(NULL) return
Preorder(NULL)
Preorder(1050)
Preorder(NULL)
Prepared By : Vaishali Koria Data Structure and Algorithms
Inorder Traversals
void Inorder(node *t) Inorder(1000)
{
if(t!=NULL)
{
Inorder(t->left);
printf(" %d",t->data);
Inorder(t->right);
}
}

) )

return
return

return

return

Prepared By : Vaishali Koria Data Structure and Algorithms


Postorder Traversals
void preorder(node *t) Postorder(1000)
{
if(t!=NULL)
{
postorder(t->left);
postorder(t->right);
printf(" %d",t->data);
}
}

return
return

return

return

Prepared By : Vaishali Koria Data Structure and Algorithms


Binary Search Tree (BST)
• What is the difference between Binary Tree and Binary Search Tree?

Binary tree: Tree where each Binary search tree:


node has up to two leaves. Used for searching. A binary tree where the left
child contains only nodes with values less than the
parent node…
1 and where the right child only contains nodes with
values greater than or equal to the parent.

3 2
2

3
1

Prepared By : Vaishali Koria Data Structure and Algorithms


Search From BST
Search for 67
67 > 50 – go to right
67 < 76 – go to left
67 > 54 – go to right
67 < 72 – go to left
67 = 67 – found the element

Search for 12

Ref: https://en.wikipedia.org/wiki/Self-balancing_binary_search_tree

Prepared By : Vaishali Koria Data Structure and Algorithms


Search From BST
struct node* search(struct node* root, int key)
{
if (root == NULL || root->key == key)
return root;

// Key is greater than root's key


if (root->key < key)
return search(root->right, key);

// Key is smaller than root's key


return search(root->left, key);
}

Prepared By : Vaishali Koria Data Structure and Algorithms


GATE example
Suppose the numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are inserted in that order
into an initially empty binary search tree. The binary search tree uses the
usual ordering on natural numbers. What is the inorder transversal
sequence of the resultant tree ?

(A) 7 5 1 0 3 2 4 6 8 9
(B) 0 2 4 3 1 6 5 9 8 7
(C) 0 1 2 3 4 5 6 7 8 9
(D) 9 8 6 4 2 3 0 1 5 7

Prepared By : Vaishali Koria Data Structure and Algorithms


Insert into Binary Search Tree
Create Binary Search Tree: int main()
{
50 struct node *root = NULL;
root = insert(root, 50);
insert(root, 30);
30 70 insert(root, 20);
insert(root, 40);
insert(root, 70);
20 40 60 80 insert(root, 60);
insert(root, 80);

inorder(root);

return 0;
}

Prepared By : Vaishali Koria Data Structure and Algorithms


Insert into Binary Search Tree
struct node* insert(struct node* node, int key)
#include<stdio.h>
{
struct node
if (node == NULL)
{
return newNode(key);
int key;
struct node *left, *right;
if (key < node->key)
};
struct node *newNode(int item)
node->left = insert(node->left, key);
{
struct node *temp = (struct node*) malloc
else if (key > node->key)
(sizeof(struct node));
node->right = insert(node->right, key);
temp->key = item;
temp->left = temp->right = NULL;
return node;
return temp;
}
}

Prepared By : Vaishali Koria Data Structure and Algorithms


Create BST from keys: 50,30,60
Initially root = NULL Call function to insert 30
Insert(root,30)
Call function to insert 50
root= Insert(root,50) So, now root=100 Insert(node=root=100, key=30) Insert(node=NULL, key=30)

Insert(node=root=NULL, key=50) If(node==NULL) no… False If(node==NULL) Yes… true


If(node==NULL) Yes… true If(key(30)< node->key (100->key) 50…. Temp=newNode(30)
Temp=newNode(50) True
Suppose 200 is the address of
Suppose 100 is the address of
node->left= Insert(node->left,30) the new node.
the new node.
NUL 50 NUL node->left= Insert(NULL,30) NUL 30 NUL
L L L L

Temp=node=100 200 50 NUL Temp=node=200


L
Return node(100) Return node(200)
NUL 30 NUL
L L

Prepared By : Vaishali Koria Data Structure and Algorithms


Create BST from keys: 50,30,60
Initially root = 100

Call function to insert 60


root= Insert(root,60) Insert(node=root=NULL, key=60)
200 50 300
Insert(node=root=100, key=60) If(node==NULL) Yes… true

If(node==NULL) no… False Temp=newNode(60)


NUL 30 NUL NUL 60 NUL
If(key(60)< node->key (100->key) 50 false Suppose 300 is the address of L L L L
the new node.
If(key(60)> node->key (100->key) 50 True
NUL 60 NUL
node->right= Insert(node->right,60) L L
Temp=node=300
Return node(300)
So, 100->right= 300

Prepared By : Vaishali Koria Data Structure and Algorithms


Trace algorithm to insert 28

Prepared By : Vaishali Koria Data Structure and Algorithms


GATE example
Suppose the numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are inserted in that order
into an initially empty binary search tree. The binary search tree uses the
usual ordering on natural numbers. What is the inorder transversal
sequence of the resultant tree ?

(A) 7 5 1 0 3 2 4 6 8 9
(B) 0 2 4 3 1 6 5 9 8 7
(C) 0 1 2 3 4 5 6 7 8 9
(D) 9 8 6 4 2 3 0 1 5 7

Prepared By : Vaishali Koria Data Structure and Algorithms


Delete the node from BST

Prepared By : Vaishali Koria Data Structure and Algorithms


Delete the node from BST

Prepared By : Vaishali Koria Data Structure and Algorithms


Delete the node from BST

Prepared By : Vaishali Koria Data Structure and Algorithms


Question:
What is the worst case time complexity for search, insert and delete operations
in a general Binary Search Tree?

(A) O(n) for all


(B) O(Logn) for all

Prepared By : Vaishali Koria Data Structure and Algorithms


Find Minimum Key Node
struct node * minValueNode(struct node* node)
{
struct node* current = node;

/* loop down to find the leftmost leaf */


while (current->left != NULL)
current = current->left;

return current;
}

Prepared By : Vaishali Koria Data Structure and Algorithms


Delete from BST
struct node* deleteNode(struct node* root, int key) else if (root->right == NULL)
{ {
if (root == NULL) return root; struct node *temp = root->left;
free(root);
if (key < root->key) return temp;
root->left = deleteNode(root->left, key); }
struct node* temp = minValueNode(root->right);
else if (key > root->key)
root->right = deleteNode(root->right, key); root->key = temp->key;
else
{ root->right = deleteNode(root->right, temp->key);
if (root->left == NULL) }
{ return root;
struct node *temp = root->right; }
free(root);
return temp;
}

Prepared By : Vaishali Koria Data Structure and Algorithms


Delete 50 from BST:
200 50 300 Root=100 1. deleteNode(100,50) 2. deleteNode(300,60)
100!=NULL 300!=NULL

200 400 30 500 600 70 700 300 50<50 No 60<70 yes


50>50 No root->left = deleteNode(root->left,
key);
100->left == 200 not null
null 20 null null 40 null null 60 null null 80 null 300->left = deleteNode(600,60)
100->right == 300 not null
Return root;
400 500 600 700 Temp = 600
struct node* deleteNode(struct node* root, int key) 100->key = 60
{
if (root == NULL) return root; 100->right = deleteNode(300,60)
3. deleteNode(600,60)
if (key < root->key) Return root;
root->left = deleteNode(root->left, key); 600!=NULL
else if (key > root->key) 60<60 No
root->right = deleteNode(root->right, key);
else 60>60 No
{
if (root->left == NULL) 600->left == null yes. null
{
struct node *temp = root->right; Temp = NULL (600->right)
free(root);
return temp; Return temp;
}
else if (root->right == NULL)
{
struct node *temp = root->left;
free(root);
return temp;
}
struct node* temp = minValueNode(root->right);

root->key = temp->key;

root->right = deleteNode(root->right, temp->key);


}
return root;
} Prepared By : Vaishali Koria Data Structure and Algorithms
Delete 20 from BST:
200 35 300 Root=100 1. deleteNode(100,20) 2. deleteNode(200,20)
100!=NULL 200!=NULL

400 20 500 20<35 yes 20<20 no


200 null 42 null 300
100->left = deleteNode(200,20) 20>20 No
Return root; 200->left == null no
null 16 null 600 29 700 500 200->right == null no

400 struct node* deleteNode(struct node* root, int key)


Temp = 600
{
if (root == NULL) return root; 200->key = 24
null 24 800 null 33 null
if (key < root->key) 200->right = deleteNode(500,24)
600 700 root->left = deleteNode(root->left, key);
Return root;
else if (key > root->key)
null 27 null root->right = deleteNode(root->right, key);
else
{
800 if (root->left == NULL)
4. deleteNode(600,24) 3. deleteNode(500,24)
{
struct node *temp = root->right; 600!=NULL 500!=NULL
free(root);
return temp; 24<24 no 24<29 yes
}
else if (root->right == NULL) 500->left = deleteNode(600,24)
24>24 No
{
struct node *temp = root->left; Return root;
free(root); 600->left == null yes
return temp;
} Temp= 800
struct node* temp = minValueNode(root->right);
Return 800;
root->key = temp->key;

root->right = deleteNode(root->right, temp->key);


}
return root;
}

Prepared By : Vaishali Koria Data Structure and Algorithms


Gate 1996
A binary search tree is generated by inserting in order the
following integers:
50, 15, 62, 5, 20, 58, 91, 3, 8, 37, 60, 24 The number of
nodes in the left subtree and right subtree of the root
respectively is

(A) (4, 7) (B) (7, 4) (C) (8, 3) (D) (3, 8)

Answer: (B)

Note: In order Traversal of BST gives us elements in sorted form.

Prepared By : Vaishali Koria Data Structure and Algorithms


GATE 2005
17. A binary search tree contains the numbers 1, 2, 3, 4, 5, 6, 7, 8. When the tree is
traversed in pre-order and the values in each node printed out, the sequence of values
obtained is 5, 3, 1, 2, 4, 6, 8, 7. If the tree is traversed in post-order, the sequence
obtained would be

(A) 8, 7, 6, 5, 4, 3, 2, 1
(B) 1, 2, 3, 4, 8, 7, 6, 5
(C) 2, 1, 4, 3, 6, 7, 8,5

(D) 2, 1, 4, 3, 7, 8, 6, 5

Answer: (D)

Prepared By : Vaishali Koria Data Structure and Algorithms


Question:
Postorder traversal of a given binary search tree T
produces the following sequence of keys 10, 9, 23, 22, 27,
25, 15, 50, 95, 60, 40, 29.

Which one of the following sequences of keys can be the


result of an inorder traversal of the tree T?
(A) 9, 10, 15, 22, 23, 25, 27, 29, 40, 50, 60, 95
(B) 9, 10, 15, 22, 40, 50, 60, 95, 23, 25, 27, 29
(C) 29, 15, 9, 10, 25, 22, 23, 27, 40, 60, 50, 95
(D) 95, 50, 60, 40, 27, 23, 22, 25, 10, 9, 15, 29
Answer : (A)

Prepared By : Vaishali Koria Data Structure and Algorithms


GATE 2006
Suppose that we have numbers between 1 and 100 in a
binary search tree and want to search for the number
55. Which of the following sequences CANNOT be the
sequence of nodes examined?

(A) {10, 75, 64, 43, 60, 57, 55} (B) {90, 12, 68, 34, 62, 45,
55}

(C) {9, 85, 47, 68, 43, 57, 55} (D) {79, 14, 72, 56, 16, 53,
55}

Answer: (C)

Prepared By : Vaishali Koria Data Structure and Algorithms


GATE 2008
The following three are known to be the preorder, inorder and
postorder sequences of a binary tree. But it is not known which is
which.
MBCAFHPYK
KAMCBYPFH
MABCKYFPH

Pick the true statement from the following.

(A) I and II are preorder and inorder sequences, respectively


(B) I and III are preorder and postorder sequences, respectively
(C) II is the inorder sequence, but nothing more can be said about the
other two sequences.
(D) II and III are the preorder and inorder sequences, respectively

Answer: (D)

Prepared By : Vaishali Koria Data Structure and Algorithms


GATE 2008
A Binary Search Tree (BST) stores values in the range 37 to 573. Consider
the following sequence of keys.

I. 81, 537, 102, 439, 285, 376, 305

II. 52, 97, 121, 195, 242, 381, 472

III. 142, 248, 520, 386, 345, 270, 307

IV. 550, 149, 507, 395, 463, 402, 270


Suppose the BST has been unsuccessfully searched for key 273. Which all
of the above sequences list nodes in the order in which we could have
encountered them in the search?

(A) II and III only (B) I and III only

(C) III and IV only (D) III only

Answer: (D)
Prepared By : Vaishali Koria Data Structure and Algorithms
GATE 2008
A Binary Search Tree (BST) stores values in the range 37 to 573.
Consider the following sequence of keys.
I. 81, 537, 102, 439, 285, 376, 305 II. 52, 97, 121, 195, 242,
381, 472
III. 142, 248, 520, 386, 345, 270, 307 IV. 550, 149, 507, 395,
463, 402, 270
Which of the following statements is TRUE?

(A) I, II and IV are inorder sequences of three different BSTs


(B) I is a preorder sequence of some BST with 439 as the root
(C) II is an inorder sequence of some BST where 121 is the root
and 52 is a leaf
(D) IV is a postorder sequence of some BST with 149 as the root
Answer: (C)
Prepared By : Vaishali Koria Data Structure and Algorithms
BST limitations
• Most of the BST operations (e.g., search, max, min,
insert, delete.. etc) take O(h) time where h is the
height of the BST.
• The cost of these operations may become O(n) for a
skewed Binary tree.
• If we make sure that height of the tree remains
O(Logn) after every insertion and deletion, then we
can guarantee an upper bound of O(Logn) for all
these operations.
• The height of an AVL tree is always O(Logn) where n
is the number of nodes in the tree

Prepared By : Vaishali Koria Data Structure and Algorithms


AVL Tree
AVL Tree is an extension of Balanced Search Tree(BST) with
an additional feature of balanced condition.

It was introduced by Adelson, Velski and Landis in 1962


and known as AVL Tree.

Prepared By : Vaishali Koria Data Structure and Algorithms


AVL Tree : Insert 50,30,60,24,67,22
50 Find the balance factor of each node.
Balance factor of node with key 50…
height of left subtree- height of right subtree = 3-2 = 1
30 60 Balance factor of node with key 60… 0 – 1= -1

Balance factor of node with key 67… 0 – 0= 0


24 67 Balance factor of node with key 24… 1 – 0= 1

Balance factor of node with key 22… 0 – 0= 0


22
Balance factor of node with key 30… 2 – 0= 2

Balance factor of every node of AVL tree can be 1,0,or -1.

Prepared By : Vaishali Koria Data Structure and Algorithms


Height of an AVL Tree
Minimum no of nodes in an AVL tree with height h.

h=0 Minimum no of nodes is 1. Minimum no of nodes is with height 3


is: N(2)+1+N(1) = 4 + 1+ 2 = 7

h=1 Minimum no of nodes is 2.


Minimum no of nodes is with height h
is: N(h-1) + 1 + N(h-2)
h=2
Minimum no of nodes is 4.

Minimum no of nodes is 7.
h=3

Prepared By : Vaishali Koria Data Structure and Algorithms


AVL Tree : Insert 50,30,60,24,67,22
50 This tree needs to be rotated to make it height balanced or
AVL tree.

30 60

24 67

22

Prepared By : Vaishali Koria Data Structure and Algorithms


Insertion in an AVL Tree
• Insertion is simple and same as in BST but it is done by keeping in mind the balanced
factor.

• If balanced factor is affected after doing insertion, we have to perform some rotations
to restore the balanced factor of node.

• In order to balance the tree, there are 4 cases of rotations such as -


LL Rotation : RR Rotation :

Prepared By : Vaishali Koria Data Structure and Algorithms


Insertion in an AVL Tree
LR Rotation :

Inserted node J is in the right subtree Y of left subtree of node X.

Prepared By : Vaishali Koria Data Structure and Algorithms


Insertion in an AVL Tree
RL Rotation :

Inserted node J is in the left subtree Y of right subtree of node X.

0
J

X Y

0 0

Prepared By : Vaishali Koria Data Structure and Algorithms


Example of an AVL Tree
Construct a AVL tree using elements 64, 1, 14, 26, 13, 110, 98, 85.
2
01 64 2
64

RR Rotation
-1 1 14 1

Which rotation is required?


14 LR 1 0
0

14 LL rotation

1 64

Prepared By : Vaishali Koria Data Structure and Algorithms


Example of an AVL Tree
Construct a AVL tree using elements 64, 1, 14, 26, 13, 110, 98, 85.

14 1-2= -1 1-2= -1
14
14 Insert 26
Insert 13

1 64 1 1 64 1
1 64
0 -
1
26 13 26
0
0 0

Prepared By : Vaishali Koria Data Structure and Algorithms


Example of an AVL Tree
Construct a AVL tree using elements 64, 1, 14, 26, 13, 110, 98, 85.

14 0
1-2= -1 14 2-3= -1
14

Insert 110 Insert 98


1 64 0
1 64 1 1 64 -
1
-
- -
1
1 1
13 26 110
13 26 13 26 110
0 1
0 0 0
0 0 0

98

Prepared By : Vaishali Koria Data Structure and Algorithms


Example of an AVL Tree
Construct a AVL tree using elements 64, 1, 14, 26, 13, 110, 98, 85.

14 2-3= -1 14 2-4= -2
Which rotations are
required?
Insert 85

1 64 - 1 64 -2
1
- - Balance factor of 110 is 2,
1 1
LL rotation is required.
13 26 110 13 26 110
1 2
0 0 0 0

98 98 1

0
85
0

Prepared By : Vaishali Koria Data Structure and Algorithms


Example of an AVL Tree
Construct an AVL tree using elements 64, 1, 14, 26, 13, 110, 98, 85.
2-4= -2 14 2-3= -1
14

1 64 -2 1 64 -1

-1 -1

13 26 110 13 26 98
2 0
0 0 0 0

98 1 85 110
LL rotation

0 0
85
0

Prepared By : Vaishali Koria Data Structure and Algorithms


Que:

Prepared By : Vaishali Koria Data Structure and Algorithms


Insert 14,17,11,7,53,4,13

Prepared By : Vaishali Koria Data Structure and Algorithms


Deletion from an AVL Tree
Deletion from an AVL tree is same as BST but we need to check for height imbalance
after deletion.
Consider the following cases
: Consider deleting B from the tree:
CASE 1:

As we have deleted from left tree and


-2
-1 balance factor of C is -1, this is L-1
A rotation.
L-1 = RR
0
-1 A
C
B C

0 C
A D
D

Prepared By : Vaishali Koria Data Structure and Algorithms


Deletion from an AVL Tree
Deletion from an AVL tree is same as BST but we need to check for height imbalance
after deletion.
Consider the following cases
: Consider deleting B from the tree:
CASE 2:

As we have deleted from left tree and


-2
-1 balance factor of C is 1, this is L1
A rotation.
L1 = RL
0
A A
1 D
B C

C D
A C
0
D

D C

Prepared By : Vaishali Koria Data Structure and Algorithms


Deletion from an AVL Tree
Deletion from an AVL tree is same as BST but we need to check for height imbalance
after deletion.
Consider the following cases
: Consider deleting B from the tree:
CASE 3:

As we have deleted from left tree and


-2
-1 balance factor of C is 0, this is L0
A rotation.
L0 = RR
0
A A
0 C
B C

C C
A E
D E

0 0
D E E D

Prepared By : Vaishali Koria Data Structure and Algorithms


Derive the cases for deletion from right.

Prepared By : Vaishali Koria Data Structure and Algorithms


Example of deletion from an AVL Tree

Prepared By : Vaishali Koria Data Structure and Algorithms


Example of deletion from an AVL Tree

Prepared By : Vaishali Koria Data Structure and Algorithms


Example of deletion from an AVL Tree

Prepared By : Vaishali Koria Data Structure and Algorithms


Example of deletion from an AVL Tree

Prepared By : Vaishali Koria Data Structure and Algorithms


Thank You!!!

Prepared By : Vaishali Koria Data Structure and Algorithms

You might also like