0% found this document useful (0 votes)
5 views16 pages

Tree and Graph

The document provides an overview of tree and graph data structures, explaining their hierarchical nature and various types such as binary, ternary, and N-ary trees. It also discusses basic operations for trees, including creation, insertion, searching, and traversal methods like Depth-First Search (DFS) and Breadth-First Search (BFS) for graphs. Additionally, it highlights the application of graph data structures in fields such as social network analysis and sports data science.

Uploaded by

rubaibaainayat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views16 pages

Tree and Graph

The document provides an overview of tree and graph data structures, explaining their hierarchical nature and various types such as binary, ternary, and N-ary trees. It also discusses basic operations for trees, including creation, insertion, searching, and traversal methods like Depth-First Search (DFS) and Breadth-First Search (BFS) for graphs. Additionally, it highlights the application of graph data structures in fields such as social network analysis and sports data science.

Uploaded by

rubaibaainayat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Tree and Graph

BY
ATTIA AGHA
Introduction to tree data structure

 Tree data structure is a hierarchical structure that is used to


represent and organize data in the form of parent child
relationship. The following are some real world situations which
are naturally a tree.
 Folder structure in an operating system.
 Tag structure in an HTML (root tag the as html tag) or XML
document.
 The topmost node of the tree is called the root, and the nodes
below it are called the child nodes. Each node can have multiple
child nodes, and these child nodes can also have their own child
nodes, forming a recursive structure
Why Tree is considered a non-
linear data structure?
 The data in a tree are not stored in a sequential manner i.e., they
are not stored linearly. Instead, they are arranged on multiple
levels or we can say it is a hierarchical structure. For this reason,
the tree is considered to be a non-linear data structure.
Types of Tree data structures:

 Tree data structure can be classified into three types based upon the
number of children each node of the tree can have. The types are:
 Binary tree: In a binary tree, each node can have a maximum of two
children linked to it. Some common types of binary trees include full
binary trees, complete binary trees, balanced binary trees, and
degenerate or pathological binary trees. Examples of Binary Tree are
Binary Search Tree and Binary Heap.
 Ternary Tree: A Ternary Tree is a tree data structure in which each node
has at most three child nodes, usually distinguished as “left”, “mid” and
“right”.
 N-ary Tree or Generic Tree: Generic trees are a collection of nodes
where each node is a data structure that consists of records and a list of
references to its children(duplicate references are not allowed). Unlike the
linked list, each node stores the address of multiple nodes.
Basic Operations Of Tree Data
Structure:
 Create – create a tree in the data structure.
 Insert − Inserts data in a tree.
 Search − Searches specific data in a tree to check whether it is
present or not.
 Traversal:
 Depth-First-Search Traversal
 Breadth-First-Search Traversal
Graph

 Graph Data Structure is a collection of nodes connected


by edges. It's used to represent relationships between different
entities.

 Graph algorithms are methods used to manipulate and analyze


graphs, solving various range of problems like finding the
shortest path, cycles detection.
 Graph Data Structure is a non-linear data structure consisting
of vertices and edges. It is useful in fields such as social network
analysis, recommendation systems, and computer networks. In
the field of sports data science, graph data structure can be used
to analyze and understand the dynamics of team performance
and player interactions on the field.
Breadth First Search or BFS
for a Graph
 given a undirected graph represented by an adjacency list
adj, where each adj[i] represents the list of vertices
connected to vertex i. Perform a Breadth First Search
(BFS) traversal starting from vertex 0, visiting vertices
from left to right according to the adjacency list, and
return a list containing the BFS traversal of the graph.
Examples:
 Input: adj = [[1,2], [0,2,3], [0,4], [1,4], [2,3]]
 Output: [0, 1, 2, 3, 4]
Explanation: Starting from 0, the BFS traversal will follow these
steps:
Visit 0 → Output: [0]
Visit 1 (first neighbor of 0) → Output: [0, 1]
Visit 2 (next neighbor of 0) → Output: [0, 1, 2]
Visit 3 (next neighbor of 1) → Output: [0, 1, 2,3]
Visit 4 (neighbor of 2) → Final Output: [0, 1, 2, 3, 4]
DFS tree

 Depth-First Search (DFS) is a method used to explore all the


nodes in a tree by going as deep as possible along each branch
before moving to the next one. It starts at the root node and
visits every node in the tree.
 Depth-First Search (DFS) can be classified into three main types
based on the order in which the nodes are visited:
 Pre-order Traversal: Visits the root node first, then recursively
explores the left and right subtrees.
 In-order Traversal: Explores the left subtree first, then visits the
root, and finally the right subtree.
 Post-order Traversal: Explores the left and right subtrees first,
then visits the root node.

You might also like