SlideShare a Scribd company logo
3 TREE
A tree is a (possibly non-linear) data structure made up of nodes or vertices and edges without
having any cycle. The tree with no nodes is called the null orempty tree. A tree that is not
empty consists of a root node and potentially many levels of additional nodes that form a
hierarchy.
Not a tree: cy cle B→C→E→D→B. B has more than one parent (inbound edge
Not a tree: two non-connected parts, A→B and C→D→E. There is more than one root
Not a tree: undirected cy cle 1-2-4-3. 4 has more than one parent (inbound edge
Not a tree: cy cle A→A. A is the root but it also has a parent.
Each linear list is triv ially a tree
# TERMINOLOGIES OF TREES:
1.ROOTS: THE FIRST NODE OF TREE IS CALLED ROOT NODE.
2.PARENTS: In simple words,the node whichhasbranchfromit to anyothernode is calledas
parentnode.Parentnode can alsobe definedas"The node whichhaschild/children".
3. EDGES: the node whichhasa linkfromitsparentnode iscalledaschildnode.Ina tree,any
parentnode can have any numberof childnodes.Ina tree,all the nodesexceptrootare child nodes.
4.CHILD : In simple words,the node whichhasalinkfromitsparentnode iscalledas childnode.
In a tree,anyparentnode can have anynumberof childnodes.
5.SIBLING: a group of nodes with same parents.
6.LEAF: a node with no child.
7. INTERNAL NODE: In a tree data structure,nodesotherthanleaf nodesare called
as Internal Nodes. The rootnode isalsosaidto be Internal Node if the tree hasmore than one
node. Internal nodesare alsocalledas'Non-Terminal'nodes.
8.HEIGHT:(for height of of tree we need to move from downward (root node ) to upward
upto which height of node is asked.) ina tree datastructure,the total numberof egdes fromleaf
node to a particularnode inthe longestpathiscalledas HEIGHT of that Node. Ina tree,heightof the
root node issaidto be heightof the tree.
9. DEGREE: the total numberof childrenof a node iscalledas DEGREE of that Node
10.DEPTH: In a tree data structure,the total numberof egdesfromrootnode to a particular
node iscalledas DEPTH of that Node. Ina tree,the total numberof edgesfromrootnode to a leaf node
inthe longestpathissaidto be Depthof the tree.
11. PATH: the sequence of NodesandEdgesfromone node toanothernode iscalled
as PATH betweenthattwoNodes.
12. SUBTREE: each childfroma node formsa subtree recursively.Everychildnodewillforma
subtree onitsparentnode.
13. LEVEL: ina tree each stepfromtop to bottomiscalledas a Level andthe Level countstarts
with'0' and incrementedbyone ateach level (Step).
Figure 1.
14.DESCENDANT: youjust needtomove formroot node to leaf node (takingrootnode).
Consider fig. 1 letwe needtofind descendantand ancestorfor ‘J’,’G’,’k’.
Thenanswerwill be
>>> descendentof ‘j ‘: A,B,E,J & FOR ‘G’ : A,C,G & FOR ‘K’: A,C,G,K.
15.ANCESTOR: youjustneedtomove formgivennode toroot nodes takingall nodes coming
inbetween.(takinggivennode node).(eg.Same problem )
>>ancestor of ‘j ‘: J,E,B,A & FOR ‘G’ : G,C,A & FOR ‘K’: K,G,C,A.
#####################################################################################
APPLICATIONS OF TREE :
1. Organizationstructureof corporation.
2. Table of contentof books.
3. Dos or window file system.
##############################################
# BINARY TREE
In a normal tree,everynode canhave any numberof children.Binarytree isaspecial type of tree data
structure inwhicheverynode can have a maximumof 2 children.One isknownasleftchildandthe
otheris knownasright child.
TYPE OF BINARY TREES :
1. STRICTLY BINARY TREE
2.COMPLETEBINARYTREE
3. EXTRENDED BINARYTREE
>> STRICTLY BINARYTREE : In a binarytree,everynode canhave a maximumof two children.
But instrictlybinarytree,everynode shouldhave exactlytwochildrenornone.Thatmeansevery
internal node musthave exactlytwochildren.
>>2. COMPLETEBINARY TREE(PerfectBinaryTree)
In a binarytree,everynode canhave a maximumof twochildren.Butinstrictlybinarytree,everynode
shouldhave exactlytwochildrenornone andincomplete binarytree all the nodesmusthave exactly
twochildrenandat everylevel of complete binarytree theremustbe 2^n level numberof nodes.For
example atlevel 2there mustbe 2^2 = 4 nodesandat level 3there mustbe 2^3 = 8 nodes.
A binary tree in which every internal node has exactly two children and all leaf
nodes are at samelevel is called Complete Binary Tree.
Complete binarytree isalsocalledas PerfectBinaryTree.
3.EXTRENDED BINARYTREE
A binarytree can be convertedintoFull Binarytree byaddingdummynodestoexistingnodeswherever
required .The full binary tree obtained by adding dummy nodes to a binary tree is called as
Extended Binary Tree.
########################################
# REPRESENTATION OF BINARY TREE:
(normal trees cann’t be represent as array and link list,justbecauseof there irregular shape.)
1. linked list representation
2. Array representation (Let we have this binary tree.)
1. linked list representation
2. Array representation
(1-D) HERE THE ARRANGEMENT OF DATA IS DONE ON THE BASESE OF “ 2*K , 2*K
+1 “ OFCHILDS OF EVERY NODE . ( K IS THE NODE NUMBER GIVEN ,(STARTS FORM 0).
###############################################
#################################################
###############################################
# BINARY TREE TRAVERSALS
(TO DISPLAY A BINARY TREE WE NEED TO FOLLOW SOME
ORDERS )
1. INORDER
2. PREORDER
3. POSTORDER
(HOPELLY U ALL KNOW HOW TO DO THESE JUST VERIFY ANSWERS )
1. 1. In - Order Traversal( leftChild - root- rightChild )
I - D - J - B - F - A - G - K - C – H
2. Pre - OrderTraversal ( root - leftChild - rightChild)
A - B - D - I - J - F - C - G - K - H
3. Post - OrderTraversal ( leftChild - rightChild - root)
I - J - D - F - B - K - G - H - C – A
Program for all three method is :
#include <stdio.h>
#include <stdlib.h>
struct node
{ intvalue;
struct node*left;
struct node*right;
};
struct node*root;
struct node*insert(structnode*r,intdata);
voidinOrder(structnode*r);
voidpreOrder(structnode*r);
voidpostOrder(structnode*r);
intmain()
{ root= NULL;
int n,i,v;
printf("Howmanydata'sdoyou wantto insert?n");
scanf("%d",&n);
for( i=0; i<n;i++){
printf("Data%d:",i+1); scanf("%d",&v);
root= insert(root,v);}
printf("InorderTraversal:");
inOrder(root);
printf("n");
printf("PreorderTraversal:");
preOrder(root);
printf("n");
printf("PostorderTraversal:");
postOrder(root);
printf("n");
return0; }
struct node*insert(structnode*r,intdata)
{ if(r==NULL)
{ r = (structnode*) malloc(sizeof(structnode));
r->value =data;
r->left=NULL;
r->right= NULL;
}
else if(data<r->value){
r->left=insert(r->left,data);}
else {
r->right= insert(r->right,data);
}returnr; }
voidinOrder(structnode*r)
{ if(r!=NULL){
inOrder(r->left);
printf("%d",r->value);
inOrder(r->right);
}
}
voidpreOrder(structnode*r)
{ if(r!=NULL){
printf("%d",r->value);
preOrder(r->left);
preOrder(r->right);
}
}
voidpostOrder(structnode*r)
{ if(r!=NULL){
postOrder(r->left);
postOrder(r->right);
printf("%d",r->value); }
}
# Threaded Binary Tree
A binarytree isrepresentedusingarrayrepresentationorlinkedlistrepresentation.Whenabinarytree
isrepresentedusinglinkedlistrepresentation,if anynode isnothavinga childwe use NULL pointerin
that position.Inanybinarytree linkedlistrepresentation,there are more numberof NULL pointerthan
actual pointers.Generally,inanybinarytree linkedlistrepresentation,if there are 2N numberof
reference fields,then N+1numberof reference fieldsare filledwithNULL( N+1 are NULL outof 2N ).
ThisNULL pointerdoesnotplayanyrole exceptindicatingthereisnolink(nochild).
Threaded Binary Tree is also a binary tree in which all left child pointers
that are NULL (in Linked list representation) points to its in-order
predecessor, and all right child pointers that are NULL (in Linked list
representation) points to its in-order successor.
(STEPS TO SOLVE THREDED BINARY TREE . (REPRESENT THIS))
1. WRITE INORDER OF TREE.
2. ONE WAY  RIGHT NODE: jointhe inoder successerof of node represented byarrow .
3. TWO WAY -> RIGHT +LEFT NODE : jointhe inorder predecessor.
4. else if nither predecessorof successerispresentof the node thenthatnode isrepresentbyNULL.
# HUFFMAN ALGORITHM (IMPORTANT EVEN)
HOPLLY U CAN DO IT
(THIS ALGO IS GREAT , FOR STORE THE DATA IN ANOTHER WAY (BY ITS WAY) EXCEPT OF OUR
OWN )
*HEIGHT OF TREE FORM IS -> N-1 (WHERE THE ‘N’ IS TOTAL NO. OF ELEMENTS TO BE
ARRANGE.)
An example is
Q. arrange these element 2,3,5,7,9,11 using huffman algorithm ?
Steps are:
1. Add smallest elements at level first.
2. Compare answer with given element if
a. If answer is smaller or equal to any element then proceed to add at upper level.
b. If answer is greater then the other elements , proceed to add any other two smaller
numbers(can include answer or another element).
Made by Gaurav Sharma…
Trees in data structrures

More Related Content

What's hot

Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
Daniyal Khan
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
Äshïsh Jäïn
 
Lecture7 data structure(tree)
Lecture7 data structure(tree)Lecture7 data structure(tree)
Tree
TreeTree
Data structure tree - beginner
Data structure tree - beginnerData structure tree - beginner
Data structure tree - beginner
MD. MARUFUZZAMAN .
 
Binary tree
Binary  treeBinary  tree
Binary tree
Vanitha Chandru
 
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeData Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary Tree
ManishPrajapati78
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
Trees - Data structures in C/Java
Trees - Data structures in C/JavaTrees - Data structures in C/Java
Trees - Data structures in C/Javageeksrik
 
Binary tree
Binary tree Binary tree
Binary tree
Rajendran
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
Katang Isip
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
ER Punit Jain
 
Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary tree
Zaid Shabbir
 
Chapter 8 ds
Chapter 8 dsChapter 8 ds
Chapter 8 ds
Hanif Durad
 
Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
Dhrumil Panchal
 
Binary tree traversal ppt
Binary tree traversal pptBinary tree traversal ppt
Binary tree traversal ppt
PREEYANKAV
 
Data structure tree - intermediate
Data structure tree - intermediateData structure tree - intermediate
Data structure tree - intermediate
MD. MARUFUZZAMAN .
 

What's hot (20)

Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
Lecture7 data structure(tree)
Lecture7 data structure(tree)Lecture7 data structure(tree)
Lecture7 data structure(tree)
 
Tree
TreeTree
Tree
 
Data structure tree - beginner
Data structure tree - beginnerData structure tree - beginner
Data structure tree - beginner
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeData Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary Tree
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Binary tree
Binary treeBinary tree
Binary tree
 
Trees - Data structures in C/Java
Trees - Data structures in C/JavaTrees - Data structures in C/Java
Trees - Data structures in C/Java
 
Binary tree
Binary tree Binary tree
Binary tree
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary tree
 
Chapter 8 ds
Chapter 8 dsChapter 8 ds
Chapter 8 ds
 
binary tree
binary treebinary tree
binary tree
 
Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
 
Binary tree traversal ppt
Binary tree traversal pptBinary tree traversal ppt
Binary tree traversal ppt
 
Data structure tree - intermediate
Data structure tree - intermediateData structure tree - intermediate
Data structure tree - intermediate
 
Lecture 5 trees
Lecture 5 treesLecture 5 trees
Lecture 5 trees
 

Similar to Trees in data structrures

Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
Om Prakash
 
Data Structures
Data StructuresData Structures
Data Structures
Nitesh Bichwani
 
Dsc++ unit 3 notes
Dsc++ unit 3 notesDsc++ unit 3 notes
Dsc++ unit 3 notes
Guru Nanak Institute Of Tech
 
NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxNON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
Rajitha Reddy Alugati
 
Binary tree and operations
Binary tree and operations Binary tree and operations
Binary tree and operations
varagilavanya
 
Trees unit 3
Trees unit 3Trees unit 3
Trees unit 3
praveena p
 
Unit 4.1 (tree)
Unit 4.1 (tree)Unit 4.1 (tree)
Unit 4.1 (tree)
DurgaDeviCbit
 
UNIT-4 TREES.ppt
UNIT-4 TREES.pptUNIT-4 TREES.ppt
UNIT-4 TREES.ppt
SIVAKUMARM603675
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binary
SSE_AndyLi
 
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
nakulvarshney371
 
Graph Traversals
Graph TraversalsGraph Traversals
Graph Traversals
MaryJacob24
 
#include iostream using namespace std; const int nil = 0; cl.docx
#include iostream using namespace std; const int nil = 0; cl.docx#include iostream using namespace std; const int nil = 0; cl.docx
#include iostream using namespace std; const int nil = 0; cl.docx
ajoy21
 
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
BHARATH KUMAR
 
Tree Leetcode - Interview Questions - Easy Collections
Tree Leetcode - Interview Questions - Easy CollectionsTree Leetcode - Interview Questions - Easy Collections
Tree Leetcode - Interview Questions - Easy Collections
Sunil Yadav
 
C Language Unit-8
C Language Unit-8C Language Unit-8
C Language Unit-8
kasaragadda srinivasrao
 
Chapter 5_Trees.pdf
Chapter 5_Trees.pdfChapter 5_Trees.pdf
Chapter 5_Trees.pdf
ssuser50179b
 
Unit 3,4.docx
Unit 3,4.docxUnit 3,4.docx
Unit 3,4.docx
Revathiparamanathan
 
Trees
TreesTrees

Similar to Trees in data structrures (20)

Trees
TreesTrees
Trees
 
Unit8 C
Unit8 CUnit8 C
Unit8 C
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
 
Data Structures
Data StructuresData Structures
Data Structures
 
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
 
Binary tree and operations
Binary tree and operations Binary tree and operations
Binary tree and operations
 
Trees unit 3
Trees unit 3Trees unit 3
Trees unit 3
 
Unit 4.1 (tree)
Unit 4.1 (tree)Unit 4.1 (tree)
Unit 4.1 (tree)
 
UNIT-4 TREES.ppt
UNIT-4 TREES.pptUNIT-4 TREES.ppt
UNIT-4 TREES.ppt
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binary
 
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
 
Graph Traversals
Graph TraversalsGraph Traversals
Graph Traversals
 
#include iostream using namespace std; const int nil = 0; cl.docx
#include iostream using namespace std; const int nil = 0; cl.docx#include iostream using namespace std; const int nil = 0; cl.docx
#include iostream using namespace std; const int nil = 0; cl.docx
 
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
 
Tree Leetcode - Interview Questions - Easy Collections
Tree Leetcode - Interview Questions - Easy CollectionsTree Leetcode - Interview Questions - Easy Collections
Tree Leetcode - Interview Questions - Easy Collections
 
C Language Unit-8
C Language Unit-8C Language Unit-8
C Language Unit-8
 
Chapter 5_Trees.pdf
Chapter 5_Trees.pdfChapter 5_Trees.pdf
Chapter 5_Trees.pdf
 
Unit 3,4.docx
Unit 3,4.docxUnit 3,4.docx
Unit 3,4.docx
 
Trees
TreesTrees
Trees
 

Recently uploaded

The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
Pipe Restoration Solutions
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
Kamal Acharya
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
Jayaprasanna4
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
PrashantGoswami42
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
Kamal Acharya
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
DuvanRamosGarzon1
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 

Recently uploaded (20)

The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 

Trees in data structrures

  • 1. 3 TREE A tree is a (possibly non-linear) data structure made up of nodes or vertices and edges without having any cycle. The tree with no nodes is called the null orempty tree. A tree that is not empty consists of a root node and potentially many levels of additional nodes that form a hierarchy. Not a tree: cy cle B→C→E→D→B. B has more than one parent (inbound edge Not a tree: two non-connected parts, A→B and C→D→E. There is more than one root Not a tree: undirected cy cle 1-2-4-3. 4 has more than one parent (inbound edge Not a tree: cy cle A→A. A is the root but it also has a parent. Each linear list is triv ially a tree # TERMINOLOGIES OF TREES: 1.ROOTS: THE FIRST NODE OF TREE IS CALLED ROOT NODE.
  • 2. 2.PARENTS: In simple words,the node whichhasbranchfromit to anyothernode is calledas parentnode.Parentnode can alsobe definedas"The node whichhaschild/children". 3. EDGES: the node whichhasa linkfromitsparentnode iscalledaschildnode.Ina tree,any parentnode can have any numberof childnodes.Ina tree,all the nodesexceptrootare child nodes. 4.CHILD : In simple words,the node whichhasalinkfromitsparentnode iscalledas childnode. In a tree,anyparentnode can have anynumberof childnodes. 5.SIBLING: a group of nodes with same parents. 6.LEAF: a node with no child. 7. INTERNAL NODE: In a tree data structure,nodesotherthanleaf nodesare called as Internal Nodes. The rootnode isalsosaidto be Internal Node if the tree hasmore than one node. Internal nodesare alsocalledas'Non-Terminal'nodes. 8.HEIGHT:(for height of of tree we need to move from downward (root node ) to upward upto which height of node is asked.) ina tree datastructure,the total numberof egdes fromleaf node to a particularnode inthe longestpathiscalledas HEIGHT of that Node. Ina tree,heightof the root node issaidto be heightof the tree.
  • 3. 9. DEGREE: the total numberof childrenof a node iscalledas DEGREE of that Node 10.DEPTH: In a tree data structure,the total numberof egdesfromrootnode to a particular node iscalledas DEPTH of that Node. Ina tree,the total numberof edgesfromrootnode to a leaf node inthe longestpathissaidto be Depthof the tree. 11. PATH: the sequence of NodesandEdgesfromone node toanothernode iscalled as PATH betweenthattwoNodes. 12. SUBTREE: each childfroma node formsa subtree recursively.Everychildnodewillforma subtree onitsparentnode. 13. LEVEL: ina tree each stepfromtop to bottomiscalledas a Level andthe Level countstarts with'0' and incrementedbyone ateach level (Step). Figure 1. 14.DESCENDANT: youjust needtomove formroot node to leaf node (takingrootnode).
  • 4. Consider fig. 1 letwe needtofind descendantand ancestorfor ‘J’,’G’,’k’. Thenanswerwill be >>> descendentof ‘j ‘: A,B,E,J & FOR ‘G’ : A,C,G & FOR ‘K’: A,C,G,K. 15.ANCESTOR: youjustneedtomove formgivennode toroot nodes takingall nodes coming inbetween.(takinggivennode node).(eg.Same problem ) >>ancestor of ‘j ‘: J,E,B,A & FOR ‘G’ : G,C,A & FOR ‘K’: K,G,C,A. ##################################################################################### APPLICATIONS OF TREE : 1. Organizationstructureof corporation. 2. Table of contentof books. 3. Dos or window file system. ############################################## # BINARY TREE In a normal tree,everynode canhave any numberof children.Binarytree isaspecial type of tree data structure inwhicheverynode can have a maximumof 2 children.One isknownasleftchildandthe otheris knownasright child. TYPE OF BINARY TREES : 1. STRICTLY BINARY TREE 2.COMPLETEBINARYTREE 3. EXTRENDED BINARYTREE >> STRICTLY BINARYTREE : In a binarytree,everynode canhave a maximumof two children. But instrictlybinarytree,everynode shouldhave exactlytwochildrenornone.Thatmeansevery internal node musthave exactlytwochildren.
  • 5. >>2. COMPLETEBINARY TREE(PerfectBinaryTree) In a binarytree,everynode canhave a maximumof twochildren.Butinstrictlybinarytree,everynode shouldhave exactlytwochildrenornone andincomplete binarytree all the nodesmusthave exactly twochildrenandat everylevel of complete binarytree theremustbe 2^n level numberof nodes.For example atlevel 2there mustbe 2^2 = 4 nodesandat level 3there mustbe 2^3 = 8 nodes. A binary tree in which every internal node has exactly two children and all leaf nodes are at samelevel is called Complete Binary Tree. Complete binarytree isalsocalledas PerfectBinaryTree. 3.EXTRENDED BINARYTREE A binarytree can be convertedintoFull Binarytree byaddingdummynodestoexistingnodeswherever required .The full binary tree obtained by adding dummy nodes to a binary tree is called as Extended Binary Tree.
  • 6. ######################################## # REPRESENTATION OF BINARY TREE: (normal trees cann’t be represent as array and link list,justbecauseof there irregular shape.) 1. linked list representation 2. Array representation (Let we have this binary tree.) 1. linked list representation 2. Array representation (1-D) HERE THE ARRANGEMENT OF DATA IS DONE ON THE BASESE OF “ 2*K , 2*K +1 “ OFCHILDS OF EVERY NODE . ( K IS THE NODE NUMBER GIVEN ,(STARTS FORM 0).
  • 8. ############################################### # BINARY TREE TRAVERSALS (TO DISPLAY A BINARY TREE WE NEED TO FOLLOW SOME ORDERS ) 1. INORDER 2. PREORDER 3. POSTORDER (HOPELLY U ALL KNOW HOW TO DO THESE JUST VERIFY ANSWERS ) 1. 1. In - Order Traversal( leftChild - root- rightChild ) I - D - J - B - F - A - G - K - C – H
  • 9. 2. Pre - OrderTraversal ( root - leftChild - rightChild) A - B - D - I - J - F - C - G - K - H 3. Post - OrderTraversal ( leftChild - rightChild - root) I - J - D - F - B - K - G - H - C – A Program for all three method is : #include <stdio.h> #include <stdlib.h> struct node { intvalue; struct node*left; struct node*right; }; struct node*root; struct node*insert(structnode*r,intdata); voidinOrder(structnode*r); voidpreOrder(structnode*r); voidpostOrder(structnode*r); intmain() { root= NULL; int n,i,v; printf("Howmanydata'sdoyou wantto insert?n"); scanf("%d",&n); for( i=0; i<n;i++){ printf("Data%d:",i+1); scanf("%d",&v);
  • 10. root= insert(root,v);} printf("InorderTraversal:"); inOrder(root); printf("n"); printf("PreorderTraversal:"); preOrder(root); printf("n"); printf("PostorderTraversal:"); postOrder(root); printf("n"); return0; } struct node*insert(structnode*r,intdata) { if(r==NULL) { r = (structnode*) malloc(sizeof(structnode)); r->value =data; r->left=NULL; r->right= NULL; } else if(data<r->value){ r->left=insert(r->left,data);} else { r->right= insert(r->right,data); }returnr; } voidinOrder(structnode*r)
  • 12. A binarytree isrepresentedusingarrayrepresentationorlinkedlistrepresentation.Whenabinarytree isrepresentedusinglinkedlistrepresentation,if anynode isnothavinga childwe use NULL pointerin that position.Inanybinarytree linkedlistrepresentation,there are more numberof NULL pointerthan actual pointers.Generally,inanybinarytree linkedlistrepresentation,if there are 2N numberof reference fields,then N+1numberof reference fieldsare filledwithNULL( N+1 are NULL outof 2N ). ThisNULL pointerdoesnotplayanyrole exceptindicatingthereisnolink(nochild). Threaded Binary Tree is also a binary tree in which all left child pointers that are NULL (in Linked list representation) points to its in-order predecessor, and all right child pointers that are NULL (in Linked list representation) points to its in-order successor. (STEPS TO SOLVE THREDED BINARY TREE . (REPRESENT THIS)) 1. WRITE INORDER OF TREE. 2. ONE WAY  RIGHT NODE: jointhe inoder successerof of node represented byarrow . 3. TWO WAY -> RIGHT +LEFT NODE : jointhe inorder predecessor. 4. else if nither predecessorof successerispresentof the node thenthatnode isrepresentbyNULL.
  • 13. # HUFFMAN ALGORITHM (IMPORTANT EVEN) HOPLLY U CAN DO IT (THIS ALGO IS GREAT , FOR STORE THE DATA IN ANOTHER WAY (BY ITS WAY) EXCEPT OF OUR OWN ) *HEIGHT OF TREE FORM IS -> N-1 (WHERE THE ‘N’ IS TOTAL NO. OF ELEMENTS TO BE ARRANGE.) An example is Q. arrange these element 2,3,5,7,9,11 using huffman algorithm ? Steps are: 1. Add smallest elements at level first. 2. Compare answer with given element if a. If answer is smaller or equal to any element then proceed to add at upper level. b. If answer is greater then the other elements , proceed to add any other two smaller numbers(can include answer or another element). Made by Gaurav Sharma…