Skip to main content

Posts

Showing posts from March, 2019

Trees problem solving from geeksforgeeks

This will be a long post, including many questions solved from geeksforgeeks. I will continue to develop this with time. please follow regularly to see more solutions. write a Function to print the sum of all leaf nodes: int sumLeaf(Node* root) {      int sum=0;     if(root==NULL)     {return 0;}     else     {         if(root->left==NULL && root->right==NULL)         {             sum+=root->data;         }         else         {   if(root->left!=NULL && root->right!=NULL)             {sum=sumLeaf(root->left)+sumLeaf(root->right);}             if(root->left!=NULL && root->right==NULL)             {sum=sumLeaf(root->left);}  ...

object oriented programming with python

Introduction: Object oriented programming is the newest style of programming. In the history of programming there have been three types of programming. The first style, which can be considered programming in which sense we use programming nowadays, was structural programming . This was really low level programming languages, and even C falls in the same. Then came the functional programming , when, relatively bigger problems came into picture and people started to break down them into small small problems, wrote functions for them, and after that "stitched" them together to solve the main problems. Many of the similar low level languages were of this type. But, then came object oriented programming , which was way cooler and smarter than all these. This abstracted the data out into objects which had actions associated to do, thus created a faster, much more reusable way to deal with problems. This, basically is the current standard. Python , java(partially at ...

graph traversal dfs

graph traversal codes: #include <stdio.h> #include <stdlib.h> struct node { int vertex ; struct node * next ; }; struct node * createNode ( int v ); struct Graph { int numVertices ; int * visited ; struct node ** adjLists ; }; struct Graph * createGraph ( int ); void addEdge ( struct Graph *, int , int ); void printGraph ( struct Graph *); void DFS ( struct Graph *, int ); int main () { struct Graph * graph = createGraph ( 4 ); addEdge ( graph , 0 , 1 ); addEdge ( graph , 0 , 2 ); addEdge ( graph , 1 , 2 ); addEdge ( graph , 2 , 3 ); printGraph ( graph ); DFS ( graph , 2 ); return 0 ; } void DFS ( struct Graph * graph , int vertex ) { struct node * adjList = graph -> adjLists [ vertex ]; struct node * temp = adjList ; graph -> visited [ vertex ] = 1 ; printf ( "Visit...

graph traversals

Graphs are one of the many data structures which are actually mathematical structures. A graph is a triplet of E,V,G; where E represents the set of edges, V represents the set of vertices, G represents a function linking the corresponding vertices to its edges. In this post, I will talk about graph traversals. But before that, lets first talk about how to represent a graph and then, we will talk about traversing a graph. Graph representation: readymade codes are: #include <stdio.h> #include <stdlib.h> #define SIZE 40 struct queue { int items [ SIZE ]; int front ; int rear ; }; struct queue * createQueue (); void enqueue ( struct queue * q , int ); int dequeue ( struct queue * q ); void display ( struct queue * q ); int isEmpty ( struct queue * q ); void printQueue ( struct queue * q ); struct node { int vertex ; struct node * next ; }; struct node * createNode ( int ); struct Graph { in...