Professional Documents
Culture Documents
AND ALGORITHMS
Course Instructor:
Zain Arshad
Outline
■ Tree
■ Binary Tree
■ Tree Traversal Algorithms
– Breadth First
– Depth First (Preorder, In order, Post order)
■ AVL Trees
– Rotations (Left, Right, Left-Right, Right-Left)
– Insertion
– Deletion
Tree
■ Examples
– Organization’s chart
– File systems
– Programming Environments
Tree Definition
■ Tree is ordered if there is a linear ordering defined for the children of each node, that is
we can identify children of a node as being first, second, third and so on…
Binary Tree
Binary Tree is a rooted tree
in which root can have
maximum two children
such that each of them is
again a binary tree. That
means, there can be 0,1, or
2 children of any node.
Strict Binary Tree
Strict Binary Tree is a Binary tree in
which root can have exactly
childre two orno children at all.
n
means, there
Thatcan be 0 or 2 children
of any node.
Complete Binary Tree
Complete Binary Tree is
a Strict Binary tree in
which every leaf node is
at same level. That
means, there are equal
number of children in
right and left subtree for
every node.
Traversals
■ A traversal is a process that visits all the nodes in the tree. Since a tree
is a nonlinear data structure, there is no unique traversal. We will
consider several traversal algorithms with we group in the following
two kinds
– breadth-first traversal
– depth-first traversal
There are three different types of depth-first traversals, :
■ PreOrder traversal - visit the parent first and then left and right
children;
■ InOrder traversal - visit the left child, then the parent and the right
child;
■ PostOrder traversal - visit left child, then the right child and then
the parent;
Breadth First Traversal
Tree Traversal : Pre order
Pre order (N L
R)
Tree Traversal : Pre order
Pre order (N L
R)
A
Tree Traversal : Pre order
Pre order (N L
R)
A, B
Tree Traversal : Pre order
Pre order (N L
R)
A, B, D
Tree Traversal : Pre order
Pre order (N L
R)
A, B, D, E
Tree Traversal : Pre order
Pre order (N L
R)
A, B, D, E, C
Tree Traversal : Pre order
Pre order (N L
R)
A, B, D, E, C, F
Tree Traversal : Pre order
Pre order (N L
R)
A, B, D, E, C, F, G
Tree Traversal : In order
In order (L N
R)
Tree Traversal : In order
In order (L N
R)
D
Tree Traversal : In order
In order (L N
R)
D, B
Tree Traversal : In order
In order (L N
R)
D, B, E
Tree Traversal : In order
In order (L N
R)
D, B, E, A
Tree Traversal : In order
In order (L N
R)
D, B, E, A, F
Tree Traversal : In order
In order (L N
R)
D, B, E, A, F, C
Tree Traversal : In order
In order (L N R)
D, B, E, A, F, C, G
Tree Traversal : Post order
Post order (L R
N)
Tree Traversal : Post order
Post order (L R
N)
D
Tree Traversal : Post order
Post order (L R
N)
D, E
Tree Traversal : Post order
Post order (L R
N)
D, E, B
Tree Traversal : Post order
Post order (L R
N)
D, E, B, F
Tree Traversal : Post order
Post order (L R
N)
D, E, B, F, G
Tree Traversal : Post order
Post order (L R
N)
D, E, B, F, G, C
Tree Traversal : Post order
Post order (L R
N)
D, E, B, F, G, C, A
Traversal
■ Solve the following tree for:
– BFS
– DFS
Answers
left (i ) i right (i )
Example
12
8 14
5 9 20
What else can we say?
left (i ) i right (i )
■ All elements to the left of a
node are less than the node
■ All elements to the right of
12
a node are greater than or
equal to the node
8 14 ■ The smallest element is the
left-most element
■ The largest element is the
right-most element
5 9 20
AVL Tree
AVL trees are height-balanced binary search trees
Balance factor of a node=
height(left sub tree) - height(right sub tree)
An AVL tree has balance factor calculated at every
node
For every node, heights of left and right sub tree can
differ by no more than 1
Store current heights in each node
AVL Tree
A binary tree in which the difference of height of the
right and left subtree of any node is less than or
equal to 1 is known as AVL Tree.
Height of left subtree – height of right
subtree can be either -1,0,1
AVL Tree
0 0 0
0
AVL Tree
1
-1 0
3 2
0 1 0 0
Balanced as LST-RST=1
Balancing AVL Tree
■ Rotations
– Left
– Right
– Left-Right
– Right-Left
AVL Tree Example:
• Insert 14, 17, 11, 7, 53, 4, 13 into an empty AVL tree
14
11 17
7 53
4
AVL Tree Example:
• Insert 14, 17, 11, 7, 53, 4, 13 into an empty AVL tree
14
7 17
4 11 53
13
AVL Tree Example:
• Now insert 12
14
7 17
4 11 53
13
12
AVL Tree Example:
• Now insert 12
14
7 17
4 11 53
12
13
AVL Tree Example:
• Now the AVL tree is balanced.
14
7 17
4 12 53
11 13
AVL Tree Example:
• Now insert 8
14
7 17
4 12 53
11 13
8
AVL Tree Example:
• Now insert 8
14
7 17
4 11 53
8 12
13
AVL Tree Example:
• Now the AVL tree is balanced.
14
11 17
7 12 53
4 8 13
AVL Tree Example:
• Now remove 53
14
11 17
7 12 53
4 8 13
AVL Tree Example:
• Now remove 53, unbalanced
14
11 17
7 12
4 8 13
AVL Tree Example:
• Balanced! Remove 11
11
7 14
4 8 12 17
13
AVL Tree Example:
• Remove 11, replace it with the largest in its left branch
7 14
4 12 17
13
AVL Tree Example:
• Remove 8, unbalanced
4 14
12 17
13
AVL Tree Example:
• Remove 8, unbalanced
4 12
14
13 17
AVL Tree Example:
• Balanced!!
12
7 14
4 13 17
In Class Exercises
■ Build an AVL tree with the following values:
15, 20, 24, 10, 13, 7, 30, 36, 25
15, 20, 24, 10, 13, 7, 30, 36, 25
20
15
15 24
20
10
24
13
20 20
13 24 15 24
10 15 13
10
15, 20, 24, 10, 13, 7, 30, 36, 25
20
13
13 24 10 20
10 15 7 15 24
7 30
13 36
10 20
7 15 30
24 36
15, 20, 24, 10, 13, 7, 30, 36, 25
13 13
10 20 10 20
7 15 30 7 15 24
24 36 30
25 13 25 36
10 24
7 20 30
15 25 36
Remove 24 and 20 from the AVL tree.
13 13
10 24 10 20
7 20 30 7 15 30
15 25 36 25 36
13 13
10 30 10 15
7 15 36 7 30
25 25 36
Deletion: Really Easy Case
3
Delete(17) 10
2 2
5 15
1 0 0 1
2 9 12 20
0 0 0
3 17 30
Deletion: Pretty Easy Case
3
Delete(15) 10
2 2
5 15
1 0 0 1
2 9 12 20
0 0 0
3 17 30
Deletion: Pretty Easy Case (cont.)
3
Delete(15) 10
2 2
5 17
1 0 0 1
2 9 12 20
0 0
3 30
Deletion (Hard Case #1)
Delete(12) 3
10
2 2
5 17
1 0 0 1
2 9 12 20
0 0
3 30
Single Rotation on Deletion
3 3
10 10
2 2 2 1
5 17 5 20
1 0 1 1 0 0 0
2 9 20 2 9 17 30
0 0 0
3 30 3
2 3
5 17
1 0 2 2
2 9 12 20
0 0 1 0 1
3 11 15 18 30
0 0
13 33
Double Rotation on Deletion
4 4 Not
10 10 finished!
2 3 1 3
5 17 3 17
1 2 2 0 0 2 2
2 12 20 2 5 12 20
0 0 1 0 1 0 1 0 1
3 11 15 18 30 11 15 18 30
0 0 0 0
13 33 13 33
Deletion with Propagation
4
10
What different about this case?
1 3
3 17
0 0 2 2
2 5 12 20
We get to choose whether
0 1 0 1 to single or double rotate!
11 15 18 30
0 0
13 33
Propagated Single Rotation
4 4
10 17
1 3 3 2
3 17 10 20
0 0 2 2 1 2 0 1
2 5 12 20 3 12 18 30
0 1 0 1 0 0 0 1 0
11 15 18 30 2 5 11 15 33
0 0 0
13 33 13
Propagated Double Rotation
4 4
10 12
1 3 2 3
3 17 10 17
0 0 2 2 1 0 1 2
2 5 12 20 3 11 15 20
0 1 0 1 0 0 0 0 1
11 15 18 30 2 5 13 18 30
0 0 0
13 33 33
Q/A