Вы находитесь на странице: 1из 17

Data Structure and Algorithms

Dr. D. P. Acharjya
Professor, SCOPE
24-Mar-17 Dr. D. P. Acharjya 1
Introduction to Trees
A connected acyclic graph is a tree.
A tree is a finite set of one or more
nodes such that
There is a specially designated node called root.
Remaining nodes are partitioned into n-disjoint
sets Ti, i = 1,2, , n where each Ti is a tree.
It is an efficient way of storing and
organizing data that is naturally
hierarchical.
24-Mar-17 Dr. D. P. Acharjya 2
Example of Tree

24-Mar-17 Dr. D. P. Acharjya 3


Fundamental Terminologies
Node: It is the main component of
any tree structure that stores actual
data and links to other nodes.
Parent: Parent of a node is the
immediate predecessor of a node.
Child: All immediate successors of a
node are known as child. If it has two
child, we can say left child and right
child.

24-Mar-17 Dr. D. P. Acharjya 4


Root: It is a special designated node
which has no parent.
Leaf: A node which does not have any
child is called leaf.
Sibling: Nodes having same parent
are called siblings.
Internal node: Nodes that are having
parent as well as child is called
internal node.
24-Mar-17 Dr. D. P. Acharjya 5
Pictorial View
Root

Parent Internal nodes

Sibling
Link

Leaf

24-Mar-17 Dr. D. P. Acharjya 6


Level, Height and Degree
Level: Level is the rank of the
hierarchy and root node is termed as
in level 0.
Height: Maximum number of nodes
that is possible in a path starting
from root node to a leaf node is called
the height of a tree.
Degree: The number of sub-trees of a
node is called degree of that node.

24-Mar-17 Dr. D. P. Acharjya 7


Level 0

Degree 2 Degree 3 Level 1

Degree 1 Level 2

Level 3

Height 5 Level 4

24-Mar-17 Dr. D. P. Acharjya 8


Binary Tree
A binary tree can be empty or each node of
the tree may have at most two children.

Full Binary Tree: A binary tree that contains


maximum possible number of nodes in all levels
is called a full binary tree.

Complete Binary Tree: A binary tree that


contains maximum possible number of nodes in
all levels except the last level is called complete
binary tree.
24-Mar-17 Dr. D. P. Acharjya 9
Example of Binary Tree
Level 0

Level 1

Level 2

Level 3

Level 4

24-Mar-17 Dr. D. P. Acharjya 10


Example of Full Binary Tree

Level 0 Nodes 1

Level 1 Nodes 2

Level 2 Nodes 4

Level 3 Nodes 8

Level 4 Nodes 16

24-Mar-17 Dr. D. P. Acharjya 11


Example of Complete Binary Tree

Level 0 Nodes 1

Level 1 Nodes 2

Level 2 Nodes 4

Level 3 Nodes 8

Level 4 Nodes 12

24-Mar-17 Dr. D. P. Acharjya 12


Tree Traversal
Preorder
In this traversal root is visited first, then left subtree
in preorder and then right subtree in preorder. (RTlTr)

Inorder
In this traversal, before visiting the root node, left
subtree of the root node is to be visited, then root
node and after right subtree of the root node will be
visited. (TlRTr)

Postorder
In this traversal left subtree of the root node is
visited first followed by right subtree and at end the
root node. (TlTrR)

24-Mar-17 Dr. D. P. Acharjya 13


Preorder Traversal Algorithm

1. Visit the root node R.


2. Traverse the left subtree of R in
preorder.
3. Traverse the right subtree of R in
preorder.

24-Mar-17 Dr. D. P. Acharjya 14


Inorder Traversal Algorithm

1. Traverse the left subtree of the root


node R in inorder.
2. Visit the root node R.
3. Traverse the right subtree of the root
node R in inorder.

24-Mar-17 Dr. D. P. Acharjya 15


Postorder Traversal Algorithm

1. Traverse the left subtree of R in


postorder.
2. Traverse the right subtree of R in
postorder.
3. Visit the root node R.

24-Mar-17 Dr. D. P. Acharjya 16


Illustrative Example
Consider the expression: +
(A-B) + (c * (D/E))

Preorder traversal - *
+AB*C/DE
A B C /
Inorder traversal
AB+C*D/E D E

Postorder traversal
ABCDE/*+

24-Mar-17 Dr. D. P. Acharjya 17

Вам также может понравиться