Professional Documents
Culture Documents
Problem Solving
Problem Solving
Problem Solving
Prepared by
Dr. Umakant Swami
• A tree data structure is a hierarchical data
structure that consists of nodes
connected by edges.
Tree Search • Each node can have multiple child nodes,
but only one parent node.
• The topmost node in the tree is called the
root node.
• A graph data structure is a collection of
nodes (also called vertices) and edges
that connect them.
Graph Search • Nodes can represent entities, such as
people, places, or things, while edges
represent relationships between those
entities.
Difference Between Graph and Tree
Feature Graph Tree
A collection of nodes (vertices) and edges, where A hierarchical data structure consisting of nodes
Definition
edges connect nodes. connected by edges with a single root node.
Can have cycles (loops) and disconnected No cycles; connected structure with exactly one
Structure
components. path between any two nodes.
No root node; nodes may have multiple parents or
Root Node Has a designated root node that has no parent.
no parents at all.
Parent-child relationship; each node (except the
Node Relationship Relationships between nodes are arbitrary.
root) has exactly one parent.
If there is n nodes then there would be n-
Edges Each node can have any number of edges.
1 number of edges
Traversal can be complex due to cycles and Traversal is straightforward and can be done in
Traversal Complexity
disconnected components. linear time.
Stands for BFS stands for Breadth First Search. DFS stands for Depth First Search.
Conceptual Difference BFS builds the tree level by level. DFS builds the tree sub-tree by sub-tree.
BFS is more suitable for searching vertices closer DFS is more suitable when there are solutions
Suitable for
to the given source. away from source.