SlideShare a Scribd company logo
1 of 27
DATA STRUCTURE:
        It is a method of representation of logical relationships between individual data
elements related to the solution of a given problem.


LINEAR:
         In the linear data structure, values are arranged in a linear fashion. Linked list,
stacks and queues are the examples of linear data structure in which values are stored in
sequence.


NON LINEAR:
        The data values in this structure are not arranged in order. Trees, graphs are
examples of non-linear data structure.
 Trees are used extensively in computer science to represent algebraic formulas as an
efficient method for searching large, dynamic lists and for such diverse applications as
artificial intelligence systems and encoding algorithms.
 A tree consists of a finite set of elements called nodes, and a finite set of directed
lines, called branches that called branches that connect the nodes.

                                  DATA STRUCTURE




              LINEAR                                           NON-LINEAR




LINEAR
LINEAR       LINKED LISTS
             LINKED LISTS          STACKS
                                    STACKS        QUEUES
                                                  QUEUES         TREES
                                                                  TREES          GRAPHS
                                                                                 GRAPHS
 The number of branches associated with a node is the degree of the node.
 When the branch is directed towards the node, it is an in degree branch.
 When the branch is directed away from the node, it is an out degree branch.
 The sum of the in degree and out degree branches is degree of node.
 If the tree is not empty, the first node is called the root.


TERMINOLOGY:
• A leaf is any node with an out degree of zero i.e., a node with no successors.
• A node is a parent if it has successor nodes i.e., if it has out degree > zero.
• A node with a predecessor is a child. A child node has an in degree of one.
• Two or more nodes with the same parent are siblings.
• An ancestor is any node in the path from the root to the node.
• A descendent is any node in the path below.
 A tree may be defined as a set of certain elements called as nodes.
 One node is named as root, while the remaining nodes may be grouped into
sub-sets, each of which is a tree in itself.
 Each sub tree is referred to as child of the root, while the root is referred to as
the parent of each sub tree.
 If a tree consists of a simple node, then that node is called a leaf.


                                        A



                      B                            C               D




      E        F          G      H            I


            In the above figure . A is root and E,F,G,H,I,D are leafs.
BINARY TREE:
 A binary tree is a tree, which is either empty , or in which every node
a) has no children or
b) has just a left child or
c) has just a right child or
d) has just a left and right child.

LEVEL OF A NODE:
 The level of a node refers to its distance from the root node.

                                      1                             LEVEL 0

                           2                                        LEVEL 1
                                          3

                 4             5
                                                                    LEVEL 2

                       6
                                                                    LEVEL 3
COMPLETE BINARY TREE:
  A complete binary tree may be defined as a tree in which , except
the last level, at each level n there should be 2^n nodes.
                              1                                       LEVEL 0

                   2

                                           3                          LEVEL 1
         4               5


                   6                             9                    LEVEL 2
                              7        8


DEPTH( OR HEIGHT) OF A TREE:                                          1
                                                                                1
    Length of path from x to y is the total number of           2         3
branches lying along that path.                                                 2
                                                        4           5
Max level number of a binary tree is known as its                               3
height( or depth).                                                        7
                                                            6
Depth of a complete binary tree with n nodes=log n                              4
                                                                      8
BINARY SEARCH TREE:
                   A binary search tree is a binary tree that is either empty or in
         which every node contains a key and satisfies the conditions
   1.     The key in the left child of a node ( if exists) is less than the key in its
         parent node.
   2.     The key in the right child of a node (if exists ) is greater than the key in
         its parent node.
   3.    The left and right sub trees are again binary search trees.
                                                d
                                                                 The nodes of right sub
        A binary                  b                              tree are greater than d and
                                                        f        nodes of left sub tree are
        search
        tree.                                                    less than d.
                           a          c             c
Operations on binary search tree: The main operations performed on binary search trees are
                                 a. Insertion
                                 b. Deletion.
a. Inserting a new node in binary search tree
               To insert a new key into binary search tree,
 The following actions are performed:
 1. If the tree is empty, the root pointer is assigned address of new node.
 2. If the tree is not empty, then compare the key with one in the root. If it is less than
    the key of root, then move to left sub tree otherwise, more to right sub tree. Of the
    key is equal then it is a duplicate.
 3. Repeat the step2 until, the node is inserted at any place. The node is placed at the
    position when there are no nodes to compare after repeating the step2.


                                18                       18                      18
     18


                         10          28             10        28         10            28
10        28


                     8                          8        11        8        11        25
b. Deletion from a binary search tree:
                                                                                   g
   We have to consider different cases for deletion:
   Case1: If the node to be deleted is leaf                             b
                                                                                           i
         Then replace the link to the deleted node by NULL

                                                             a               c         h




                                            g


                        b                                           i       null



null a     null                null c   null        null h   null
Delete c:
  The binary search tree after deletion will be




                             g

                                                                 g

          b null                             i    null

                                                             b
                                                                         i


null a null                 null h null                  a           h
Case 2: If the deleted node has only one sub tree
            Then adjust the link from parent of the deleted
node to point to its right sub tree.
Example : delete ‘i’ from tree as shown in figure below



                                                                      g
                          g

                                                                  b
                                                                          h
             b                          h


                                                              a       c


  a                      c
Case 3 : If the deleted node has two sub trees:
    In this case attach right sub tree is place of deleted node. Then
hang left sub tree onto an appropriate node of right sub tree.
Example: delete node ‘b’ from binary tree as shown in fig below




                       g
                                                                            g


            c                           i
                                                                        c           i



 a                                                                 a            h
                              h
TRAVERSING INTO A BINARY SEARCH TREE:
In order traversing:                          18
1) Traverse the left sub tree.
                                        10             28
2) Visit the root.
3) Traverse the right sub tree.   8      11          25     8 10 11 18 25 28

Pre order traversing:
1) Visit the root.
                                  18 10 8 11 28 25
2) Traverse the left sub tree.
3) Traverse the right sub tree.

Post order traversing:
1) Traverse the left sub tree.    8 11 10 25 28 18
2) Traverse the right sub tree.
3) Traverse the root.
GRAPHS
DEFINITION:
     A graph is a collection of nodes, called vertices, and a collection of
segments, called lines, connecting pairs of vertices.
A graph consists of 2 sets, a set of vertices and a set of lines or A graph is a set of
nodes and arcs. The nodes are also termed as vertices and arcs are termed as
edges.
The set of nodes denoted as                            O               P
{O,P,R,S,Q}
The set of arcs denoted as                                       R
                                                       Q
{ (O,P),(O,R),(O,Q),(Q,S),(R,S)}                                        S

Graphs may be either directed or undirected. A directed graph or digraph is a graph in
which each line has a direction ( arrow head) to its successor.
The lines in a directed graph are known as arcs.
An undirected graph is graph in which there is no direction (arrow head) on
any of the lines, which are known as edges.
In an undirected graph, the flow between 2 vertices can go in either direction.
                  A                                               A


          B             E                                  B              E
                                   F                                                      F
          C              D                                 C               D


      a) Directed graph.                                  b) Undirected graph.
 •A path is a sequence of vertices in which each vertex is adjacent to the next one.
  {A,B,C,E } is one path.
  { A,B,E,F} is another path.
 •A cycle is a path consisting of at least three vertices that starts and ends with the
 same vertex.
  from above fig: B,C,D,E,B is a cycle
Loop is a special case id a cycle in which a single       A           B
are begins and ends with the same vertex.
                                                          C           D

A graph is linked if there is a pathway between any 2 nodes of the graph, such a
graph is called a connected graph or else it is a non connected graph.


    P             Q                                   P                Q




        R               S                                     R            S


        Connected graph                                   Non connected graph

SUB GRAPH: A sub graph of a graph G=(V,E) is a graph G where V(G) is a
subset of V(G). E(G) consists of edges (V1,V2) in E(G), such that both V1 and V2
are in V(G).
a) A directed graph is strongly connected, if there is a path from each vertex to
every other vertex in the digraph.


                                  A

                                                                          G
                       B                   E

                                                  F

                       C                   D              H               I



                                                         A
b) A directed graph is weakly
connected if at least 2 vertices are not          B           E                     G
connected
                                                                      F

                                                 C           D
                                                                              H     I
                                                      Weakly connected
A

    c) A graph is a disjoint graph              B            E
    if it is not connected.                                                       G
                                                                       F


                                                 C           D                H   I

                                                     Strongly connected.
The degree of a vertex is the number of lines incident to it.
The out degree of a vertex is the number of arcs leaving the vertex.
In degree is the number of arcs entering the vertex.

From the above figure:
The degree of vertex B is 3.           The maximum number of edges in an
The degree of vertex E is 4.           undirected graph with n vertices is n(n-1)/2.

In degree of vertex E is 2             In a directed graph , it is n(n-1).

Out degree of vertex E is 2.
GRAPH REPRESENTATION:
Array representation:

                                           S.NO 1       2   3       4
 One way of representing a graph with
                                            1   0       1   1       1
 n vertices is to use an n^2 matrix.                                            1
 (i.e., n rows and n columns)               2   1       0   1       1
                                                                        2            4
 If G is a directed graph, only e           3   1       1   0       1
 entries would have been set to 1 in
 the adjacency matrix.                          1       1   1       0           3
                                            4
Linked list representation:
Another way of representing a
graph G is to maintain a list for
                                                    2           3           4       NULL
every vertex containing all vertices 1
adjacent to that vertex .                                                           NULL
                                     2              1           3           4
Struct edge
{ int num;                             3            1           2           4       NULL
Sturuct edge *p;
                                       4            1           2           3       NULL
};
Adjacency matrix:
     It can be used to represent the graph. The                   1
information of adjacency nodes can be stored
in the matrix.                                        2                        5

Nodes [i] [k]-0 indicates absence                                     6
             1 indicates the presence of edge
              between two nodes i and k
                                                              3               4
        1     2    3    4     5     6
                                                      In degree       Out degree
 1      0    0    0     0      0    0             1       3               0
 2      1     0   0     1     0     0             2       2               2
 3      0    1     0    0     0     1             3       1               2

 4      0     0    1    0     1     1             4       1               3
 5      1    0     0    0     0     0             5       2               1
 6      1    1     0    0      1    0                     2
                                                  6                       3
Spanning tree:
     It is an un directed tree, containing only those nodes that are
necessary to join all the nodes in the graph. The nodes of spanning trees
have only one path between them.
 In spanning trees, the number of edges are less by one than the number
of nodes.
 In other words, a tree that contains edges including all vertices is called
spanning tree.
  Also, in a graph, when the edges are shown connected between the
nodes in the order in which they are to be visited, resulting sub graph is
called spanning tree.
DIGRAPH (DIRECTED GRAPH):
A digraph is a graph in which edges have a direction                    v1       E5
         e=(u, v)                                                E1
                                                                                 E8
Edge ‘e’ can be traversed only from u to v,                 v2                        v3
                                                                      E6
u- initial vertex.                                          E2             E7         E4
v-terminal vertex.                                               v4             v5
                                                                       E3



 SIMPLE DIRECTED GRAPH:

                                                                 V1
                                                                        E4
 Simple directed graph is a simple graph in             E1
                                                                      E5
 which edges have a direction.                               E6
                                                       V2                   V4

                                                       E2
                                                                  V3       E3
WEIGHTED GRAPH:
     In a weighted graph, each edge is assigned some weight i.e., a real positive number
is associated with each edge.


PATH:
In a non- weighted graph, a path P of length n from u to v can be defined as follows:
P={ V0, V1, V2, …….., Vn}
Where u=V0 and v=Vn and there is an edge from Vi-1 to Vi (0<i<n).
In a directed graph Vi-1 is taken as initial vertex and Vi is taken as terminal vertex.
A simple path from u to v is a path where no vertex appears more than once.


CYCLE:
A path P={ V1, V2,………….. Vn} is said to be a cycle, when all its vertices are
distinct except V0 and Vn , meaning thereby V0=Vn then a simple path P is called a
cycle.
v1
CONNECTED GRAPH:
A graph G is called a connected graph, if there        v2               v3
is at least one path between every pair of
vertices.
                                                        v4              v5




COMPLETE GRAPH:
                                                             v1
A graph with an edge between each pair of
vertices is said to be a complete graph.
|E| =n*(n-1)/2                                    v2                      v5
|E|- total number of edges
n- number of vertices.
0<= |E|<= n(n-1)/2.                                    v3          v4
DEPTH FIRST TRAVERSAL:
                       In this method, we start from a given vertex v in the
  graph. We mark this vertex is visited. Now any of the unvisited vertex
  adjacent to v is visited. Then neighbor of v is visited. The process is
  continued until no new node can be visited. Now we bock track from the path
  to visit unvisited vertices if any left. Stack is used to keep track of all nodes
  adjacent to a vertex.


NOTE:
       There may be several depth first
traversals and depth first spanning trees for                 A          B              C
a given graph. It mainly depends on how
the graph is represented (adjacency matrix
or adjacency list), on how the nodes are                            D               E
numbered, on the starting node and on how
the traversal technique is implemented             Here i) D is first successor of A
(i.e., using first successor, next successor       and (B is next successor).
etc.)
                                                   ii) C is first successor of B.
ALGORITHM DEPTH FIRST[V]:


1. [Initialize all the nodes to ready state and stack to empty]
         state [v]=1 ( 1 indicates ready state)
2. [Begin with any arbitrary node s in graph and push it on to stack and
   change its state to waiting]
       state [s]=2 (2 indicates waiting state)
3. Repeat through step5 while stack not empty
4. [ pop node N of stack and mark the status of node to be visited]
        state [N]= 3 (3 indicates visited)
5. [push all nodes w adjacent to N to stack and mark their status as waiting]
       state [W]=2
6. If the graph still contains nodes which are in ready state go to step2
7. Return.
BREADTH FIRST TRAVERSAL:
 In this we start with given vertexV and visit all the nodes adjacent to V from left
to right. Data structure queue is use to keep track of all the adjacent nodes.
The first node visited is the first node whose successors are visited.
ALGORITHM FOR BREADTH FIRST [V]:
1. [Initialize all nodes to ready state]
     state[V]=1 [ here V represents all nodes of graph]
2. [Place starting node ‘S’ in queue and change its state to waiting]
             state [S]=2
3. Repeat through step5 until queue not empty.
4. [Remove a node N from queue and change status of N to visited state]
         state [N]=3.
5. [ add to queue all neighbors W of ‘N’ which are in ready state and change their
   states to waiting state]
         state [W]= 2
6.    End.

More Related Content

Similar to Unit 8 (20)

Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithms
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
 
09 binary-trees
09 binary-trees09 binary-trees
09 binary-trees
 
binary tree.pptx
binary tree.pptxbinary tree.pptx
binary tree.pptx
 
Trees
TreesTrees
Trees
 
Unit – vi tree
Unit – vi   treeUnit – vi   tree
Unit – vi tree
 
Unit 3.ppt
Unit 3.pptUnit 3.ppt
Unit 3.ppt
 
Binary tree
Binary treeBinary tree
Binary tree
 
NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxNON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
Data Structures 4
Data Structures 4Data Structures 4
Data Structures 4
 
Dsc++ unit 3 notes
Dsc++ unit 3 notesDsc++ unit 3 notes
Dsc++ unit 3 notes
 
AD3251-Data Structures Design-Notes-Tree.pdf
AD3251-Data Structures  Design-Notes-Tree.pdfAD3251-Data Structures  Design-Notes-Tree.pdf
AD3251-Data Structures Design-Notes-Tree.pdf
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptxLecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
 
DSA-Unit-2.pptx
DSA-Unit-2.pptxDSA-Unit-2.pptx
DSA-Unit-2.pptx
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Tree
TreeTree
Tree
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
 
Binary tree
Binary treeBinary tree
Binary tree
 

More from mrecedu

Unit4 (2)
Unit4 (2)Unit4 (2)
Unit4 (2)mrecedu
 
Unit5 (2)
Unit5 (2)Unit5 (2)
Unit5 (2)mrecedu
 
Unit6 jwfiles
Unit6 jwfilesUnit6 jwfiles
Unit6 jwfilesmrecedu
 
Unit3 jwfiles
Unit3 jwfilesUnit3 jwfiles
Unit3 jwfilesmrecedu
 
Unit2 jwfiles
Unit2 jwfilesUnit2 jwfiles
Unit2 jwfilesmrecedu
 
Unit1 jwfiles
Unit1 jwfilesUnit1 jwfiles
Unit1 jwfilesmrecedu
 
Unit7 jwfiles
Unit7 jwfilesUnit7 jwfiles
Unit7 jwfilesmrecedu
 
M1 unit vi-jntuworld
M1 unit vi-jntuworldM1 unit vi-jntuworld
M1 unit vi-jntuworldmrecedu
 
M1 unit iv-jntuworld
M1 unit iv-jntuworldM1 unit iv-jntuworld
M1 unit iv-jntuworldmrecedu
 
M1 unit iii-jntuworld
M1 unit iii-jntuworldM1 unit iii-jntuworld
M1 unit iii-jntuworldmrecedu
 
M1 unit ii-jntuworld
M1 unit ii-jntuworldM1 unit ii-jntuworld
M1 unit ii-jntuworldmrecedu
 
M1 unit i-jntuworld
M1 unit i-jntuworldM1 unit i-jntuworld
M1 unit i-jntuworldmrecedu
 
M1 unit viii-jntuworld
M1 unit viii-jntuworldM1 unit viii-jntuworld
M1 unit viii-jntuworldmrecedu
 
M1 unit vii-jntuworld
M1 unit vii-jntuworldM1 unit vii-jntuworld
M1 unit vii-jntuworldmrecedu
 
Unit vii
Unit viiUnit vii
Unit viimrecedu
 

More from mrecedu (20)

Unit i
Unit iUnit i
Unit i
 
Unit4 (2)
Unit4 (2)Unit4 (2)
Unit4 (2)
 
Unit4
Unit4Unit4
Unit4
 
Unit5 (2)
Unit5 (2)Unit5 (2)
Unit5 (2)
 
Unit6 jwfiles
Unit6 jwfilesUnit6 jwfiles
Unit6 jwfiles
 
Unit3 jwfiles
Unit3 jwfilesUnit3 jwfiles
Unit3 jwfiles
 
Unit2 jwfiles
Unit2 jwfilesUnit2 jwfiles
Unit2 jwfiles
 
Unit1 jwfiles
Unit1 jwfilesUnit1 jwfiles
Unit1 jwfiles
 
Unit7 jwfiles
Unit7 jwfilesUnit7 jwfiles
Unit7 jwfiles
 
M1 unit vi-jntuworld
M1 unit vi-jntuworldM1 unit vi-jntuworld
M1 unit vi-jntuworld
 
M1 unit iv-jntuworld
M1 unit iv-jntuworldM1 unit iv-jntuworld
M1 unit iv-jntuworld
 
M1 unit iii-jntuworld
M1 unit iii-jntuworldM1 unit iii-jntuworld
M1 unit iii-jntuworld
 
M1 unit ii-jntuworld
M1 unit ii-jntuworldM1 unit ii-jntuworld
M1 unit ii-jntuworld
 
M1 unit i-jntuworld
M1 unit i-jntuworldM1 unit i-jntuworld
M1 unit i-jntuworld
 
M1 unit viii-jntuworld
M1 unit viii-jntuworldM1 unit viii-jntuworld
M1 unit viii-jntuworld
 
M1 unit vii-jntuworld
M1 unit vii-jntuworldM1 unit vii-jntuworld
M1 unit vii-jntuworld
 
Unit vii
Unit viiUnit vii
Unit vii
 
Unit vi
Unit viUnit vi
Unit vi
 
Unit v
Unit vUnit v
Unit v
 
Unit iv
Unit ivUnit iv
Unit iv
 

Recently uploaded

Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...lizamodels9
 
Call Girls In Panjim North Goa 9971646499 Genuine Service
Call Girls In Panjim North Goa 9971646499 Genuine ServiceCall Girls In Panjim North Goa 9971646499 Genuine Service
Call Girls In Panjim North Goa 9971646499 Genuine Serviceritikaroy0888
 
Insurers' journeys to build a mastery in the IoT usage
Insurers' journeys to build a mastery in the IoT usageInsurers' journeys to build a mastery in the IoT usage
Insurers' journeys to build a mastery in the IoT usageMatteo Carbone
 
2024 Numerator Consumer Study of Cannabis Usage
2024 Numerator Consumer Study of Cannabis Usage2024 Numerator Consumer Study of Cannabis Usage
2024 Numerator Consumer Study of Cannabis UsageNeil Kimberley
 
RE Capital's Visionary Leadership under Newman Leech
RE Capital's Visionary Leadership under Newman LeechRE Capital's Visionary Leadership under Newman Leech
RE Capital's Visionary Leadership under Newman LeechNewman George Leech
 
7.pdf This presentation captures many uses and the significance of the number...
7.pdf This presentation captures many uses and the significance of the number...7.pdf This presentation captures many uses and the significance of the number...
7.pdf This presentation captures many uses and the significance of the number...Paul Menig
 
M.C Lodges -- Guest House in Jhang.
M.C Lodges --  Guest House in Jhang.M.C Lodges --  Guest House in Jhang.
M.C Lodges -- Guest House in Jhang.Aaiza Hassan
 
Monte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSMMonte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSMRavindra Nath Shukla
 
A DAY IN THE LIFE OF A SALESMAN / WOMAN
A DAY IN THE LIFE OF A  SALESMAN / WOMANA DAY IN THE LIFE OF A  SALESMAN / WOMAN
A DAY IN THE LIFE OF A SALESMAN / WOMANIlamathiKannappan
 
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,noida100girls
 
Regression analysis: Simple Linear Regression Multiple Linear Regression
Regression analysis:  Simple Linear Regression Multiple Linear RegressionRegression analysis:  Simple Linear Regression Multiple Linear Regression
Regression analysis: Simple Linear Regression Multiple Linear RegressionRavindra Nath Shukla
 
Pharma Works Profile of Karan Communications
Pharma Works Profile of Karan CommunicationsPharma Works Profile of Karan Communications
Pharma Works Profile of Karan Communicationskarancommunications
 
Keppel Ltd. 1Q 2024 Business Update Presentation Slides
Keppel Ltd. 1Q 2024 Business Update  Presentation SlidesKeppel Ltd. 1Q 2024 Business Update  Presentation Slides
Keppel Ltd. 1Q 2024 Business Update Presentation SlidesKeppelCorporation
 
Grateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdfGrateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdfPaul Menig
 
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...lizamodels9
 
Call Girls in Mehrauli Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Mehrauli Delhi 💯Call Us 🔝8264348440🔝Call Girls in Mehrauli Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Mehrauli Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Vip Female Escorts Noida 9711199171 Greater Noida Escorts Service
Vip Female Escorts Noida 9711199171 Greater Noida Escorts ServiceVip Female Escorts Noida 9711199171 Greater Noida Escorts Service
Vip Female Escorts Noida 9711199171 Greater Noida Escorts Serviceankitnayak356677
 
Vip Dewas Call Girls #9907093804 Contact Number Escorts Service Dewas
Vip Dewas Call Girls #9907093804 Contact Number Escorts Service DewasVip Dewas Call Girls #9907093804 Contact Number Escorts Service Dewas
Vip Dewas Call Girls #9907093804 Contact Number Escorts Service Dewasmakika9823
 

Recently uploaded (20)

Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...
 
Call Girls In Panjim North Goa 9971646499 Genuine Service
Call Girls In Panjim North Goa 9971646499 Genuine ServiceCall Girls In Panjim North Goa 9971646499 Genuine Service
Call Girls In Panjim North Goa 9971646499 Genuine Service
 
Insurers' journeys to build a mastery in the IoT usage
Insurers' journeys to build a mastery in the IoT usageInsurers' journeys to build a mastery in the IoT usage
Insurers' journeys to build a mastery in the IoT usage
 
2024 Numerator Consumer Study of Cannabis Usage
2024 Numerator Consumer Study of Cannabis Usage2024 Numerator Consumer Study of Cannabis Usage
2024 Numerator Consumer Study of Cannabis Usage
 
RE Capital's Visionary Leadership under Newman Leech
RE Capital's Visionary Leadership under Newman LeechRE Capital's Visionary Leadership under Newman Leech
RE Capital's Visionary Leadership under Newman Leech
 
7.pdf This presentation captures many uses and the significance of the number...
7.pdf This presentation captures many uses and the significance of the number...7.pdf This presentation captures many uses and the significance of the number...
7.pdf This presentation captures many uses and the significance of the number...
 
M.C Lodges -- Guest House in Jhang.
M.C Lodges --  Guest House in Jhang.M.C Lodges --  Guest House in Jhang.
M.C Lodges -- Guest House in Jhang.
 
Monte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSMMonte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSM
 
A DAY IN THE LIFE OF A SALESMAN / WOMAN
A DAY IN THE LIFE OF A  SALESMAN / WOMANA DAY IN THE LIFE OF A  SALESMAN / WOMAN
A DAY IN THE LIFE OF A SALESMAN / WOMAN
 
Forklift Operations: Safety through Cartoons
Forklift Operations: Safety through CartoonsForklift Operations: Safety through Cartoons
Forklift Operations: Safety through Cartoons
 
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
 
Regression analysis: Simple Linear Regression Multiple Linear Regression
Regression analysis:  Simple Linear Regression Multiple Linear RegressionRegression analysis:  Simple Linear Regression Multiple Linear Regression
Regression analysis: Simple Linear Regression Multiple Linear Regression
 
KestrelPro Flyer Japan IT Week 2024 (English)
KestrelPro Flyer Japan IT Week 2024 (English)KestrelPro Flyer Japan IT Week 2024 (English)
KestrelPro Flyer Japan IT Week 2024 (English)
 
Pharma Works Profile of Karan Communications
Pharma Works Profile of Karan CommunicationsPharma Works Profile of Karan Communications
Pharma Works Profile of Karan Communications
 
Keppel Ltd. 1Q 2024 Business Update Presentation Slides
Keppel Ltd. 1Q 2024 Business Update  Presentation SlidesKeppel Ltd. 1Q 2024 Business Update  Presentation Slides
Keppel Ltd. 1Q 2024 Business Update Presentation Slides
 
Grateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdfGrateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdf
 
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...
 
Call Girls in Mehrauli Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Mehrauli Delhi 💯Call Us 🔝8264348440🔝Call Girls in Mehrauli Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Mehrauli Delhi 💯Call Us 🔝8264348440🔝
 
Vip Female Escorts Noida 9711199171 Greater Noida Escorts Service
Vip Female Escorts Noida 9711199171 Greater Noida Escorts ServiceVip Female Escorts Noida 9711199171 Greater Noida Escorts Service
Vip Female Escorts Noida 9711199171 Greater Noida Escorts Service
 
Vip Dewas Call Girls #9907093804 Contact Number Escorts Service Dewas
Vip Dewas Call Girls #9907093804 Contact Number Escorts Service DewasVip Dewas Call Girls #9907093804 Contact Number Escorts Service Dewas
Vip Dewas Call Girls #9907093804 Contact Number Escorts Service Dewas
 

Unit 8

  • 1. DATA STRUCTURE: It is a method of representation of logical relationships between individual data elements related to the solution of a given problem. LINEAR: In the linear data structure, values are arranged in a linear fashion. Linked list, stacks and queues are the examples of linear data structure in which values are stored in sequence. NON LINEAR: The data values in this structure are not arranged in order. Trees, graphs are examples of non-linear data structure.
  • 2.  Trees are used extensively in computer science to represent algebraic formulas as an efficient method for searching large, dynamic lists and for such diverse applications as artificial intelligence systems and encoding algorithms.  A tree consists of a finite set of elements called nodes, and a finite set of directed lines, called branches that called branches that connect the nodes. DATA STRUCTURE LINEAR NON-LINEAR LINEAR LINEAR LINKED LISTS LINKED LISTS STACKS STACKS QUEUES QUEUES TREES TREES GRAPHS GRAPHS
  • 3.  The number of branches associated with a node is the degree of the node.  When the branch is directed towards the node, it is an in degree branch.  When the branch is directed away from the node, it is an out degree branch.  The sum of the in degree and out degree branches is degree of node.  If the tree is not empty, the first node is called the root. TERMINOLOGY: • A leaf is any node with an out degree of zero i.e., a node with no successors. • A node is a parent if it has successor nodes i.e., if it has out degree > zero. • A node with a predecessor is a child. A child node has an in degree of one. • Two or more nodes with the same parent are siblings. • An ancestor is any node in the path from the root to the node. • A descendent is any node in the path below.
  • 4.  A tree may be defined as a set of certain elements called as nodes.  One node is named as root, while the remaining nodes may be grouped into sub-sets, each of which is a tree in itself.  Each sub tree is referred to as child of the root, while the root is referred to as the parent of each sub tree.  If a tree consists of a simple node, then that node is called a leaf. A B C D E F G H I In the above figure . A is root and E,F,G,H,I,D are leafs.
  • 5. BINARY TREE: A binary tree is a tree, which is either empty , or in which every node a) has no children or b) has just a left child or c) has just a right child or d) has just a left and right child. LEVEL OF A NODE: The level of a node refers to its distance from the root node. 1 LEVEL 0 2 LEVEL 1 3 4 5 LEVEL 2 6 LEVEL 3
  • 6. COMPLETE BINARY TREE: A complete binary tree may be defined as a tree in which , except the last level, at each level n there should be 2^n nodes. 1 LEVEL 0 2 3 LEVEL 1 4 5 6 9 LEVEL 2 7 8 DEPTH( OR HEIGHT) OF A TREE: 1 1 Length of path from x to y is the total number of 2 3 branches lying along that path. 2 4 5 Max level number of a binary tree is known as its 3 height( or depth). 7 6 Depth of a complete binary tree with n nodes=log n 4 8
  • 7. BINARY SEARCH TREE: A binary search tree is a binary tree that is either empty or in which every node contains a key and satisfies the conditions 1. The key in the left child of a node ( if exists) is less than the key in its parent node. 2. The key in the right child of a node (if exists ) is greater than the key in its parent node. 3. The left and right sub trees are again binary search trees. d The nodes of right sub A binary b tree are greater than d and f nodes of left sub tree are search tree. less than d. a c c Operations on binary search tree: The main operations performed on binary search trees are a. Insertion b. Deletion.
  • 8. a. Inserting a new node in binary search tree To insert a new key into binary search tree, The following actions are performed: 1. If the tree is empty, the root pointer is assigned address of new node. 2. If the tree is not empty, then compare the key with one in the root. If it is less than the key of root, then move to left sub tree otherwise, more to right sub tree. Of the key is equal then it is a duplicate. 3. Repeat the step2 until, the node is inserted at any place. The node is placed at the position when there are no nodes to compare after repeating the step2. 18 18 18 18 10 28 10 28 10 28 10 28 8 8 11 8 11 25
  • 9. b. Deletion from a binary search tree: g We have to consider different cases for deletion: Case1: If the node to be deleted is leaf b i Then replace the link to the deleted node by NULL a c h g b i null null a null null c null null h null
  • 10. Delete c: The binary search tree after deletion will be g g b null i null b i null a null null h null a h
  • 11. Case 2: If the deleted node has only one sub tree Then adjust the link from parent of the deleted node to point to its right sub tree. Example : delete ‘i’ from tree as shown in figure below g g b h b h a c a c
  • 12. Case 3 : If the deleted node has two sub trees: In this case attach right sub tree is place of deleted node. Then hang left sub tree onto an appropriate node of right sub tree. Example: delete node ‘b’ from binary tree as shown in fig below g g c i c i a a h h
  • 13. TRAVERSING INTO A BINARY SEARCH TREE: In order traversing: 18 1) Traverse the left sub tree. 10 28 2) Visit the root. 3) Traverse the right sub tree. 8 11 25 8 10 11 18 25 28 Pre order traversing: 1) Visit the root. 18 10 8 11 28 25 2) Traverse the left sub tree. 3) Traverse the right sub tree. Post order traversing: 1) Traverse the left sub tree. 8 11 10 25 28 18 2) Traverse the right sub tree. 3) Traverse the root.
  • 14. GRAPHS DEFINITION: A graph is a collection of nodes, called vertices, and a collection of segments, called lines, connecting pairs of vertices. A graph consists of 2 sets, a set of vertices and a set of lines or A graph is a set of nodes and arcs. The nodes are also termed as vertices and arcs are termed as edges. The set of nodes denoted as O P {O,P,R,S,Q} The set of arcs denoted as R Q { (O,P),(O,R),(O,Q),(Q,S),(R,S)} S Graphs may be either directed or undirected. A directed graph or digraph is a graph in which each line has a direction ( arrow head) to its successor. The lines in a directed graph are known as arcs.
  • 15. An undirected graph is graph in which there is no direction (arrow head) on any of the lines, which are known as edges. In an undirected graph, the flow between 2 vertices can go in either direction. A A B E B E F F C D C D a) Directed graph. b) Undirected graph. •A path is a sequence of vertices in which each vertex is adjacent to the next one. {A,B,C,E } is one path. { A,B,E,F} is another path. •A cycle is a path consisting of at least three vertices that starts and ends with the same vertex. from above fig: B,C,D,E,B is a cycle
  • 16. Loop is a special case id a cycle in which a single A B are begins and ends with the same vertex. C D A graph is linked if there is a pathway between any 2 nodes of the graph, such a graph is called a connected graph or else it is a non connected graph. P Q P Q R S R S Connected graph Non connected graph SUB GRAPH: A sub graph of a graph G=(V,E) is a graph G where V(G) is a subset of V(G). E(G) consists of edges (V1,V2) in E(G), such that both V1 and V2 are in V(G).
  • 17. a) A directed graph is strongly connected, if there is a path from each vertex to every other vertex in the digraph. A G B E F C D H I A b) A directed graph is weakly connected if at least 2 vertices are not B E G connected F C D H I Weakly connected
  • 18. A c) A graph is a disjoint graph B E if it is not connected. G F C D H I Strongly connected. The degree of a vertex is the number of lines incident to it. The out degree of a vertex is the number of arcs leaving the vertex. In degree is the number of arcs entering the vertex. From the above figure: The degree of vertex B is 3. The maximum number of edges in an The degree of vertex E is 4. undirected graph with n vertices is n(n-1)/2. In degree of vertex E is 2 In a directed graph , it is n(n-1). Out degree of vertex E is 2.
  • 19. GRAPH REPRESENTATION: Array representation: S.NO 1 2 3 4 One way of representing a graph with 1 0 1 1 1 n vertices is to use an n^2 matrix. 1 (i.e., n rows and n columns) 2 1 0 1 1 2 4 If G is a directed graph, only e 3 1 1 0 1 entries would have been set to 1 in the adjacency matrix. 1 1 1 0 3 4 Linked list representation: Another way of representing a graph G is to maintain a list for 2 3 4 NULL every vertex containing all vertices 1 adjacent to that vertex . NULL 2 1 3 4 Struct edge { int num; 3 1 2 4 NULL Sturuct edge *p; 4 1 2 3 NULL };
  • 20. Adjacency matrix: It can be used to represent the graph. The 1 information of adjacency nodes can be stored in the matrix. 2 5 Nodes [i] [k]-0 indicates absence 6 1 indicates the presence of edge between two nodes i and k 3 4 1 2 3 4 5 6 In degree Out degree 1 0 0 0 0 0 0 1 3 0 2 1 0 0 1 0 0 2 2 2 3 0 1 0 0 0 1 3 1 2 4 0 0 1 0 1 1 4 1 3 5 1 0 0 0 0 0 5 2 1 6 1 1 0 0 1 0 2 6 3
  • 21. Spanning tree: It is an un directed tree, containing only those nodes that are necessary to join all the nodes in the graph. The nodes of spanning trees have only one path between them. In spanning trees, the number of edges are less by one than the number of nodes. In other words, a tree that contains edges including all vertices is called spanning tree. Also, in a graph, when the edges are shown connected between the nodes in the order in which they are to be visited, resulting sub graph is called spanning tree.
  • 22. DIGRAPH (DIRECTED GRAPH): A digraph is a graph in which edges have a direction v1 E5 e=(u, v) E1 E8 Edge ‘e’ can be traversed only from u to v, v2 v3 E6 u- initial vertex. E2 E7 E4 v-terminal vertex. v4 v5 E3 SIMPLE DIRECTED GRAPH: V1 E4 Simple directed graph is a simple graph in E1 E5 which edges have a direction. E6 V2 V4 E2 V3 E3
  • 23. WEIGHTED GRAPH: In a weighted graph, each edge is assigned some weight i.e., a real positive number is associated with each edge. PATH: In a non- weighted graph, a path P of length n from u to v can be defined as follows: P={ V0, V1, V2, …….., Vn} Where u=V0 and v=Vn and there is an edge from Vi-1 to Vi (0<i<n). In a directed graph Vi-1 is taken as initial vertex and Vi is taken as terminal vertex. A simple path from u to v is a path where no vertex appears more than once. CYCLE: A path P={ V1, V2,………….. Vn} is said to be a cycle, when all its vertices are distinct except V0 and Vn , meaning thereby V0=Vn then a simple path P is called a cycle.
  • 24. v1 CONNECTED GRAPH: A graph G is called a connected graph, if there v2 v3 is at least one path between every pair of vertices. v4 v5 COMPLETE GRAPH: v1 A graph with an edge between each pair of vertices is said to be a complete graph. |E| =n*(n-1)/2 v2 v5 |E|- total number of edges n- number of vertices. 0<= |E|<= n(n-1)/2. v3 v4
  • 25. DEPTH FIRST TRAVERSAL: In this method, we start from a given vertex v in the graph. We mark this vertex is visited. Now any of the unvisited vertex adjacent to v is visited. Then neighbor of v is visited. The process is continued until no new node can be visited. Now we bock track from the path to visit unvisited vertices if any left. Stack is used to keep track of all nodes adjacent to a vertex. NOTE: There may be several depth first traversals and depth first spanning trees for A B C a given graph. It mainly depends on how the graph is represented (adjacency matrix or adjacency list), on how the nodes are D E numbered, on the starting node and on how the traversal technique is implemented Here i) D is first successor of A (i.e., using first successor, next successor and (B is next successor). etc.) ii) C is first successor of B.
  • 26. ALGORITHM DEPTH FIRST[V]: 1. [Initialize all the nodes to ready state and stack to empty] state [v]=1 ( 1 indicates ready state) 2. [Begin with any arbitrary node s in graph and push it on to stack and change its state to waiting] state [s]=2 (2 indicates waiting state) 3. Repeat through step5 while stack not empty 4. [ pop node N of stack and mark the status of node to be visited] state [N]= 3 (3 indicates visited) 5. [push all nodes w adjacent to N to stack and mark their status as waiting] state [W]=2 6. If the graph still contains nodes which are in ready state go to step2 7. Return.
  • 27. BREADTH FIRST TRAVERSAL: In this we start with given vertexV and visit all the nodes adjacent to V from left to right. Data structure queue is use to keep track of all the adjacent nodes. The first node visited is the first node whose successors are visited. ALGORITHM FOR BREADTH FIRST [V]: 1. [Initialize all nodes to ready state] state[V]=1 [ here V represents all nodes of graph] 2. [Place starting node ‘S’ in queue and change its state to waiting] state [S]=2 3. Repeat through step5 until queue not empty. 4. [Remove a node N from queue and change status of N to visited state] state [N]=3. 5. [ add to queue all neighbors W of ‘N’ which are in ready state and change their states to waiting state] state [W]= 2 6. End.