Enable contrast version

Tutor profile: Sowmya V.

Inactive
Sowmya V.
Master's Student at USC, Ex-Software Developer at Yatra.com
Tutor Satisfaction Guarantee

Questions

Subject: Java Programming

TutorMe
Question:

Implement Min-Heap using Java.

Inactive
Sowmya V.
Answer:

import java.util.ArrayList; class Node<T> { int priority; T data; public Node(int priority, T data){ this.priority = priority; this.data = data; } public Node(Node<T> node) { this.data = node.data; this.priority = node.priority; } public String toString() { return "priority: " + priority + "\n" + "data: " + data.toString() + "\n"; } } public class MinHeap<T> { private ArrayList<Node<T>> tree; public MinHeap(){ tree = new ArrayList<Node<T>>(); } public void insert(Node<T> node) { tree.add(node); if(tree.size() == 1) return; int lastParent = parentIndex(tree.size()-1); this.buildHeap(lastParent); } public Node<T> getMin() { if(tree.size() == 0) { System.out.println("empty queue"); return null; } return tree.get(0); } public Node<T> extractMin() { if(tree.size() == 0) { System.out.println("empty queue"); return null; } Node<T> root = new Node<T>(tree.get(0)); swap(tree.get(0), tree.get(tree.size()-1)); tree.remove(tree.size()-1); topDownHeapify(0); return root; } public boolean isEmpty() { if(tree.size() == 0) return true; return false; } private void buildHeap(int parent) { int lChild = leftChildIndex(parent); if(lChild >= tree.size()) return; int rChild = rightChildIndex(parent); int minIndex = lChild; if(rChild < tree.size()) { minIndex = rChild; if(tree.get(lChild).priority < tree.get(rChild).priority) minIndex = lChild; } if(tree.get(parent).priority > tree.get(minIndex).priority) { int currParent = parentIndex(parent); swap(tree.get(parent), tree.get(minIndex)); if(parent == 0) return; buildHeap(currParent); } } private void topDownHeapify(int parent) { int lChild = leftChildIndex(parent); if(lChild >= tree.size()) return; int rChild = rightChildIndex(parent); int minIndex = lChild; if(rChild < tree.size()) { minIndex = rChild; if(tree.get(lChild).priority < tree.get(rChild).priority) minIndex = lChild; } if(tree.get(parent).priority > tree.get(minIndex).priority) { swap(tree.get(parent), tree.get(minIndex)); topDownHeapify(minIndex); } } private int parentIndex(int index) { return (index-1)/2; } private int leftChildIndex(int index) { return (2*index + 1); } private int rightChildIndex(int index) { return (2*index + 2); } private void swap(Node<T> x, Node<T> y) { T tempD = x.data; int tempP = x.priority; x.data = y.data; x.priority = y.priority; y.data = tempD; y.priority = tempP; } public static void main(String args[]) { MinHeap<String> mh = new MinHeap<String>(); mh.insert(new Node<String>(4, "COPY")); mh.insert(new Node<String>(10, "PRINT")); mh.insert(new Node<String>(3, "MAIL")); mh.insert(new Node<String>(5, "AUTOCORRECT")); mh.insert(new Node<String>(1, "CPU")); while(!mh.isEmpty()) { System.out.println(mh.extractMin()); } } }

Subject: Basic Math

TutorMe
Question:

Profit by a company is shared among X and Y in a 2:3 ratio. When the company generates a profit of 6000, is X's share greater than 3000?

Inactive
Sowmya V.
Answer:

Let X get 2a amount and Y get 3a amount, 'a' being the variable to solve. If the total profit is P, then 2a + 3a = P implying 5a = P, which simply means a = P/5. Substituting this in 2a and 3a values of X and Y for a total profit P, X gets a share of 2P/5 and Y gets a share of 3P/5. When P = 6000, X's share is (2 * 6000)/5 = 2400 < 3000

Subject: Computer Science (General)

TutorMe
Question:

What are the P, NP class of problems?

Inactive
Sowmya V.
Answer:

Problems that can be solved in polynomial time, i.e, in the worst-case they take O(n^k) time for an input size of n and some constant k, belong to the P class. But not all problems can be solved in polynomial time. To understand the complexity of these problems, we have P, NP, NP-Hard classes. NP class problems are those for which we do not yet have a polynomial-time algorithm but given a solution, it can be verified in polynomial time whether the solution is valid or invalid. It is clear from above that all P class problems are a subset of NP class problems.

Contact tutor

Send a message explaining your
needs and Sowmya will reply soon.
Contact Sowmya

Request lesson

Ready now? Request a lesson.
Start Lesson

FAQs

What is a lesson?
A lesson is virtual lesson space on our platform where you and a tutor can communicate. You'll have the option to communicate using video/audio as well as text chat. You can also upload documents, edit papers in real time and use our cutting-edge virtual whiteboard.
How do I begin a lesson?
If the tutor is currently online, you can click the "Start Lesson" button above. If they are offline, you can always send them a message to schedule a lesson.
Who are TutorMe tutors?
Many of our tutors are current college students or recent graduates of top-tier universities like MIT, Harvard and USC. TutorMe has thousands of top-quality tutors available to work with you.
BEST IN CLASS SINCE 2015
TutorMe homepage
Made in California by Zovio
© 2013 - 2021 TutorMe, LLC
High Contrast Mode
On
Off