SlideShare une entreprise Scribd logo
1  sur  19
Chapter 6 
Tree
Data structure is divided into two categories 
By Prof. Raj Sarode 2 
Linear Data 
Structure 
Non Linear Data 
Structure 
Data Structure 
Data elements in Sequential Order 
E.g.- Array, Stack, Link list, Queue 
Data elements in Hierarchical 
/ Non-Sequential Order 
E.g.- Tree, Graph 
Tree is Non-Linear DS used in computer science 
E.g. 
Solving Algebraic Equation 
Information Retrieval 
Data Mining 
Artificial Intelligence 
Natural Language Processing
Tree 
“Tree is a finite collection of special Data items 
called the NODE ” 
The node of Tree are arranged in the 
hierarchical structure to represent the parent-child 
relationship. 
 First node of tree does not have any parent is 
called the Root (node) of tree. 
Parent is connected through an edge to its 
child or descendents. 
By Prof. Raj Sarode 3
By Prof. Raj Sarode 4 
Tree Terminology :- 
Node 
Root 
Edge 
1. Node: Each Elements of Tree is called node 
2. Edges: The Lines Connecting the nodes are called Edges. 
3. Parent Node: The Immediate predecessor of node is called parent node. 
4. Grand Parent: Parent of Parent 
5. Child Node: All the immediate successor of a node are its child node. 
6. Root Node: The node does not having any parent. 
7. Leaf Node: The node does not having any child called Leaf node or Terminal node. 
8. Level: Level of Node is distance of that node from root. 
(Rot node at distance 0 from itself hence is at level 0) 
9. Height: Total No. of level in tree (is equal to depth of tree) 
10. Degree of Node: No. of children it has. 
11. Siblings: children of the same parent node (two or more node have same parent are 
called sibling or brother
Binary Tree 
“Is a tree which is either empty or each node can have maximum two child” 
Means each node have 0, or 1 or 2 Childs 
Left Child: The node on the left of any node is called left child. 
Right child: The node on the right of any node is called right child. 
Left Sub tree: sub tree attached to left side of root node is called left sub tree. 
Right Sub tree: sub tree attached to right side of root node is called right sub tree. 
By Prof. Raj Sarode 5 
Left Subtree 
Left Subtree 
The node of a binary tree is divided into three parts : 
Left child Address Left Info Right Right child Address
Classification of Binary Tree 
By Prof. Raj Sarode 6 
1. Strictly Binary Tree: 
Each node in a tree is either Leaf or has exactly two children 
Strictly binary tree with n Leaves always contains 2n-1 nodes 
E.g. 
2 Leaves in First Example having 2*2-1=3 Nodes. 
3 Leaves in Second Example having 3*2-1=5 Nodes. 
4 Leaves in Third Example having 4*2-1=7 Nodes
Classification of Binary Tree 
By Prof. Raj Sarode 7 
2. Complete Binary Tree: 
A binary tree where having height h is a strictly binary tree and all 
leaves are at h levels that tree is complete binary tree 
Or 
All the terminal node of a tree are at level d ,where d is the depth of 
tree, then such trees are called complete binary tree.
Classification of Binary Tree 
3. Almost Complete Binary Tree: 
A binary tree in which all the terminal node or leaf node of tree are at 
level d or d-1, where d is the depth of tree, then such trees are 
called almost complete binary tree. 
L 0 
L 1 
L 2 
L 3 
By Prof. Raj Sarode 8 
L 0 
L 1 
L 2
Classification of Binary Tree 
By Prof. Raj Sarode 9 
4. Extended Binary Tree: 
If in a binary tree ,each empty sub tree (Leaf Node) is replaced by a 
special node then the resulting tree is extended binary tree or 2-tree 
 We can add special node to leaf node & node that have only one child. 
 The special nodes added to the tree are called External Node & original 
node of tree are called Internal Node 
External Node 
Internal Node 
Binary Tree 
Extended Binary Tree 
Note: Binary Tree of Height h has maximum (2 h+1 - 1) nodes
Representation of Tree 
1. Sequential Representation using array 
 Tree is store in single array 
 Number are assigned to each node from leftmost to rightmost node. 
 Root node always assign no. 1 
 Left Child is placed at position [2 * K] (k is position of root) 
 Right Child is placed at position [2 * K + 1] 
 Size of array is Depends on Depth of tree i.e. 2 d+1 
By Prof. Raj Sarode 10 
A 
B C 
D E F G 
H I 
1 
2 3 
4 5 6 7 
8 9 
Root (A) =1 
L Child = [2*k]= 2 * 1 =2 (B) 
R child = [2 * K + 1] = 2 * 1 + 1 = 3 (C) 
Root(B)=2 
L Child = [2*k]= 2 * 2 =4 (D) 
R child = [2 * K + 1] = 2 * 2 + 1 = 5 (E) 
Note: If root is placed at 0th position then 
L Child = [2 * k + 1 ] 
R child = [2 * K + 2 ]
Representation of Tree 
2. Linked Representation using array 
 Tree is store in single array 
Structure of Node 
Struct mode 
{ 
Int Left; 
Int Info; 
Int Right; 
}; 
Node Tree[10]; 
1 
2 3 
B C 
6 7 
By Prof. Raj Sarode 11 
A 
4 5 
D E 
Tree [ 1] . Info = A 
Tree [ 1 ] . Left = 2 
Tree [ 1 ] . Right = 3 
F G 
2 A 3 
E.g.
Representation of Tree 
3. Linked Representation using Pointer 
Structure of Node 
Struct node 
{ 
node *Left; 
Int Info; 
node *Right; 
}; 
node *root; 
2 3 
6 7 
By Prof. Raj Sarode 12 
Ptr -> Info = Data 
Ptr -> Left = Left Child 
Ptr -> Right = Right Child 
1 
A 
B C 
4 5 
D E 
F G 
Left Data Right 
E.g. 
Note: 
If n number of node in B.T. then total number of null link will be n+1 
If tree is empty then root will be NULL
Binary Search Tree 
A binary Search Tree is special kind if tree that satisfied 
the following conditions. 
1. The data elements of left sub tree are smaller than 
the root of the tree. 
2. The data elements of right sub tree are greater than 
or equal to the root of the tree. 
3. The left sub tree and right sub tree are also the 
binary search trees, i.e. they must also follow the 
above two rules. 
By Prof. Raj Sarode 13
Operation on Binary Search Tree 
1. Construction of binary Search Tree (BST) 
Step.1: Initially tree is empty ,place the first element at the root. 
Step.2: Compare the next element with root 
if element is grater than or equal to root then place it in right child position. 
else 
place it in the left child position. 
Step.3: Repeat the process (step 2) for the next element until end of elements or nodes. 
E.g. 30, 26, 35, 9, 12, 19, 32, 40, 50, 45, 2, 9 
30 
26 35 
9 12 32 40 
2 9 19 45 50 
By Prof. Raj Sarode 14
Operation on Binary Search Tree 
2. Traversal of binary Search Tree 
it is the process in which each node of tree is visited exactly once 
Step.1: Visit the root node denoted by N. 
Step.2: Visit the Left Sub Tree (node) denoted by L. 
Step.3: Visit the Right sub Tree (node) denoted by R. 
Methods of Traversal 
1.In-order Traversal 
2.Pre-order Traversal 
3.Post-order Traversal 
By Prof. Raj Sarode 15
1. In-order Traversal (L N R) 
Step.1: Visit the Left Sub Tree (node) denoted by L in In-order. 
Step.2: Visit the root node denoted by N. 
Step.3: Visit the Right sub Tree (node) denoted by R in In-order. 
Algorithm 1:- 
Inorder (ptr) 
1. If ptr!=NULL 
6 
By Prof. Raj Sarode 16 
a. Inorder (ptr -> Left) 
b. Print ptr ->info. 
c. Inorder (ptr -> right) 
2. Exit. 
78 
26 94 
23 43 97 
Solution:- 23 , 26 , 43 , 78 , 94 , 97 
A 
B C 
D E F G 
H I J 
Solution:- D , B , H , E , I , A , F , C , J , G 
E.g. 1 
E.g. 2 
1 
2 
3 
4 
5 
6 
1 
2 
3 
4 
5 
7 
8 
9 
10
2. Pre-order Traversal (N L R) 
Step.1: Visit the root node denoted by N. 
Step.2: Visit the Left Sub Tree (node) denoted by L in Pre-order. 
Step.3: Visit the Right sub Tree (node) denoted by R in Pre-order. 
Algorithm 2:- 
Preorder (ptr) 
1. If ptr!=NULL 
By Prof. Raj Sarode 17 
a. Print ptr ->info. 
b. Preorder (ptr -> Left) 
c. Preorder (ptr -> right) 
2. Exit. 
78 
26 94 
23 43 97 
Solution:- 78 , 26 , 23 , 43 , 94 , 97 
A 
B C 
D E F G 
H I J 
Solution:- A , B , D , E , H , I , C , F , G , J 
E.g. 1 
E.g. 2 
1 
2 
3 4 
5 
6 
1 
2 
3 4 
5 6 
7 
8 9 
10
3. Post-order Traversal (L R N) 
Step.1: Visit the Left Sub Tree (node) denoted by L in Post-order. 
Step.2: Visit the Right sub Tree (node) denoted by R in Post-order. 
Step.3: Visit the root node denoted by N. 
Algorithm 3:- 
Postorder (ptr) 
1. If ptr !=NULL 
4 
5 
By Prof. Raj Sarode 18 
a. Postorder (ptr -> Left) 
b. Postorder (ptr -> right) 
c. Print ptr ->info. 
2. Exit. 
78 
3 
26 94 
23 43 97 
Solution:- 23 , 43 , 26 , 97 , 94 , 78 
A 
B C 
D E F G 
H I J 
Solution:- D , H , I , E , B , F , J , G , C , A 
E.g. 1 
E.g. 2 
1 2 
4 
5 
6 
1 
2 3 
6 
7 
8 
9 
10
Thank You 
By Prof. Raj Sarode 19

Contenu connexe

Tendances (20)

Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Expression trees
Expression treesExpression trees
Expression trees
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Threaded Binary Tree
Threaded Binary TreeThreaded Binary Tree
Threaded Binary Tree
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
AVL Tree in Data Structure
AVL Tree in Data Structure AVL Tree in Data Structure
AVL Tree in Data Structure
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
 
Trees
TreesTrees
Trees
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
Red black trees
Red black treesRed black trees
Red black trees
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Terminology of tree
Terminology of treeTerminology of tree
Terminology of tree
 
Linked List
Linked ListLinked List
Linked List
 
Binary search tree in data structures
Binary search tree in  data structuresBinary search tree in  data structures
Binary search tree in data structures
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 

Similaire à Tree (20)

Tree
TreeTree
Tree
 
Chapter 5_Trees.pdf
Chapter 5_Trees.pdfChapter 5_Trees.pdf
Chapter 5_Trees.pdf
 
Unit iii(dsc++)
Unit iii(dsc++)Unit iii(dsc++)
Unit iii(dsc++)
 
Data Structures
Data StructuresData Structures
Data Structures
 
Trees
TreesTrees
Trees
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
 
Dsc++ unit 3 notes
Dsc++ unit 3 notesDsc++ unit 3 notes
Dsc++ unit 3 notes
 
Final tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentationFinal tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentation
 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptx
 
NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxNON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
 
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptxTREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
 
Unit 4 tree
Unit 4   treeUnit 4   tree
Unit 4 tree
 
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 Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 
UNIT-4 TREES.ppt
UNIT-4 TREES.pptUNIT-4 TREES.ppt
UNIT-4 TREES.ppt
 
Binary tree
Binary treeBinary tree
Binary tree
 
Unit8 C
Unit8 CUnit8 C
Unit8 C
 
Unit – vi tree
Unit – vi   treeUnit – vi   tree
Unit – vi tree
 
binary tree.pptx
binary tree.pptxbinary tree.pptx
binary tree.pptx
 

Plus de Raj Sarode

Chap 7 binary threaded tree
Chap 7 binary threaded treeChap 7 binary threaded tree
Chap 7 binary threaded treeRaj Sarode
 
Chap 6 cloud security
Chap 6 cloud securityChap 6 cloud security
Chap 6 cloud securityRaj Sarode
 
Chap 5 software as a service (saass)
Chap 5 software as a service (saass)Chap 5 software as a service (saass)
Chap 5 software as a service (saass)Raj Sarode
 
Chap 4 platform as a service (paa s)
Chap 4 platform as a service (paa s)Chap 4 platform as a service (paa s)
Chap 4 platform as a service (paa s)Raj Sarode
 
Chap 3 infrastructure as a service(iaas)
Chap 3 infrastructure as a service(iaas)Chap 3 infrastructure as a service(iaas)
Chap 3 infrastructure as a service(iaas)Raj Sarode
 
Chap 2 virtulizatin
Chap 2 virtulizatinChap 2 virtulizatin
Chap 2 virtulizatinRaj Sarode
 
Chap 1 introduction to cloud computing
Chap 1 introduction to cloud computingChap 1 introduction to cloud computing
Chap 1 introduction to cloud computingRaj Sarode
 

Plus de Raj Sarode (10)

Chap 8 graph
Chap 8 graphChap 8 graph
Chap 8 graph
 
Chap 7 binary threaded tree
Chap 7 binary threaded treeChap 7 binary threaded tree
Chap 7 binary threaded tree
 
Chap 6 cloud security
Chap 6 cloud securityChap 6 cloud security
Chap 6 cloud security
 
Chap 5 software as a service (saass)
Chap 5 software as a service (saass)Chap 5 software as a service (saass)
Chap 5 software as a service (saass)
 
Chap 4 platform as a service (paa s)
Chap 4 platform as a service (paa s)Chap 4 platform as a service (paa s)
Chap 4 platform as a service (paa s)
 
Chap 3 infrastructure as a service(iaas)
Chap 3 infrastructure as a service(iaas)Chap 3 infrastructure as a service(iaas)
Chap 3 infrastructure as a service(iaas)
 
Chap 2 virtulizatin
Chap 2 virtulizatinChap 2 virtulizatin
Chap 2 virtulizatin
 
Chap 1 introduction to cloud computing
Chap 1 introduction to cloud computingChap 1 introduction to cloud computing
Chap 1 introduction to cloud computing
 
Queue
QueueQueue
Queue
 
stack
stackstack
stack
 

Dernier

Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 

Dernier (20)

Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 

Tree

  • 2. Data structure is divided into two categories By Prof. Raj Sarode 2 Linear Data Structure Non Linear Data Structure Data Structure Data elements in Sequential Order E.g.- Array, Stack, Link list, Queue Data elements in Hierarchical / Non-Sequential Order E.g.- Tree, Graph Tree is Non-Linear DS used in computer science E.g. Solving Algebraic Equation Information Retrieval Data Mining Artificial Intelligence Natural Language Processing
  • 3. Tree “Tree is a finite collection of special Data items called the NODE ” The node of Tree are arranged in the hierarchical structure to represent the parent-child relationship.  First node of tree does not have any parent is called the Root (node) of tree. Parent is connected through an edge to its child or descendents. By Prof. Raj Sarode 3
  • 4. By Prof. Raj Sarode 4 Tree Terminology :- Node Root Edge 1. Node: Each Elements of Tree is called node 2. Edges: The Lines Connecting the nodes are called Edges. 3. Parent Node: The Immediate predecessor of node is called parent node. 4. Grand Parent: Parent of Parent 5. Child Node: All the immediate successor of a node are its child node. 6. Root Node: The node does not having any parent. 7. Leaf Node: The node does not having any child called Leaf node or Terminal node. 8. Level: Level of Node is distance of that node from root. (Rot node at distance 0 from itself hence is at level 0) 9. Height: Total No. of level in tree (is equal to depth of tree) 10. Degree of Node: No. of children it has. 11. Siblings: children of the same parent node (two or more node have same parent are called sibling or brother
  • 5. Binary Tree “Is a tree which is either empty or each node can have maximum two child” Means each node have 0, or 1 or 2 Childs Left Child: The node on the left of any node is called left child. Right child: The node on the right of any node is called right child. Left Sub tree: sub tree attached to left side of root node is called left sub tree. Right Sub tree: sub tree attached to right side of root node is called right sub tree. By Prof. Raj Sarode 5 Left Subtree Left Subtree The node of a binary tree is divided into three parts : Left child Address Left Info Right Right child Address
  • 6. Classification of Binary Tree By Prof. Raj Sarode 6 1. Strictly Binary Tree: Each node in a tree is either Leaf or has exactly two children Strictly binary tree with n Leaves always contains 2n-1 nodes E.g. 2 Leaves in First Example having 2*2-1=3 Nodes. 3 Leaves in Second Example having 3*2-1=5 Nodes. 4 Leaves in Third Example having 4*2-1=7 Nodes
  • 7. Classification of Binary Tree By Prof. Raj Sarode 7 2. Complete Binary Tree: A binary tree where having height h is a strictly binary tree and all leaves are at h levels that tree is complete binary tree Or All the terminal node of a tree are at level d ,where d is the depth of tree, then such trees are called complete binary tree.
  • 8. Classification of Binary Tree 3. Almost Complete Binary Tree: A binary tree in which all the terminal node or leaf node of tree are at level d or d-1, where d is the depth of tree, then such trees are called almost complete binary tree. L 0 L 1 L 2 L 3 By Prof. Raj Sarode 8 L 0 L 1 L 2
  • 9. Classification of Binary Tree By Prof. Raj Sarode 9 4. Extended Binary Tree: If in a binary tree ,each empty sub tree (Leaf Node) is replaced by a special node then the resulting tree is extended binary tree or 2-tree  We can add special node to leaf node & node that have only one child.  The special nodes added to the tree are called External Node & original node of tree are called Internal Node External Node Internal Node Binary Tree Extended Binary Tree Note: Binary Tree of Height h has maximum (2 h+1 - 1) nodes
  • 10. Representation of Tree 1. Sequential Representation using array  Tree is store in single array  Number are assigned to each node from leftmost to rightmost node.  Root node always assign no. 1  Left Child is placed at position [2 * K] (k is position of root)  Right Child is placed at position [2 * K + 1]  Size of array is Depends on Depth of tree i.e. 2 d+1 By Prof. Raj Sarode 10 A B C D E F G H I 1 2 3 4 5 6 7 8 9 Root (A) =1 L Child = [2*k]= 2 * 1 =2 (B) R child = [2 * K + 1] = 2 * 1 + 1 = 3 (C) Root(B)=2 L Child = [2*k]= 2 * 2 =4 (D) R child = [2 * K + 1] = 2 * 2 + 1 = 5 (E) Note: If root is placed at 0th position then L Child = [2 * k + 1 ] R child = [2 * K + 2 ]
  • 11. Representation of Tree 2. Linked Representation using array  Tree is store in single array Structure of Node Struct mode { Int Left; Int Info; Int Right; }; Node Tree[10]; 1 2 3 B C 6 7 By Prof. Raj Sarode 11 A 4 5 D E Tree [ 1] . Info = A Tree [ 1 ] . Left = 2 Tree [ 1 ] . Right = 3 F G 2 A 3 E.g.
  • 12. Representation of Tree 3. Linked Representation using Pointer Structure of Node Struct node { node *Left; Int Info; node *Right; }; node *root; 2 3 6 7 By Prof. Raj Sarode 12 Ptr -> Info = Data Ptr -> Left = Left Child Ptr -> Right = Right Child 1 A B C 4 5 D E F G Left Data Right E.g. Note: If n number of node in B.T. then total number of null link will be n+1 If tree is empty then root will be NULL
  • 13. Binary Search Tree A binary Search Tree is special kind if tree that satisfied the following conditions. 1. The data elements of left sub tree are smaller than the root of the tree. 2. The data elements of right sub tree are greater than or equal to the root of the tree. 3. The left sub tree and right sub tree are also the binary search trees, i.e. they must also follow the above two rules. By Prof. Raj Sarode 13
  • 14. Operation on Binary Search Tree 1. Construction of binary Search Tree (BST) Step.1: Initially tree is empty ,place the first element at the root. Step.2: Compare the next element with root if element is grater than or equal to root then place it in right child position. else place it in the left child position. Step.3: Repeat the process (step 2) for the next element until end of elements or nodes. E.g. 30, 26, 35, 9, 12, 19, 32, 40, 50, 45, 2, 9 30 26 35 9 12 32 40 2 9 19 45 50 By Prof. Raj Sarode 14
  • 15. Operation on Binary Search Tree 2. Traversal of binary Search Tree it is the process in which each node of tree is visited exactly once Step.1: Visit the root node denoted by N. Step.2: Visit the Left Sub Tree (node) denoted by L. Step.3: Visit the Right sub Tree (node) denoted by R. Methods of Traversal 1.In-order Traversal 2.Pre-order Traversal 3.Post-order Traversal By Prof. Raj Sarode 15
  • 16. 1. In-order Traversal (L N R) Step.1: Visit the Left Sub Tree (node) denoted by L in In-order. Step.2: Visit the root node denoted by N. Step.3: Visit the Right sub Tree (node) denoted by R in In-order. Algorithm 1:- Inorder (ptr) 1. If ptr!=NULL 6 By Prof. Raj Sarode 16 a. Inorder (ptr -> Left) b. Print ptr ->info. c. Inorder (ptr -> right) 2. Exit. 78 26 94 23 43 97 Solution:- 23 , 26 , 43 , 78 , 94 , 97 A B C D E F G H I J Solution:- D , B , H , E , I , A , F , C , J , G E.g. 1 E.g. 2 1 2 3 4 5 6 1 2 3 4 5 7 8 9 10
  • 17. 2. Pre-order Traversal (N L R) Step.1: Visit the root node denoted by N. Step.2: Visit the Left Sub Tree (node) denoted by L in Pre-order. Step.3: Visit the Right sub Tree (node) denoted by R in Pre-order. Algorithm 2:- Preorder (ptr) 1. If ptr!=NULL By Prof. Raj Sarode 17 a. Print ptr ->info. b. Preorder (ptr -> Left) c. Preorder (ptr -> right) 2. Exit. 78 26 94 23 43 97 Solution:- 78 , 26 , 23 , 43 , 94 , 97 A B C D E F G H I J Solution:- A , B , D , E , H , I , C , F , G , J E.g. 1 E.g. 2 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 10
  • 18. 3. Post-order Traversal (L R N) Step.1: Visit the Left Sub Tree (node) denoted by L in Post-order. Step.2: Visit the Right sub Tree (node) denoted by R in Post-order. Step.3: Visit the root node denoted by N. Algorithm 3:- Postorder (ptr) 1. If ptr !=NULL 4 5 By Prof. Raj Sarode 18 a. Postorder (ptr -> Left) b. Postorder (ptr -> right) c. Print ptr ->info. 2. Exit. 78 3 26 94 23 43 97 Solution:- 23 , 43 , 26 , 97 , 94 , 78 A B C D E F G H I J Solution:- D , H , I , E , B , F , J , G , C , A E.g. 1 E.g. 2 1 2 4 5 6 1 2 3 6 7 8 9 10
  • 19. Thank You By Prof. Raj Sarode 19