TREE-AND-GRAPH-DATA-STRUCTURE

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

TREE AND GRAPH DATA STRUCTURE

A tree is an abstract model of a hierarchical structure that consists of nodes with a parent-child relationship.
 Tree is a sequence of nodes
 There is a starting node known as a root node
 Every node other than the root has a parent node.
 Nodes may have any number of children
 The first subset contains a single element called the root of the tree.
 The other two subsets are themselves binary trees called the left and right sub-trees of the original tree.
 A left or right sub-tree can be empty.
 Each element of a binary tree is called a node of the tree.

Some Key Terms:


 Root − Node at the top of the tree is called root.
 Parent − Any node except root node has one edge upward to a node called parent.
 Child − Node below a given node connected by its edge downward is called its child node.
 Sibling – Child of same node are called siblings
 Leaf − Node which does not have any child node is called leaf node.
 Sub tree − Sub tree represents descendants of a node.
 Levels − Level of a node represents the generation of a node. If root node is at level 0, then its next child node is
at level 1, its grandchild is at level 2 and so on.
 keys − Key represents a value of a node based on which a search operation is to be carried out for a node.
 Path − It is the sequence of consecutive edges from source node to destination node.
 Height of a node −The height of a node is the max path length from that node to a leaf node.
 Depth of a tree − Depth of a tree is the number of edges in path from root to x.

Binary Expression Tree:

Binary expression tree is a specific kind of binary used to represent expressions.


Binary expression tree can represent two types of expressions:
o Algebraic expressions
o Boolean expressions
The leaves of a binary expression tree are operands and the other node contains the operator

GRAPH DATA STRUCTURE


Graph is used to model and represent variety of systems

When we study Graph, we first study graph as mathematical or Logical models.

A Graph is just like a tree, it is a collection of objects or entities that we call nodes or vertices connected to each other
through a set of edges.

In a tree, all nodes must be reachable from the root node and there must be exactly one possible path from root to a
node

In graph, there are no rules dictating the connection among the nodes.
A graph contains a set of nodes and a set of edges can be connecting nodes in any possible way.
Graph data structure is often referred to as Graph Theory.

In Mathematical terms, graph can be defined like this:

A graph G is an ordered pair of a set V of vertices and a set E of edges.


Or can be written in mathematical notation as:
G = (V, E)

*when you say, an ordered pair, is just a pair of mathematical objects in which the order of objects in the pair matters.
So, this is how we write and represent an ordered pair of objects, separated by a comma and put within parenthesis
(V, E). Meaning the order here matters. V should be the first object in the pair and E is the second object in the pair.

If I have an ordered pair of (a,b), it is not equal to (b,a), unless a and b are equal.

(a, b) ≠ (b, a)

Unordered pair – simply a set of two elements in which the order is not important.
{a, b} = {b, a}

A graph is an ordered pair of a set of Vertices and a set of Edges.


G = (V, E)

Two types of Edges:


Edges- is uniquely identified by its two endpoints, the origin and the destination
1. Directed edge – a type of edge in which the connection is in one-way
2. Undirected edge – a type of edge in which the connection is in two-way

Representation of a Graph
1. Edge List representation
2. Adjacency Matric representation
3. Adjacency List representation

DIJKSTRA’S ALGORITHM
Dijkstra's algorithm to find the shortest path between nodes in a graph. It picks the unvisited vertex with the lowest
distance, calculates the distance through it to each unvisited neighbor, and updates the neighbor's distance if smaller.
Mark visited when done with neighbors.

PRIMS ALGORITHM
Prim's algorithm is to find a minimum spanning tree for a weighted undirected graph. This means it finds a subset of the
edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized.

You might also like