SlideShare a Scribd company logo
1 of 39
Unit- 4 (Part -1)
TREES
CA3CRT09- Data Structures Using C++
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
Syllabus
Data Structures using C++
Unit 1 (12 hrs. )
Concept of Structured data - Data structure definition, Different types and classification of data structures,
Arrays – Memory allocation and implementation of arrays in memory, array operations, Applications -
sparse matrix representation and operations, polynomials representation and addition, Concept of search
and sort – linear search, binary search, selection sort, insertion sort, quick sort.
Unit 2 (12 hrs.)
Stacks – Concepts, organization and operations on stacks using arrays (static), examples, Applications -
Conversion of infix to postfix and infix to prefix, postfix evaluation, subprogram calls and execution,
Multiple stacks representation.
Queues - Concepts, organization and operations on queues, examples.
Circular queue – limitations of linear queue, organization and operations on circular queue. Double ended
queue, Priority queue.
Unit 3 (18 hrs.)
Linked list: Concept of dynamic data structures, linked list, types of linked list, linked list using pointers,
insertion and deletion examples, circular linked list, doubly linked lists
Applications- linked stacks and queues, memory management basic concepts, garbage collection.
Unit 4 (15)
Trees - Concept of recursion, trees, tree terminology, binary trees, representation of binary trees, strictly
binary trees, complete binary tree, extended binary trees, creation and operations on binary tree, binary
search trees, Creation of binary search tree, tree traversing methods – examples, binary tree representation
of expressions.
Unit 5 (15)
File - Definition, Operations on file (sequential), File organizations - sequential, Indexed sequential,
random files, linked organization, inverted files, cellular partitioning, hashing – hash tables, hashing
functions, collisions, collision resolving methods.
38
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
1. Trees - Concept of recursion,
2. trees, tree terminology,
3. binary trees, representation of binary trees,
1. Strictly binary trees,
2. complete binary tree,
3. extended binary trees,
4. creation and operations on binary tree,
5. Binary search trees, Creation of binary search tree,
6. tree traversing methods – examples,
7. binary tree representation of expressions.
TREES
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
MES College Marampally
Tree
⚫ Tree is a non-linear data structure in which items are
arranged in a sorted sequence. It is used to represent
hierarchical relationship existing among several data items.
⚫ In tree data structure, every individual element is called
as Node. Node in a tree data structure, stores the actual data
of that particular element and link to next element in
hierarchical structure.
In a tree data structure, if we have N nS
uy
m
ste
b
m
e
Ad
rmi
o
nis
f
tra
n
ti
o
on
des then we
can have a maximum of N-1 number of links.
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
Tree Terminology
1. Root
In a tree data structure, the first node is called as Root Node.
Every tree must have root node. We can say that root node is
the origin of tree data structure. In any tree, there must be
only one root node. We never have multiple root nodes in a
tree.
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
2. Edge
In a tree data structure, the connecting link between any two
nodes is called as EDGE. In a tree with 'N' number of nodes
there will be a maximum of 'N-1' number of edges.
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
3. Parent
In a tree data structure, the node which is predecessor of any
node is called as PARENT NODE. In simple words, the node
which has branch from it to any other node is called as parent
node. Parent node can also be defined as "The node which has
child / children".
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
4. Child
In a tree data structure, the node which is descendant of any
node is called as CHILD Node. In simple words, the node which
has a link from its parent node is called as child node. In a tree,
any parent node can have any number of child nodes. In a tree,
all the nodes except root are child nodes.
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
5. Siblings
In a tree data structure, nodes which belong to same Parent are
called as SIBLINGS. In simple words, the nodes with same
parent are called as Sibling nodes.
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
6. Leaf
In a tree data structure, the node which does not have a child
is called as LEAF Node. In simple words, a leaf is a node with no
child.
In a tree data structure, the leaf nodes are also called
as External Nodes. External node is also a node with no child.
In a tree, leaf node is also called as 'Terminal' node.
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
7. Internal Nodes
In a tree data structure, the node which has atleast one child is
called as INTERNAL Node. In simple words, an internal node is a
node with atleast one child.
In a tree data structure, nodes other than leaf nodes are called
as Internal Nodes. The root node is also said to be Internal
Node if the tree has more than one node. Internal nodes are also
called as 'Non-Terminal' nodes.
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
8. Degree
In a tree data structure, the total number of children of a node
is called as DEGREE of that Node. In simple words, the Degree
of a node is total number of children it has. The highest degree
of a node among all the nodes in a tree is called as 'Degree of
Tree'
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
9. Level
In a tree data structure, the root node is said to be at Level 0
and the children of root node are at Level 1 and the children of
the nodes which are at Level 1 will be at Level 2 and so on... In
simple words, in a tree each step from top to bottom is called as
a Level and the Level count starts with '0' and incremented by
one at each level (Step).
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
10. Height
In a tree data structure, the total number of egdes from leaf
node to a particular node in the longest path is called
as HEIGHT of that Node. In a tree, height of the root node is
said to be height of the tree. In a tree, height of all leaf
nodes is '0'.
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
11. Depth
In a tree data structure, the total number of egdes from root
node to a particular node is called as DEPTH of that Node. In a
tree, the total number of edges from root node to a leaf node
in the longest path is said to be Depth of the tree. In simple
words, the highest depth of any leaf node in a tree is said to be
depth of that tree. In a tree, depth of the root node is '0'.
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
12. Path
In a tree data structure, the sequence of Nodes and Edges
from one node to another node is called as PATH between that
two Nodes. Length of a Path is total number of nodes in that
path. In below example the path A - B - E - J has length 4.
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
13. Sub Tree
In a tree data structure, each child from a node forms a
subtree recursively. Every child node will form a subtree on its
parent node.
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
Binary Tree
⚫ In a normal tree, every node can have any number of children.
Binary tree is a special type of tree data structure in which
every node can have a maximum of 2 children. One is known as
left child and the other is known as right child.
⚫ In a binary tree, every node can have either 0 children or 1
child or 2 children but not more than 2 children.
Binary Tree Representations
A binary tree data structure is represented using two methods.
Those methods are as follows...
⚫ Array Representation
⚫ Linked List Representation
Consider the following binary tree..
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
1. Binary tree Array Representation
⚫ In array representation of binary tree, we use a one dimensional
array (1-D Array) to represent a binary tree.
Consider the above example of binary tree and it is represented
as follows...
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
⚫ The root node is always at index 0.
⚫ Then in successive memory locations the left child and right
child are stored.
2. Binary tree Linked List Representation
⚫ We use doubly linked list to represent a binary tree.
⚫ In a doubly linked list, every node consists of three fields.
⚫ First field for storing left child address,
⚫ second for storing actual data and
⚫ third for storing right child address.
In this linked list representation, a node has the following structure...
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
ASH
Assis
Depa
Syst
MES
IKA K A
tant Professor & Head
rtment of Software Development and
em Administration
College Marampally
struct node
{
int data;
node *lchild;
node *rchild;
}
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
⚫ binary tree represented using Linked list representation is
shown as follows...
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
Operations on binary trees
1. Creation
2. Insertion of nodes
3. Deletion of nodes
4. Tree traversal
5. Searching for the node
6. Copying the binary tree
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
Binary Tree Traversals
⚫ When we wanted to display a binary tree, we need to follow
some order in which all the nodes of that binary tree must be
displayed. In any binary tree displaying order of nodes depends
on the traversal method.
⚫ Displaying (or) visiting order of nodes in a binary tree is
called as Binary Tree Traversal.
⚫ There are three types of binary tree traA
S
vH
eI
K
rA
sK
aA
ls.
1. Inorder Traversal
2. Preorder Traversal
3. Postorder Traversal
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
⚫ Inorder Traversal ( leftChild - root - rightChild )
⚫ Preorder Traversal ( root - leftChild - rightChild )
⚫ Postorder Traversal ( leftChild - rightChild - root )
ASHIKA K A
Department of Software Development and
Inorder traversal
The inorder traversal of non-empty binary tree is defined as
follows:
1. Traverse the left subtree in inorder
2. Visit the root node
3. Traverse the right subtree in inorder.
Ie. In inorder traversal left subtree is tra
Av
sse
ist
r
an
s
te
Pr
d
ofe
r
ss
e
or
c&
uH
r
es
ad
ively in
inorder before visiting the root node.aS
f
yt
ste
em
rAv
dm
is
ini
ist
tr
ia
n
tig
on the root
node , the right subtree is taken up and
M
E
S
it
C
o
i
l
l
s
e
g
e
t
M
r
a
a
r
a
v
m
e
p
a
r
l
l
y
sed
recursively again in inorder.
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
Inorder traversal
Void inorder(struct node *tree)
{
If (tree!=NULL)
{
Inorder (tree->left);
Cout << tree->num;
Inorder (tree->right);
}
}
ASHIKA K A
Department of Software Development and
System Administration
MES College Marampally
preorder traversal
The preorder traversal of non-empty binary tree is defined as
follows:
1. Visit the root node
2. Traverse the left subtree in preorder
3. Traverse the right subtree in preorder.
Ie. In preorder traversal root node is visitA
s
es
i
ds
t
a
bn
teP
r
fo
f
oe
s
rs
o
er&
vH
ie
sa
id
ting left
and right subtrees.
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
preorder traversal
Void preorder(struct node *tree)
{
If (tree!=NULL)
{
Cout << tree->num;
preorder (tree->left);
preorder (tree->right);
}
}
ASHIKA K A
Department of Software Development and
System Administration
MES College Marampally
postorder traversal
The postorder traversal of non-empty binary tree is defined as
follows:
1. Traverse the left subtree in postorder
2. Traverse the right subtree in postorder.
3. Visit the root node
Ie. In postorder traversal left and right sA
us
s
bi
s
tt
a
rn
t
eP
er
o
sf
e
s
as
o
rr
e
&Hreaedcursively
processed before visiting the root.
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
postrder traversal
Void postorder(struct node *tree)
{
If (tree!=NULL)
{
postorder (tree->left);
postorder (tree->right);
Cout << tree->num;
}
}
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
12 9
6
5
Inorder traversal : 5->12->6->1->9
Preorder traversal : 1->12->5->6->9
Postorder traversal : 5->6->12->9->1
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
12 9
6
5
Inorder traversal : 5->12->6->1->9
Preorder traversal : 1->12->5->6->9
Postorder traversal : 5->6->12->9->1
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
12 9
6
5
Inorder traversal : 5->12->6->1->9
Preorder traversal : 1->12->5->6->9
Postorder traversal : 5->6->12->9->1
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node *left, *right;
Node(int data) {
this->data = data;
left = right = NULL;
}
};
// Preorder traversal
void preorderTraversal(struct Node* node) {
if (node == NULL)
return;
cout << node->data << "->";
preorderTraversal(node->left);
preorderTraversal(node->right);
}
// Postorder traversal
void postorderTraversal(struct Node* no
de) {
}
) {
if (node == NULL)
return;
postorderTraversal(node->left);
postorderTraversal(node->right);
cout << node->data << "->";
// Inorder traversal
void inorderTraversal(struct Node* node
ASHIKA K A
if (node A
=s
=
sisN
ta
U
n
L
tL
P)
rofessor & Head
Department of Software Development and
return;System Administration
MES College Marampally
inorderTraversal(node->left);
cout << node->data << "->";
inorderTraversal(node->right);
}
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
int main() {
struct Node* root = new Node(1)
;
root->left = new Node(12);
root->right = new Node(9);
root->left->left = new Node(5);
root->left->right = new Node(6)
;
cout << "Inorder traversal ";
inorderTraversal(root);
cout << "nPreorder traversal
"; preorderTraversal(root);
cout << "nPostorder traversal ";
postorderTraversal(root);
}
Output:
Inorder traversal : 5->12->6->1->9
Preorder traversal : 1->12->5->6->9
Postorder traversal : 5->6->12->9->1

More Related Content

Similar to DATA STUCTURES-TREES.pptx

SIX WEEKS SUMMER TRAINING REPORT.pptx
SIX WEEKS SUMMER TRAINING REPORT.pptxSIX WEEKS SUMMER TRAINING REPORT.pptx
SIX WEEKS SUMMER TRAINING REPORT.pptxsagabo1
 
Trees in data structures
Trees in data structuresTrees in data structures
Trees in data structuresASairamSairam1
 
ICS Part 2 Computer Science Short Notes
ICS Part 2 Computer Science Short NotesICS Part 2 Computer Science Short Notes
ICS Part 2 Computer Science Short NotesAbdul Haseeb
 
Terminology of tree
Terminology of treeTerminology of tree
Terminology of treeRacksaviR
 
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 presentationnakulvarshney371
 
Tree data structure in java
Tree data structure in javaTree data structure in java
Tree data structure in javaIrfan CH
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanDaniyal Khan
 
Week 3 Classification of Database Management Systems & Data Modeling
Week 3 Classification of Database Management Systems & Data ModelingWeek 3 Classification of Database Management Systems & Data Modeling
Week 3 Classification of Database Management Systems & Data Modelingoudesign
 
Database Concepts & SQL(1).pdf
Database Concepts & SQL(1).pdfDatabase Concepts & SQL(1).pdf
Database Concepts & SQL(1).pdfrsujeet169
 
Introduction To Data Structures
Introduction To Data StructuresIntroduction To Data Structures
Introduction To Data StructuresSpotle.ai
 
Data Models & Introduction to UML
Data Models & Introduction to UML Data Models & Introduction to UML
Data Models & Introduction to UML نبيله نواز
 
Unit1 rdbms study_materials
Unit1 rdbms study_materialsUnit1 rdbms study_materials
Unit1 rdbms study_materialsgayaramesh
 
Unit1 rdbms study_materials-converted (1) (1)
Unit1 rdbms study_materials-converted (1) (1)Unit1 rdbms study_materials-converted (1) (1)
Unit1 rdbms study_materials-converted (1) (1)gayaramesh
 
Bca examination 2015 dbms
Bca examination 2015 dbmsBca examination 2015 dbms
Bca examination 2015 dbmsAnjaan Gajendra
 

Similar to DATA STUCTURES-TREES.pptx (20)

SIX WEEKS SUMMER TRAINING REPORT.pptx
SIX WEEKS SUMMER TRAINING REPORT.pptxSIX WEEKS SUMMER TRAINING REPORT.pptx
SIX WEEKS SUMMER TRAINING REPORT.pptx
 
Nagaraju
NagarajuNagaraju
Nagaraju
 
dsa2.ppt
dsa2.pptdsa2.ppt
dsa2.ppt
 
Trees
TreesTrees
Trees
 
Trees in data structures
Trees in data structuresTrees in data structures
Trees in data structures
 
ICS Part 2 Computer Science Short Notes
ICS Part 2 Computer Science Short NotesICS Part 2 Computer Science Short Notes
ICS Part 2 Computer Science Short Notes
 
Terminology of tree
Terminology of treeTerminology of tree
Terminology of tree
 
Trees-Terminology.pptx
Trees-Terminology.pptxTrees-Terminology.pptx
Trees-Terminology.pptx
 
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
 
Tree data structure in java
Tree data structure in javaTree data structure in java
Tree data structure in java
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 
Week 3 Classification of Database Management Systems & Data Modeling
Week 3 Classification of Database Management Systems & Data ModelingWeek 3 Classification of Database Management Systems & Data Modeling
Week 3 Classification of Database Management Systems & Data Modeling
 
Database Concepts & SQL(1).pdf
Database Concepts & SQL(1).pdfDatabase Concepts & SQL(1).pdf
Database Concepts & SQL(1).pdf
 
Introduction To Data Structures
Introduction To Data StructuresIntroduction To Data Structures
Introduction To Data Structures
 
Data Models & Introduction to UML
Data Models & Introduction to UML Data Models & Introduction to UML
Data Models & Introduction to UML
 
BINARY SEARCH TREE
BINARY SEARCH TREE BINARY SEARCH TREE
BINARY SEARCH TREE
 
Unit1 rdbms study_materials
Unit1 rdbms study_materialsUnit1 rdbms study_materials
Unit1 rdbms study_materials
 
Unit1 rdbms study_materials-converted (1) (1)
Unit1 rdbms study_materials-converted (1) (1)Unit1 rdbms study_materials-converted (1) (1)
Unit1 rdbms study_materials-converted (1) (1)
 
Ch12 Tree
Ch12 TreeCh12 Tree
Ch12 Tree
 
Bca examination 2015 dbms
Bca examination 2015 dbmsBca examination 2015 dbms
Bca examination 2015 dbms
 

Recently uploaded

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
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
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
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
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
 
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
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
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
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
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
 
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
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
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
 

Recently uploaded (20)

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
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
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
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
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
 
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
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
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
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
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
 
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
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
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
 

DATA STUCTURES-TREES.pptx

  • 1. Unit- 4 (Part -1) TREES CA3CRT09- Data Structures Using C++
  • 2. ASHIKA K A Assistant Professor & Head Department of Software Development and System Administration MES College Marampally Syllabus Data Structures using C++ Unit 1 (12 hrs. ) Concept of Structured data - Data structure definition, Different types and classification of data structures, Arrays – Memory allocation and implementation of arrays in memory, array operations, Applications - sparse matrix representation and operations, polynomials representation and addition, Concept of search and sort – linear search, binary search, selection sort, insertion sort, quick sort. Unit 2 (12 hrs.) Stacks – Concepts, organization and operations on stacks using arrays (static), examples, Applications - Conversion of infix to postfix and infix to prefix, postfix evaluation, subprogram calls and execution, Multiple stacks representation. Queues - Concepts, organization and operations on queues, examples. Circular queue – limitations of linear queue, organization and operations on circular queue. Double ended queue, Priority queue. Unit 3 (18 hrs.) Linked list: Concept of dynamic data structures, linked list, types of linked list, linked list using pointers, insertion and deletion examples, circular linked list, doubly linked lists Applications- linked stacks and queues, memory management basic concepts, garbage collection. Unit 4 (15) Trees - Concept of recursion, trees, tree terminology, binary trees, representation of binary trees, strictly binary trees, complete binary tree, extended binary trees, creation and operations on binary tree, binary search trees, Creation of binary search tree, tree traversing methods – examples, binary tree representation of expressions. Unit 5 (15) File - Definition, Operations on file (sequential), File organizations - sequential, Indexed sequential, random files, linked organization, inverted files, cellular partitioning, hashing – hash tables, hashing functions, collisions, collision resolving methods. 38
  • 3. ASHIKA K A Assistant Professor & Head Department of Software Development and System Administration MES College Marampally 1. Trees - Concept of recursion, 2. trees, tree terminology, 3. binary trees, representation of binary trees, 1. Strictly binary trees, 2. complete binary tree, 3. extended binary trees, 4. creation and operations on binary tree, 5. Binary search trees, Creation of binary search tree, 6. tree traversing methods – examples, 7. binary tree representation of expressions. TREES
  • 4. ASHIKA K A Assistant Professor & Head Department of Software Development and MES College Marampally Tree ⚫ Tree is a non-linear data structure in which items are arranged in a sorted sequence. It is used to represent hierarchical relationship existing among several data items. ⚫ In tree data structure, every individual element is called as Node. Node in a tree data structure, stores the actual data of that particular element and link to next element in hierarchical structure. In a tree data structure, if we have N nS uy m ste b m e Ad rmi o nis f tra n ti o on des then we can have a maximum of N-1 number of links.
  • 5. ASHIKA K A Assistant Professor & Head Department of Software Development and System Administration MES College Marampally
  • 6. ASHIKA K A Assistant Professor & Head Department of Software Development and System Administration MES College Marampally Tree Terminology 1. Root In a tree data structure, the first node is called as Root Node. Every tree must have root node. We can say that root node is the origin of tree data structure. In any tree, there must be only one root node. We never have multiple root nodes in a tree.
  • 7. ASHIKA K A Assistant Professor & Head Department of Software Development and System Administration MES College Marampally 2. Edge In a tree data structure, the connecting link between any two nodes is called as EDGE. In a tree with 'N' number of nodes there will be a maximum of 'N-1' number of edges.
  • 8. ASHIKA K A Assistant Professor & Head Department of Software Development and System Administration MES College Marampally 3. Parent In a tree data structure, the node which is predecessor of any node is called as PARENT NODE. In simple words, the node which has branch from it to any other node is called as parent node. Parent node can also be defined as "The node which has child / children".
  • 9. ASHIKA K A Assistant Professor & Head Department of Software Development and System Administration MES College Marampally 4. Child In a tree data structure, the node which is descendant of any node is called as CHILD Node. In simple words, the node which has a link from its parent node is called as child node. In a tree, any parent node can have any number of child nodes. In a tree, all the nodes except root are child nodes.
  • 10. ASHIKA K A Assistant Professor & Head Department of Software Development and System Administration MES College Marampally 5. Siblings In a tree data structure, nodes which belong to same Parent are called as SIBLINGS. In simple words, the nodes with same parent are called as Sibling nodes.
  • 11. ASHIKA K A Assistant Professor & Head Department of Software Development and System Administration MES College Marampally 6. Leaf In a tree data structure, the node which does not have a child is called as LEAF Node. In simple words, a leaf is a node with no child. In a tree data structure, the leaf nodes are also called as External Nodes. External node is also a node with no child. In a tree, leaf node is also called as 'Terminal' node.
  • 12. ASHIKA K A Assistant Professor & Head Department of Software Development and System Administration MES College Marampally 7. Internal Nodes In a tree data structure, the node which has atleast one child is called as INTERNAL Node. In simple words, an internal node is a node with atleast one child. In a tree data structure, nodes other than leaf nodes are called as Internal Nodes. The root node is also said to be Internal Node if the tree has more than one node. Internal nodes are also called as 'Non-Terminal' nodes.
  • 13. ASHIKA K A Assistant Professor & Head Department of Software Development and System Administration MES College Marampally 8. Degree In a tree data structure, the total number of children of a node is called as DEGREE of that Node. In simple words, the Degree of a node is total number of children it has. The highest degree of a node among all the nodes in a tree is called as 'Degree of Tree'
  • 14. ASHIKA K A Assistant Professor & Head Department of Software Development and System Administration MES College Marampally 9. Level In a tree data structure, the root node is said to be at Level 0 and the children of root node are at Level 1 and the children of the nodes which are at Level 1 will be at Level 2 and so on... In simple words, in a tree each step from top to bottom is called as a Level and the Level count starts with '0' and incremented by one at each level (Step).
  • 15. ASHIKA K A Assistant Professor & Head Department of Software Development and System Administration MES College Marampally 10. Height In a tree data structure, the total number of egdes from leaf node to a particular node in the longest path is called as HEIGHT of that Node. In a tree, height of the root node is said to be height of the tree. In a tree, height of all leaf nodes is '0'.
  • 16. ASHIKA K A Assistant Professor & Head Department of Software Development and System Administration MES College Marampally 11. Depth In a tree data structure, the total number of egdes from root node to a particular node is called as DEPTH of that Node. In a tree, the total number of edges from root node to a leaf node in the longest path is said to be Depth of the tree. In simple words, the highest depth of any leaf node in a tree is said to be depth of that tree. In a tree, depth of the root node is '0'.
  • 17. ASHIKA K A Assistant Professor & Head Department of Software Development and System Administration MES College Marampally 12. Path In a tree data structure, the sequence of Nodes and Edges from one node to another node is called as PATH between that two Nodes. Length of a Path is total number of nodes in that path. In below example the path A - B - E - J has length 4.
  • 18. ASHIKA K A Assistant Professor & Head Department of Software Development and System Administration MES College Marampally 13. Sub Tree In a tree data structure, each child from a node forms a subtree recursively. Every child node will form a subtree on its parent node.
  • 19. ASHIKA K A Assistant Professor & Head Department of Software Development and System Administration MES College Marampally Binary Tree ⚫ In a normal tree, every node can have any number of children. Binary tree is a special type of tree data structure in which every node can have a maximum of 2 children. One is known as left child and the other is known as right child. ⚫ In a binary tree, every node can have either 0 children or 1 child or 2 children but not more than 2 children.
  • 20. Binary Tree Representations A binary tree data structure is represented using two methods. Those methods are as follows... ⚫ Array Representation ⚫ Linked List Representation Consider the following binary tree.. ASHIKA K A Assistant Professor & Head Department of Software Development and System Administration MES College Marampally
  • 21. ASHIKA K A Assistant Professor & Head Department of Software Development and System Administration MES College Marampally 1. Binary tree Array Representation ⚫ In array representation of binary tree, we use a one dimensional array (1-D Array) to represent a binary tree. Consider the above example of binary tree and it is represented as follows...
  • 22. ASHIKA K A Assistant Professor & Head Department of Software Development and System Administration MES College Marampally ⚫ The root node is always at index 0. ⚫ Then in successive memory locations the left child and right child are stored.
  • 23. 2. Binary tree Linked List Representation ⚫ We use doubly linked list to represent a binary tree. ⚫ In a doubly linked list, every node consists of three fields. ⚫ First field for storing left child address, ⚫ second for storing actual data and ⚫ third for storing right child address. In this linked list representation, a node has the following structure... ASHIKA K A Assistant Professor & Head Department of Software Development and System Administration MES College Marampally
  • 24. ASH Assis Depa Syst MES IKA K A tant Professor & Head rtment of Software Development and em Administration College Marampally struct node { int data; node *lchild; node *rchild; }
  • 25. ASHIKA K A Assistant Professor & Head Department of Software Development and System Administration MES College Marampally ⚫ binary tree represented using Linked list representation is shown as follows...
  • 26. ASHIKA K A Assistant Professor & Head Department of Software Development and System Administration MES College Marampally Operations on binary trees 1. Creation 2. Insertion of nodes 3. Deletion of nodes 4. Tree traversal 5. Searching for the node 6. Copying the binary tree
  • 27. Assistant Professor & Head Department of Software Development and System Administration MES College Marampally Binary Tree Traversals ⚫ When we wanted to display a binary tree, we need to follow some order in which all the nodes of that binary tree must be displayed. In any binary tree displaying order of nodes depends on the traversal method. ⚫ Displaying (or) visiting order of nodes in a binary tree is called as Binary Tree Traversal. ⚫ There are three types of binary tree traA S vH eI K rA sK aA ls. 1. Inorder Traversal 2. Preorder Traversal 3. Postorder Traversal
  • 28. ASHIKA K A Assistant Professor & Head Department of Software Development and System Administration MES College Marampally ⚫ Inorder Traversal ( leftChild - root - rightChild ) ⚫ Preorder Traversal ( root - leftChild - rightChild ) ⚫ Postorder Traversal ( leftChild - rightChild - root )
  • 29. ASHIKA K A Department of Software Development and Inorder traversal The inorder traversal of non-empty binary tree is defined as follows: 1. Traverse the left subtree in inorder 2. Visit the root node 3. Traverse the right subtree in inorder. Ie. In inorder traversal left subtree is tra Av sse ist r an s te Pr d ofe r ss e or c& uH r es ad ively in inorder before visiting the root node.aS f yt ste em rAv dm is ini ist tr ia n tig on the root node , the right subtree is taken up and M E S it C o i l l s e g e t M r a a r a v m e p a r l l y sed recursively again in inorder.
  • 30. ASHIKA K A Assistant Professor & Head Department of Software Development and System Administration MES College Marampally Inorder traversal Void inorder(struct node *tree) { If (tree!=NULL) { Inorder (tree->left); Cout << tree->num; Inorder (tree->right); } }
  • 31. ASHIKA K A Department of Software Development and System Administration MES College Marampally preorder traversal The preorder traversal of non-empty binary tree is defined as follows: 1. Visit the root node 2. Traverse the left subtree in preorder 3. Traverse the right subtree in preorder. Ie. In preorder traversal root node is visitA s es i ds t a bn teP r fo f oe s rs o er& vH ie sa id ting left and right subtrees.
  • 32. ASHIKA K A Assistant Professor & Head Department of Software Development and System Administration MES College Marampally preorder traversal Void preorder(struct node *tree) { If (tree!=NULL) { Cout << tree->num; preorder (tree->left); preorder (tree->right); } }
  • 33. ASHIKA K A Department of Software Development and System Administration MES College Marampally postorder traversal The postorder traversal of non-empty binary tree is defined as follows: 1. Traverse the left subtree in postorder 2. Traverse the right subtree in postorder. 3. Visit the root node Ie. In postorder traversal left and right sA us s bi s tt a rn t eP er o sf e s as o rr e &Hreaedcursively processed before visiting the root.
  • 34. ASHIKA K A Assistant Professor & Head Department of Software Development and System Administration MES College Marampally postrder traversal Void postorder(struct node *tree) { If (tree!=NULL) { postorder (tree->left); postorder (tree->right); Cout << tree->num; } }
  • 35. ASHIKA K A Assistant Professor & Head Department of Software Development and System Administration MES College Marampally 12 9 6 5 Inorder traversal : 5->12->6->1->9 Preorder traversal : 1->12->5->6->9 Postorder traversal : 5->6->12->9->1
  • 36. ASHIKA K A Assistant Professor & Head Department of Software Development and System Administration MES College Marampally 12 9 6 5 Inorder traversal : 5->12->6->1->9 Preorder traversal : 1->12->5->6->9 Postorder traversal : 5->6->12->9->1
  • 37. ASHIKA K A Assistant Professor & Head Department of Software Development and System Administration MES College Marampally 12 9 6 5 Inorder traversal : 5->12->6->1->9 Preorder traversal : 1->12->5->6->9 Postorder traversal : 5->6->12->9->1
  • 38. #include <iostream> using namespace std; struct Node { int data; struct Node *left, *right; Node(int data) { this->data = data; left = right = NULL; } }; // Preorder traversal void preorderTraversal(struct Node* node) { if (node == NULL) return; cout << node->data << "->"; preorderTraversal(node->left); preorderTraversal(node->right); } // Postorder traversal void postorderTraversal(struct Node* no de) { } ) { if (node == NULL) return; postorderTraversal(node->left); postorderTraversal(node->right); cout << node->data << "->"; // Inorder traversal void inorderTraversal(struct Node* node ASHIKA K A if (node A =s = sisN ta U n L tL P) rofessor & Head Department of Software Development and return;System Administration MES College Marampally inorderTraversal(node->left); cout << node->data << "->"; inorderTraversal(node->right); }
  • 39. ASHIKA K A Assistant Professor & Head Department of Software Development and System Administration MES College Marampally int main() { struct Node* root = new Node(1) ; root->left = new Node(12); root->right = new Node(9); root->left->left = new Node(5); root->left->right = new Node(6) ; cout << "Inorder traversal "; inorderTraversal(root); cout << "nPreorder traversal "; preorderTraversal(root); cout << "nPostorder traversal "; postorderTraversal(root); } Output: Inorder traversal : 5->12->6->1->9 Preorder traversal : 1->12->5->6->9 Postorder traversal : 5->6->12->9->1