Module 4
Presented by
Bhavana G
Assistant Professor
Dept. of Computer Science(MCA)
Center for post Graduate studies, VTU
Muddenahalli.
1
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
2
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
Introduction of Trees in Data
Structure
Course Code: OBCA201
Course outcomes
• CO 1 : Identify different types of data structures, operations and algorithms.
• CO2: Illustrate searching and sorting operations on files.
• CO3:Demonstrate the working of stack, Queue, Lists, Trees and Graphs in
problem solving & implement all data structures in a high-level language for
problem solving.
3
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
Textbooks & Reference Books
1. Adam Drozdek, “Data Structures and Algorithms in C++”, 2013, Fourth Edition,
Cengage Learning .
2. Bhushan Trivedi, “Programming with ANSI C++”, Oxford Press, Second Edition, 2012.
3. Balagurusamy E, Object Oriented Programming with C++, Tata McGraw Hill
Education Pvt.Ltd, Fourth Edition 2010.
4. R.S. Salaria, “ Data Structures & Algorithms” , Khanna Book Publishing Co.
(P)Ltd..,2002
4
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
AGENDA:
• What is Tree Data Structure?
• Why Tree Data Structure?
• Tree Terminologies.
• Tree Node.
• Types of Trees.
• Tree Traversal
• Application of Trees(BST)
5
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
WHAT IS TREE DATA STRUCTURE?
• A Tree is a Non-Linear data Structure that consists of
nodes and is connected by edges.
6
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
WHY TREE DATA STRUCTURE?
7
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
• A linear Data structure that stores data elements in sequential form.
• In operation performed in linear data structure, the time complexity
increases with increased data size.
8
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
10 20 30 40 50 60 70
9
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
10
20 30
40 50 60 70
The Tree Data structure is non-linear and allows easier and
quicker access to the data elements.
The Tree data structure can be useful in many
cases:
• Hierarchical Data: File systems, organizational models, etc.
• Databases: Used for quick data retrieval.
• Routing Tables: Used for routing data in network algorithms.
• Sorting/Searching: Used for sorting data and searching for data.
• Priority Queues: Priority queue data structures are commonly implemented using
trees, such as binary heaps.
10
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
Tree Terminologies
• Tree Node :A node is a structure or an entity that contains a key or
value and pointers to its child node.
•
•
11
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
Tree Terminologies
• Root: In a tree data structure, the root is the first node of
the tree. The root node is the initial node of the tree in data
structures.
• In the tree data structure, there must be only one root node.
•
12
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
13
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
• Edge :In a tree in data structures, the connecting link of any
two nodes is called the edge of the tree data structure.
• In the tree data structure, N number of nodes connecting
with N -1 number of edges.
Tree Terminologies
• Parent :In the tree in data structures, the node that is the
predecessor of any node is known as a parent node, or a node
with a branch from itself to any other successive node is called
the parent node.
14
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
Tree Terminologies
Tree Terminologies
Child:
• The node, a descendant of any node, is
known as child nodes in data structures.
• In a tree, any number of parent nodes can
have any number of child nodes.
• In a tree, every node except the root node
is a child node.
15
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
Tree Terminologies
Siblings:
• In trees in the data structure, nodes
that belong to the same parent are
called siblings.
16
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
Tree Terminologies
Leaf :
• Trees in the data structure, the
node with no child, is known as a
leaf node.
• In trees, leaf nodes are also called
external nodes or terminal nodes.
17
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
Tree Terminologies
Internal nodes
• Trees in the data structure have at least one child
node known as internal nodes.
• In trees, nodes other than leaf nodes are internal
nodes.
• Sometimes root nodes are also called internal nodes
if the tree has more than one node.
18
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
Tree Terminologies
19
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
Degree:
• In the tree data structure, the total number of
children of a node is called the degree of the
node.
•The highest degree of the node among all the
nodes in a tree is called the Degree of Tree.
Tree Terminologies
Level:
• In tree data structures, the root node
is said to be at level 0, and the root
node's children are at level 1, and the
children of that node at level 1 will be
level 2, and so on.
20
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
Tree Terminologies
• Height:
• In a tree data structure, the number of
edges from the leaf node to the particular
node in the longest path is known as the
height of that node.
• In the tree, the height of the root node is
called "Height of Tree".
• The tree height of all leaf nodes is 0.
21
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
Depth:
• In a tree, many edges from the root
node to the particular node are called
the depth of the tree.
• In the tree, the total number of edges
from the root node to the leaf node in
the longest path is known as "Depth of
Tree".
• In the tree data structures, the depth of
the root node is 0.
22
Tree Terminologies
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
Path:
• In the tree in data structures, the
sequence of nodes and edges from one
node to another node is called the path
between those two nodes.
• The length of a path is the total number
of nodes in a path.zx
23
Tree Terminologies
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
Subtree
• In the tree in data structures, each
child from a node shapes a sub-tree
recursively and every child in the tree
will form a sub-tree on its parent
node.
24
Tree Terminologies
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
Here are the different kinds of tree in data structures:
General Tree:
• The general tree is the type of tree where there are no
constraints on the hierarchical structure.
Properties
• The general tree follows all properties of the tree data
structure.
• A node can have any number of nodes.
25
Types of Tree in Data Structures
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
Types of Tree in Data Structures
Binary Tree
• A binary tree has the following properties:
Properties
• Follows all properties of the tree data structure.
• Binary trees can have at most two child nodes.
• These two children are called the left child and the
right child.
26
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
Binary Search Tree
• A binary search tree is a type of tree that is a
more constricted extension of a binary tree data
structure.
Properties
• Follows all properties of the tree data structure.
• The binary search tree has a unique property
known as the binary search property. This states
that the value of a left child node of the tree
should be less than or equal to the parent node
value of the tree. And the value of the right child
node should be greater than or equal to the
parent value.
27
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
AVL Tree
• An AVL tree is a type of tree that is a self-balancing
binary search tree.
Properties
• Follows all properties of the tree data structure.
• Self-balancing.
• Each node stores a value called a balanced factor, which
is the difference in the height of the left sub-tree and
right sub-tree.
• All the nodes in the AVL tree must have a balance factor
of -1, 0, and 1.
28
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
Tree Traversal
29
Traversal of the tree in data structures is a process of visiting
each node and prints their value. There are three ways to
traverse tree data structure.
Pre-order Traversal
In-Order Traversal
Post-order Traversal
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
In the in-order traversal, the left subtree is visited first, then
the root, and later the right subtree.
Algorithm:
Inorder(tree)
• Traverse the left subtree, i.e., call Inorder(left->subtree)
• Visit the root.
• Traverse the right subtree, i.e., call Inorder(right-
>subtree)
30
In-Order Traversal
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
In pre-order traversal, it visits the root node
first, then the left subtree, and lastly right
subtree.
Algorithm:
Preorder(tree)
• Visit the root.
• Traverse the left subtree, i.e., call Preorder(left->subtree)
• Traverse the right subtree, i.e., call Preorder(right-
>subtree)
31
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
Pre-Order Traversal
Post-Order Traversal
It visits the left subtree first in post-order traversal, then the
right subtree, and finally the root node.
Algorithm:
Postorder(tree)
• Traverse the left subtree, i.e., call Postorder(left->subtree)
• Traverse the right subtree, i.e., call Postorder(right->subtree)
• Visit the root.
32
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
33
• Binary Search Tree (BST) is used to check whether elements
present or not.
• Heap is a type of tree that is used to heap sort.
• Tries are the modified version of the tree used in modem routing to
information of the router.
• The widespread database uses B-tree.
• Compilers use syntax trees to check every syntax of a program.
• With this, you’ve come to the end of this tutorial about the tree in
data structures.
Application of Tree in Data Structures
Latharani T R, Assistant Professor, Dept. of CS&E, JIT, Davangere 34

data structure module fourth module linked list

  • 1.
    Module 4 Presented by BhavanaG Assistant Professor Dept. of Computer Science(MCA) Center for post Graduate studies, VTU Muddenahalli. 1 Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
  • 2.
    2 Bhavana G, AssistantProfessor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli Introduction of Trees in Data Structure Course Code: OBCA201
  • 3.
    Course outcomes • CO1 : Identify different types of data structures, operations and algorithms. • CO2: Illustrate searching and sorting operations on files. • CO3:Demonstrate the working of stack, Queue, Lists, Trees and Graphs in problem solving & implement all data structures in a high-level language for problem solving. 3 Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
  • 4.
    Textbooks & ReferenceBooks 1. Adam Drozdek, “Data Structures and Algorithms in C++”, 2013, Fourth Edition, Cengage Learning . 2. Bhushan Trivedi, “Programming with ANSI C++”, Oxford Press, Second Edition, 2012. 3. Balagurusamy E, Object Oriented Programming with C++, Tata McGraw Hill Education Pvt.Ltd, Fourth Edition 2010. 4. R.S. Salaria, “ Data Structures & Algorithms” , Khanna Book Publishing Co. (P)Ltd..,2002 4 Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
  • 5.
    AGENDA: • What isTree Data Structure? • Why Tree Data Structure? • Tree Terminologies. • Tree Node. • Types of Trees. • Tree Traversal • Application of Trees(BST) 5 Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
  • 6.
    WHAT IS TREEDATA STRUCTURE? • A Tree is a Non-Linear data Structure that consists of nodes and is connected by edges. 6 Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
  • 7.
    WHY TREE DATASTRUCTURE? 7 Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
  • 8.
    • A linearData structure that stores data elements in sequential form. • In operation performed in linear data structure, the time complexity increases with increased data size. 8 Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli 10 20 30 40 50 60 70
  • 9.
    9 Bhavana G, AssistantProfessor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli 10 20 30 40 50 60 70 The Tree Data structure is non-linear and allows easier and quicker access to the data elements.
  • 10.
    The Tree datastructure can be useful in many cases: • Hierarchical Data: File systems, organizational models, etc. • Databases: Used for quick data retrieval. • Routing Tables: Used for routing data in network algorithms. • Sorting/Searching: Used for sorting data and searching for data. • Priority Queues: Priority queue data structures are commonly implemented using trees, such as binary heaps. 10 Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
  • 11.
    Tree Terminologies • TreeNode :A node is a structure or an entity that contains a key or value and pointers to its child node. • • 11 Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
  • 12.
    Tree Terminologies • Root:In a tree data structure, the root is the first node of the tree. The root node is the initial node of the tree in data structures. • In the tree data structure, there must be only one root node. • 12 Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
  • 13.
    13 Bhavana G, AssistantProfessor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli • Edge :In a tree in data structures, the connecting link of any two nodes is called the edge of the tree data structure. • In the tree data structure, N number of nodes connecting with N -1 number of edges. Tree Terminologies
  • 14.
    • Parent :Inthe tree in data structures, the node that is the predecessor of any node is known as a parent node, or a node with a branch from itself to any other successive node is called the parent node. 14 Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli Tree Terminologies
  • 15.
    Tree Terminologies Child: • Thenode, a descendant of any node, is known as child nodes in data structures. • In a tree, any number of parent nodes can have any number of child nodes. • In a tree, every node except the root node is a child node. 15 Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
  • 16.
    Tree Terminologies Siblings: • Intrees in the data structure, nodes that belong to the same parent are called siblings. 16 Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
  • 17.
    Tree Terminologies Leaf : •Trees in the data structure, the node with no child, is known as a leaf node. • In trees, leaf nodes are also called external nodes or terminal nodes. 17 Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
  • 18.
    Tree Terminologies Internal nodes •Trees in the data structure have at least one child node known as internal nodes. • In trees, nodes other than leaf nodes are internal nodes. • Sometimes root nodes are also called internal nodes if the tree has more than one node. 18 Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
  • 19.
    Tree Terminologies 19 Bhavana G,Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli Degree: • In the tree data structure, the total number of children of a node is called the degree of the node. •The highest degree of the node among all the nodes in a tree is called the Degree of Tree.
  • 20.
    Tree Terminologies Level: • Intree data structures, the root node is said to be at level 0, and the root node's children are at level 1, and the children of that node at level 1 will be level 2, and so on. 20 Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
  • 21.
    Tree Terminologies • Height: •In a tree data structure, the number of edges from the leaf node to the particular node in the longest path is known as the height of that node. • In the tree, the height of the root node is called "Height of Tree". • The tree height of all leaf nodes is 0. 21 Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
  • 22.
    Depth: • In atree, many edges from the root node to the particular node are called the depth of the tree. • In the tree, the total number of edges from the root node to the leaf node in the longest path is known as "Depth of Tree". • In the tree data structures, the depth of the root node is 0. 22 Tree Terminologies Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
  • 23.
    Path: • In thetree in data structures, the sequence of nodes and edges from one node to another node is called the path between those two nodes. • The length of a path is the total number of nodes in a path.zx 23 Tree Terminologies Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
  • 24.
    Subtree • In thetree in data structures, each child from a node shapes a sub-tree recursively and every child in the tree will form a sub-tree on its parent node. 24 Tree Terminologies Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
  • 25.
    Here are thedifferent kinds of tree in data structures: General Tree: • The general tree is the type of tree where there are no constraints on the hierarchical structure. Properties • The general tree follows all properties of the tree data structure. • A node can have any number of nodes. 25 Types of Tree in Data Structures Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
  • 26.
    Types of Treein Data Structures Binary Tree • A binary tree has the following properties: Properties • Follows all properties of the tree data structure. • Binary trees can have at most two child nodes. • These two children are called the left child and the right child. 26 Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
  • 27.
    Binary Search Tree •A binary search tree is a type of tree that is a more constricted extension of a binary tree data structure. Properties • Follows all properties of the tree data structure. • The binary search tree has a unique property known as the binary search property. This states that the value of a left child node of the tree should be less than or equal to the parent node value of the tree. And the value of the right child node should be greater than or equal to the parent value. 27 Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
  • 28.
    AVL Tree • AnAVL tree is a type of tree that is a self-balancing binary search tree. Properties • Follows all properties of the tree data structure. • Self-balancing. • Each node stores a value called a balanced factor, which is the difference in the height of the left sub-tree and right sub-tree. • All the nodes in the AVL tree must have a balance factor of -1, 0, and 1. 28 Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
  • 29.
    Tree Traversal 29 Traversal ofthe tree in data structures is a process of visiting each node and prints their value. There are three ways to traverse tree data structure. Pre-order Traversal In-Order Traversal Post-order Traversal Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
  • 30.
    In the in-ordertraversal, the left subtree is visited first, then the root, and later the right subtree. Algorithm: Inorder(tree) • Traverse the left subtree, i.e., call Inorder(left->subtree) • Visit the root. • Traverse the right subtree, i.e., call Inorder(right- >subtree) 30 In-Order Traversal Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
  • 31.
    In pre-order traversal,it visits the root node first, then the left subtree, and lastly right subtree. Algorithm: Preorder(tree) • Visit the root. • Traverse the left subtree, i.e., call Preorder(left->subtree) • Traverse the right subtree, i.e., call Preorder(right- >subtree) 31 Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli Pre-Order Traversal
  • 32.
    Post-Order Traversal It visitsthe left subtree first in post-order traversal, then the right subtree, and finally the root node. Algorithm: Postorder(tree) • Traverse the left subtree, i.e., call Postorder(left->subtree) • Traverse the right subtree, i.e., call Postorder(right->subtree) • Visit the root. 32 Bhavana G, Assistant Professor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli
  • 33.
    Bhavana G, AssistantProfessor, Dept. of CS(MCA), CPGSB,VTU, Muddenahalli 33 • Binary Search Tree (BST) is used to check whether elements present or not. • Heap is a type of tree that is used to heap sort. • Tries are the modified version of the tree used in modem routing to information of the router. • The widespread database uses B-tree. • Compilers use syntax trees to check every syntax of a program. • With this, you’ve come to the end of this tutorial about the tree in data structures. Application of Tree in Data Structures
  • 34.
    Latharani T R,Assistant Professor, Dept. of CS&E, JIT, Davangere 34