You are on page 1of 24

University Of Science &

Technology Chittagong
Course Title: Data Structures Laboratory
Course Code: CSE212
Samia Akhter
ID:17010117, 3rd Semester, 29th Batch
Department of CSE, FSET, USTC
Traversing Binary Trees
Binary Tree
Definition
A binary tree is made of nodes, where each node contains a "left" reference, a "right"
reference, and a data element. The topmost node in the tree is called the root.

An Instance:
Root

A
Parent
B C
Node

Left Node D E F Right Node


Siblings
Child Node
Every node (excluding a root) in a tree is connected by a directed edge from exactly one
other node. This node is called a parent. On the other hand, each node can be connected
to arbitrary number of nodes, called children. Nodes with no children are called leaves,
or external nodes. Nodes which are not leaves are called internal nodes. Nodes with
the same parent are called siblings.

More Tree Terminology:


• The depth of a node is the number of edges from the root to the node.

• The height of a node is the number of edges from the node to the deepest leaf.
• The height of a tree is a height of the root.
• A full binary tree.is a binary tree in which each node has exactly zero or two children.
• A complete binary tree is a binary tree, which is completely filled, with the possible
exception of the bottom level, which is filled from left to right.
Full Tree Complete Tree

A complete binary tree is very special tree, it provides the best possible ratio between the
number of nodes and the height. The height h of a complete binary tree with N nodes is at
most O(log N). We can easily prove this by counting nodes on each level, starting with the
root, assuming that each level has the maximum number of nodes
Traversals
Definition
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

• Depth-first traversal
• Breadth-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;
In the next picture we demonstrate the order of node visitation. Number 1 denote the
first node in a particular traversal and 7 denote the last node.

1 5 7

2 6 2 6 4 6

3 4 7 1 4 7 1 3 5

5 3 2

PreOrder InOrder PostOrder


Algorithms
Algorithm Preorder(tree)
1. Visit the root.
2. Traverse the left subtree, i.e., call Preorder(left-subtree)
3. Traverse the right subtree, i.e., call Preorder(right-subtree)

Algorithm Inorder(tree)
1. Traverse the left subtree, i.e., call Inorder(left-subtree)
2. Visit the root.
3. Traverse the right subtree, i.e., call Inorder(right-subtree)

Algorithm Postorder(tree)
1. Traverse the left subtree, i.e., call Postorder(left-subtree)
2. Traverse the right subtree, i.e., call Postorder(right-subtree)
3. Visit the root.
How To Traverse a Binary Tree?
Pre-order Traversal

1 1 1

2 6 2 6 2 6

3 4 7 3 4 3 4

5
Main Tree
1
1
2
Depth First Traversals:
Preorder (Root, Left, Right) 2 6
4
We start from 1, and following pre-order traversal, we first visit 1 itself 7
and then move to its left subtree 2. 2 is also traversed pre-order. The
5
process goes on until all the nodes are visited. The output of pre-order
tra ersal of this tree ill be −
→ → → → → →7
In-order Traversal

5 5 5 5
2 6 2 2 2 6

1 4 7 1 4 1 4 1 4 7

3 3 3 3
Main Tree

Depth First Traversals:


Inorder (Left, Root, Right) 1

2 6
We start from 5, and following in-order traversal, we move to its left
subtree 2. 2 is also traversed in-order. The process goes on until all the
7
nodes are visited. The output of inorder tra ersal of this tree ill be −
1→2→3→4→5→6→7
Post-order Traversal

7 7 7
4 6 4 4 6

1 3 5 1 3 1 3 5

2 2
7
Main Tree 7
Depth First Traversals: 4 6
4 6
Postorder (Left, Right, Root)
5
We start from 7, and following Post-order traversal, we first visit the left
5
subtree 4. 4 is also traversed post-order. The process goes on until all
the nodes are visited. The output of post-order traversal of this tree will
be −
1→ →3→4→5→6→7
More Instances of Traversing Binary Trees
#1 D

a E

T u c e

A c s 2 m

s T 1

1 x
U
t r
E a
r

Preorder: DaTAstrucTUrEcse211Exam
Inorder: AtsrTacrUTuDcsE21E1xaem
Postorder: trsATrUTcuascE1ax12meED
#2
A

B C

D E F G

H I J K

L
N M
N O

L
Preorder: ABDEHLNLNOOICFGJNMK
N O Inorder: DBNLONLOHEIAFCNJMGK
Postorder: DNOLNOLHIEBFNMJKGCA
#3
A

B C

D E G H

F J K

Preorder: ABDEFCGHJLK
Inorder: DBFEAGCLJHK
Postorder: DFEBGLJKHCA
#4
A

B C

D E F G

H I J K L M N O

Preorder: ABDHIEJKCFLMGNO
Inorder: HDIBJEKALFMCNGO
Postorder: HIDJKEBLMFNOGCA
#5

C D

E F G H

I J K L

Preorder: ABCEIFJDGHKL
Inorder: EICFJBGDKHLA
Postorder: IEJFCGKLHDBA
#6
A

B C

D E F G

H I J K

M
L

N O Preorder: ABDEHILNOCFGJMK
Inorder: DBNLOHEIAFCJMGK
Postorder: DNOLHIEBFMJKGCA
#7
1

2 3

4 5 8

6 9 ’

7 ’

Preorder: ’ ’
Inorder: ’ ’
Postorder: ’ ’
#8

B C

D E F G

H I J

Preorder: ABDHIEJCFG
Inorder: HDIBJEAFCG
Postorder: HIDJEBFGCA
#9
A

F C

G I D

M H E

F N J

O K
Preorder: ABFGMFNOHCIDEJKL
Inorder: FFMNOGHBICDJ
Postorder: FONMHGFILKJEDCBA L
#10
A

B G

C H L

D I M

E J P N

F K R Q O

Preorder: ABCDEFGHIJKLMPRQNO
Inorder: BDEFCAIJKHGRPQMNOL
Postorder: FEDCBKJIHRQPONMLGA
‫‪Thank You‬‬

‫جزاك ُ‬
‫هللا خيرًا‬

You might also like