Skip to main content

Tree and Graph


Tree and graph can be classified under the non linear data structure. Here I have shared few algorithms of tree and graph.

Graph algorithms

Algorithm for DFS(Depth First Search) graph traversal:


Step 1:Define a Stack of size total number of vertices in the graph.

Step 2:Select any vertex as starting point for traversal.Visit that vertex and push it on to the Stack.

Step 3:Visit any one of the adjacent vertex of the vertex which is at top of the stack which is not visited and push it on to the stack.

Step 5:When there is no new vertex to be visit then use back tracking and pop one vertex from the stack.

Step 6:Repeat steps 3, 4 and 5 until stack becomes Empty.

Step 7:When stack becomes Empty, then produce final spanning tree by removing unused edges from the graph.



Algorithm for BFS(Breadth First Search) graph traversal:


Step 1:Define a Queue of size total number of vertices in the graph.

Step 2:Select any vertex as starting point for traversal.Visit that vertex and insert it into the Queue.

Step 3:Visit all the adjacent vertices of the vertex which is at front of the Queue which is not visited and insert them into the Queue.

Step 4:When there is no new vertex to be visit from the vertex at front of the Queue then delete that vertex from the Queue.

Step 5:Repeat step 3 and 4 until queue becomes empty.

Step 6:When queue becomes Empty, then produce final spanning tree by removing unused edges from the graph.";


Tree algorithms

Algorithm for In - order tree traversal:


Step 1:Recursively traverse left subtree.

Step 2:Visit root node.

Step 3:Recursively traverse right subtree.




Algorithm for pre - order tree traversal:


Step 1:Visit root node.

Step 2:Recursively traverse left subtree.

Step 3:Recursively traverse right subtree



Algorithm for post - order tree traversal:


Step 1:Recursively traverse left subtree.

Step 2:Recursively traverse right subtree.

Step 3−Visit root node.



Algorithm for inserting nodes in Binary Search Tree:


If root is NULL then
    create root node
    return
End If
If root exists then
    compare the data with node.data
    While until insertion position is located
        If data is greater than node.data
            goto right subtree
        else
            goto left subtree
    End While
    insert data
End If



Algorithm for searching a node in Binary Search Tree:


If root.data is equal to search.data
    return root
Else
    While data not found
        If data is greater than node.data
            goto right subtree
        Else
            goto left subtree
            If data found
                return node
    End While
    return data not found
End if

End



Algorithm for deleting a node in Binary Search Tree:


Case 1:Deleting a leaf node
    We use the following steps to delete a leaf node from BST…
   
    Step 1:Find the node to be deleted using search operation
    Step 2:Delete the node using free function (If it is a leaf)and terminate the function.
  
Case 2:Deleting a node with one child
    We use the following steps to delete a node with one child from BST…
   
    Step 1:Find the node to be deleted using search operation
    Step 2:If it has only one child, then create a link between its parent and child node.
    Step 3:Delete the node using free function and terminate the function.
   
Case 3:Deleting a node with two children
    We use the following steps to delete a node with two children from BST…
       
    Step 1:Find the node to be deleted using search operation
    Step 2:If it has two children, then find the largest node in its left subtree(OR) the smallest node in its right subtree.
    Step 3:Swap both deleting node and node which found in above step.
    Step 4:Then, check whether deleting node came to
        case 1 or case 2
        else goto step 2
    Step 5:If it comes to case 1,then delete using case 1 logic.
    Step 6:If it comes to case 2,then delete using case 2 logic.
    Step 7:Repeat the same process until node is deleted from the tree.


I hope you find this blog useful.
Please Like, comment and subscribe.

Comments

Popular posts from this blog

Linear Data Structure Algorithm: Part II Linked List is another variation of Linear Data Structure. Though it is categorized in Linear Data Structure, it is not contiguous in nature. Rather it stores nodes in different locations at memory and links the nodes to each other forming a list, Therefore derives the name Linked List. Single Linked List Algorithm to create a node in Single Linked List: Allocate memory for ptr (pointer to new node) If ptr = NULL     print: Overflow: Memory not allocated End If     Read Data to be inserted in node Set nptr->info = data Set nptr->next = NULL     return nptr End Algorithm to Count Node in Linked List: Set count to 0 Set temp to First While temp != NULL     Set count = count + 1     Set temp = temp->next End While return count End Algorithm to Reverse a given Linked List: reverse(First)    Set temp = First  ...
Linear Data Structure Algorithm - Part I Hello all you young hearts! "You are never given a wish without also being given a power to make it come true. You may have to work for it however." -Richard David Bach Here is my blog that gives you information about various algorithm of Data Structure. In this blog I have shared information about Linear Data Structure. Linear Data Structure includes Stack, Queue and Linked List. This blog basically focuses on Stack and Queue. Stack Algorithms Algorithm for Push operation: If(top>size-1)     Print Stack is full Else     Top=top+1     a[top]=item     Print a[top] is added End If  Algorithm for Pop operation: If(top is less than 0)     Print Stack is empty Else     a[top]=item     Top=top-1     Print a[top] is deleted End If Algorithm for Peek operation If(top is less than 0)  ...