 ### maurice

Credit: 3675
Favorite: 0
Register at: 6 months ago
maurice questions
maurice 6 months ago
maurice 6 months ago
maurice 6 months ago
maurice 6 months ago
maurice 6 months ago
maurice 6 months ago
maurice 6 months ago
maurice 6 months ago
maurice 6 months ago
maurice 6 months ago
maurice 6 months ago
maurice 6 months ago
maurice 6 months ago
maurice 6 months ago
maurice 6 months ago
maurice 6 months ago
maurice 6 months ago
maurice 6 months ago
maurice 6 months ago
maurice 6 months ago
 maurice 6 months ago Answer question Write a program to calculate the difference between the sum of the odd level and even level nodes of a Binary Tree ``````import java.util.LinkedList; import java.util.Queue; public class DiffOddEven { //Represent a node of binary tree public static class Node{ int data; Node left; Node right; public Node(int data){ //Assign data to the new node, set left and right children to null this.data = data; this.left = null; this.right = null; } } //Represent the root of binary tree public Node root; public DiffOddEven(){ root = null; } //difference() will calculate the difference between sum of odd and even levels of binary tree public int difference() { int oddLevel = 0, evenLevel = 0, diffOddEven = 0; //Variable nodesInLevel keep tracks of number of nodes in each level int nodesInLevel = 0; //Variable currentLevel keep track of level in binary tree int currentLevel = 0; //Queue will be used to keep track of nodes of tree level-wise Queue queue = new LinkedList(); //Check if root is null if(root == null) { System.out.println("Tree is empty"); return 0; } else { //Add root node to queue as it represents the first level queue.add(root); currentLevel++; while(queue.size() != 0) { //Variable nodesInLevel will hold the size of queue i.e. number of elements in queue nodesInLevel = queue.size(); while(nodesInLevel > 0) { Node current = queue.remove(); //Checks if currentLevel is even or not. if(currentLevel % 2 == 0) //If level is even, add nodes's to variable evenLevel evenLevel += current.data; else //If level is odd, add nodes's to variable oddLevel oddLevel += current.data; //Adds left child to queue if(current.left != null) queue.add(current.left); //Adds right child to queue if(current.right != null) queue.add(current.right); nodesInLevel--; } currentLevel++; } //Calculates difference between oddLevel and evenLevel diffOddEven = Math.abs(oddLevel - evenLevel); } return diffOddEven; } public static void main (String[] args) { DiffOddEven bt = new DiffOddEven(); //Add nodes to the binary tree bt.root = new Node(1); bt.root.left = new Node(2); bt.root.right = new Node(3); bt.root.left.left = new Node(4); bt.root.right.left = new Node(5); bt.root.right.right = new Node(6); //Display the difference between sum of odd level and even level nodes System.out.println("Difference between sum of odd level and even level nodes: " + bt.difference()); } } `````` Output: ``````Difference between sum of odd level and even level nodes: 11 `````` maurice 6 months ago Answer question Write a program in Java to find the maximum and minimum value node from a circular linked list. ``````public class MinMax { //Represents the node of list. public class Node{ int data; Node next; public Node(int data) { this.data = data; } } //Declaring head and tail pointer as null. public Node head = null; public Node tail = null; //This function will add the new node at the end of the list. public void add(int data){ //Create new node Node newNode = new Node(data); //Checks if the list is empty. if(head == null) { //If list is empty, both head and tail would point to new node. head = newNode; tail = newNode; newNode.next = head; } else { //tail will point to new node. tail.next = newNode; //New node will become new tail. tail = newNode; //Since, it is circular linked list tail will points to head. tail.next = head; } } //Finds out the minimum value node in the list public void minNode() { Node current = head; //Initializing min to initial node data int min = head.data; if(head == null) { System.out.println("List is empty"); } else { do{ //If current node's data is smaller than min //Then replace value of min with current node's data if(min > current.data) { min = current.data; } current= current.next; }while(current != head); System.out.println("Minimum value node in the list: "+ min); } } //Finds out the maximum value node in the list public void maxNode() { Node current = head; //Initializing max to initial node data int max = head.data; if(head == null) { System.out.println("List is empty"); } else { do{ //If current node's data is greater than max //Then replace value of max with current node's data if(max < current.data) { max = current.data; } current= current.next; }while(current != head); System.out.println("Maximum value node in the list: "+ max); } } public static void main(String[] args) { MinMax cl = new MinMax(); //Adds data to the list cl.add(5); cl.add(20); cl.add(10); cl.add(1); //Prints the minimum value node in the list cl.minNode(); //Prints the maximum value node in the list cl.maxNode(); } } `````` Output: ``````Minimum value node in the list: 1 Maximum value node in the list: 20 `````` maurice 6 months ago Answer question Write a program in Java to create a doubly linked list containing n nodes `````` public class CountList { //Represent a node of the doubly linked list class Node{ int data; Node previous; Node next; public Node(int data) { this.data = data; } } //Represent the head and tail of the doubly linked list Node head, tail = null; //addNode() will add a node to the list public void addNode(int data) { //Create a new node Node newNode = new Node(data); //If list is empty if(head == null) { //Both head and tail will point to newNode head = tail = newNode; //head's previous will point to null head.previous = null; //tail's next will point to null, as it is the last node of the list tail.next = null; } else { //newNode will be added after tail such that tail's next will point to newNode tail.next = newNode; //newNode's previous will point to tail newNode.previous = tail; //newNode will become new tail tail = newNode; //As it is last node, tail's next will point to null tail.next = null; } } //countNodes() will count the nodes present in the list public int countNodes() { int counter = 0; //Node current will point to head Node current = head; while(current != null) { //Increment the counter by 1 for each node counter++; current = current.next; } return counter; } //display() will print out the elements of the list public void display() { //Node current will point to head Node current = head; if(head == null) { System.out.println("List is empty"); return; } System.out.println("Nodes of doubly linked list: "); while(current != null) { //Prints each node by incrementing the pointer. System.out.print(current.data + " "); current = current.next; } } public static void main(String[] args) { CountList dList = new CountList(); //Add nodes to the list dList.addNode(1); dList.addNode(2); dList.addNode(3); dList.addNode(4); dList.addNode(5); //Displays the nodes present in the list dList.display(); //Counts the nodes present in the given list System.out.println("\nCount of nodes present in the list: " + dList.countNodes()); } } `````` Output: ``````Nodes of doubly linked list: 1 2 3 4 5 Count of nodes present in the list: 5 `````` maurice 6 months ago Answer question How to perform quicksort in Java? ``````public class QuickSort { public static void main(String[] args) { int i; int[] arr={90,23,101,45,65,23,67,89,34,23}; quickSort(arr, 0, 9); System.out.println("\n The sorted array is: \n"); for(i=0;i<10;i++) System.out.println(arr[i]); } public static int partition(int a[], int beg, int end) { int left, right, temp, loc, flag; loc = left = beg; right = end; flag = 0; while(flag != 1) { while((a[loc] <= a[right]) && (loc!=right)) right--; if(loc==right) flag =1; elseif(a[loc]>a[right]) { temp = a[loc]; a[loc] = a[right]; a[right] = temp; loc = right; } if(flag!=1) { while((a[loc] >= a[left]) && (loc!=left)) left++; if(loc==left) flag =1; elseif(a[loc] = beg) { mid = (beg + end)/2; if(a[mid] == item) { return mid+1; } else if(a[mid] < item) { return binarySearch(a,mid+1,end,item); } else { return binarySearch(a,beg,mid-1,item); } } return -1; } } `````` Output: ``````Enter the item which you want to search 45 the location of the item is 5 `````` maurice 6 months ago Answer question How to perform Bubble Sort in Java? ``````public class BubbleSort { public static void main(String[] args) { int[] a = {10, 9, 7, 101, 23, 44, 12, 78, 34, 23}; for(int i=0;i<10;i++) { for (int j=0;j<10;j++) { if(a[i]