0% found this document useful (0 votes)
15 views

Data - Structure - and - Algorithms - Lecture - 7 Trees

Uploaded by

Hasnain Nisar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Data - Structure - and - Algorithms - Lecture - 7 Trees

Uploaded by

Hasnain Nisar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Course Instructor

Engr. Adiba Jafar


CHAPTER #7
TREES
Introduction to Tree DS
 A tree is a nonlinear data structure, compared to
arrays, linked lists, stacks and queues which are
linear data structures. A tree can be empty with no
nodes or a tree is a structure consisting of one
node called the root and zero or one or more
subtrees.

TREE
Tree is a non-linear data structure which organizes
data in a hierarchical structure, and this is a recursive
definition.
OR
A tree is a connected graph without any circuits.
OR
If in a graph, there is one and only one path between
every pair of vertices, then graph is called as a TREE
DIFFERENCE BETEEN TREE AND
GRAPH
Properties of tree
 The important properties of tree data structure are-
1. There is one and only one path between every
pair of vertices in a tree.
2. A tree with n vertices has exactly (n-1) edges.
3. A graph is a tree if and only if it is minimally
connected.
4. Any connected graph with n vertices and (n-1)
edges is a tree.
Tree Terminology
Root
• The first node from where the tree originates is
called as a root node.
• In any tree, there must be only one root node.
• We can never have multiple root nodes in a tree data
structure.
EDGE
• The connecting link between any two nodes is called
as an edge.
• In a tree with n number of nodes, there are exactly
(n-1) number of edges.
PARENT NODE
• The node which has a branch from it to any other
node is called as a parent node.
• In other words, the node which has one or more
children is called as a parent node.
• In a tree, a parent node can have any number of
child nodes.
CHILD NODE
• The node which is a descendant of some node is
called as a child node.
• All the nodes except root node are child nodes.
SIBLINGS NODES
• Nodes which belong to the same parent are called
as siblings.
• In other words, nodes with the same parent are
sibling nodes.
DEGREE OF NODE
• Degree of a node is the total number of children of
that node.
• Degree of a tree is the highest degree of a node
among all the nodes in the tree.
INTERNAL NODE
• The node which has at least one child is called as
an internal node.
• Internal nodes are also called as non-terminal
nodes.
• Every non-leaf node is an internal node.
LEAF NODE
• The node which does not have any child is called as
a leaf node.
• Leaf nodes are also called as external
nodes or terminal nodes.
LEVEL
• In a tree, each step from top to bottom is called
as level of a tree.
• The level count starts with 0 and increments by 1 at
each level or step.
HEIGHT OF TREE
• Total number of edges that lies on the longest path
from any leaf node to a particular node is called
as height of that node.
• Height of a tree is the height of root node.
• Height of all leaf nodes = 0
DEPTH OF TREE
• Total number of edges from root node to a particular
node is called as depth of that node.
• Depth of a tree is the total number of edges from
root node to a leaf node in the longest path.
• Depth of the root node = 0
• The terms “level” and “depth” are used
interchangeably.
DEPTH OF TREE
SUBTREE
• In a tree, each child from a node forms
a subtree recursively.
• Every child node forms a subtree on its parent node.
FOREST
 A forest is a set of disjoint trees.
BINARY TREE
 Binary tree is a special tree data structure in which
each node can have at most 2 children.
 Thus, in a binary tree,Each node has either 0 child or
1 child or 2 children.
TYPES OF BINARY TREE
ROOTED BINARY TREE
A rooted binary tree is a binary tree that satisfies the
following 2 properties-
• It has a root node.
• Each node has at most 2 children.
FULL/STRICTLY BINARY TREE
• A binary tree in which every node has either 0 or 2
children is called as a Full binary tree.
• Full binary tree is also called as Strictly binary tree.
COMPLETE/PERFECT BINARY
TREE
A complete binary tree is a binary tree that satisfies
the following 2 properties-
• Every internal node has exactly 2 children.
• All the leaf nodes are at the same level.
Complete binary tree is also called as Perfect binary
tree.
ALMOST COMPLETE BINARY
TREE
An almost complete binary tree is a binary tree that
satisfies the following 2 properties-
• All the levels are completely filled except possibly
the last level.
• The last level must be strictly filled from left to right.
SKEWED BINARY TREE
A skewed binary tree is a binary tree that satisfies the
following 2 properties-
• All the nodes except one node has one and only one
child.
• The remaining node has no child.
OR
A skewed binary tree is a binary tree of n nodes such
that its depth is (n-1).
CONTINUE
Linked representation of binary
tree
CONTINUE
TREE TRAVERSALS
Tree traversal refers to the process of visiting each node
in a tree data structure exactly once.
 Various tree traversal techniques are-
PREORDER TRAVERSAL
 Algorithm-
1. Visit the root
2. Traverse the left sub tree i.e. call Preorder (left sub
tree)
3. Traverse the right sub tree i.e. call Preorder (right
sub tree) Root → Left → Right
Preorder traversal shortcut
Traverse the entire tree starting from the root node
keeping yourself to the left.
Application
• Preorder traversal is used to get prefix expression of
an expression tree.

• Preorder traversal is used to create a copy of the


tree.
INORDER TRAVERSAL
Algorithm-
1. Traverse the left sub tree i.e. call Inorder (left sub
tree)
2. Visit the root
3. Traverse the right sub tree i.e. call Inorder (right sub
tree)
Left → Root → Right
Short cut of Inorder Traversal

Inorder traversal is used to get infix expression of an expression tree.


POST ORDER TRAVERSAL
Algorithm:-
1. Traverse the left sub tree i.e. call Postorder (left sub
tree)
2. Traverse the right sub tree i.e. call Postorder (right
sub tree)
3. Visit the root (Left → Right → Root)
Short cut of Post order traversal
APPLICATION OF POST ORDER
• Postorder traversal is used to get postfix expression
of an expression tree.
• Postorder traversal is used to delete the tree.
• This is because it deletes the children first and then
it deletes the parent.
BREADTH FIRST TRAVERSAL
• Breadth First Traversal of a tree prints all the nodes
of a tree level by level.
• Breadth First Traversal is also called as Level Order
Traversal.

Level order traversal is used to print the data in the same order as stored in the
array representation of a complete binary tree.
HOME TASKS
If the binary tree in figure is traversed in
inorder,postorder and preorder, then the order in which
the nodes will be visited is ____?
HOME TASKS
Which of the following binary trees has its inorder and
preorder traversals as BCAD and ABCD respectively-

You might also like