I've been doing this problem for a while now.
I created one file named quickSort.java - chose the last element of a list as a pivot element. and tried to sort the numbers but somehow I'm not able to generate expected output. I tried so many probable options but I'm stuck! Please help to get the correct solution.
Here's my code of the file: quickSort.java
public void quickFun(Node node)
{
Node new_node = node;
/* To find the last element as pivot*/
while(new_node.next!=null)
{
new_node = new_node.next;
}
Node head = node;
Node tail = new_node;
quickSort(head, tail);
}
public void quickSort(Node head, Node tail)
{
Node q = partition(head,tail);
if(head!=q && head!=q.prev)
{
quickSort(head, q.prev);
}
if(tail!=q && tail!=q.next)
{
quickSort(q.next, tail);
}
}
public Node partition(Node low, Node high){
int p = high.data;
Node i = low.prev;
for(Node j = low; j!=high; j=j.next)
{
if(j.data <= p)
{
if(i==null)
{
i = low;
}
else
{
i = i.next;
}
swap(i.data, j.data);
}
}
if(i==null)
{
i = low;
}
else
{
i = i.next;
}
swap(i.data, high.data);
return i;
}
public void swap(int a , int b)
{
int t;
t = a;
a = b;
b = t;
}
Here quickFun receives head of the inserted LinkedList as an argument.
I'm basically stuck at quickSort(Node, Node) condition.
Please help me here to solve this problem.
You can't swap values like this:
public void swap(int a , int b)
{
int t;
t = a;
a = b;
b = t;
}
Java is pass-by-value, so a and b are local variables to swap; any changes you make to the values are lost when swap returns.
However, you could write:
public void swap(Node a , Node b)
{
int t;
t = a.data;
a.data = b.data;
b.data = t;
}
because this is updating fields on the two node instances, which are still passed by value, but the value is the reference to the instances.
Related
I am trying to make an application that will loop through a circular linked list. As it does so, it will use another linked list of index values, and it will use these values to delete from the circular linked list.
I have it set up now where it should fetch the index value to be deleted from my random linked list via runRandomList() method. It then uses the rotate() method to loop through the circular linked list and deletes the value from it. It will then add the deleted value to "deletedLinked list". Then, control should return back to runRandomList() method and it should feed the rotate() method the next value from the random linked list. The circular linked list should begin traversing where it left off. It should keep track of the count and node it is on. The count should reset to 0 when it reaches the first node, so it can properly keep track of which index it is on.
Unfortunately, this is not happening. I have been trying different things for the last few days as the code stands right now; it enters into a continuous loop. the issue appears to be in the rotate method.
This is the rotate method code. My thought was the counter would advance until it matches the index input. If it reaches the first node, the counter would reset to 0 and then start to increment again until it reaches the index value.
private void rotate(int x)
{
while(counter <= x)
{
if(p == names.first)
{
counter = 0;
}
p = p.next;
counter++;
}
deleteList.add((String) p.value);
names.remove(x);
}
This is my linked list class:
public class List<T>{
/*
helper class, creates nodes
*/
public class Node {
T value;
Node next;
/*
Inner class constructors
*/
public Node(T value, Node next)
{
this.value = value;
this.next = next;
}
private Node(T value)
{
this.value = value;
}
}
/*
Outer class constructor
*/
Node first;
Node last;
public int size()
{
return size(first);
}
private int size(Node list)
{
if(list == null)
return 0;
else if(list == last)
return 1;
else
{
int size = size(list.next) + 1;
return size;
}
}
public void add(T value)
{
first = add(value, first);
}
private Node add(T value, Node list)
{
if(list == null)
{
last = new Node(value);
return last;
}
else
list.next = add(value, list.next);
return list;
}
public void setCircularList()
{
last.next = first;
}
public void show()
{
Node e = first;
while (e != null)
{
System.out.println(e.value);
e = e.next;
}
}
#Override
public String toString()
{
StringBuilder strBuilder = new StringBuilder();
// Use p to walk down the linked list
Node p = first;
while (p != null)
{
strBuilder.append(p.value + "\n");
p = p.next;
}
return strBuilder.toString();
}
public boolean isEmpty()
{
boolean result = isEmpty(first);
return result;
}
private boolean isEmpty(Node first)
{
return first == null;
}
public class RemovalResult
{
Node node; // The node removed from the list
Node list; // The list remaining after the removal
RemovalResult(Node remNode, Node remList)
{
node = remNode;
list = remList;
}
}
/**
The remove method removes the element at an index.
#param index The index of the element to remove.
#return The element removed.
#exception IndexOutOfBoundsException When index is
out of bounds.
*/
public T remove(int index)
{
// Pass the job on to the recursive version
RemovalResult remRes = remove(index, first);
T element = remRes.node.value; // Element to return
first = remRes.list; // Remaining list
return element;
}
/**
The private remove method recursively removes
the node at the given index from a list.
#param index The position of the node to remove.
#param list The list from which to remove a node.
#return The result of removing the node from the list.
#exception IndexOutOfBoundsException When index is
out of bounds.
*/
private RemovalResult remove(int index, Node list)
{
if (index < 0 || index >= size())
{
String message = String.valueOf(index);
throw new IndexOutOfBoundsException(message);
}
if (index == 0)
{
// Remove the first node on list
RemovalResult remRes;
remRes = new RemovalResult(list, list.next);
list.next = null;
return remRes;
}
// Recursively remove the element at index-1 in the tail
RemovalResult remRes;
remRes = remove(index-1, list.next);
// Replace the tail with the results and return
// after modifying the list part of RemovalResult
list.next = remRes.list;
remRes.list = list;
return remRes;
}
}
This contains the main(), runRandomList(), and rotate() methods.
public class lottery {
private int suitors;
private List<String> names;
private List<Integer> random;
private List<String> deleteList = new List<>();
private int counter;
private Node p;
public lottery(int suitors, List<String> names, List<Integer> random)
{
this.suitors = suitors;
this.names = names;
this.random = random;
p = names.first;
}
public void start()
{
//Set names list to circular
names.setCircularList();
runRandomList(random);
}
public void runRandomList(List<Integer> random)
{
Node i = random.first;
while(i != null)
{
rotate((int) i.value, counter, p);
i = i.next;
}
}
public List getDeleteList()
{
return deleteList;
}
private void rotate(int x, int count, Node p)
{
Node i = p;
while(count <= x)
{
if(i == names.first)
{
count = 0;
}
i = i.next;
count++;
}
deleteList.add((String) i.value);
names.remove(x);
p = i;
counter = count;
}
public static void main(String[] args)
{
List<String> namesList = new List<>();
namesList.add("a");
namesList.add("b");
namesList.add("c");
namesList.add("d");
namesList.add("e");
namesList.add("f");
List<Integer> randomList = new List<>();
randomList.add(3);
randomList.add(1);
randomList.add(5);
randomList.add(4);
randomList.add(0);
lottery obj = new lottery(6, namesList, randomList);
obj.start();
System.out.println(obj.getDeleteList());
}
}
As I suspected it was the rotate method, this is the solution.
private void rotate(int x, int count)
{
while(count != x)
{
p = p.next;
count++;
if(count == x)
{
deleteList.add((String)p.value);
counter = x;
}
if(count >= suitors)
{
for (int j = 0; j < x ; j++)
{
p = p.next;
}
deleteList.add((String)p.value);
counter = x;
count = x;
}
}
}
Given a generic tree implemented as a root node with a list of sons, which sons are nodes and again each node has a list of its sons.
__A__
/ | \
B C D
| / \
E F G
The node A has a list of its sons: B, C, D
B, C, D also have a list of their sons: B --> E ; C --> F, G ; D --> null ;
I will explain my idea of the algorithm, you can fix it or give me another completely new idea.
public Integer level(T dato) {...}
Traverse the tree adding to the queue each node of the tree or adding a "null" if the last node added is the last node of the level. Null is an identifier in the queue to know where the level has ended.
My problem is that I don't know exactly where to put the identifier after the first time.
Here is some of the code:
public Integer level(T data){
int inclu= this.include(data);
if (inclu==-1) { // if the tree doesn't include the data
return -1;
} else {
return inclu; // returns the level
}
}
public Integer include( T data ) { // returns the level where the data is
Integer inclu = -1; // -1 if the data is not included
if (this.getDataRoot()==data){
return 0; // The root of the tree has the data
}
else {
LinkedList<GenericNode<T>> queue = new LinkedList<GenericNode<T>>();
GenericNode<T> tree = new GenericNode<T>();
int level=1;
queue.addAtBeginning(this.getRoot());
queue.addAtBeginning(null);
while (queue.size()>0 && inclu==-1) {
if(queue.element(queue.size())!=null) { // if it is not the end of the level then dequeue
tree.setData(queue.element(queue.size()).getData()); //queue.element(position) returns the element in that position
tree.setListOfSons(queue.element(queue.size()).getSons());
if (tree.getSons()!=null) { // if the tree has sons
int i=1;
while(i<=tree.getSons().size() && inclu==-1) {
queue.addAtBeginning(tree.getSons().element(i));
if (tree.getSons().element(i).getData()==data) // if I found the data I'm looking for
inclu=level;
i++; // counter
}
}
} else { // if it is the end of the level (means the queue gave me a null)
level++;
}
queue.delete(queue.size()); //ending the dequeue process
} //end while
} // end main else
return inclu; //returns the summation of the levels or 0 if it was found at the root of the tree or -1 if the data was not found
}
I wrote a class that returns the level of target node in specific tree.
import java.util.LinkedList;
import java.util.List;
public class TreeLevel {
public static class Node {
public Node(String data) { this.data = data ; };
public String data;
public List<Node> childs = new LinkedList<Node>();
}
public static Integer level(Node tree, Node target){
return level(tree, target, 0);
}
private static Integer level(Node tree, Node target, int currentLevel) {
Integer returnLevel = -1;
if(tree.data.equals(target.data)) {
returnLevel = currentLevel;
} else {
for(Node child : tree.childs) {
if((returnLevel = level(child, target, currentLevel + 1)) != -1){
break;
}
}
}
return returnLevel;
}
public static void main(String[] args) {
Node a = new Node("A");
Node b = new Node("B");
Node c = new Node("C");
Node d = new Node("D");
Node e = new Node("E");
Node f = new Node("F");
Node g = new Node("G");
// childs of a:
a.childs.add(b);
a.childs.add(c);
a.childs.add(d);
// childs of b:
b.childs.add(e);
// childs of c:
c.childs.add(f);
c.childs.add(g);
// childs of d:
// d.childs = null or simply d.childs.length() is 0
Node target = new Node("G");
Integer level = level(a, target);
System.out.println("level [" + level + "]");
}
}
I think I can give you a simple code for this question. You can change the it according to your code.
public Integer include( T data ) { // returns the level where the data is
Integer inclu = -1; // -1 if the data is not included
if (this.getDataRoot() == data){
return 0; // The root of the tree has the data
}
return level(this.getRoot(), data, 1);
}
//Find data in a tree whose root is Node
//If not found, return -1
public int level(T node, T data, int level) {
if (!node.hasChildren()) {
return -1;
}
for (T child : node.getChildren()) {
if (child.getData == data) {
return level; //Aha!!! found it
} else {
int l = level(child, data, level + 1); /// find in this sub-tree
if (l != -1) {
return l;
}
}
}
return -1; /// Not found in this sub-tree.
}
P.S : == is used to compare, which is not good. .equals() should be used.
So the concept of my question is: Let's say we have nodes. Each node has an array of integers. Now, we must add an integer to end of the array. How do we do that?
Here's what I've done so far:
Created class Node:
public class Node {
private int[] data;
Node next;
public Node(int n, Node nxt) {
data = new int[n];
next = nxt;
}
}
Then the dynamic array list class:
public class DynamicArrayOfInts {
private Node head = null;
private int numOfElementsPerNode = 0;
public DynamicArrayOfInts(int elementsPerNode) {
numOfElementsPerNode = elementsPerNode;
}
public void add(int e) {
}
}
You should add an attribute in the Node class to know the current index you are for the current Node's array. I would also add an attribute in your DynamicArrayOfInts to keep a reference for the current node.
Then in your add method, check if the array that the current node have is not full (it can be done easily because you know the value of the index and the number of elements per node).
If it's not the case (or if the head is null for the first add call), create a new node and add the element in its array, otherwise just fill the next slot of the array for the current node.
This is how I would implement it.
class DynamicArrayOfInts {
private Node head, current;
private int numOfElementsPerNode;
public DynamicArrayOfInts(int elementsPerNode) {
if(elementsPerNode <= 0)
throw new IllegalArgumentException("elementsPerNode must be > 0");
numOfElementsPerNode = elementsPerNode;
}
public void add(int e) {
if(head == null){
head = new Node(numOfElementsPerNode, null);
head.data[head.index++] = e;
current = head;
return;
}
if(current.index == numOfElementsPerNode){
Node n = new Node(numOfElementsPerNode, null);
current.next = n;
current = n;
}
current.data[current.index++] = e;
}
#Override
public String toString(){
StringBuilder sb = new StringBuilder();
Node n = head;
while(n != null){
sb.append(Arrays.toString(n.data));
n = n.next;
}
return sb.toString();
}
private static class Node {
private int[] data;
private int index;
private Node next;
public Node(int n, Node nxt) {
data = new int[n];
index = 0;
next = nxt;
}
}
}
A small main to show how it behaves:
public static void main(String[] args){
int[] toAdd = {5,7,10,-1};
DynamicArrayOfInts d = new DynamicArrayOfInts(2);
for(int i : toAdd){
d.add(i);
System.out.println(d);
}
}
Output:
[5, 0]
[5, 7]
[5, 7][10, 0]
[5, 7][10, -1]
Unfortunately you have to create a new array in this case (yes, I feel your pain) which will have one more element than data. You will have to copy the content of data into your new array and set the last element to your int value. This is not too elegant, this is why Mureinik suggested that you should use an ArrayList of Node (ArrayList<Node>) instead. I can even enhance his suggestion to make your solution more general and tell you to use an AbstractList<Node> instead and instantiate it with ArrayList, but this might be too advanced compared to your current level (no offence, we all have been there). As of your exact question, I imagine a method like the following in your Node class to deal with this problem.
public void push(int newValue) {
int[] newData = new int[data.length + 1];
for (int i = 0; i < data.length; i++) {
newData[i] = data[i];
}
newData[data.length] = newValue;
data = newData;
}
So far I have the algorithm figured out to add to my binary search tree, but I'm having a bit of difficulty translating it into code. The algorithm is as follows:
public void add(int v) {
Create a new node n to hold value v.
If tree is empty
Set root to n.
Else
Create temporary node reference m, initialized to root.
Loop looking for a gap (a null left or right pointer) that is on the
correct side of m for v
If v < m.value, look at the left pointer
If v >= m.value, look at the right pointer
If pointer on correct side is not null, set m to that child node and
continue looking
m = m.left or m = m.right
The search for insertion position stops when node m has a null pointer on
the correct side.
Insert the new node n at that position
m.left = n or m.right = n
}
So far I have:
public void add(int v) {
Node n = new Node(v);
if(root==null)
root = n;
else {
Node m = root;
while(...) {
if(...)
m = m.left;
else
m = m.right;
}
if(...)
m.left = m;
else
m.right = n;
}
}
I believe most of that is correct, but I don't know what needs to be done at places marked as "..."
first of all a binary search tree should not have any duplicate values, an important requirement you have not implemented in your code. I have implemented the binary search tree recently while learning datastructures in java. Here is the code i wrote:
public class OrderedBinaryTree
{
private int _elementsPresent = 0;
private Node _root = null;
private int [] _values = null;
private class Node
{
Node _left = null;
Node _right = null;
Node _parent = null;
int _value = 0;
public Node(int value,Node parent)
{
_value = value;
_parent = parent;
}
}
public void put(int value)
{
boolean valueInserted = false;
Node temp = _root;
while(!valueInserted)
{
if(_root == null)
{
_root = new Node(value,null);
break;
}
else if(value == temp._value)
{
System.out.println("the entered value is already present");
return;
}
else if(value<=temp._value)
{
if(temp._left == null)
{
temp._left = new Node(value,temp);
break;
}
else
{
temp = temp._left;
}
}
else
{
if(temp._right == null)
{
temp._right = new Node(value,temp);
break;
}
else
{
temp = temp._right;
}
}
}
_elementsPresent++;
}
Basically, I am implementing an AVL tree by reading a set of integers from a text file and then populate the tree by using the add() method. Also, the program is supposed to print in order the set of integers.
As I run the program, a StackOverflowError pops up. I think that this error is being triggered due to something malfunctioning in the add() method.
I would really appreaciate if someone helps me as I am new to this type of programming.
This is part of the Main Class:
public static void main(String[] args) throws FileNotFoundException
{
AVL s1 = new AVL();
Scanner file = new Scanner(new File("C:\\Users\\Dell\\Desktop\\integers.txt"));
while(file.hasNext())
{
// String str = file.next();
//int b = Integer.parseInt(str);
int b = file.nextInt();
s1.add(b);
}
v1.PrintInOrder(v1.root);
These are the add() and PrintInOrder() methods:
public boolean add(int key)
{
root = add(root, key);
return true;
}
private Node add(Node b1, int key)
{
if(b1 == null)
{
return new Node(key);
}
if(key < b1.element){
b1.left = add(b1.left, key);
}
else
{
b1.right = add(b1.right, key);
}
int Left_Height = getHeight(b1.left);
int Right_Height = getHeight(b1.right);
// a height imbalance requires that two subtrees differ by two
if(Math.abs(LeftHeight - RightHeight )== 2)
return Balance(n1);
else
{
n1.ResetHeight();
return b1;
}
}
public void PrintInOrder(Node b1){
if(b1 != null){
PrintInOrder(b1.left);
System.out.println(b1.element);
PrintInOrder(b1.right);
}
}
This is the Node class:
public class Node {
Node left;
Node right;
int element;
int height;
public Node(int keys){
this(keys, null, null);
}
public Node(int d, Node right1, Node left1){
element = d;
height = 0;
left = left1;
right = right1;
}
// This method recalculates the height if the right or left subtrees have been altered
public void ResetHeight(){
int LeftHeight = AVL.getHeight(left);
int RightHeight = AVL.getHeight(right);
height = 1 + Math.max(LeftHeight,RightHeight);
}
Since stack overflows commonly occur in recursion. Use your IDE and set a break at locations where you have done recusion, then debug. Step through it.