Chapter 10 Tree
Chapter 10 Tree
AND
ALGORITHM
1
Course Content
NO TOPIC CLO
01 A General Overview CLO1
02 Introduction to Data Structures and Algorithm CLO1
03 String Processing CLO1
04 Abstract Data Types CLO1
05 Linked list CLO1
06 Stack and Queue CLO1
07 Recursion CLO1
08 Complexity Analysis CLO2
09 Sorting and Searching techniques CLO2
10 Trees CLO2
11 Graph CLO3
12 P & NP CLO3
2
CHAPTER 10
TREES
3
WHAT IS TREE?
• A tree is a very popular non-linear data structure used in a wide
range of applications.
5
TREE TERMINOLOGY
In a tree data structure, we use the following terminology...
1. Root
2. Edge
3. Parent
4. Child
5. Siblings
6. Leaf
7. Internal Nodes
8. Degree
9. Level
10. Height
11. Depth
12. Path
13. Sub Tree
6
TREE TERMINOLOGY
1. Root
Every tree must have a root node. We can say that the root node is
the origin of the tree data structure.
In any tree, there must be only one root node. We never have
multiple root nodes in a tree.
7
TREE TERMINOLOGY
2. Edge
In a tree data structure, the connecting link between any two nodes
is called as EDGE. In a tree with 'N' number of nodes there will be a
maximum of 'N-1' number of edges.
8
TREE TERMINOLOGY
3. Parent
In a tree data structure, the node which is a predecessor of any
node is called as PARENT NODE. In simple words, the node which
has a branch from it to any other node is called a parent node.
Parent node can also be defined as "The node which has child /
children".
9
TREE TERMINOLOGY
4. Child
In a tree data structure, the node which is descendant of any node is
called as CHILD Node. In simple words, the node which has a link
from its parent node is called as child node. In a tree, any parent
node can have any number of child nodes. In a tree, all the nodes
except root are child nodes.
10
TREE TERMINOLOGY
5. Siblings
In a tree data structure, nodes which belong to same Parent are
called as SIBLINGS. In simple words, the nodes with the same parent
are called Sibling nodes.
11
TREE TERMINOLOGY
6. Leaf
In a tree data structure, the node which does not have a child is
called as LEAF Node. In simple words, a leaf is a node with no child.
In a tree data structure, the leaf nodes are also called as External
Nodes. External node is also a node with no child. In a tree, leaf node
is also called as 'Terminal' node.
12
TREE TERMINOLOGY
7. Internal Nodes
In a tree data structure, the node which has atleast one child is called
as INTERNAL Node. In simple words, an internal node is a node with
atleast one child.
In a tree data structure, nodes other than leaf nodes are called
as Internal Nodes. The root node is also said to be Internal
Node if the tree has more than one node. Internal nodes are also
called as 'Non-Terminal' nodes.
13
TREE TERMINOLOGY
8. Degree
In a tree data structure, the total number of children of a node is
called as DEGREE of that Node. In simple words, the Degree of a
node is total number of children it has. The highest degree of a
node among all the nodes in a tree is called as 'Degree of Tree'
14
TREE TERMINOLOGY
9. Level
In a tree data structure, the root node is said to be at Level 0 and the
children of root node are at Level 1 and the children of the nodes
which are at Level 1 will be at Level 2 and so on... In simple words, in
a tree each step from top to bottom is called as a Level and the Level
count starts with '0' and incremented by one at each level (Step).
15
TREE TERMINOLOGY
10. Height
In a tree data structure, the total number of edges from leaf node to a
particular node in the longest path is called as HEIGHT of that
Node. In a tree, height of the root node is said to be height of the
tree. In a tree, height of all leaf nodes is '0‘.
16
TREE TERMINOLOGY
11. Depth
In a tree data structure, the total number of egdes from root node to
a particular node is called as DEPTH of that Node. In a tree, the total
number of edges from root node to a leaf node in the longest path is
said to be Depth of the tree. In simple words, the highest depth of
any leaf node in a tree is said to be depth of that tree. In a tree, depth
of the root node is '0'.
17
TREE TERMINOLOGY
12. Path
In a tree data structure, the sequence of Nodes and Edges from one
node to another node is called as PATH between that two
Nodes. Length of a Path is total number of nodes in that path. In
below example the path A - B - E - J has length 4.
18
TREE TERMINOLOGY
13. Sub Tree
In a tree data structure, each child from a node forms a subtree
recursively. Every child node will form a subtree on its parent node.
19
TREE REPRESENTATION
A tree data structure can be represented in two methods. Those
methods are as follows...
1. List Representation
2. Left Child - Right Sibling Representation
Consider the following tree...
20
TREE REPRESENTATION
1. List Representation
• In this representation, we use two types of nodes one for
representing the node with data called 'data node' and another for
representing only references called 'reference node'.
• We start with a 'data node' from the root node in the tree.
• Then it is linked to an internal node through a 'reference node'
which is further linked to any other node directly.
• This process repeats for all the nodes in the tree.
The above example tree can be represented using List representation
as follows...
21
TREE REPRESENTATION
1. List Representation (example): This tree can be represented
using List representation
22
TREE REPRESENTATION
2. LEFT CHILD- RIGHT SIBLING REPRESENTATION
In this representation, we use a list with one type of node which consists
of three fields namely Data field, Left child reference field and Right
sibling reference field.
23
TREE REPRESENTATION
2. LEFT CHILD- RIGHT SIBLING REPRESENTATION
In this representation, every node's data field stores the actual value
of that node.
If that node has left a child, then left reference field stores the
address of that left child node otherwise stores NULL.
If that node has the right sibling, then right reference field stores the
address of right sibling node otherwise stores NULL.
24
TREE REPRESENTATION
2. LEFT CHILD- RIGHT SIBLING REPRESENTATION (Example)
25
Example: TREES
Consider the four binary trees in fig. below. The
three trees (a), (c) and (d) are similar. In particular,
the trees (a) and (c) are copies since they also have
the same data at corresponding nodes. The tree (b)
is neither similar nor a copy of the tree.
A E A E
B B F
F
C D C D G H
G H (d)
(a) (b) (c)
26
BINARY TREE DATA STRUCTURE
27
STRICTLY BINARY TREE DATA
STRUCTURE
• 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
28
STRICTLY BINARY TREE DATA
STRUCTURE
• 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
29
Exercise: Arithmetic Expressions as
trees(cont.)
(i) (a*b)+(c/d)
(ii) ((a+b)+c)+d)
(iii) ((-a)+(x+y))/((+b)*(c*a))
(iv) (a-b) / ((c * d) + e)
30
30
Exercise: Arithmetic Expressions as
trees(cont.)
(i) (a*b)+(c/d)
31
31
Exercise: Arithmetic Expressions as
trees(cont.)
(ii) ((a+b)+c)+d)
32
32
Exercise: Arithmetic Expressions as
trees(cont.)
(iii) ((-a)+(x+y))/((+b)*(c*a))
33
33
Exercise: Arithmetic Expressions as
trees(cont.)
(iv) (a-b) / ((c * d) + e)
/
- +
a e
b *
c d
34
34
COMPLETE BINARY TREE DATA
STRUCTURE
A binary tree in which every internal node has exactly two
children and all leaf nodes are at same level is called
Complete Binary Tree.
Complete binary tree is also called as Perfect Binary Tree.
The second node in complete binary tree is always the left
child of root and 3rd node is right node of the root.
Next nodes always fill the next level from left to right.
35
IS THIS COMPLETE BINARY TREE
DATA STRUCTURE ?
36
IS THIS COMPLETE BINARY TREE
DATA STRUCTURE ?
38
IS THIS COMPLETE BINARY TREE
DATA STRUCTURE ?
40
IS THIS COMPLETE BINARY TREE
DATA STRUCTURE ?
41
IS THIS COMPLETE BINARY TREE
DATA STRUCTURE ?
42
IS THIS COMPLETE BINARY TREE
DATA STRUCTURE ?
43
Complete Binary tree
• The left and right children of the node K are , respectively, 2 * K and 2 * K + 1,
and the parent of K is the node [K/2].
• The children of node 9 are the nodes 18, 19, and its parent is the node [9/2] =4.
44
EXTEND BINARY TREE DATA
STRUCTURE
A binary tree can be converted into Full Binary tree by
adding dummy nodes to existing nodes wherever required.
The full binary tree obtained by adding dummy nodes to a
binary tree is called as Extended Binary Tree.
45
BINARY TREE REPRESENTATION
A binary tree data structure is represented using two methods.
Those methods are as follows...
1. Array Representation
2. Linked List Representation
46
ARRAY REPRESENTATION OF BINARY
TREE
• In array representation of a binary tree, we use one-
dimensional array (1-D Array) to represent a binary tree.
• To represent a binary tree of depth 'n' using array
representation, we need one dimensional array with a
maximum size of 2n + 1.
• Consider the example of a binary tree and it is represented as
follows
47
EXERCISE: ARRAY REPRESENTATION
OF BINARY TREE
14
10 16
8 12 15 18
7 9 11 13
48
EXERCISE: ARRAY REPRESENTATION
OF BINARY TREE
1
14
2 3
10 16
4 5 6 7
8 12 15 18
8 9 10 11
7 9 11 13
1 2 3 4 5 6 7 8 9 10 11
Array A: 14 10 16 8 12 15 18 7 9 11 13
49
ADVANTAGES AND DISADVANTAGES
OF ARRAY REPRESENTATION OF
BINARY TREE
Advantages of linear representation:
1. Simplicity.
2. Given the location of the child (say, k), the location of the parent is easy to determine (k
/ 2).
51
LNKED LLIST REPRESENTATION OF
BINARY TREE
• EXAMPLE:
52
Difference Between a Tree & a
Binary Tree
• No node in a binary tree may have a degree more than 2,
whereas there is no limit on the degree of a node in a tree.
• The sub trees of a binary search tree are ordered; those of a
tree are not ordered.
a a
b c c b
53
53
BINARY TREE TRAVERSALS
• When we wanted to display a binary tree, we need to follow
some order in which all the nodes of that binary tree must be
displayed. In any binary tree, displaying order of nodes depends
on the traversal method.
• Displaying (or) visiting order of nodes in a binary tree is called as
Binary Tree Traversal.
• There are three types of binary tree traversals.
1. In - Order Traversal
2. Pre - Order Traversal
3. Post - Order Traversal
54
54
In-Order BINARY TREE
TRAVERSALS
In - Order Traversal (leftChild - root - rightChild )
• Following operations are carried out to traverse a binary tree
using In-Order way:
(a)Traverse the left most sub-tree starting at the left external
node
(b) Visit the root
(c)Traverse the right sub-tree starting at the left external node
55
55
Example: In-Order BINARY TREE
TRAVERSALS
7
1 9 0,
10
0 3 8
2 5
4 6 56
Example: In-Order BINARY TREE
TRAVERSALS
7
1 9 0,1
10
0 3 8
2 5
4 6 57
Example: In-Order BINARY TREE
TRAVERSALS
7
1 9 0,1,2
10
0 3 8
2 5
4 6 58
Example: In-Order BINARY TREE
TRAVERSALS
7
1 9 0,1,2,3
10
0 3 8
2 5
4 6 59
Example: In-Order BINARY TREE
TRAVERSALS
7
1 9 0,1,2,3,4
10
0 3 8
2 5
4 6 60
Example: In-Order BINARY TREE
TRAVERSALS
7
1 9 0,1,2,3,4,5
10
0 3 8
2 5
4 6 61
Example: In-Order BINARY TREE
TRAVERSALS
7
1 9 0,1,2,3,4,5,6
10
0 3 8
2 5
4 6 62
Example: In-Order BINARY TREE
TRAVERSALS
7
1 9 0,1,2,3,4,5,6,7
10
0 3 8
2 5
4 6 63
Example: In-Order BINARY TREE
TRAVERSALS
7
1 9 0,1,2,3,4,5,6,7,8
10
0 3 8
2 5
4 6 64
Example: In-Order BINARY TREE
TRAVERSALS
7
1 9 0,1,2,3,4,5,6,7,8,9,
10
0 3 8
2 5
4 6 65
Example: In-Order BINARY TREE
TRAVERSALS
7
1 9 0,1,2,3,4,5,6,7,8,9,10
10
0 3 8
2 5
4 6 66
ALGORITHM: In-Order BINARY
TREE TRAVERSALS
1. Create an empty Stack S
2. Initialize current node as root
3. Push the current node to S and set Current=
Current Left until Current = Null 7
4. If Current is Null and Stack is not empty
then:
a. POP the item from S 1 9
b. Print the Popped item, Set Current 10
Right
c. Go to Step 3
0 3 8
5. If Current is Null and Stack is empty then
Exit 2 5
4 6 67
ALGORITHM: In-Order BINARY TREE TRAVERSALS
1. Create an empty Stack S
2. Initialize current node as root
3. Push the current node to S and set Current= Current Left until Current = Null
4. If Current is Null and Stack is not empty then:
a. POP the item from S
b. Print the Popped item, Set Current Right
c. Go to Step 3
7
5. If Current is Null and Stack is empty then Exit
1 9
10
0 3 8
2 5
4 6 68
ALGORITHM: In-Order BINARY TREE TRAVERSALS
1. Create an empty Stack S
2. Initialize current node as root
3. Push the current node to S and set Current= Current Left until Current = Null
4. If Current is Null and Stack is not empty then:
a. POP the item from S
b. Print the Popped item, Set Current Right
c. Go to Step 3
7
5. If Current is Null and Stack is empty then Exit
1 9
Current = Root=7
10
0 3 8
2 5
4 6 69
ALGORITHM: In-Order BINARY TREE TRAVERSALS
1. Create an empty Stack S
2. Initialize current node as root
3. Push the current node to S and set Current= Current Left until Current = Null
4. If Current is Null and Stack is not empty then:
a. POP the item from S
b. Print the Popped item, Set Current Right
c. Go to Step 3
7
5. If Current is Null and Stack is empty then Exit
1 9
Current = Current Left = 1
10
0 3 8
2 5
4 6 70
7
ALGORITHM: In-Order BINARY TREE TRAVERSALS
1. Create an empty Stack S
2. Initialize current node as root
3. Push the current node to S and set Current= Current Left until Current = Null
4. If Current is Null and Stack is not empty then:
a. POP the item from S
b. Print the Popped item, Set Current Right
c. Go to Step 3
7
5. If Current is Null and Stack is empty then Exit
1 9
Current = Current Left = 1 (Not Null)
Repeat Step 3 10
Current = Current Left = 0
0 3 8
2 5
1 4 6 71
7
ALGORITHM: In-Order BINARY TREE TRAVERSALS
1. Create an empty Stack S
2. Initialize current node as root
3. Push the current node to S and set Current= Current Left until Current = Null
4. If Current is Null and Stack is not empty then:
a. POP the item from S
b. Print the Popped item, Set Current Right
c. Go to Step 3
7
5. If Current is Null and Stack is empty then Exit
1 9
Current = Current Left = 1 (Not Null)
Repeat Step 3 10
Current = Current Left = 0
0
(Not Null) 3 8
Repeat Step 3
2 5
0
1 4 6 72
7
ALGORITHM: In-Order BINARY TREE TRAVERSALS
1. Create an empty Stack S
2. Initialize current node as root
3. Push the current node to S and set Current= Current Left until Current = Null
4. If Current is Null and Stack is not empty then:
a. POP the item from S
b. Print the Popped item, Set Current Right
c. Go to Step 3
7
5. If Current is Null and Stack is empty then Exit
1 9
Current = Current Left = 1 (Not Null)
10
Repeat Step 3
Current = Current Left = 0
0
(Not Null) 3 8
Repeat Step 3
Current = Current Left = Null 2 5
0
1 4 6 73
7
ALGORITHM: In-Order BINARY TREE TRAVERSALS
1. Create an empty Stack S
2. Initialize current node as root
3. Push the current node to S and set Current= Current Left until Current = Null
4. If Current is Null and Stack is not empty then:
a. POP the item from S
b. Print the Popped item, Set Current Right
c. Go to Step 3
7
5. If Current is Null and Stack is empty then Exit
1 9
Current = 0
Current= Check Current right = Null 10
0 3 8
2 5
0
1 4 6 74
7
ALGORITHM: In-Order BINARY TREE TRAVERSALS
1. Create an empty Stack S
2. Initialize current node as root
3. Push the current node to S and set Current= Current Left until Current = Null
4. If Current is Null and Stack is not empty then:
a. POP the item from S
b. Print the Popped item, Set Current Right
c. Go to Step 3
7
5. If Current is Null and Stack is empty then Exit
Current = 0
1 9
Current= Check Current right = Null 10
0 3 8
2 5
1 4 6 75
7 0
ALGORITHM: In-Order BINARY TREE TRAVERSALS
1. Create an empty Stack S
2. Initialize current node as root
3. Push the current node to S and set Current= Current Left until Current = Null
4. If Current is Null and Stack is not empty then:
a. POP the item from S
b. Print the Popped item, Set Current Right
c. Go to Step 3
7
5. If Current is Null and Stack is empty then Exit
1 9
Current = 1
Current= Check Current right = 3 10
Go to Step 3 0 3 8
2 5
4 6 76
7 0,1
ALGORITHM: In-Order BINARY TREE TRAVERSALS
1. Create an empty Stack S
2. Initialize current node as root
3. Push the current node to S and set Current= Current Left until Current = Null
4. If Current is Null and Stack is not empty then:
a. POP the item from S
b. Print the Popped item, Set Current Right
c. Go to Step 3
7
5. If Current is Null and Stack is empty then Exit
1 9
Current = 1
Current= Check Current right = 3 10
Go to Step 3 0 3 8
2 5
78
78
EXERCISE: In-Order BINARY TREE TRAVERSALS
(VISIT AND PRINT)
g d h b e i a f j c
79
79
BINARY TREE TRAVERSALS
Pre - Order Traversal ( root - leftChild - rightChild )
80
80
Pre-Order Traversal ( root - leftChild - rightChild )
(a)Visit the root
(b)Traverse the left most sub-tree starting at the left external
node
(c) Traverse the right sub-tree starting at the left external node
7
1 9 7,
1
0 3 8 0
2 5
4 6 81
Pre-Order Traversal ( root - leftChild - rightChild )
(a)Visit the root
(b)Traverse the left most sub-tree starting at the left external
node
(c) Traverse the right sub-tree starting at the left external node
7
1 9 7,1
10
0 3 8
2 5
4 6 82
Pre-Order Traversal ( root - leftChild - rightChild )
(a)Visit the root
(b)Traverse the left most sub-tree starting at the left external
node
(c) Traverse the right sub-tree starting at the left external node
7
1 9 7,1,0,
10
0 3 8
2 5
4 6 83
Pre-Order Traversal ( root - leftChild - rightChild )
(a)Visit the root
(b)Traverse the left most sub-tree starting at the left external
node
(c) Traverse the right sub-tree starting at the left external node
7
1 9 7,1,0,3
10
0 3 8
2 5
4 6 84
Pre-Order Traversal ( root - leftChild - rightChild )
(a)Visit the root
(b)Traverse the left most sub-tree starting at the left external
node
(c) Traverse the right sub-tree starting at the left external node
7
1 9 7,1,0,3,2
10
0 3 8
2 5
4 6 85
Pre-Order Traversal ( root - leftChild - rightChild )
(a)Visit the root
(b)Traverse the left most sub-tree starting at the left external
node
(c) Traverse the right sub-tree starting at the left external node
7
1 9 7,1,0,3,2,5
10
0 3 8
2 5
4 6 86
Pre-Order Traversal ( root - leftChild - rightChild )
(a)Visit the root
(b)Traverse the left most sub-tree starting at the left external
node
(c) Traverse the right sub-tree starting at the left external node
7
1 9 7,1,0,3,2,5,4
10
0 3 8
2 5
4 6 87
Pre-Order Traversal ( root - leftChild - rightChild )
(a)Visit the root
(b)Traverse the left most sub-tree starting at the left external
node
(c) Traverse the right sub-tree starting at the left external node
7
1 9 7,1,0,3,2,5,4,6
10
0 3 8
2 5
4 6 88
Pre-Order Traversal ( root - leftChild - rightChild )
(a)Visit the root
(b)Traverse the left most sub-tree starting at the left external
node
(c) Traverse the right sub-tree starting at the left external node
7
1 9 7,1,0,3,2,5,4,6,9
10
0 3 8
2 5
4 6 89
Pre-Order Traversal ( root - leftChild - rightChild )
(a)Visit the root
(b)Traverse the left most sub-tree starting at the left external
node
(c) Traverse the right sub-tree starting at the left external node
7
1 9 7,1,0,3,2,5,4,6,9,8,
10
0 3 8
2 5
4 6 90
Pre-Order Traversal ( root - leftChild - rightChild )
(a)Visit the root
(b)Traverse the left most sub-tree starting at the left external
node
(c) Traverse the right sub-tree starting at the left external node
7
1 9 7,1,0,3,2,5,4,6,9,8,10
10
0 3 8
2 5
4 6 91
ALGORITHM: Pre-Order BINARY TREE TRAVERSALS
1. Create an empty Stack S
2. Push the root on Stack
3. While the Stack is not empty
a. POP the item from Stack and print
b. PUSH its children in the Stack
7
1 9
10
0 3 8
2 5
4 6 92
ALGORITHM: Pre-Order BINARY TREE TRAVERSALS
1. Create an empty Stack S
2. Push the root on Stack
3. While the Stack is not empty
a. POP the item from Stack and print
b. PUSH its children in the Stack
7
1 9
10
0 3 8
2 5
4 6 93
ALGORITHM: Pre-Order BINARY TREE TRAVERSALS
1. Create an empty Stack S
2. Push the root on Stack
3. While the Stack is not empty
a. POP the item from Stack and print
b. PUSH its children in the Stack
7
1 9
10
0 3 8
2 5
4 6 94
ALGORITHM: Pre-Order BINARY TREE TRAVERSALS
1. Create an empty Stack S
2. Push the root on Stack
3. While the Stack is not empty
a. POP the item from Stack and print
b. PUSH its children in the Stack
7
1 9
10
0 3 8
2 5
4 6 95
7
ALGORITHM: Pre-Order BINARY TREE TRAVERSALS
1. Create an empty Stack S
2. Push the root on Stack
3. While the Stack is not empty
a. POP the item from Stack and print
b. PUSH its children in the Stack
7
1 9
10
0 3 8
2 5
4 6 96
7,
ALGORITHM: Pre-Order BINARY TREE TRAVERSALS
1. Create an empty Stack S
2. Push the root on Stack
3. While the Stack is not empty
a. POP the item from Stack and print
b. PUSH its children in the Stack
7
1 9
10
0 3 8
2 5
1 4 6 97
9 7,
ALGORITHM: Pre-Order BINARY TREE TRAVERSALS
1. Create an empty Stack S
2. Push the root on Stack
3. While the Stack is not empty
a. POP the item from Stack and print
b. PUSH its children in the Stack
7
1 9
10
0 3 8
2 5
4 6 98
9 7,1,
ALGORITHM: Pre-Order BINARY TREE TRAVERSALS
1. Create an empty Stack S
2. Push the root on Stack
3. While the Stack is not empty
a. POP the item from Stack and print
b. PUSH its children in the Stack
7
1 9
10
0 3 8
2 5
0
3 4 6 99
9 7,1,
ALGORITHM: Pre-Order BINARY TREE TRAVERSALS
1. Create an empty Stack S
2. Push the root on Stack
3. While the Stack is not empty
a. POP the item from Stack and print
b. PUSH its children in the Stack
7
1 9
10
0 3 8
2 5
3 4 6 100
9 7,1,0,
ALGORITHM: Pre-Order BINARY TREE TRAVERSALS
1. Create an empty Stack S
2. Push the root on Stack
3. While the Stack is not empty
a. POP the item from Stack and print
b. PUSH its children in the Stack
7
1 9
10
0 3 8
2 5
4 6 101
9 7,1,0,3
ALGORITHM: Pre-Order BINARY TREE TRAVERSALS
1. Create an empty Stack S
2. Push the root on Stack
3. While the Stack is not empty
a. POP the item from Stack and print
b. PUSH its children in the Stack
7
1 9
10
0 3 8
2 5
2
Continue the same process 6 102
5
7,1,0,3 4
9
EXERCISE: Pre-Order BINARY TREE TRAVERSALS
(VISIT AND PRINT)
10 103
3
EXERCISE: Pre-Order BINARY TREE TRAVERSALS
(VISIT AND PRINT)
a b d g h e i c f j
10 104
4
EXERCISE: Pre-Order BINARY TREE TRAVERSALS
(VISIT AND PRINT)
10 105
5
EXERCISE: Pre-Order BINARY TREE TRAVERSALS
(VISIT AND PRINT)
/ * + a b - c d + e f
Gives prefix form of expression.
10 106
6
In-Order BINARY TREE
TRAVERSALS
Post - Order Traversal ( leftChild - rightChild - root )
• Following operations are carried out to traverse a binary tree
using Post-Order way:
(a)Traverse the left most sub-tree starting at the left external
node
(b) Traverse the right sub-tree starting at the left external node
(c)Visit the root
10 107
7
Post-Order Traversal ( leftChild - rightChild - root )
(a)Traverse the left most sub-tree starting at the left external
node
(b) Traverse the right sub-tree starting at the left external node
(c)Visit the root
7
1 9
1
0 3 8 0
2 5
108
0, 4 6
Post-Order Traversal ( leftChild - rightChild - root )
(a)Traverse the left most sub-tree starting at the left external
node
(b) Traverse the right sub-tree starting at the left external node
(c)Visit the root
7
1 9
1
0 3 8 0
2 5
109
0,2 4 6
Post-Order Traversal ( leftChild - rightChild - root )
(a)Traverse the left most sub-tree starting at the left external
node
(b) Traverse the right sub-tree starting at the left external node
(c)Visit the root
7
1 9
1
0 3 8 0
2 5
110
0,2,4, 4 6
Post-Order Traversal ( leftChild - rightChild - root )
(a)Traverse the left most sub-tree starting at the left external
node
(b) Traverse the right sub-tree starting at the left external node
(c)Visit the root
7
1 9
1
0 3 8 0
2 5
111
0,2,4,6 4 6
Post-Order Traversal ( leftChild - rightChild - root )
(a)Traverse the left most sub-tree starting at the left external
node
(b) Traverse the right sub-tree starting at the left external node
(c)Visit the root
7
1 9
1
0 3 8 0
2 5
112
0,2,4,6 4 6
Post-Order Traversal ( leftChild - rightChild - root )
(a)Traverse the left most sub-tree starting at the left external
node
(b) Traverse the right sub-tree starting at the left external node
(c)Visit the root
7
1 9
1
0 3 8 0
2 5
113
0,2,4,6,5 4 6
Post-Order Traversal ( leftChild - rightChild - root )
(a)Traverse the left most sub-tree starting at the left external
node
(b) Traverse the right sub-tree starting at the left external node
(c)Visit the root
7
1 9
1
0 3 8 0
2 5
114
0,2,4,6,5,3 4 6
Post-Order Traversal ( leftChild - rightChild - root )
(a)Traverse the left most sub-tree starting at the left external
node
(b) Traverse the right sub-tree starting at the left external node
(c)Visit the root
7
1 9
1
0 3 8 0
2 5
115
0,2,4,6,5,3,1 4 6
Post-Order Traversal ( leftChild - rightChild - root )
(a)Traverse the left most sub-tree starting at the left external
node
(b) Traverse the right sub-tree starting at the left external node
(c)Visit the root
7
1 9
1
0 3 8 0
2 5
116
0,2,4,6,5,3,1,8 4 6
Post-Order Traversal ( leftChild - rightChild - root )
(a)Traverse the left most sub-tree starting at the left external
node
(b) Traverse the right sub-tree starting at the left external node
(c)Visit the root
7
1 9
1
0 3 8 0
2 5
117
0,2,4,6,5,3,1,8,10 4 6
Post-Order Traversal ( leftChild - rightChild - root )
(a)Traverse the left most sub-tree starting at the left external
node
(b) Traverse the right sub-tree starting at the left external node
(c)Visit the root
7
1 9
1
0 3 8 0
2 5
118
0,2,4,6,5,3,1,8,10,9 4 6
Post-Order Traversal ( leftChild - rightChild - root )
(a)Traverse the left most sub-tree starting at the left external
node
(b) Traverse the right sub-tree starting at the left external node
(c)Visit the root
7
1 9
1
0 3 8 0
2 5
119
0,2,4,6,5,3,1,8,10,9,7 4 6
ALGORITHM: Post-Order BINARY TREE TRAVERSALS
1. Create an empty Stack S
2. Do following While root is not NULL
a. PUSH root’s right child and then root in stack
b. Set root as root’s left child
3. POP an item from stack and set it as root
a. If the POPPED item has a right child and the right child is at top of stack, then
remove the right child from the stack, PUSH the root back and set root as
root’s right child
b. Else print root’s data and set root as NULL 7
4. Repeat step 2 and 3 while stack is not empty
1 9
1
0 3 8 0
2 5
120
4 6
EXERCISE: POST-Order BINARY TREE TRAVERSALS
(VISIT AND PRINT)
12 121
1
EXERCISE: POST-Order BINARY TREE TRAVERSALS
(VISIT AND PRINT)
g h d i e b j f c a
12 122
2
EXERCISE: POST-Order BINARY TREE TRAVERSALS
(VISIT AND PRINT)
12 123
3
EXERCISE: POST-Order BINARY TREE TRAVERSALS
(VISIT AND PRINT)
a b + c d - * e f + /
12
Gives postfix form of expression.
124
4
EXERCISE
125
EXERCISE
126
EXERCISE
Determine the order in which vertices of the following binary
trees will be visited under (1) Preorde (2) Inorder (3)
Postorder
1
2
4
3
5 6
7 8 127
EXERCISE
Determine the order in which vertices of the following binary
trees will be visited under (1) Preorde (2) Inorder (3)
Postorder
(1)Preorder: 1
1,2,3,5,7,8,6,4 2
(2)Inorder: 4
1,7,5,8,3,6,2,4 3
(3)Postorde:
5 6
7,8,5,6,3,4,2,1
7 8 128
Threaded Trees
• Threaded Binary Tree is also a binary tree in which all left
child pointers that are NULL (in Linked list representation)
points to its in-order predecessor, and all right child
pointers that are NULL (in Linked list representation)
points to its in-order successor.
• If there is no in-order predecessor or in-order successor,
then it points to the root node.
• We need to know if a pointer is an actual link or a thread,
so we keep a boolean for each pointer
129
Threaded Trees
Consider the following binary tree.
130
Threaded Trees
Consider the following binary tree.
131
Threaded Trees
• When we represent the THIS binary
tree using linked list representation,
nodes H, I, E, F, J and G left child pointers
are NULL.
• This NULL is replaced by address of
its in-order predecessor respectively (I to
D, E to B, F to A, J to F and G to C),
• but here the node H does not have its threads are indicated with dotted links.
in-order predecessor, so it points to the
root node A.
• And nodes H, I, E, J and G right child
pointers are NULL. These NULL pointers
are replaced by address of its in-order
successor respectively (H to D, I to B, E to
A, and J to C),
• but here the node G does not have its
in-order successor, so it points to the root 132
node A.
Threaded Tree Example
6
3 8
1 5 7 11
9 13
133
Threaded Tree Traversal
• We start at the leftmost node in the tree, print it, and follow its
right thread
• If we follow a thread to the right, we output the node and
continue to its right
• If we follow a link to the right, we go to the leftmost node, print it,
and continue
134
Threaded Tree Traversal
Output
6 1
3 8
1 5 7 11
9 13
Start at leftmost node, print it
135
Threaded Tree Traversal
Output
6 1
3
3 8
1 5 7 11
9 13
Follow thread to right, print node
136
Threaded Tree Traversal
Output
6 1
3
5
3 8
1 5 7 11
9 13
Follow link to right, go to
leftmost node and print
137
Threaded Tree Traversal
Output
6 1
3
5
3 8 6
1 5 7 11
9 13
Follow thread to right, print node
138
Threaded Tree Traversal
Output
6 1
3
5
3 8 6
7
1 5 7 11
9 13
Follow link to right, go to
leftmost node and print
139
Threaded Tree Traversal
Output
6 1
3
5
3 8 6
7
1 5 7 11 8
9 13
Follow thread to right, print node
140
Threaded Tree Traversal
Output
6 1
3
5
3 8 6
7
1 5 7 11 8
9
9 13
Follow link to right, go to
leftmost node and print
141
Threaded Tree Traversal
Output
6 1
3
5
3 8 6
7
1 5 7 11 8
9
11
9 13
Follow thread to right, print node
142
Threaded Tree Traversal
Output
6 1
3
5
3 8 6
7
1 5 7 11 8
9
11
9 13 13
144
Threaded Tree Modification
6
3 8
1 5 7 11
9 13
145
Binary Search Tree
A Binary Search Tree is a binary tree with the following Basic
properties:
• All items in the left subtree are less than the root.
• All items in the right subtree are greater or equal to the root.
• Each subtree is itself a binary search tree.
146
146
EXAMPLE: Binary Search Tree
The following tree is a Binary Search Tree. In this tree, left subtree of
every node contains nodes with smaller values and right subtree of
every node contains larger values.
Every binary search tree is a binary tree but every binary tree need
147
not to be binary search tree.
OPERATIONS ON BINARY SEARCH
TREE
148
SEARCH OPERATIONS ON BINARY SEARCH TREE
In a binary search tree, the search operation is performed with O(log n) time
complexity.
The search operation is performed as follows...
Step 1 - Read the search element from the user.
Step 2 - Compare the search element with the value of root node in the tree.
Step 3 - If both are matched, then display "Given node is found!!!" and terminate
the function
Step 4 - If both are not matched, then check whether search element is smaller or
larger than that node value.
Step 5 - If search element is smaller, then continue the search process in left
subtree.
Step 6- If search element is larger, then continue the search process in right
subtree.
Step 7 - Repeat the same until we find the exact element or until the search
element is compared with the leaf node
Step 8 - If we reach to the node having the value equal to the search value then
display "Element is found" and terminate the function.
Step 9 - If we reach to the leaf node and if it is also not matched with the search
element, then display "Element is not found" and terminate the function. 149
INSERTION OPERATIONS ON BINARY SEARCH TREE
In a binary search tree, the insertion operation is performed with O(log n) time
complexity.
In binary search tree, new node is always inserted as a leaf node. The insertion
operation is performed as follows...
Step 1 - Create a newNode with given value and set its left and right to NULL.
Step 2 - Check whether tree is Empty.
Step 3 - If the tree is Empty, then set root to newNode.
Step 4 - If the tree is Not Empty, then check whether the value of newNode
is smaller or larger than the node (here it is root node).
Step 5 - If newNode is smaller than or equal to the node then move to
its left child. If newNode is larger than the node then move to its right child.
Step 6- Repeat the above steps until we reach to the leaf node (i.e., reaches to
NULL).
Step 7 - After reaching the leaf node, insert the newNode as left child if the
newNode is smaller or equal to that leaf node or else insert it as right child.
150
DELETION OPERATIONS ON BINARY SEARCH TREE
151
DELETION OPERATIONS ON BINARY SEARCH TREE
152
Deleting leaf node from a BST
To delete node with key x first you need to search for it.
Once found, apply one of the following three cases
CASE 1: x is a leaf
p p
q q
r x r
We use the following steps to delete a node with one child from BST...
154
Deleting node with one child
from a BST cont.
Case 2: x is interior with only one subtree
r r
x
q
q
delete x L
L
BST property maintained
155
DELETION OPERATIONS ON BINARY SEARCH TREE
We use the following steps to delete a node with two children from BST...
Step 1 - Find the node to be deleted using search operation
Step 2 - If it has two children, then find the largest node in its left
subtree (OR) the smallest node in its right subtree.
Step 3 - Swap both deleting node and node which is found in the above
step.
Step 4 - Then check whether deleting node came to case 1 or case 2 or
else goto step 2
Step 5 - If it comes to case 1, then delete using case 1 logic.
Step 6- If it comes to case 2, then delete using case 2 logic.
Step 7 - Repeat the same process until the node is deleted from the tree.
156
Deleting node with two children
from a BST cont.
Case 3: x is interior with two subtrees
r
r
r delete x
x delete x
s Z
q r
q
W Z
W
ts t
BST property maintained
157
Deleting node with two children
from a BST cont.
Case 3 cont: … or you can also do it like this
q < x < r
r Q is smaller than the smaller
element in Z
q
R is larger than the largest
W t element in W
r
Z
s 158
EXAMPLE
Construct a Binary Search Tree by inserting the following sequence
of numbers...
10,12,5,4,20,8,7,15 and 13
159
EXAMPLE
Construct a Binary Search Tree by inserting the following sequence
of numbers...
10,12,5,4,20,8,7,15 and 13
Given elements are inserted into a Binary Search Tree as follows...
160
Inserting a new key in a BST
10
The same procedure used for
search also applies: Determine the
location by searching. Search will 8 Insert 4?
fail. Insert new key where the
search failed. 3 9 4
2 5
161
Building a BST
1) Insert C 2) Insert A
C
C
A
162
Building a BST
3) Insert B C
5) Insert M
A
B C
A L
4) Insert L C B M
A L
B 163
Building a BST
Is there a unique BST for letters A B C L M ?
NO! Different input sequences result in different
trees
Inserting: A B C L M Inserting: C A B L M
A
C
B
C A L
L B M
M
164
Sorting with a BST
23 18 12 20 44 35 52
166
166
Postorder Traversal of Binary
search tree
12 20 18 35 52 44 23
167
167
Inorder Traversal of Binary
search tree
12 18 20 23 35 44 52
52 44 35 23 20 18 12
170
Wish
You
Good Luck
171