Kandahar university
Faculty of Computer
Science
1
Data Structures
Lecture 07
Introduction to Tree Data
Structure
Sayed Shah Agha
Contents
2
Introduction to tree
Terminology of tree
Binary tree
Binary tree types
Binary Tree Implementation
Traversing the Binary Tree
End
What is Tree?
• A tree is a nonlinear hierarchical data structure that consists
of nodes connected by edges.
3
Why Tree Data Structure?
4
• Other data structures such as arrays, linked list, stack, and queue
are linear data structures that store data sequentially.
• In order to perform any operation in a linear data structure, the
time complexity increases with the increase in the data size. But,
it is not acceptable in today's computational world.
• Different tree data structures allow quicker and easier access to the
data as it is a non-linear data structure.
Tree Terminologies
• Node A node is an entity that contains a key or value and pointers
to its child nodes. The last nodes of each path are called leaf nodes
or external nodes that do not contain a link/pointer to child
nodes.
• Edge It is the link between any two nodes.
5
Tree Terminologies
6
• Root It is the topmost node of a tree.
• Height of a Node The height of a node is the number of edges
from the node to the deepest leaf (ie. the longest path from
the node to a leaf node).
• Depth of a Node The depth of a node is the number of edges
from
the root to the node.
• Height of a Tree The height of a Tree is the height of the root
node or the depth of the deepest node.
Tree Terminologies
• Height and depth of each node in a tree
7
Tree Terminologies
• Degree of a Node The degree of a node is the total number of
branches of that node.
• Forest A collection of disjoint trees is called a forest.
🞄Creating forest from a tree
🞄 You can create a forest by cutting the root of a
tree.
8
Types of Tree
9
• Binary Tree
• Binary Search Tree
• AVL Tree
• B-Tree
Tree Traversal
10
• In order to perform any operation on a tree, you need to reach to
the specific node. The tree traversal algorithm helps in visiting
a required node in the tree.
• Traversing a tree means visiting every node in the tree. You might,
for instance, want to add all the values in the tree or find the largest
one. For all these operations, you will need to visit each node of
the tree.
• Linear data structures like arrays, stacks, queues, and linked
list have only one way to read the data. But a hierarchical
data structure like a tree can be traversed in different ways.
Types of Traversal
11
• Depending on the order in which we do this, there can be three
types of traversal.
🞄Inorder traversal
🞄Preorder traversal
🞄Postorder traversal
Inorder traversal
12
• First, visit all the nodes in the left subtree
• Then the root node
• Visit all the nodes in the right subtree
🞄 Inorder(root->left)
🞄Display(root->data)
🞄 Inorder(root->right)
Preorder traversal
13
• Visit root node
• Visit all the nodes in the left subtree
• Visit all the nodes in the right subtree
🞄display(root->data)
🞄preorder(root->left)
🞄preorder(root->right)
Postorder traversal
14
• Visit all the nodes in the left subtree
• Visit all the nodes in the right subtree
• Visit the root node
🞄postorder(root->left)
🞄postorder(root->right)
🞄display(root->data)
Tree Applications
15
• Binary Search Trees(BSTs) are used to quickly check whether an
element is present in a set or not.
• Heap is a kind of tree that is used for heap sort.
• A modified version of a tree called Tries is used in modern routers
to store routing information.
• Most popular databases use B-Trees and T-Trees, which are
variants of the tree structure we learned above to store their
data
• Compilers use a syntax tree to validate the syntax of every
program you write.
Binary Tree
• A binary tree is a tree data structure in which each parent node
can have at most two children. For example,
16
Types of Binary Tree
17
• Full Binary Tree
• Perfect Binary Tree
• Complete Binary Tree
• Balanced Binary Tree
• Degenerate or Pathological Tree
Full Binary
Tree
• A full Binary tree is a special type of binary tree in which
every parent node/internal node has either two or no children.
18
Perfect Binary
Tree
• A perfect binary tree is a type of binary tree in which every
internal node has exactly two child nodes and all the leaf nodes
are at the same level.
19
Complete Binary
Tree
• A complete binary tree is just like a full binary tree, but with
two major differences
🞄 Every level must be completely filled
🞄 All the leaf elements must lean towards the left.
🞄 The last leaf element might not have a right sibling i.e. a complete
binary
tree doesn't have to be a full binary tree.
20
Balanced Binary
Tree
• It is a type of binary tree in which the difference between the left
and the right subtree for each node is either 0 or 1.
21
Degenerate or Pathological
Tree
• A degenerate or pathological tree is the tree having a single
child either left or right.
22
Binary Tree Representation
• A node of a binary tree is represented by a class containing a
data part and two objects to other classes of the same type.
🞄 Class Node{ int data; Node left; Node right; };
23
Basic Operations
24
• Following are the basic operations of a tree −
🞄 Search − Searches an element in a tree.
🞄 Insert − Inserts an element in a tree.
🞄 Pre-order Traversal − Traverses a tree in a pre-order manner.
🞄 In-order Traversal − Traverses a tree in an in-order manner.
🞄 Post-order Traversal − Traverses a tree in a post-order
manner.
Binary Tree Applications
25
• For easy and quick access to data
• In router algorithms
• To implement heap data structure
• Syntax tree
End
26
Would anyone like to ask any questions?

More Related Content

PPT
UNIT-4 TREES.ppt
PPTX
UNIT III Non Linear Data Structures - Trees.pptx
PPTX
Introduction to Tree_Data Structure.pptx
PPTX
unit-2-data structure and algorithms-tree-2024-1.pptx
PPTX
UNIT III Non Linear Data Structures - Trees.pptx
PPTX
tree Data Structures in python Traversals.pptx
PPTX
Tree.pptx
PDF
unit-2-dsa-tree-2024-1 (1) (1).pdf data structure
UNIT-4 TREES.ppt
UNIT III Non Linear Data Structures - Trees.pptx
Introduction to Tree_Data Structure.pptx
unit-2-data structure and algorithms-tree-2024-1.pptx
UNIT III Non Linear Data Structures - Trees.pptx
tree Data Structures in python Traversals.pptx
Tree.pptx
unit-2-dsa-tree-2024-1 (1) (1).pdf data structure

Similar to 07-Lecture.pptxlkjslkjdfkjskljdflksj;fdkj (20)

PPTX
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
PDF
unit-2-dsa-tree introduction of tree and terninology
PPT
Final tree.ppt tells about tree presentation
PPT
9. TREE Data Structure Non Linear Data Structure
PPTX
BASIC TREE AND TYPES OF DI CONCEPTS.pptx
PPTX
Unit 6 tree
PPTX
Unit 3 trees
PPTX
trees in data structure
PPTX
Why Tree is considered a non-linear data structure?
PPTX
Tree Data Structure Tree Data Structure Details
PPTX
Basic Tree Data Structure BST Traversals .pptx
PPTX
tree-160731205832.pptx
PPT
Tree and Binary Search tree
PPTX
PPTX
NON-LINEAR DATA STRUCTURE-TREES.pptx
PPTX
TREE PRESENTATION COMPUTER SCIENCE/DATA STRUCTURE
PPT
Data Structure And Algorithms for Computer Science
PPTX
Tree Data Structure & methods & operations
PPTX
Presentation1-Data structure S-Tree.pptx
PPTX
binary tree.pptx
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
unit-2-dsa-tree introduction of tree and terninology
Final tree.ppt tells about tree presentation
9. TREE Data Structure Non Linear Data Structure
BASIC TREE AND TYPES OF DI CONCEPTS.pptx
Unit 6 tree
Unit 3 trees
trees in data structure
Why Tree is considered a non-linear data structure?
Tree Data Structure Tree Data Structure Details
Basic Tree Data Structure BST Traversals .pptx
tree-160731205832.pptx
Tree and Binary Search tree
NON-LINEAR DATA STRUCTURE-TREES.pptx
TREE PRESENTATION COMPUTER SCIENCE/DATA STRUCTURE
Data Structure And Algorithms for Computer Science
Tree Data Structure & methods & operations
Presentation1-Data structure S-Tree.pptx
binary tree.pptx
Ad

Recently uploaded (20)

PPTX
Hushh.ai: Your Personal Data, Your Business
PPTX
Chapter security of computer_8_v8.1.pptx
PDF
Grey Minimalist Professional Project Presentation (1).pdf
PPTX
Stats annual compiled ipd opd ot br 2024
PPTX
Capstone Presentation a.pptx on data sci
PPTX
PPT for Diseases.pptx, there are 3 types of diseases
PPTX
ifsm.pptx, institutional food service management
PPTX
cyber row.pptx for cyber proffesionals and hackers
PPTX
transformers as a tool for understanding advance algorithms in deep learning
PPTX
Introduction to Fundamentals of Data Security
PPTX
Machine Learning and working of machine Learning
PPT
Classification methods in data analytics.ppt
PPTX
PPT for Diseases (1)-2, types of diseases.pptx
PPTX
indiraparyavaranbhavan-240418134200-31d840b3.pptx
PPTX
inbound2857676998455010149.pptxmmmmmmmmm
PDF
2025-08 San Francisco FinOps Meetup: Tiering, Intelligently.
PDF
9 FinOps Tools That Simplify Cloud Cost Reporting.pdf
PPTX
inbound6529290805104538764.pptxmmmmmmmmm
PDF
Mcdonald's : a half century growth . pdf
PPT
What is life? We never know the answer exactly
Hushh.ai: Your Personal Data, Your Business
Chapter security of computer_8_v8.1.pptx
Grey Minimalist Professional Project Presentation (1).pdf
Stats annual compiled ipd opd ot br 2024
Capstone Presentation a.pptx on data sci
PPT for Diseases.pptx, there are 3 types of diseases
ifsm.pptx, institutional food service management
cyber row.pptx for cyber proffesionals and hackers
transformers as a tool for understanding advance algorithms in deep learning
Introduction to Fundamentals of Data Security
Machine Learning and working of machine Learning
Classification methods in data analytics.ppt
PPT for Diseases (1)-2, types of diseases.pptx
indiraparyavaranbhavan-240418134200-31d840b3.pptx
inbound2857676998455010149.pptxmmmmmmmmm
2025-08 San Francisco FinOps Meetup: Tiering, Intelligently.
9 FinOps Tools That Simplify Cloud Cost Reporting.pdf
inbound6529290805104538764.pptxmmmmmmmmm
Mcdonald's : a half century growth . pdf
What is life? We never know the answer exactly
Ad

07-Lecture.pptxlkjslkjdfkjskljdflksj;fdkj

  • 1. Kandahar university Faculty of Computer Science 1 Data Structures Lecture 07 Introduction to Tree Data Structure Sayed Shah Agha
  • 2. Contents 2 Introduction to tree Terminology of tree Binary tree Binary tree types Binary Tree Implementation Traversing the Binary Tree End
  • 3. What is Tree? • A tree is a nonlinear hierarchical data structure that consists of nodes connected by edges. 3
  • 4. Why Tree Data Structure? 4 • Other data structures such as arrays, linked list, stack, and queue are linear data structures that store data sequentially. • In order to perform any operation in a linear data structure, the time complexity increases with the increase in the data size. But, it is not acceptable in today's computational world. • Different tree data structures allow quicker and easier access to the data as it is a non-linear data structure.
  • 5. Tree Terminologies • Node A node is an entity that contains a key or value and pointers to its child nodes. The last nodes of each path are called leaf nodes or external nodes that do not contain a link/pointer to child nodes. • Edge It is the link between any two nodes. 5
  • 6. Tree Terminologies 6 • Root It is the topmost node of a tree. • Height of a Node The height of a node is the number of edges from the node to the deepest leaf (ie. the longest path from the node to a leaf node). • Depth of a Node The depth of a node is the number of edges from the root to the node. • Height of a Tree The height of a Tree is the height of the root node or the depth of the deepest node.
  • 7. Tree Terminologies • Height and depth of each node in a tree 7
  • 8. Tree Terminologies • Degree of a Node The degree of a node is the total number of branches of that node. • Forest A collection of disjoint trees is called a forest. 🞄Creating forest from a tree 🞄 You can create a forest by cutting the root of a tree. 8
  • 9. Types of Tree 9 • Binary Tree • Binary Search Tree • AVL Tree • B-Tree
  • 10. Tree Traversal 10 • In order to perform any operation on a tree, you need to reach to the specific node. The tree traversal algorithm helps in visiting a required node in the tree. • Traversing a tree means visiting every node in the tree. You might, for instance, want to add all the values in the tree or find the largest one. For all these operations, you will need to visit each node of the tree. • Linear data structures like arrays, stacks, queues, and linked list have only one way to read the data. But a hierarchical data structure like a tree can be traversed in different ways.
  • 11. Types of Traversal 11 • Depending on the order in which we do this, there can be three types of traversal. 🞄Inorder traversal 🞄Preorder traversal 🞄Postorder traversal
  • 12. Inorder traversal 12 • First, visit all the nodes in the left subtree • Then the root node • Visit all the nodes in the right subtree 🞄 Inorder(root->left) 🞄Display(root->data) 🞄 Inorder(root->right)
  • 13. Preorder traversal 13 • Visit root node • Visit all the nodes in the left subtree • Visit all the nodes in the right subtree 🞄display(root->data) 🞄preorder(root->left) 🞄preorder(root->right)
  • 14. Postorder traversal 14 • Visit all the nodes in the left subtree • Visit all the nodes in the right subtree • Visit the root node 🞄postorder(root->left) 🞄postorder(root->right) 🞄display(root->data)
  • 15. Tree Applications 15 • Binary Search Trees(BSTs) are used to quickly check whether an element is present in a set or not. • Heap is a kind of tree that is used for heap sort. • A modified version of a tree called Tries is used in modern routers to store routing information. • Most popular databases use B-Trees and T-Trees, which are variants of the tree structure we learned above to store their data • Compilers use a syntax tree to validate the syntax of every program you write.
  • 16. Binary Tree • A binary tree is a tree data structure in which each parent node can have at most two children. For example, 16
  • 17. Types of Binary Tree 17 • Full Binary Tree • Perfect Binary Tree • Complete Binary Tree • Balanced Binary Tree • Degenerate or Pathological Tree
  • 18. Full Binary Tree • A full Binary tree is a special type of binary tree in which every parent node/internal node has either two or no children. 18
  • 19. Perfect Binary Tree • A perfect binary tree is a type of binary tree in which every internal node has exactly two child nodes and all the leaf nodes are at the same level. 19
  • 20. Complete Binary Tree • A complete binary tree is just like a full binary tree, but with two major differences 🞄 Every level must be completely filled 🞄 All the leaf elements must lean towards the left. 🞄 The last leaf element might not have a right sibling i.e. a complete binary tree doesn't have to be a full binary tree. 20
  • 21. Balanced Binary Tree • It is a type of binary tree in which the difference between the left and the right subtree for each node is either 0 or 1. 21
  • 22. Degenerate or Pathological Tree • A degenerate or pathological tree is the tree having a single child either left or right. 22
  • 23. Binary Tree Representation • A node of a binary tree is represented by a class containing a data part and two objects to other classes of the same type. 🞄 Class Node{ int data; Node left; Node right; }; 23
  • 24. Basic Operations 24 • Following are the basic operations of a tree − 🞄 Search − Searches an element in a tree. 🞄 Insert − Inserts an element in a tree. 🞄 Pre-order Traversal − Traverses a tree in a pre-order manner. 🞄 In-order Traversal − Traverses a tree in an in-order manner. 🞄 Post-order Traversal − Traverses a tree in a post-order manner.
  • 25. Binary Tree Applications 25 • For easy and quick access to data • In router algorithms • To implement heap data structure • Syntax tree
  • 26. End 26 Would anyone like to ask any questions?