Skip to main content

Posts

Showing posts from January, 2016

Driver program to perform operations in graph

Driver program to perform operations in graph:- package com . problems . graph ; public class GraphDriver { public static void main ( String [] args ) { BFSGraph g = new BFSGraph (); g . addVertex ( 'A' ); g . addVertex ( 'B' ); g . addVertex ( 'C' ); g . addVertex ( 'D' ); g . addVertex ( 'E' ); g . addVertex ( 'F' ); g . addVertex ( 'G' ); g . addVertex ( 'H' ); g . addEdge ( 0 , 1 ); g . addEdge ( 1 , 2 ); g . addEdge ( 1 , 7 ); g . addEdge ( 2 , 3 ); g . addEdge ( 2 , 4 ); g . addEdge ( 7 , 4 ); g . addEdge ( 4 , 5 ); g . addEdge ( 4 , 6 ); g . bfs (); } }

BREADTH FIRST SEARCH IN JAVA

BREADTH FIRST SEARCH IN JAVA:- package com . problems . graph ; import java.awt.DisplayMode ; import java.util.Iterator ; import java.util.LinkedList ; public class BFSGraph { int maxsize ; Vertex vertexlist []; int matrixlist [][]; int vertexcount ; @SuppressWarnings ({ "rawtypes" , "unused" }) LinkedList queue ; public BFSGraph () { maxsize = 20 ; matrixlist = new int [ maxsize ][ maxsize ]; vertexlist = new Vertex [ maxsize ]; for ( int i = 0 ; i < maxsize ; i ++) { for ( int j = 0 ; j < maxsize ; j ++) { matrixlist [ i ][ j ]= 0 ; } } queue = new LinkedList (); } public void addVertex ( char label ) { vertexlist [ vertexcount ++]= new Vertex ( label ); } public void addEdge ( int i , int j ) { matrixlist [ i ][ j ]= 1 ; matrixlist [ j ][ i ]= 1 ; } public void displayVertex ( int v ) { System . out . println ( vertexlis

DEPTH FIRST SEARCH IN JAVA

DEPTH FIRST SEARCH IN JAVA public void dfs () { vertex [ 0 ]. visited = true ; displyVertex ( 0 ); stackobj . push ( 0 ); while (! stackobj . isEmpty ()) { int v = adjVertex (( Integer ) stackobj . peek ()); if ( v ==- 1 ) { stackobj . pop (); } else { vertex [ v ]. visited = true ; displyVertex ( v ); stackobj . push ( v ); } } }

Designing graph in JAVA

How to design a Graph in JAVA:- Vertex:- package com . problems . graph ; public class Vertex { char label ; boolean visited ; public Vertex ( char label ){ this . label = label ; this . visited = false ; } } Functions of Graphs public class Graph { int maxsize = 20 ; Vertex vertex []; int matrix [][]; int vertexcount ; Stack stackobj ; public Graph () { vertex = new Vertex [ maxsize ]; matrix = new int [ maxsize ][ maxsize ]; vertexcount = 0 ; for ( int i = 0 ; i < maxsize ; i ++) { for ( int j = 0 ; j < maxsize ; j ++) { matrix [ i ][ j ]= 0 ; } } stackobj = new Stack (); } public void addVertex ( char label ) { vertex [ vertexcount ++]= new Vertex ( label ); } public void addEdge ( int i , int j ) { matrix [ i ][ j ]= 1 ; matrix [ j ][ i ]= 1 ; } public void displyVertex ( int v ) { System . out . println ( vertex [ v ].

Heap implementation in JAVA

In this tutorial we will see all the functionalities of heaps implemented through java language. package com . problems . heap ; public class HeapFunctions { //Function to generate maxheapify where root is max than childs public void maxHeapify ( int Arr [], int i , int N ) { int largest ; int left = 2 * i + 1 ; //left child int right = 2 * i + 2 ; //right child System . out . println ( "left" + " " + left ); System . out . println ( "right" + " " + right ); System . out . println ( "Max size" + " " + N ); if ( left < N && Arr [ left ] > Arr [ i ] ) { largest = left ; System . out . println ( "largest left" + largest ); } else { largest = i ; System . out . println ( "largest i" + largest ); } if ( rig

Priority Queue implementation in java -Arrays

Priority Queue implementation in java -Arrays Input:- 4,8,1,7,3,5 output:-8,7,5,4,3,1, package com . problems . heap ; public class Priority_Queue { public int [] priority_Queue ( int arr [], int N ) { int [] newarray = new int [ N ]; for ( int i = 0 ; i < N ; i ++) { System . out . println ( i + "priority_Queue" + arr [ i ]); insert ( newarray , N , arr [ i ]); } return newarray ; } //inserting new element in array public void insert ( int Arr [], int N , int a ) { for ( int i = 0 ; i < N ; i ++) { System . out . println ( "insert" ); // search for the correct position in array if ( a > Arr [ i ]) { System . out . println ( "call insertAtThis" ); Arr = insertAtThis ( Arr , N , a , i ); break ; } } } //insert and swap public int [] insertAtThis ( int arr [], int N , int a , int index ) { System . out . println ( &qu

Driver Program to Test Tree in JAVA

Driver Program to Test Tree:- A basic driver program to test all the functions that i will posting in my blogs using this sample tree. package com . BST ; public class BSTOperation { /** * @param args */ public static void main ( String [] args ) { BinarySearchTree btree = new BinarySearchTree (); btree . inorder ( btree . root ); Node node1 = new Node ( 8 ); Node node2 = new Node ( 3 ); Node node3 = new Node ( 1 ); Node node4 = new Node ( 6 ); Node node5 = new Node ( 4 ); Node node6 = new Node ( 7 ); Node node7 = new Node ( 10 ); Node node8 = new Node ( 14 ); Node node9 = new Node ( 13 ); btree . root = btree . insert ( node1 , btree . root ); btree . inorder ( btree . root ); System . out . println ( "" ); btree . root = btree . insert ( node2 , btree . root ); btree . inorder ( btree . root ); System . out . println ( "" ); btree . root = btree . insert ( nod

Tree Traversal in JAVA (InOder/preOrder/postOrder)

Tree Traversal in JAVA (InOder/preOrder/postOrder):- Tree traversal can be done through three ways. 1)Inorder:- Go recursively to the left node. Read a node Go recursively to the right node. 2)Pre Order. Read a node Go recursively to the left node. Go recursively to the right node. 2)Post Order. Go recursively to the left node. Go recursively to the right node. Read a node public void inorder ( Node root ) { if ( root == null ) { return ; } inorder ( root . left ); System . out . print ( root . data + "," ); inorder ( root . right ); } public void preOrder ( Node root ) { if ( root == null ) { return ; } System . out . print ( root . data + "," ); preOrder ( root . left ); preOrder ( root . right ); } public void postOrder ( Node root ) { if ( root == null ) { return ; } postOrder ( root . left ); postOrder ( root . right ); Sy

Insertion in a Binary Search Tree JAVA

Insertion in a Binary Search Tree :_ Insertion in a BST follow simple operations. All the elements less than root lies on left subtree and all the elements greater than root lies on right subtree. Every BST class will have a root variable of Node type that represents the head node of the tree. public class BinarySearchTree { Node root ; public BinarySearchTree () { root = null ; } public Node insert ( Node node , Node root ) { if ( root == null ) { root = node ; return root ; } else { if ( node . data > root . data ) { root . right = insert ( node , root . right ); } else if ( node . data < root . data ) { root . left = insert ( node , root . left ); } } return root ; } }

How to design a Node in Tree?

How to design a Node in Tree? There are three main components of a tree in a node.  1) Integer holding data. 2) Left pointer holding node in a left subtree. 3) Right pointer holding node in a right subtree. The following design is having data of int type and left,right pointers of a node to the subtrees. package com . BST ; public class Node { int data ; Node left ; Node right ; /** * @return the data */ public Node ( int data ) { this . left = null ; this . right = null ; this . data = data ; } public int getData () { return data ; } /** * @param data the data to set */ public void setData ( int data ) { this . data = data ; } /** * @return the left */ public Node getLeft () { return left ; } /** * @param left the left to set */ public void setLeft ( Node left ) { this . left = left ; } /** * @return the right */ public Node getRight () { return r

.