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

tree

The document discusses non-linear data structures, focusing on trees and graphs, and contrasts them with linear data structures. It defines key concepts related to trees, such as nodes, edges, parent/child relationships, and various traversal methods (inorder, preorder, postorder). Additionally, it explains tree properties like degree, height, depth, and the concepts of internal and leaf nodes.

Uploaded by

prafulla_mng
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)
6 views

tree

The document discusses non-linear data structures, focusing on trees and graphs, and contrasts them with linear data structures. It defines key concepts related to trees, such as nodes, edges, parent/child relationships, and various traversal methods (inorder, preorder, postorder). Additionally, it explains tree properties like degree, height, depth, and the concepts of internal and leaf nodes.

Uploaded by

prafulla_mng
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/ 48

Unit-4

Non linear Data structure


Nonlinear Data Structures
Trees
Definition and Concepts, Operations on Binary Trees, Linked Storage
Representation of Binary Trees, Tree creation and Traversal.
Graphs
Matrix representation of graphs, Breadth First Search, Depth First
Search.
Trees
Linear Data Structure:
• Data structure where data elements are arranged
sequentially or linearly where the elements are attached
to its previous and next adjacent in what is called a linear
data structure.
• In linear data structure, single level is involved.
Therefore, we can traverse all the elements in single run
only.
• Linear data structures are easy to implement because
computer memory is arranged in a linear way.
• Its examples are array, stack, queue, linked list, etc.
Non-linear Data Structure:

Data structures where data elements are not
arranged sequentially or linearly are called non-
linear data structures.
• In a non-linear data structure, single level is not
involved. Therefore, we can’t traverse all the
elements in single run only.
• Non-linear data structures are not easy to
implement in comparison to linear data
structure. It utilizes computer memory
efficiently in comparison to a linear data
structure.
• Its examples are trees and graphs
Difference between Linear and Non-linear Data Structures:
trees
• Tree represents the nodes connected by edges.
1. 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.
2. 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.
3. Parent-

• 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.
4. Child-

• The node which is a descendant of some


node is called as a child node.
• All the nodes except root node are child
nodes.
5. Siblings-

• Nodes which belong to the same parent


are called as siblings.
• In other words, nodes with the same
parent are sibling nodes.
Here,
Nodes B and C are siblings
Nodes D, E and F are siblings
Nodes G and H are siblings
Nodes I and J are siblings
6. Degree-

• 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.
Here,
Degree of node A = 2
Degree of node B = 3
Degree of node C = 2
Degree of node D = 0
Degree of node E = 2
Degree of node F = 0
Degree of node G = 1
Degree of node H = 0
Degree of node I = 0
Degree of node J = 0
Degree of node K = 0
6. Degree-

• 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.

Here,
Degree of node A = 2
Degree of node B = 3
Degree of node C = 2
Degree of node D = 0
Degree of node E = 2
Degree of node F = 0
Degree of node G = 1
Degree of node H = 0
Degree of node I = 0
Degree of node J = 0
Degree of node K = 0
7. 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.
8. 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.
9.

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.
10. Height-
• 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. Here,
• Height of a tree is the height of root node.
Height of node A = 3
Height of node B = 2
• Height of all leaf nodes = 0 Height of node C = 2
Height of node D = 0
Height of node E = 1
Height of node F = 0
Height of node G = 1
Height of node H = 0
Height of node I = 0
Height of node J = 0
Height of node K = 0
11. Depth-

• 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 Here,
Depth of node A = 0
• The terms “level” and “depth” are used interchangeably.
Depth of node B = 1
• Depth of node C = 1
Depth of node D = 2
Depth of node E = 2
Depth of node F = 2
Depth of node G = 2
Depth of node H = 2
Depth of node I = 3
Depth of node J = 3
Depth of node K = 3
12. Subtree-

• In a tree, each child from a node forms a subtree recursively.
• Every child node forms a subtree on its parent node.

13. Forest-

• A forest is a set of disjoint trees.

Tree Traversal
• tree traversal means traversing or visiting each
node of a tree. Linear data structures like Stack,
Queue, linked list have only one way for
traversing, whereas the tree has various ways to
traverse or visit each node. The following are the
three different ways of traversal:
• Inorder traversal
• Preorder traversal
• Postorder traversal
Inorder Traversal

• An inorder traversal is a traversal technique that follows the policy,


i.e., Left Root Right. Here, Left Root Right means that the left
subtree of the root node is traversed first, then the root node, and
then the right subtree of the root node is traversed.
• Here, inorder name itself suggests that the root node comes in
between the left and the right subtrees.
Preorder Traversal

• A preorder traversal is a traversal technique that follows


the policy, i.e., Root Left Right. Here, Root Left Right means
root node of the tree is traversed first, then the left subtree
and finally the right subtree is traversed.
• Here, the Preorder name itself suggests that the root node
would be traversed first.
Postorder Traversal

• A Postorder traversal is a traversal technique that follows the policy,


i.e., Left Right Root. Here, Left Right Root means the left subtree of
the root node is traversed first, then the right subtree, and finally,
the root node is traversed.
• Here, the Postorder name itself suggests that the root node of the
tree would be traversed at the last.

You might also like