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
COMPLETE AND FULL BINARY TREE
 A complete/full binary tree is a binary tree,
which is completely filled with nodes from
top to bottom and left to right.
 The tree starts from the root (top), goes to
the next level with first the left child and
then the right child (left to right). The
process repeats for each next level with
each node till the last (bottom) level.
 In complete binary tree some of the nodes
only for the bottom level might be absent
(here nodes after N).
 In Full binary tree the last/bottom level
must also be filled up.
Tree  9
A
B D
E F IH
GK ML N C J P
COMPLETE AND FULL BINARY TREE
 A full binary tree of depth n is a binary tree of depth n with 2n - 1 nodes, n >=0.
 A binary tree with k nodes and depth n is a complete binary tree if and only if its
nodes correspond to the nodes numbered from 0 to k-1 in the full binary tree of
depth n.
Tree  10
A
B D
E F IH
GK ML N C J P
Level=0,
# of nodes=20=1
Level=1,
# of nodes=21=2
Level=2,
# of nodes=22=4
Level=3,
# of nodes=23=8
Total Number of Nodes:
20+ 21+ 22+ 23 = 24 - 1 = 15
Height of the tree: Log215 = 4
If total number of nodes are n,
Nodes at each level L = 2L
Bottom Level = BL
Height h = BL+1 = Log2n
Total nodes = 2h – 1 = n
TRAVERSAL
 Systematic way of visiting all the nodes.
 Methods:
 Inorder
 Postorder
 Preorder
 They all traverse the left subtree before the right subtree.
Tree  11
INORDER TRAVERSAL – LEFT_PARENTNODE_RIGHT
 Traverse the left subtree
 Visit the parent node
 Traverse the right subtree
Tree  12
A
B D
E F H
K ML C
K E L B F M A H C D
POSTORDER TRAVERSAL – LEFT_RIGHT_PARENTNODE
 Traverse the left subtree
 Traverse the right subtree
 Visit the parent node
Tree  13
A
B D
E F H
K ML C
K L E M F B C H D A
PREORDER TRAVERSAL – PARENTNODE_LEFT_RIGHT
 Visit the parent node
 Traverse the left subtree
 Traverse the right subtree
Tree  14
A
B D
E F H
K ML C
A B E K L F M D H C
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  15
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  16
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  17
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  18
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
Graphs in data structure
hamza javed
 
PPT
trees.ppt
jawraabdul
 
PDF
Tree Data Structure by Daniyal Khan
Daniyal Khan
 
PPTX
Trees data structure
Sumit Gupta
 
PDF
Relational Database Design
Prabu U
 
PPTX
Graph data structure and algorithms
Anandhasilambarasan D
 
PDF
Double ended queue
jyoti_lakhani
 
PPT
graph ASS (1).ppt
ARVIND SARDAR
 
Graphs in data structure
hamza javed
 
trees.ppt
jawraabdul
 
Tree Data Structure by Daniyal Khan
Daniyal Khan
 
Trees data structure
Sumit Gupta
 
Relational Database Design
Prabu U
 
Graph data structure and algorithms
Anandhasilambarasan D
 
Double ended queue
jyoti_lakhani
 
graph ASS (1).ppt
ARVIND SARDAR
 

What's hot (20)

PPTX
Slides Chapter10.1 10.2
showslidedump
 
PPTX
Graph ASS DBATU.pptx
ARVIND SARDAR
 
PPT
Cisco ip-addressing
askme
 
PDF
Data Structures and Algorithms
Pierre Vigneras
 
PPT
1.8 splay tree
Krish_ver2
 
PPT
2.5 graph dfs
Krish_ver2
 
PPTX
A presentation on prim's and kruskal's algorithm
Gaurav Kolekar
 
PPTX
Dfs
Ashish Ranjan
 
PPTX
trees in data structure
shameen khan
 
PPT
Graph traversal-BFS & DFS
Rajandeep Gill
 
PPTX
SQL Server Learning Drive
TechandMate
 
PDF
CBSE XII Database Concepts And MySQL Presentation
Guru Ji
 
PPT
Binary Search Tree and AVL
Katang Isip
 
PDF
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
PPTX
Data structure - Graph
Madhu Bala
 
PPT
Breadth first search
Sazzad Hossain
 
PPTX
Normalization in databases
baabtra.com - No. 1 supplier of quality freshers
 
PPT
Best for b trees
DineshRaaja
 
PPTX
Data Definition and Data Manipulation Language-DDL & DML
Md. Selim Hossain
 
PPTX
Relational databases
Fiddy Prasetiya
 
Slides Chapter10.1 10.2
showslidedump
 
Graph ASS DBATU.pptx
ARVIND SARDAR
 
Cisco ip-addressing
askme
 
Data Structures and Algorithms
Pierre Vigneras
 
1.8 splay tree
Krish_ver2
 
2.5 graph dfs
Krish_ver2
 
A presentation on prim's and kruskal's algorithm
Gaurav Kolekar
 
trees in data structure
shameen khan
 
Graph traversal-BFS & DFS
Rajandeep Gill
 
SQL Server Learning Drive
TechandMate
 
CBSE XII Database Concepts And MySQL Presentation
Guru Ji
 
Binary Search Tree and AVL
Katang Isip
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
Data structure - Graph
Madhu Bala
 
Breadth first search
Sazzad Hossain
 
Best for b trees
DineshRaaja
 
Data Definition and Data Manipulation Language-DDL & DML
Md. Selim Hossain
 
Relational databases
Fiddy Prasetiya
 
Ad

Similar to Data structure tree- advance (20)

PPTX
Data structure tree - intermediate
MD. MARUFUZZAMAN .
 
PPTX
Data structure tree - beginner
MD. MARUFUZZAMAN .
 
PPTX
Introduction to Tree_Data Structure.pptx
PoojariniMitra1
 
PPTX
UNIT III Non Linear Data Structures - Trees.pptx
kncetaruna
 
PPTX
NON-LINEAR DATA STRUCTURE-TREES.pptx
Rajitha Reddy Alugati
 
PPTX
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
DRCARIBOU
 
PPTX
BASIC TREE AND TYPES OF DI CONCEPTS.pptx
tpvvsreenivasarao
 
PPTX
Unit 3 trees
LavanyaJ28
 
PPT
Lecture 5 tree.pptx
Abirami A
 
PPT
Unit 3.ppt
JITTAYASHWANTHREDDY
 
PPTX
Tree
bhumish
 
PPTX
Tree Data Structure & methods & operations
mmuhammadumar869
 
PPT
358 33 powerpoint-slides_10-trees_chapter-10
sumitbardhan
 
PPTX
Basic Tree Data Structure BST Traversals .pptx
rajinooka
 
PPTX
Binary Trees.pptx module 122img 787554yau
rithusagar5
 
PPTX
tree Data Structures in python Traversals.pptx
RupaRaj6
 
PPTX
tree-160731205832.pptx
MouDhara1
 
PPT
Final tree.ppt tells about tree presentation
nakulvarshney371
 
PPTX
Data structure using c module 2
smruti sarangi
 
PPT
BINARY TREE REPRESENTATION.ppt
SeethaDinesh
 
Data structure tree - intermediate
MD. MARUFUZZAMAN .
 
Data structure tree - beginner
MD. MARUFUZZAMAN .
 
Introduction to Tree_Data Structure.pptx
PoojariniMitra1
 
UNIT III Non Linear Data Structures - Trees.pptx
kncetaruna
 
NON-LINEAR DATA STRUCTURE-TREES.pptx
Rajitha Reddy Alugati
 
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
DRCARIBOU
 
BASIC TREE AND TYPES OF DI CONCEPTS.pptx
tpvvsreenivasarao
 
Unit 3 trees
LavanyaJ28
 
Lecture 5 tree.pptx
Abirami A
 
Tree
bhumish
 
Tree Data Structure & methods & operations
mmuhammadumar869
 
358 33 powerpoint-slides_10-trees_chapter-10
sumitbardhan
 
Basic Tree Data Structure BST Traversals .pptx
rajinooka
 
Binary Trees.pptx module 122img 787554yau
rithusagar5
 
tree Data Structures in python Traversals.pptx
RupaRaj6
 
tree-160731205832.pptx
MouDhara1
 
Final tree.ppt tells about tree presentation
nakulvarshney371
 
Data structure using c module 2
smruti sarangi
 
BINARY TREE REPRESENTATION.ppt
SeethaDinesh
 
Ad

Recently uploaded (20)

PDF
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
PDF
RA 12028_ARAL_Orientation_Day-2-Sessions_v2.pdf
Seven De Los Reyes
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PPTX
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PPTX
FSSAI (Food Safety and Standards Authority of India) & FDA (Food and Drug Adm...
Dr. Paindla Jyothirmai
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
PDF
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PDF
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
PPTX
Trends in pediatric nursing .pptx
AneetaSharma15
 
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
RA 12028_ARAL_Orientation_Day-2-Sessions_v2.pdf
Seven De Los Reyes
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
FSSAI (Food Safety and Standards Authority of India) & FDA (Food and Drug Adm...
Dr. Paindla Jyothirmai
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
Trends in pediatric nursing .pptx
AneetaSharma15
 

Data structure tree- advance

  • 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. COMPLETE AND FULL BINARY TREE  A complete/full binary tree is a binary tree, which is completely filled with nodes from top to bottom and left to right.  The tree starts from the root (top), goes to the next level with first the left child and then the right child (left to right). The process repeats for each next level with each node till the last (bottom) level.  In complete binary tree some of the nodes only for the bottom level might be absent (here nodes after N).  In Full binary tree the last/bottom level must also be filled up. Tree  9 A B D E F IH GK ML N C J P
  • 10. COMPLETE AND FULL BINARY TREE  A full binary tree of depth n is a binary tree of depth n with 2n - 1 nodes, n >=0.  A binary tree with k nodes and depth n is a complete binary tree if and only if its nodes correspond to the nodes numbered from 0 to k-1 in the full binary tree of depth n. Tree  10 A B D E F IH GK ML N C J P Level=0, # of nodes=20=1 Level=1, # of nodes=21=2 Level=2, # of nodes=22=4 Level=3, # of nodes=23=8 Total Number of Nodes: 20+ 21+ 22+ 23 = 24 - 1 = 15 Height of the tree: Log215 = 4 If total number of nodes are n, Nodes at each level L = 2L Bottom Level = BL Height h = BL+1 = Log2n Total nodes = 2h – 1 = n
  • 11. TRAVERSAL  Systematic way of visiting all the nodes.  Methods:  Inorder  Postorder  Preorder  They all traverse the left subtree before the right subtree. Tree  11
  • 12. INORDER TRAVERSAL – LEFT_PARENTNODE_RIGHT  Traverse the left subtree  Visit the parent node  Traverse the right subtree Tree  12 A B D E F H K ML C K E L B F M A H C D
  • 13. POSTORDER TRAVERSAL – LEFT_RIGHT_PARENTNODE  Traverse the left subtree  Traverse the right subtree  Visit the parent node Tree  13 A B D E F H K ML C K L E M F B C H D A
  • 14. PREORDER TRAVERSAL – PARENTNODE_LEFT_RIGHT  Visit the parent node  Traverse the left subtree  Traverse the right subtree Tree  14 A B D E F H K ML C A B E K L F M D H C
  • 15. 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  15 43 31 64 20 40 8956 3328 47 59 Fred Dan Mary Alan Eve SueKate EricBill Greg Len Integer Key String Key
  • 16. BST - INSERT Tree  16 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
  • 17. BST - SEARCH  Search Elements 59 and 42 Fahad Ahmed CSC 2015: Data Structures Tree  17 43 31 64 20 40 8956 3328 47 59 43 < 59 64 > 59 56 < 59 59 = 59 43 > 42 31 < 42 40 < 42 ? 42
  • 18. BST - DELETE  Delete 47 (leaf node);  Delete 40 (have only one child);  Delete 64 and 31 (have both child). Tree  18 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