SlideShare a Scribd company logo
1 of 18
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

What's hot

Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++Himanshu Choudhary
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsAakash deep Singhal
 
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeData Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeManishPrajapati78
 
Search tree,Tree and binary tree and heap tree
Search tree,Tree  and binary tree and heap treeSearch tree,Tree  and binary tree and heap tree
Search tree,Tree and binary tree and heap treezia eagle
 
Trees - Non Linear Data Structure
Trees - Non Linear Data StructureTrees - Non Linear Data Structure
Trees - Non Linear Data StructurePriyanka Rana
 
Trees - Data structures in C/Java
Trees - Data structures in C/JavaTrees - Data structures in C/Java
Trees - Data structures in C/Javageeksrik
 
Data Structure: TREES
Data Structure: TREESData Structure: TREES
Data Structure: TREESTABISH HAMID
 
THREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREETHREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREESiddhi Shrivas
 
Cinterviews Binarysearch Tree
Cinterviews Binarysearch TreeCinterviews Binarysearch Tree
Cinterviews Binarysearch Treecinterviews
 

What's hot (20)

Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithms
 
Tree-In Data Structure
Tree-In Data StructureTree-In Data Structure
Tree-In Data Structure
 
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeData Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary Tree
 
Chapter 8 ds
Chapter 8 dsChapter 8 ds
Chapter 8 ds
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
Search tree,Tree and binary tree and heap tree
Search tree,Tree  and binary tree and heap treeSearch tree,Tree  and binary tree and heap tree
Search tree,Tree and binary tree and heap tree
 
Trees - Non Linear Data Structure
Trees - Non Linear Data StructureTrees - Non Linear Data Structure
Trees - Non Linear Data Structure
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
Tree
TreeTree
Tree
 
Trees - Data structures in C/Java
Trees - Data structures in C/JavaTrees - Data structures in C/Java
Trees - Data structures in C/Java
 
Tree
TreeTree
Tree
 
Data Structure: TREES
Data Structure: TREESData Structure: TREES
Data Structure: TREES
 
THREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREETHREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREE
 
Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
 
7.tree
7.tree7.tree
7.tree
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Binary tree
Binary treeBinary tree
Binary tree
 
Tree
TreeTree
Tree
 
Cinterviews Binarysearch Tree
Cinterviews Binarysearch TreeCinterviews Binarysearch Tree
Cinterviews Binarysearch Tree
 

Similar to Data structure tree- advance

NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxNON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxRajitha Reddy Alugati
 
Lecture 5 tree.pptx
Lecture 5 tree.pptxLecture 5 tree.pptx
Lecture 5 tree.pptxAbirami A
 
part4-trees.ppt
part4-trees.pptpart4-trees.ppt
part4-trees.pptSuneel61
 
Data structure using c module 2
Data structure using c module 2Data structure using c module 2
Data structure using c module 2smruti sarangi
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures treemaamir farooq
 
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptxLecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptxHamzaUsman48
 
358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10sumitbardhan
 
Lecture 21_Trees - I.pptx
Lecture 21_Trees - I.pptxLecture 21_Trees - I.pptx
Lecture 21_Trees - I.pptxfizzaahmed9
 
Trees and Graphs in data structures and Algorithms
Trees and Graphs in data structures and AlgorithmsTrees and Graphs in data structures and Algorithms
Trees and Graphs in data structures and AlgorithmsBHARATH KUMAR
 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptxskilljiolms
 
Unit iii(dsc++)
Unit iii(dsc++)Unit iii(dsc++)
Unit iii(dsc++)Durga Devi
 
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDYDATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDYMalikireddy Bramhananda Reddy
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data StructureOm Prakash
 

Similar to Data structure tree- advance (20)

Dsc++ unit 3 notes
Dsc++ unit 3 notesDsc++ unit 3 notes
Dsc++ unit 3 notes
 
NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxNON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
 
Lecture 5 tree.pptx
Lecture 5 tree.pptxLecture 5 tree.pptx
Lecture 5 tree.pptx
 
part4-trees.ppt
part4-trees.pptpart4-trees.ppt
part4-trees.ppt
 
Data structure using c module 2
Data structure using c module 2Data structure using c module 2
Data structure using c module 2
 
Unit 3.ppt
Unit 3.pptUnit 3.ppt
Unit 3.ppt
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures tree
 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
 
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptxLecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
 
358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10
 
Unit 4.1 (tree)
Unit 4.1 (tree)Unit 4.1 (tree)
Unit 4.1 (tree)
 
Lecture 21_Trees - I.pptx
Lecture 21_Trees - I.pptxLecture 21_Trees - I.pptx
Lecture 21_Trees - I.pptx
 
Trees and Graphs in data structures and Algorithms
Trees and Graphs in data structures and AlgorithmsTrees and Graphs in data structures and Algorithms
Trees and Graphs in data structures and Algorithms
 
Trees
TreesTrees
Trees
 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptx
 
Unit iii(dsc++)
Unit iii(dsc++)Unit iii(dsc++)
Unit iii(dsc++)
 
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDYDATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
 
Data Structures
Data StructuresData Structures
Data Structures
 

Recently uploaded

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 

Recently uploaded (20)

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 

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