SlideShare a Scribd company logo
DATA STRUCTURE
TREE
DATA STRUCTURE - TREE
 Many applications are
hierarchical in nature.
 Linear data structures are not
appropriate for these type of
applications.
Tree  2
President
Vice President
Executive Executive
Vice President
Executive
Vice President
Executive Executive
Mashrafi Khan
Mairufa Khan
Junayed
Ahmed
Masrufa
Ahmed
Fahim Khan
Ashfar
Khan
Moin Khan
Imran
Khan
Bushra
Khan
Faizul Khan
Mahboob
Khan
Mashrufa
Khan
Mashraba
Khan
Hierarchical structure of a company
Genealogy tree of a family
COMMON USE OF TREE AS A DATA STRUCTURE
 Representing hierarchical data
 Storing data in a way that makes it easily searchable
 Representing sorted lists of data
 As a workflow for compositing digital images for visual effects
 Routing algorithms
Tree  3
TREE - DEFINITION
 As a data structure, a tree consists of one
or more nodes, where each node has a
value and a list of references to other (its
children) nodes.
 A tree must have a node designated as
root. If a tree has only one node, that
node is the root node. Root is never
referenced by any other node.
 Having more than one node indicates that
the root have some (at least one)
references to its children nodes and the
children nodes (might) have references to
their children nodes and so on.
Tree  4
A
B C D
E F JIHG
K ML ON
ROOT
VALUE
CHILDCHILD
TREE - DEFINITION
 Generally it is considered that in a tree,
there is only one path going from one
node to another node.
 There cannot be any cycle or loop.
 The link from a node to other node is
called an edge.
 An arrowed edge
indicates flow from P to Q.
 An straight line edge
indicates flow from P to Q and Q to P.
Tree  5
A
B C D
E F JIHG
K ML ON
If node F is reached through node B, than the link from
node K to node F will not be considered.
If link from node L to node C is considered, than there
will be a cycle among nodes C, G, and L.
P Q
P Q
A straight line is generally used to represent the links
between the nodes of a tree.
TREE - DEFINITION
 Nodes
 Parent Nodes & Child Nodes
 Leaf Nodes: nodes with no child
 Root Node: node with no parent
 Sub Tree: the tree rooted by a child
 Level of a tree:
 Root at level 0;
 Each children have the level one more than
its parent.
 Height/depth of the tree: Total
number of Levels
 Height of a node: Total number of
levels from bottom
[Tree height – node level].
Tree  6
A
B C D
E F JIHG
K ML ON
LEVEL
0
1
2
3
Height of this tree is 4, as there are four levels (0…3).
Height of root A is 4;
Height of nodes B, C, D is 3;
Height of E, F, G, H, I, J is 2;
Height of nodes K, L , M, N, O is 1.
m-ARY TREE
 A Tree is an m-ary Tree when each of its node has no more than m children.
Tree  7
A
B C D
E F JIHG
K ML ON
A
B D
E F IH
GK
ML
ON
2-ary tree 3-ary tree
BINARY TREE (BT)
 Each node of a binary Tree has at most 2 children.
Tree  8
A
B D
E F IH
GK
ML
ON
Binary tree
BINARY SEARCH TREE (BST)
 Is a Binary Tree such that:
 Every node entry has a unique key (i.e. no duplication item).
 All the keys in the left subtree of a node are less than the key of the node.
 All the keys in the right subtree of a node are greater than the key of the node.
Tree  9
43
31 64
20 40 8956
3328 47 59
Fred
Dan Mary
Alan Eve SueKate
EricBill Greg Len
Integer Key String Key
BST - INSERT
Tree  10
43
31 64
20 40 8956
3328 47 59
> <
< <
<><
>
>
>
Fred
Dan Mary
Alan Eve SueKate
EricBill Greg Len
> <
< >
> <<
>
>
<
Integer Key String Key
43 31 64 40 20 89 56 47 33 28 59Fred Mary Kate Dan Len Alan Eve Bill Sue Greg Eric
BST - SEARCH
 Search Elements 59 and 42
Fahad Ahmed CSC 2015: Data Structures Tree  11
43
31 64
20 40 8956
3328 47 59
43 < 59
64 > 59
56 < 59
59 = 59
43 > 42
31 < 42
40 < 42
? 42
BST - DELETE
 Delete 47 (leaf node);
 Delete 40 (have only one child);
 Delete 64 and 31 (have both child).
Tree  12
43
31 64
20 40 8956
3828 47 59
32
33
5932
Leaf Node: Just DeleteLeaf Node: Just Delete
Node with one child:
connect the parent to
the child and Delete
Node with one child: connect the
parent to the child and Delete
Node with two
children on right
subtree: Replace with
the child with highest
value either from left
or right subtree
Node with two
children on left
subtree: Replace with
the child with lowest
value either from left
or right subtree

More Related Content

PPTX
Tree in data structure
PPTX
Data structure tree- advance
PPTX
PPTX
Trees data structure
PPTX
trees in data structure
PPTX
Tree in data structure
PPTX
PPT
Joins in SQL
Tree in data structure
Data structure tree- advance
Trees data structure
trees in data structure
Tree in data structure
Joins in SQL

What's hot (20)

PPT
Elementary data organisation
PPTX
Data structure using c module 1
PPTX
Binary Search Tree
PPTX
Terminology of tree
PPT
Data structure lecture 1
PPTX
Data structure power point presentation
PPTX
Trees (data structure)
PDF
PPSX
Data Structure (Tree)
PPTX
Graph traversals in Data Structures
PPT
Binary search tree in data structures
PPTX
Linked List
PPT
Tree and Binary Search tree
PDF
Tree Data Structure by Daniyal Khan
PPT
Linked list
PPTX
B tree
PPT
BINARY TREE REPRESENTATION.ppt
PPTX
Tree_Definition.pptx
Elementary data organisation
Data structure using c module 1
Binary Search Tree
Terminology of tree
Data structure lecture 1
Data structure power point presentation
Trees (data structure)
Data Structure (Tree)
Graph traversals in Data Structures
Binary search tree in data structures
Linked List
Tree and Binary Search tree
Tree Data Structure by Daniyal Khan
Linked list
B tree
BINARY TREE REPRESENTATION.ppt
Tree_Definition.pptx
Ad

Similar to Data structure tree - beginner (20)

PPTX
Data structure tree - intermediate
PDF
Dsc++ unit 3 notes
PPTX
Binary tree
PDF
481803111-Trees-Unit-Review-pdf - Copy.pdf
PDF
Unit 4.1 (tree)
PPTX
BST.pptx this isp used for learning binary search trees
PPTX
BST.pptx this is Good for data structure
PPTX
Tree structure and its definitions with an example
PPT
Chapter 8: tree data structure
PPTX
Tree Data Structure Tree Data Structure Details
PPTX
Trees — Tree Terminology – Binary Trees – Binary Search Trees – Tree Traversa...
PPTX
17. Java data structures trees representation and traversal
PPTX
UNIT III Non Linear Data Structures - Trees.pptx
PPTX
NON-LINEAR DATA STRUCTURE-TREES.pptx
PPTX
Data Structure of computer science and technology
PPT
part4-trees.ppt
PPTX
UNIT III Non Linear Data Structures - Trees.pptx
PPTX
Trees, Basic Terminology and Binary Trees
PPTX
Binary tree and operations
PPTX
Trees in data structures
Data structure tree - intermediate
Dsc++ unit 3 notes
Binary tree
481803111-Trees-Unit-Review-pdf - Copy.pdf
Unit 4.1 (tree)
BST.pptx this isp used for learning binary search trees
BST.pptx this is Good for data structure
Tree structure and its definitions with an example
Chapter 8: tree data structure
Tree Data Structure Tree Data Structure Details
Trees — Tree Terminology – Binary Trees – Binary Search Trees – Tree Traversa...
17. Java data structures trees representation and traversal
UNIT III Non Linear Data Structures - Trees.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
Data Structure of computer science and technology
part4-trees.ppt
UNIT III Non Linear Data Structures - Trees.pptx
Trees, Basic Terminology and Binary Trees
Binary tree and operations
Trees in data structures
Ad

Recently uploaded (20)

PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Basic Mud Logging Guide for educational purpose
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
RMMM.pdf make it easy to upload and study
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Institutional Correction lecture only . . .
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Computing-Curriculum for Schools in Ghana
PDF
Complications of Minimal Access Surgery at WLH
PPTX
master seminar digital applications in india
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
2.FourierTransform-ShortQuestionswithAnswers.pdf
Microbial diseases, their pathogenesis and prophylaxis
Basic Mud Logging Guide for educational purpose
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Cell Types and Its function , kingdom of life
Abdominal Access Techniques with Prof. Dr. R K Mishra
Supply Chain Operations Speaking Notes -ICLT Program
RMMM.pdf make it easy to upload and study
O5-L3 Freight Transport Ops (International) V1.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Institutional Correction lecture only . . .
Insiders guide to clinical Medicine.pdf
Final Presentation General Medicine 03-08-2024.pptx
Computing-Curriculum for Schools in Ghana
Complications of Minimal Access Surgery at WLH
master seminar digital applications in india
Sports Quiz easy sports quiz sports quiz
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape

Data structure tree - beginner

  • 2. DATA STRUCTURE - TREE  Many applications are hierarchical in nature.  Linear data structures are not appropriate for these type of applications. Tree  2 President Vice President Executive Executive Vice President Executive Vice President Executive Executive Mashrafi Khan Mairufa Khan Junayed Ahmed Masrufa Ahmed Fahim Khan Ashfar Khan Moin Khan Imran Khan Bushra Khan Faizul Khan Mahboob Khan Mashrufa Khan Mashraba Khan Hierarchical structure of a company Genealogy tree of a family
  • 3. COMMON USE OF TREE AS A DATA STRUCTURE  Representing hierarchical data  Storing data in a way that makes it easily searchable  Representing sorted lists of data  As a workflow for compositing digital images for visual effects  Routing algorithms Tree  3
  • 4. TREE - DEFINITION  As a data structure, a tree consists of one or more nodes, where each node has a value and a list of references to other (its children) nodes.  A tree must have a node designated as root. If a tree has only one node, that node is the root node. Root is never referenced by any other node.  Having more than one node indicates that the root have some (at least one) references to its children nodes and the children nodes (might) have references to their children nodes and so on. Tree  4 A B C D E F JIHG K ML ON ROOT VALUE CHILDCHILD
  • 5. TREE - DEFINITION  Generally it is considered that in a tree, there is only one path going from one node to another node.  There cannot be any cycle or loop.  The link from a node to other node is called an edge.  An arrowed edge indicates flow from P to Q.  An straight line edge indicates flow from P to Q and Q to P. Tree  5 A B C D E F JIHG K ML ON If node F is reached through node B, than the link from node K to node F will not be considered. If link from node L to node C is considered, than there will be a cycle among nodes C, G, and L. P Q P Q A straight line is generally used to represent the links between the nodes of a tree.
  • 6. TREE - DEFINITION  Nodes  Parent Nodes & Child Nodes  Leaf Nodes: nodes with no child  Root Node: node with no parent  Sub Tree: the tree rooted by a child  Level of a tree:  Root at level 0;  Each children have the level one more than its parent.  Height/depth of the tree: Total number of Levels  Height of a node: Total number of levels from bottom [Tree height – node level]. Tree  6 A B C D E F JIHG K ML ON LEVEL 0 1 2 3 Height of this tree is 4, as there are four levels (0…3). Height of root A is 4; Height of nodes B, C, D is 3; Height of E, F, G, H, I, J is 2; Height of nodes K, L , M, N, O is 1.
  • 7. m-ARY TREE  A Tree is an m-ary Tree when each of its node has no more than m children. Tree  7 A B C D E F JIHG K ML ON A B D E F IH GK ML ON 2-ary tree 3-ary tree
  • 8. BINARY TREE (BT)  Each node of a binary Tree has at most 2 children. Tree  8 A B D E F IH GK ML ON Binary tree
  • 9. BINARY SEARCH TREE (BST)  Is a Binary Tree such that:  Every node entry has a unique key (i.e. no duplication item).  All the keys in the left subtree of a node are less than the key of the node.  All the keys in the right subtree of a node are greater than the key of the node. Tree  9 43 31 64 20 40 8956 3328 47 59 Fred Dan Mary Alan Eve SueKate EricBill Greg Len Integer Key String Key
  • 10. BST - INSERT Tree  10 43 31 64 20 40 8956 3328 47 59 > < < < <>< > > > Fred Dan Mary Alan Eve SueKate EricBill Greg Len > < < > > << > > < Integer Key String Key 43 31 64 40 20 89 56 47 33 28 59Fred Mary Kate Dan Len Alan Eve Bill Sue Greg Eric
  • 11. BST - SEARCH  Search Elements 59 and 42 Fahad Ahmed CSC 2015: Data Structures Tree  11 43 31 64 20 40 8956 3328 47 59 43 < 59 64 > 59 56 < 59 59 = 59 43 > 42 31 < 42 40 < 42 ? 42
  • 12. BST - DELETE  Delete 47 (leaf node);  Delete 40 (have only one child);  Delete 64 and 31 (have both child). Tree  12 43 31 64 20 40 8956 3828 47 59 32 33 5932 Leaf Node: Just DeleteLeaf Node: Just Delete Node with one child: connect the parent to the child and Delete Node with one child: connect the parent to the child and Delete Node with two children on right subtree: Replace with the child with highest value either from left or right subtree Node with two children on left subtree: Replace with the child with lowest value either from left or right subtree