You are on page 1of 84

DATA STRUCTURES

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

■ ADT stores elements hierarchically


■ Consists of nodes with a parent-child relationship

■ Examples
– Organization’s chart
– File systems
– Programming Environments
Tree Definition

■ Tree T is a set of nodes with parent-child relationship


– If T is non empty it has a root
– Each node v of T different from the root has a unique parent node w
– Trees store entries at nodes
Terminologies
■ Root is a node without parent
– A
■ Internal Node has at-least 1 child
– A,B, C, F
■ External (Leaf) Node has no children
– E,I, J, K, G, H, D
■ Depth of a node is the number of ancestors
■ Height of a tree is the maximum depth of any node
– 3 in this scenario
Terminologies
■ Subtree of tree T rooted at node v is
the tree consisting of v itself and all
f the descendants of v

■ Edge of tree T is a pair of nodes (u,


v) i.e. u is the parent of v

■ A path in T is a sequence of nodes


– Any two consecutive nodes
from an edge
Ordered Tree

■ 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

■ PreOrder - 8, 5, 9, 7, 1, 12, 2, 4, 11, 3 


InOrder - 9, 5, 1, 7, 2, 12, 8, 4, 3, 11 
PostOrder - 9, 1, 2, 12, 7, 5, 3, 11, 4, 8 
LevelOrder - 8, 5, 4, 9, 7, 11, 1, 12, 3, 2
AVL Binary Search Trees
■ BST – A binary tree where a parent’s value is greater than
its left child and less than or equal to it’s right child

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

Deletion can differ from insertion – How?


Deletion (Hard Case)
Delete(9) 4
10

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

You might also like