Data Structure: Tree and Graph
Data Structure: Tree and Graph
DATA STRUCTURES:
Tree/Graph/Hashing
Monday,
17/02/25
Uchenna-Declan
Mba
Tree
A tree is a non-linear and hierarchical data
structure where the elements are arranged
in a multilevel tree-like structure.
In a tree, the topmost node is called the root
node. Each node contains some data (can be
of any type).
It consists of a central node, structural
nodes, and sub-nodes which are the leaves
and are connected via edges.
Tree
Composition/Types of
Tree
Composition/properties Types
Node/vertex General Tree
Root
Binary Tree
Edge
Binary Search
Path
Height of a tree/node Tree
AVL Tree
Depth of a node
Degree of a Red Black Tree
tree/node Splay Tree, etc.
Balancing, etc.
Note the following;
The Height of the tree Balancing: The
can be determined by
counting the number of tree can be
edges from the root node balanced to
to the furthest leaf node.
ensure that the
The Depth of a node can height of the tree
be determined by is minimized and
counting the number of the distribution of
edges from the root node
to the current node. nodes is as even
as possible.
Characteristics
A tree is also known as a Recursive
data structure.
In a tree, the Height of the root can be
defined as the longest path from the
root node to the leaf node.
One can calculate the depth from the
top to any node. The root node has a
depth of 0.
Operations performed
Insertion: New nodes can be added to the tree to
create a new branch or to increase the height of the
tree.
Deletion: Nodes can be removed from the tree by
updating the references of the parent node to remove
the reference to the current node.
Search: Elements can be searched for in a tree by
starting from the root node and traversing the tree
based on the value of the current node until the
desired node is found.
Traversal: The elements in a tree can be traversed in
several different ways, including in-order, pre-order,
and post-order traversal.
Applications
Folder structure in an operating system.
Tag structure in an HTML (root tag the as html
tag) or XML document.
Binary Search Tree (BST) is used to check
whether elements present or not.
Syntax Tree helps in scanning, parsing,
generation of code, and evaluation of
arithmetic expressions in Compiler design.
Spanning trees are used in routers in
computer networks.
Real-Life Applications
In real life, tree data structure helps in Game
Development.
It also helps in indexing in databases.
A Decision Tree is an efficient machine-
learning tool, commonly used in decision
analysis. It has a flowchart-like structure that
helps to understand data.
Domain Name Server also uses a tree data
structure.
The most common use case of a tree is any
social networking site.
Graph
A graph G can be defined as an ordered
set G(V, E) where V(G) represents the
set of vertices and E(G) represents the
set of edges which are used to connect
these vertices.
A Graph G(V, E) with 5 vertices (A, B, C,
D, E) and eight edges ((A,B), (B,C),
(C,E), (E,D), (D,B), (D,A)).
The graph is used to solve the most
challenging and complex programming
problems.
Diagram of a Graph
Properties of a Graph 1
Path A path can be
Degree defined as the
Adjacent vertices sequence of
Connected nodes that are
components followed in order
Close path to reach some
Simple path terminal node V
Cycle, etc. from the initial
node U.
Properties of a Graph 2
Closed Path Simple Path
A path will be called If all the nodes of
as closed path if the
the graph are
initial node is same
as terminal node. A distinct with an
path will be closed exception V0=VN,
path if V0=VN then such path P
is called as closed
simple path.
Properties of a Graph 3
Cycle Connected Graph
A cycle can be A connected
defined as the path
graph is the one
which has no
repeated edges or in which some
vertices except the path exists
first and last between every
vertices. two vertices (u, v)
in V. There are no
isolated nodes in
connected graph.
.
Properties of a Graph 4
A complete graph A connected
is the one in which graph is the one
every node is
in which some
connected with all
other nodes. path exists
If two nodes u and v between every
are connected via an two vertices (u, v)
edge e, then the in V. There are no
nodes u and v are isolated nodes in
called as neighbours connected graph.
or adjacent nodes.
.
Properties of a Graph 4
Weighted Graph
Digraph
Loop
Adjacent Nodes
Degree of the Node
Degree of a node is the number of edges that
are connected with that node. A node with
degree 0 is called as isolated node.
Characteristics
The maximum distance from a vertex to all
the other vertices is considered the
Eccentricity of that vertex.
The vertex having minimum Eccentricity is
considered the central point of the graph.
The minimum value of Eccentricity from all
vertices is considered the radius of a
connected graph.
Operations performed
Add Vertex: New vertices can be added to
the graph to represent a new node.
Add Edge: Edges can be added between
vertices to represent a relationship between
nodes.
Remove Vertex: Vertices can be removed
from the graph by updating the references of
adjacent vertices to remove the reference to
the current vertex.
Remove Edge: Edges can be removed by
updating the references of the adjacent
vertices to remove the reference to the
Operations performed
Depth-First Search (DFS): A graph can be traversed using
a depth-first search by visiting the vertices in a depth-first
manner.
Breadth-First Search (BFS): A graph can be traversed
using a breadth-first search by visiting the vertices in a
breadth-first manner.
Shortest Path: The shortest path between two vertices can
be determined using algorithms such as Dijkstra’s algorithm.
Connected Components: The connected components of a
graph can be determined by finding sets of vertices that are
connected to each other but not to any other vertices in the
graph.
Cycle Detection
Applications
The graph is used to represent the
flow of computation.
It is used in modelling graphs.
The operating system uses
Resource Allocation Graph.
Also used in the World Wide Web
where the web pages represent
the nodes.
Real-Life Applications
One of the most common real-world
examples of a graph is Google Maps where
cities are located as vertices and paths
connecting those vertices are located as
edges of the graph.
A social network is also one real-world
example of a graph where every person on
the network is a node, and all of their
friendships on the network are the edges of
the graph.
A graph is also used to study molecules in
physics and chemistry.
Hashing
Read about it