You are on page 1of 144

Binary Tree

Structures
WMSU

WESTERN MINDANAO STATE UNIVERSITY


Binary Tree
• A binary tree has nodes, similar to nodes in a linked list
structure.
• Data of one sort or another may be stored at each node.
• But it is the connections between the nodes which
characterize a binary tree.
Binary Trees
• Binary search trees are a good implementation of data
types such as sets, bags, and dictionaries.
• Searching for an item is generally quick since you move
from the root to the item, without looking at many other
items.
• Adding and deleting items is also quick.
A Binary Tree of Letters
• In this example, the data A
contained at each node is
one of the 26 Letters. G D
E C G
H F B
A Binary Tree of Letters
Root A
• Each tree has a special
node called its root, G D
usually drawn at the top.
E C G
H F B
A Binary Tree of Letters
Children A
• Each node is permitted to
have two links to other G D
nodes, called the left child
and the right child. E C G
• Children are usually drawn
below a node. H F B
A Binary Tree of Letters
• Some nodes have only one A
child.
G D
E C G
H F B
A Binary Tree of Letters
Leaf A
• A node with no children is
called a leaf. G D
E C G
H F B
A Binary Tree of Letters
Parent A
• Each node is called the
parent of its children. G D
• Rules about Parents:
– The root has no parent. E C G
– Every other node has
exactly one parent. H F B
A Binary Tree of Letters
Sibling A
• Two nodes with the same
parent are called siblings. G D
E C G
H F B
Complete Binary Trees
• A complete binary tree is a A
special kind of binary tree which
will be useful to us
• When a complete binary tree is
built, its first node must be the
root.

We want to Make a Complete Binary


Tree with the FF. Data:
A, G, D, E, C, G, H, F, B
Complete Binary Trees
• The second node of a A
complete binary tree is
always the left child of G
the root…
Complete Binary Trees
• The second node of a A
complete binary tree is
always the left child of G D
the root…

• ...and the third node is


always the right child of
the root.
Complete Binary Trees
• The next nodes must A
always fill the next level
from left to right. G D
E C G H
F B
Complete Binary Trees
• Can you consider the A
following example a
Complete Binary Tree? G D
E C G
H F B
Complete Binary Trees
• Can you consider the A
following example a
Complete Binary Tree? G D
E C G H
F B
Complete Binary Trees
• Can you consider the A
following example a
Complete Binary Tree? G D
E C G
F B
Complete Binary Trees
• Can you consider the
following example a
Complete Binary Tree? A
D
Complete Binary Trees
• Can you consider the
following example a
Complete Binary Tree? A
Complete Binary Trees
• Can you consider the
following example a
Complete Binary Tree?

• Yes! It is called the


empty tree, and it has no
nodes, not even a root.
Depth of a Binary Tree
• Refers to How deep the
Tree reaches based on its A
Nodes’ Implementation
G
1
D
• For Example, The Tree on
E C G
2
the right has a binary
depth of 3
F B
3
Depth of a Binary Tree
This example has a binary depth = 2

A
G D
E C G
Depth of a Binary Tree
This example has a binary depth = 0

A
Complete Binary Trees
• Given a complete binary tree of N nodes, what is the
depth?

When N = 1,
then D = 0 When N = 2, When N = 3,
then D = 1 then D = 1 When N = 4, When N = 7,
then D = 2 then D = 2

D = floor(log N) = O(log N)
Complete Binary Trees
• Given a binary tree of N nodes, what is the maximum
possible depth?

When N = 1,
then D = 0 When N = 2, When N = 7,
then D = 1 When N = 3, then D = 6
then D = 2 When N = 4,
then D = 3

D = O(N)
Binary Tree: Summary
• Binary trees contain nodes.
• Each node may have a left child and a right child.
• If you start from any node and move upward, you will
eventually reach the root.
• Every node except the root has one parent. The root has
no parent.
• Complete binary trees require the nodes to fill in each
level from left-to-right before starting the next level.
Python Implementation of Binary Tree
• Each Node in a Binary Tree Contains 3 Pieces of
Information:
– The Left Node
– The Right Node
– Data
• We just create a Node class and add assign a value to
the node. This becomes tree with only a root node.
Python Implementation of Binary Tree
• Binary Trees when
implemented are Often 10
Sorted to a degree.
• In our example, The Left
Child is Less than the 8 14
Parent, while the Right
Child is Greater than the
Parent. 5 9 12
Python Implementation of Binary Tree
• Following this Pattern,
Let’s try to Insert: 10

11
8 14

5 9 12
Python Implementation of Binary Tree
10 11 ?

8 14

5 9 12
Python Implementation of Binary Tree
10

8 14 11 ?

5 9 12
Python Implementation of Binary Tree
10

8 14

5 9 12 11 ?
Python Implementation of Binary Tree
10

8 14

5 9 12

11
Python Implementation of Binary Tree: Create

Define a class called Node

Allow Node to hold data for


left and right children
objects, with an initial value
of None for both
Python Implementation of Binary Tree: Create

Allow Node to Hold a single


piece of data

Function to Print the Data


(Self means that the object
will refer to itself)
Python Implementation of Binary Tree: Create
Tree:

10

Output:

10
Python Implementation of Binary Tree: Insert
• To insert into a tree we use the same Node class created
above and add a insert method to it.
• The insert class compares the value of the node to the
parent node and decides to add it as a left node or a right
node.
• Finally the PrintTree class is used to print the tree.
Python Implementation of Binary Tree: Insert

Same Class Definition as before


New INSERT Method/Function inside the
Node Class
• If Left is empty AND Less
than Current Data,
• Then insert New Data as
Left Child

• Else, Insert New Data Into


Already Existing Left
Child
“ The insert class compares the value of
the node to the parent node and decides
to add it as a left node or a right node. “
• Else, If Right is empty
AND Greater than
Current Data,
• Then insert New Data
as Right Child

• Else, Insert New Data Into


Already Existing Right Child
• Finally, if it’s neither
Greater than nor Less than,
then Current Data must be
equal to New Data
• Finally, if it’s neither
Greater than nor Less than,
then Current Data must be
equal to New Data
Python Implementation of Binary Tree: Printing the Tree

If Left Exists, recursively


call PrintTree Function

Function to Print the Data


(Self means that the object
will refer to itself)

If Right Exists, recursively


call PrintTree Function
Python Implementation Example

12
Python Implementation Example

12 6 ?

True?
Python Implementation Example

12 6 ?

True?
Python Implementation Example
12

6
Python Implementation Example

12

6 14
Python Implementation Example

12

6 14

3
Python Implementation Example
3 ?

12
True?
6 14

3
Python Implementation Example
3 ?

12

True?
6 14

3
Python Implementation Example
3 ?

12

6 14
Python Implementation Example
3 ?

12

True? 6 14
Python Implementation Example
3 ?

12

6 14
True?
Python Implementation Example
12

6 14

3
Python Implementation Example
12

6 14

True? 3
Python Implementation Example
12

6 14

3
Python Implementation Example
12

6 14

True? 3
Python Implementation Example
12

6 14

3
Python Implementation Example
12

6 14

3
True?
Python Implementation Example
12

6 14

3
Python Implementation Example
12

6 14

True?

3
Python Implementation Example
12

6 14

3 6
Python Implementation Example
12

6 14

True?

3 6
Python Implementation Example
12

6 14

3 6 12
Python Implementation Example
12

6 14

True?

3 6 12
Python Implementation Example
12

6 14

True? 3

3 6 12
Python Implementation Example
12

6 14

3 6 12 14
Python Implementation Example
12

6 14

True?

3 6 12 14
Python Implementation Example

12

6 14

Printing the Tree Output:

3 6 12 14
Traversing a Tree
• The tree can be traversed by deciding on a sequence to
visit each node.
• We can start at a node then visit the left sub-tree first and
right sub-tree next, or we can also visit the right sub-tree
first and left sub-tree next.
Traversing a Tree
• Accordingly there are different names for these tree
traversal methods
• Traversal is a process to visit all the nodes of a tree and
may print their values too.
• Because, all nodes are connected via edges (links) we
always start from the root (head) node.
• That is, we cannot randomly access a node in a tree
Traversing a Tree
• There are three ways which we use to traverse a tree:
• In-order Traversal
• Pre-order Traversal
• Post-order Traversal
In-Order Traversal
• In this traversal method, the left subtree is visited first,
then the root and later the right sub-tree.
• Always remember that every node may represent a
subtree itself.
In-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• Add Data of This Node
to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
• Return Result
In-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• Add Data of This Node
to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
• Return Result
In-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• Add Data of This Node
to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
• Return Result
In-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• Add Data of This Node
to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
• Return Result
In-Order Traversal
27
Traverse()
• Initialize Result [ ]
14 35
• If there is Left Child
– Traverse() Left Child and
add return to Result
10 19 31 42
• Add Data of This Node
to Result
• If there is Right Child
– Traverse() Right Child
10
and add return to Result
• Return Result
In-Order Traversal
27
Traverse()
• Initialize Result [ ]
14 35
• If there is Left Child
– Traverse() Left Child and
add return to Result
10 19 31 42
• Add Data of This Node
to Result
• If there is Right Child
– Traverse() Right Child
10
and add return to Result
• Return Result
In-Order Traversal
27
Traverse()
• Initialize Result [ ]
14 35
• If there is Left Child
– Traverse() Left Child and
add return to Result
10 19 31 42
• Add Data of This Node
to Result
• If there is Right Child
– Traverse() Right Child
10 14
and add return to Result
• Return Result
In-Order Traversal
27
Traverse()
• Initialize Result [ ]
14 35
• If there is Left Child
– Traverse() Left Child and
add return to Result
10 19 31 42
• Add Data of This Node
to Result
• If there is Right Child
– Traverse() Right Child
10 14
and add return to Result
• Return Result
In-Order Traversal
27
Traverse()
• Initialize Result [ ]
14 35
• If there is Left Child
– Traverse() Left Child and
add return to Result
10 19 31 42
• Add Data of This Node
to Result
• If there is Right Child
– Traverse() Right Child
10 14 19
and add return to Result
• Return Result
In-Order Traversal
27
Traverse()
• Initialize Result [ ]
14 35
• If there is Left Child
– Traverse() Left Child and
add return to Result
10 19 31 42
• Add Data of This Node
to Result
• If there is Right Child
– Traverse() Right Child
10 14 19 27
and add return to Result
• Return Result
In-Order Traversal
27
Traverse()
• Initialize Result [ ]
14 35
• If there is Left Child
– Traverse() Left Child and
add return to Result
10 19 31 42
• Add Data of This Node
to Result
• If there is Right Child
– Traverse() Right Child
10 14 19 27
and add return to Result
• Return Result
In-Order Traversal
27
Traverse()
• Initialize Result [ ]
14 35
• If there is Left Child
– Traverse() Left Child and
add return to Result
10 19 31 42
• Add Data of This Node
to Result
• If there is Right Child
– Traverse() Right Child
10 14 19 27
and add return to Result
• Return Result
In-Order Traversal
27
Traverse()
• Initialize Result [ ]
14 35
• If there is Left Child
– Traverse() Left Child and
add return to Result
10 19 31 42
• Add Data of This Node
to Result
• If there is Right Child
– Traverse() Right Child
10 14 19 27 31
and add return to Result
• Return Result
In-Order Traversal
27
Traverse()
• Initialize Result [ ]
14 35
• If there is Left Child
– Traverse() Left Child and
add return to Result
10 19 31 42
• Add Data of This Node
to Result
• If there is Right Child
– Traverse() Right Child
10 14 19 27 31 35
and add return to Result
• Return Result
In-Order Traversal
27
Traverse()
• Initialize Result [ ]
14 35
• If there is Left Child
– Traverse() Left Child and
add return to Result
10 19 31 42
• Add Data of This Node
to Result
• If there is Right Child
– Traverse() Right Child
10 14 19 27 31 35 42
and add return to Result
• Return Result
In-Order Traversal Output
27

14 35

10 19 31 42

10 14 19 27 31 35 42
Pre-Order Traversal
• In this traversal method, the root node is visited first, then
the left subtree and finally the right subtree.
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10 19
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10 19
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10 19
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10 19
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10 19
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10 19
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10 19
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10 19 35
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10 19 35
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10 19 35
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10 19 35 31
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10 19 35 31
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10 19 35 31
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10 19 35 31
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10 19 35 31 42
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10 19 35 31 42
• Return Result
In-Order Traversal Output
27

14 35

10 19 31 42

27 14 10 19 35 31 42
Post-Order Traversal
• In this traversal method, the root node is visited last,
hence the name.
• First, we traverse the left subtree, then the right subtree
and finally the root node.
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
10
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
10
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
10 19
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
10 19
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
10 19 14
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
10 19 14
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
10 19 14
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
10 19 14
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
10 19 14 31
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
10 19 14 31
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
10 19 14 31
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
10 19 14 31 42
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
10 19 14 31 42
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
10 19 14 31 42 35
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
10 19 14 31 42 35
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
10 19 14 31 42 35 27
• Return Result
Euler Tour Traversal
• Is the Generic Traversal of a
binary tree, includes special
cases which are the pre-order,
post-order, and in-order
traversals.
• “Walk” around the tree and visit
each node
Euler Tour Traversal

Output: 1 5 4 2 4 3 4 5 1
Euler Tour Traversal

Output: 1 5 4 2 4 3 4 5 1
Euler Tour Traversal
Euler Tour Traversal
Breadth First Traversals
• Breadth First Traversals will
look through all nodes for a
certain depth of the Binary
Tree before moving to the
next depth

Output: 0 1 2 3 4 5 6
Depth First Traversals
• Depth First Traversals will
look through the left
subtrees first, then move on
the right subtrees.
• Moves similarly to Pre-
Order Traversals, as we
Check the Data First, before
checking the Left Subtree,
then finally checking the
Right Subtree Output: 0 1 3 4 2 5 6

You might also like