SlideShare a Scribd company logo
1 of 21
Course: BCA-II
Subject: Data Structure
Unit-3
Tree and Graph
NONLINEAR DATA STRUCTURE
 2 primary types:
• Trees
• Graphs
• All trees are graphs, but not all graphs are trees
• Recursion is useful and is the easiest way to process
them.
• It helps keep track of what's been processed and what
remains
Graphs
•Graphs can have multiple references in and multiple references out
(whereas tree node only has one reference in)
•Graphs can be directed or undirected and cyclic or acyclic
Trees
• Single parent
• 0 or more children
• A node with no children is called a "leaf"
• The topmost node is called the "root"
• N-ary trees
• Binary trees
N-ary Trees
 The best example of an n-ary tree is your computer’s
directory system.
 It has a single starting point and then 0 or more
branches.
Binary Trees and Binary Search Trees
• Binary search trees allow for fast insertion and removal of
elements
• They are specially designed for fast searching
• A binary tree consists of two nodes, each of which has two
child nodes
• All nodes in a binary search tree fulfill the property that:
• Descendants to the left have smaller data values than the node
data value
• Descendants to the right have larger data values than the node
data value
BST Tree Nodes
• All nodes in a binary search tree fulfill the property that:
• Descendants to the left have smaller data values than
the node data value
• Descendants to the right have larger data values than the
node data value
BST (Balanced Binary Trees)
• Balanced tree: each node has approximately as many
descendants on the left as on the right
• If a binary search tree is balanced, then adding an
element takes O(log(n)) time
• If the tree is unbalanced, insertion can be slow
• Perhaps as slow as insertion into a linked list
Traversing a Tree
 Which were used for the binary tree can now be used for the
general.
 When the general tree has been represented as a binary tree, the
algorithms l tree.
 In-order traversals make no sense when a general tree is converted
to a binary tree.
 In the general tree each node can have more than two children so
trying to insert the parent node in between the children is rather
difficult, especially if there is an odd number of children.
 Pre - order
 This is a process where the root is accessed and processed and
then each of the subtrees is preorder processed. It is also called a
depth-first traversal.
Traversing a Tree
 Pre-order Traversal
 In this way the resulting printout has all nodes at any given
level starting in the same tab column.
 It is relatively easy to draw lines to produce the original
general tree except that the tree is on its side with it's root at the
left rather than with the root at the top.
Tree Traversal
• Tree traversal schemes include
• Preorder traversal (root, left, right)
• Inorder traversal (left, root, right)
• Postorder traversal ( left, right, root)
• Preorder generates prefix expression,(polish notation) from
an expression trees
• Inorder generates a sorted ordering
• Postorder generates a post fix expression, also useful for
node deletion
Height of a BST
 Insert 7
 Insert 4
 Insert 1
 Insert 9
 Insert 5
 It’s a complete tree!
7
4 9
1 5
height = log(5)+1 =
3
BSTs with heights O(log n)
 It would be ideal if a BST was always close to a full
binary tree
 It’s enough to guarantee that the height of tree is
O(log n)
 To guarantee that we have to make the structure of the
tree and insertion and deletion algorithms more
complex
 e.g. AVL trees (balanced), 2-3 trees, 2-3-4 trees (full
but not binary), red–black trees (if red vertices are
ignored then it’s like a full tree)
What is a graph?
 A data structure that consists of a set of nodes
(vertices) and a set of edges that relate the nodes to
each other
 The set of edges describes relationships among the
vertices
Formal definition of graphs
 A graph G is defined as follows:
G=(V,E)
V(G): a finite, nonempty set of vertices
E(G): a set of edges (pairs of vertices)
Directed vs. undirected graphs (cont.)
 When the edges in a graph have a direction, the
graph is called directed (or digraph)
 When the edges in a graph have no direction, the
graph is called undirected.
 Warning: if the graph is directed, the order of the
vertices in each edge is important !!
Breadth-first searching[1]
 A breadth-first search (BFS)
explores nodes nearest the root
before exploring nodes further
away
 For example, after searching A,
then B, then C, the search
proceeds with D, E, F, G
 Node are explored in the order
A B C D E F G H I J K L M N O P Q
 J will be found before N
L M N O P
G
Q
H JI K
FED
B C
A
Depth-first searching[2]
 A depth-first search (DFS)
explores a path all the way to a
leaf before backtracking and
exploring another path
 For example, after searching A,
then B, then D, the search
backtracks and tries another path
from B
 Node are explored in the order A
B D E H L M N I O P C F G J K
Q
 N will be found before JL M N O P
G
Q
H JI K
FED
B C
A
A spanning tree of a graph is just a subgraph that
contains all the vertices and is a tree.
A graph may have many spanning trees.
o
r
o
r
o
r
Some Spanning Trees from Graph AGraph
A
Spanning Trees
Minimum Spanning Trees
The Minimum Spanning Tree for a given graph is the
Spanning Tree of minimum cost for that graph.
5
7
2
1
3
4
2
1
3
Complete Graph Minimum Spanning Tree
References
 An introduction to Datastructure with application by jean Trembley and sorrenson
 Data structures by schaums and series –seymour lipschutz
 http://en.wikipedia.org/wiki/Book:Data_structures
 http://www.amazon.com/Data-Structures-Algorithms
 http://www.amazon.in/Data-Structures-Algorithms-Made-Easy/dp/0615459811/
 http://www.amazon.in/Data-Structures-SIE-Seymour-Lipschutz/dp
 List of Images
1. http://www.algolist.net/Algorithms/Graph/Undirected/breadth-first_search
2. http://www.expertsmind.com/questions/depth-first-search-30153061.aspx

More Related Content

What's hot (20)

trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Lecture 5 trees
Lecture 5 treesLecture 5 trees
Lecture 5 trees
 
Trees
TreesTrees
Trees
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
Unit – vi tree
Unit – vi   treeUnit – vi   tree
Unit – vi tree
 
Tree
TreeTree
Tree
 
introduction to_trees
introduction to_treesintroduction to_trees
introduction to_trees
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 
Tree(Directed and undirected tree)
Tree(Directed and undirected tree)Tree(Directed and undirected tree)
Tree(Directed and undirected tree)
 
Tree
TreeTree
Tree
 
Binary tree traversal ppt
Binary tree traversal pptBinary tree traversal ppt
Binary tree traversal ppt
 
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
 
Trees - Data structures in C/Java
Trees - Data structures in C/JavaTrees - Data structures in C/Java
Trees - Data structures in C/Java
 
Chapter 8 ds
Chapter 8 dsChapter 8 ds
Chapter 8 ds
 
Cinterviews Binarysearch Tree
Cinterviews Binarysearch TreeCinterviews Binarysearch Tree
Cinterviews Binarysearch Tree
 
Binary tree
Binary treeBinary tree
Binary tree
 
Search tree,Tree and binary tree and heap tree
Search tree,Tree  and binary tree and heap treeSearch tree,Tree  and binary tree and heap tree
Search tree,Tree and binary tree and heap tree
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
 
Trees
TreesTrees
Trees
 

Viewers also liked

Uniformed tree searching
Uniformed tree searching Uniformed tree searching
Uniformed tree searching Ayaelshiwi
 
Algorithm chapter 5
Algorithm chapter 5Algorithm chapter 5
Algorithm chapter 5chidabdu
 
Depth firstsearchalgorithm
Depth firstsearchalgorithmDepth firstsearchalgorithm
Depth firstsearchalgorithmhansa khan
 
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11sumitbardhan
 
THREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREETHREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREESiddhi Shrivas
 
Анализ тональности с помощью ДСМ метода
Анализ тональности с помощью ДСМ методаАнализ тональности с помощью ДСМ метода
Анализ тональности с помощью ДСМ методаDima Kostyaev
 
Ідентифікація багатофакторних залежностей на основі нечіткої бази знань з рі...
Ідентифікація  багатофакторних залежностей на основі нечіткої бази знань з рі...Ідентифікація  багатофакторних залежностей на основі нечіткої бази знань з рі...
Ідентифікація багатофакторних залежностей на основі нечіткої бази знань з рі...Роман Тилець
 
Экспертные системы
Экспертные системыЭкспертные системы
Экспертные системыОтшельник
 
The Science of UX Design
The Science of UX DesignThe Science of UX Design
The Science of UX DesignZack Naylor
 
Expert system (unit 1 & 2)
Expert system (unit 1 & 2)Expert system (unit 1 & 2)
Expert system (unit 1 & 2)Lakshya Gupta
 
Logical Inference in RTE
Logical Inference in RTELogical Inference in RTE
Logical Inference in RTEKilian Evang
 
Inference rulesproofmethods
Inference rulesproofmethodsInference rulesproofmethods
Inference rulesproofmethodsRajendran
 
CPSC 125 Ch 2 Sec 1
CPSC 125 Ch 2 Sec 1CPSC 125 Ch 2 Sec 1
CPSC 125 Ch 2 Sec 1David Wood
 
Mayo aug1, jsm slides (3)
Mayo aug1, jsm slides (3)Mayo aug1, jsm slides (3)
Mayo aug1, jsm slides (3)jemille6
 
Logical Abduction and an Application on Business Rules Management
Logical Abduction and an Application on Business Rules ManagementLogical Abduction and an Application on Business Rules Management
Logical Abduction and an Application on Business Rules ManagementTobias Trapp
 
Proofs by contraposition
Proofs by contrapositionProofs by contraposition
Proofs by contrapositionAbdur Rehman
 

Viewers also liked (20)

Uniformed tree searching
Uniformed tree searching Uniformed tree searching
Uniformed tree searching
 
Algorithm chapter 5
Algorithm chapter 5Algorithm chapter 5
Algorithm chapter 5
 
Depth firstsearchalgorithm
Depth firstsearchalgorithmDepth firstsearchalgorithm
Depth firstsearchalgorithm
 
Bfs
BfsBfs
Bfs
 
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
 
THREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREETHREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREE
 
Анализ тональности с помощью ДСМ метода
Анализ тональности с помощью ДСМ методаАнализ тональности с помощью ДСМ метода
Анализ тональности с помощью ДСМ метода
 
Ідентифікація багатофакторних залежностей на основі нечіткої бази знань з рі...
Ідентифікація  багатофакторних залежностей на основі нечіткої бази знань з рі...Ідентифікація  багатофакторних залежностей на основі нечіткої бази знань з рі...
Ідентифікація багатофакторних залежностей на основі нечіткої бази знань з рі...
 
Экспертные системы
Экспертные системыЭкспертные системы
Экспертные системы
 
Нечеткие знания в экспертных системах
Нечеткие знания в экспертных системахНечеткие знания в экспертных системах
Нечеткие знания в экспертных системах
 
The Science of UX Design
The Science of UX DesignThe Science of UX Design
The Science of UX Design
 
Lecture5
Lecture5Lecture5
Lecture5
 
Expert system (unit 1 & 2)
Expert system (unit 1 & 2)Expert system (unit 1 & 2)
Expert system (unit 1 & 2)
 
Logical Inference in RTE
Logical Inference in RTELogical Inference in RTE
Logical Inference in RTE
 
Inference rulesproofmethods
Inference rulesproofmethodsInference rulesproofmethods
Inference rulesproofmethods
 
CPSC 125 Ch 2 Sec 1
CPSC 125 Ch 2 Sec 1CPSC 125 Ch 2 Sec 1
CPSC 125 Ch 2 Sec 1
 
Mayo aug1, jsm slides (3)
Mayo aug1, jsm slides (3)Mayo aug1, jsm slides (3)
Mayo aug1, jsm slides (3)
 
Logical Abduction and an Application on Business Rules Management
Logical Abduction and an Application on Business Rules ManagementLogical Abduction and an Application on Business Rules Management
Logical Abduction and an Application on Business Rules Management
 
Backtracking
BacktrackingBacktracking
Backtracking
 
Proofs by contraposition
Proofs by contrapositionProofs by contraposition
Proofs by contraposition
 

Similar to Bca ii dfs u-3 tree and graph

tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptxMouDhara1
 
B tree ,B plus and graph
B tree ,B plus and graph B tree ,B plus and graph
B tree ,B plus and graph RaaviKapoor
 
Farhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana Shaikh
 
data_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.pptdata_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.pptssuser5c874e
 
chapter 6.1.pptx
chapter 6.1.pptxchapter 6.1.pptx
chapter 6.1.pptxTekle12
 
Data structures and Algorithm analysis_Lecture4.pptx
Data structures and Algorithm analysis_Lecture4.pptxData structures and Algorithm analysis_Lecture4.pptx
Data structures and Algorithm analysis_Lecture4.pptxAhmedEldesoky24
 
cppggggggggggggggggggggggggggggggggggggggg.pptx
cppggggggggggggggggggggggggggggggggggggggg.pptxcppggggggggggggggggggggggggggggggggggggggg.pptx
cppggggggggggggggggggggggggggggggggggggggg.pptxShruthiS594607
 
Lecture 21_Trees - I.pptx
Lecture 21_Trees - I.pptxLecture 21_Trees - I.pptx
Lecture 21_Trees - I.pptxfizzaahmed9
 
Data structure(Part 2)
Data structure(Part 2)Data structure(Part 2)
Data structure(Part 2)SURBHI SAROHA
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structureAnusruti Mitra
 
Tree Introduction.pptx
Tree Introduction.pptxTree Introduction.pptx
Tree Introduction.pptxRahulAI
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binarySSE_AndyLi
 

Similar to Bca ii dfs u-3 tree and graph (20)

Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
 
Tree 11.ppt
Tree 11.pptTree 11.ppt
Tree 11.ppt
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
 
B tree ,B plus and graph
B tree ,B plus and graph B tree ,B plus and graph
B tree ,B plus and graph
 
Unit 3 trees
Unit 3   treesUnit 3   trees
Unit 3 trees
 
Data Structures 5
Data Structures 5Data Structures 5
Data Structures 5
 
Module - 5_Trees.pdf
Module - 5_Trees.pdfModule - 5_Trees.pdf
Module - 5_Trees.pdf
 
Farhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructure
 
data_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.pptdata_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.ppt
 
chapter 6.1.pptx
chapter 6.1.pptxchapter 6.1.pptx
chapter 6.1.pptx
 
Data structures and Algorithm analysis_Lecture4.pptx
Data structures and Algorithm analysis_Lecture4.pptxData structures and Algorithm analysis_Lecture4.pptx
Data structures and Algorithm analysis_Lecture4.pptx
 
DSA-Unit-2.pptx
DSA-Unit-2.pptxDSA-Unit-2.pptx
DSA-Unit-2.pptx
 
cppggggggggggggggggggggggggggggggggggggggg.pptx
cppggggggggggggggggggggggggggggggggggggggg.pptxcppggggggggggggggggggggggggggggggggggggggg.pptx
cppggggggggggggggggggggggggggggggggggggggg.pptx
 
Lecture 21_Trees - I.pptx
Lecture 21_Trees - I.pptxLecture 21_Trees - I.pptx
Lecture 21_Trees - I.pptx
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Data structure(Part 2)
Data structure(Part 2)Data structure(Part 2)
Data structure(Part 2)
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
Tree Introduction.pptx
Tree Introduction.pptxTree Introduction.pptx
Tree Introduction.pptx
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binary
 
Module 8.1 Trees.pdf
Module 8.1 Trees.pdfModule 8.1 Trees.pdf
Module 8.1 Trees.pdf
 

More from Rai University

Brochure Rai University
Brochure Rai University Brochure Rai University
Brochure Rai University Rai University
 
Bdft ii, tmt, unit-iii, dyeing & types of dyeing,
Bdft ii, tmt, unit-iii,  dyeing & types of dyeing,Bdft ii, tmt, unit-iii,  dyeing & types of dyeing,
Bdft ii, tmt, unit-iii, dyeing & types of dyeing,Rai University
 
Bsc agri 2 pae u-4.4 publicrevenue-presentation-130208082149-phpapp02
Bsc agri  2 pae  u-4.4 publicrevenue-presentation-130208082149-phpapp02Bsc agri  2 pae  u-4.4 publicrevenue-presentation-130208082149-phpapp02
Bsc agri 2 pae u-4.4 publicrevenue-presentation-130208082149-phpapp02Rai University
 
Bsc agri 2 pae u-4.3 public expenditure
Bsc agri  2 pae  u-4.3 public expenditureBsc agri  2 pae  u-4.3 public expenditure
Bsc agri 2 pae u-4.3 public expenditureRai University
 
Bsc agri 2 pae u-4.2 public finance
Bsc agri  2 pae  u-4.2 public financeBsc agri  2 pae  u-4.2 public finance
Bsc agri 2 pae u-4.2 public financeRai University
 
Bsc agri 2 pae u-4.1 introduction
Bsc agri  2 pae  u-4.1 introductionBsc agri  2 pae  u-4.1 introduction
Bsc agri 2 pae u-4.1 introductionRai University
 
Bsc agri 2 pae u-3.3 inflation
Bsc agri  2 pae  u-3.3  inflationBsc agri  2 pae  u-3.3  inflation
Bsc agri 2 pae u-3.3 inflationRai University
 
Bsc agri 2 pae u-3.2 introduction to macro economics
Bsc agri  2 pae  u-3.2 introduction to macro economicsBsc agri  2 pae  u-3.2 introduction to macro economics
Bsc agri 2 pae u-3.2 introduction to macro economicsRai University
 
Bsc agri 2 pae u-3.1 marketstructure
Bsc agri  2 pae  u-3.1 marketstructureBsc agri  2 pae  u-3.1 marketstructure
Bsc agri 2 pae u-3.1 marketstructureRai University
 
Bsc agri 2 pae u-3 perfect-competition
Bsc agri  2 pae  u-3 perfect-competitionBsc agri  2 pae  u-3 perfect-competition
Bsc agri 2 pae u-3 perfect-competitionRai University
 

More from Rai University (20)

Brochure Rai University
Brochure Rai University Brochure Rai University
Brochure Rai University
 
Mm unit 4point2
Mm unit 4point2Mm unit 4point2
Mm unit 4point2
 
Mm unit 4point1
Mm unit 4point1Mm unit 4point1
Mm unit 4point1
 
Mm unit 4point3
Mm unit 4point3Mm unit 4point3
Mm unit 4point3
 
Mm unit 3point2
Mm unit 3point2Mm unit 3point2
Mm unit 3point2
 
Mm unit 3point1
Mm unit 3point1Mm unit 3point1
Mm unit 3point1
 
Mm unit 2point2
Mm unit 2point2Mm unit 2point2
Mm unit 2point2
 
Mm unit 2 point 1
Mm unit 2 point 1Mm unit 2 point 1
Mm unit 2 point 1
 
Mm unit 1point3
Mm unit 1point3Mm unit 1point3
Mm unit 1point3
 
Mm unit 1point2
Mm unit 1point2Mm unit 1point2
Mm unit 1point2
 
Mm unit 1point1
Mm unit 1point1Mm unit 1point1
Mm unit 1point1
 
Bdft ii, tmt, unit-iii, dyeing & types of dyeing,
Bdft ii, tmt, unit-iii,  dyeing & types of dyeing,Bdft ii, tmt, unit-iii,  dyeing & types of dyeing,
Bdft ii, tmt, unit-iii, dyeing & types of dyeing,
 
Bsc agri 2 pae u-4.4 publicrevenue-presentation-130208082149-phpapp02
Bsc agri  2 pae  u-4.4 publicrevenue-presentation-130208082149-phpapp02Bsc agri  2 pae  u-4.4 publicrevenue-presentation-130208082149-phpapp02
Bsc agri 2 pae u-4.4 publicrevenue-presentation-130208082149-phpapp02
 
Bsc agri 2 pae u-4.3 public expenditure
Bsc agri  2 pae  u-4.3 public expenditureBsc agri  2 pae  u-4.3 public expenditure
Bsc agri 2 pae u-4.3 public expenditure
 
Bsc agri 2 pae u-4.2 public finance
Bsc agri  2 pae  u-4.2 public financeBsc agri  2 pae  u-4.2 public finance
Bsc agri 2 pae u-4.2 public finance
 
Bsc agri 2 pae u-4.1 introduction
Bsc agri  2 pae  u-4.1 introductionBsc agri  2 pae  u-4.1 introduction
Bsc agri 2 pae u-4.1 introduction
 
Bsc agri 2 pae u-3.3 inflation
Bsc agri  2 pae  u-3.3  inflationBsc agri  2 pae  u-3.3  inflation
Bsc agri 2 pae u-3.3 inflation
 
Bsc agri 2 pae u-3.2 introduction to macro economics
Bsc agri  2 pae  u-3.2 introduction to macro economicsBsc agri  2 pae  u-3.2 introduction to macro economics
Bsc agri 2 pae u-3.2 introduction to macro economics
 
Bsc agri 2 pae u-3.1 marketstructure
Bsc agri  2 pae  u-3.1 marketstructureBsc agri  2 pae  u-3.1 marketstructure
Bsc agri 2 pae u-3.1 marketstructure
 
Bsc agri 2 pae u-3 perfect-competition
Bsc agri  2 pae  u-3 perfect-competitionBsc agri  2 pae  u-3 perfect-competition
Bsc agri 2 pae u-3 perfect-competition
 

Recently uploaded

Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvRicaMaeCastro1
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1GloryAnnCastre1
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseCeline George
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsPooky Knightsmith
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 

Recently uploaded (20)

Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 Database
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young minds
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 

Bca ii dfs u-3 tree and graph

  • 1. Course: BCA-II Subject: Data Structure Unit-3 Tree and Graph
  • 2. NONLINEAR DATA STRUCTURE  2 primary types: • Trees • Graphs • All trees are graphs, but not all graphs are trees • Recursion is useful and is the easiest way to process them. • It helps keep track of what's been processed and what remains
  • 3. Graphs •Graphs can have multiple references in and multiple references out (whereas tree node only has one reference in) •Graphs can be directed or undirected and cyclic or acyclic
  • 4. Trees • Single parent • 0 or more children • A node with no children is called a "leaf" • The topmost node is called the "root" • N-ary trees • Binary trees
  • 5. N-ary Trees  The best example of an n-ary tree is your computer’s directory system.  It has a single starting point and then 0 or more branches.
  • 6. Binary Trees and Binary Search Trees • Binary search trees allow for fast insertion and removal of elements • They are specially designed for fast searching • A binary tree consists of two nodes, each of which has two child nodes • All nodes in a binary search tree fulfill the property that: • Descendants to the left have smaller data values than the node data value • Descendants to the right have larger data values than the node data value
  • 7. BST Tree Nodes • All nodes in a binary search tree fulfill the property that: • Descendants to the left have smaller data values than the node data value • Descendants to the right have larger data values than the node data value
  • 8. BST (Balanced Binary Trees) • Balanced tree: each node has approximately as many descendants on the left as on the right • If a binary search tree is balanced, then adding an element takes O(log(n)) time • If the tree is unbalanced, insertion can be slow • Perhaps as slow as insertion into a linked list
  • 9. Traversing a Tree  Which were used for the binary tree can now be used for the general.  When the general tree has been represented as a binary tree, the algorithms l tree.  In-order traversals make no sense when a general tree is converted to a binary tree.  In the general tree each node can have more than two children so trying to insert the parent node in between the children is rather difficult, especially if there is an odd number of children.  Pre - order  This is a process where the root is accessed and processed and then each of the subtrees is preorder processed. It is also called a depth-first traversal.
  • 10. Traversing a Tree  Pre-order Traversal  In this way the resulting printout has all nodes at any given level starting in the same tab column.  It is relatively easy to draw lines to produce the original general tree except that the tree is on its side with it's root at the left rather than with the root at the top.
  • 11. Tree Traversal • Tree traversal schemes include • Preorder traversal (root, left, right) • Inorder traversal (left, root, right) • Postorder traversal ( left, right, root) • Preorder generates prefix expression,(polish notation) from an expression trees • Inorder generates a sorted ordering • Postorder generates a post fix expression, also useful for node deletion
  • 12. Height of a BST  Insert 7  Insert 4  Insert 1  Insert 9  Insert 5  It’s a complete tree! 7 4 9 1 5 height = log(5)+1 = 3
  • 13. BSTs with heights O(log n)  It would be ideal if a BST was always close to a full binary tree  It’s enough to guarantee that the height of tree is O(log n)  To guarantee that we have to make the structure of the tree and insertion and deletion algorithms more complex  e.g. AVL trees (balanced), 2-3 trees, 2-3-4 trees (full but not binary), red–black trees (if red vertices are ignored then it’s like a full tree)
  • 14. What is a graph?  A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes to each other  The set of edges describes relationships among the vertices
  • 15. Formal definition of graphs  A graph G is defined as follows: G=(V,E) V(G): a finite, nonempty set of vertices E(G): a set of edges (pairs of vertices)
  • 16. Directed vs. undirected graphs (cont.)  When the edges in a graph have a direction, the graph is called directed (or digraph)  When the edges in a graph have no direction, the graph is called undirected.  Warning: if the graph is directed, the order of the vertices in each edge is important !!
  • 17. Breadth-first searching[1]  A breadth-first search (BFS) explores nodes nearest the root before exploring nodes further away  For example, after searching A, then B, then C, the search proceeds with D, E, F, G  Node are explored in the order A B C D E F G H I J K L M N O P Q  J will be found before N L M N O P G Q H JI K FED B C A
  • 18. Depth-first searching[2]  A depth-first search (DFS) explores a path all the way to a leaf before backtracking and exploring another path  For example, after searching A, then B, then D, the search backtracks and tries another path from B  Node are explored in the order A B D E H L M N I O P C F G J K Q  N will be found before JL M N O P G Q H JI K FED B C A
  • 19. A spanning tree of a graph is just a subgraph that contains all the vertices and is a tree. A graph may have many spanning trees. o r o r o r Some Spanning Trees from Graph AGraph A Spanning Trees
  • 20. Minimum Spanning Trees The Minimum Spanning Tree for a given graph is the Spanning Tree of minimum cost for that graph. 5 7 2 1 3 4 2 1 3 Complete Graph Minimum Spanning Tree
  • 21. References  An introduction to Datastructure with application by jean Trembley and sorrenson  Data structures by schaums and series –seymour lipschutz  http://en.wikipedia.org/wiki/Book:Data_structures  http://www.amazon.com/Data-Structures-Algorithms  http://www.amazon.in/Data-Structures-Algorithms-Made-Easy/dp/0615459811/  http://www.amazon.in/Data-Structures-SIE-Seymour-Lipschutz/dp  List of Images 1. http://www.algolist.net/Algorithms/Graph/Undirected/breadth-first_search 2. http://www.expertsmind.com/questions/depth-first-search-30153061.aspx