RPN with operators other than *,/,+,- - recursion

I have a class called Node.
public Node
{
public int data;
public Node primaryNext;
public Node secondaryNext;
}
I have a Node root = null; And when the first value is received from the input, it then runs something like this.
root = new Node;
root.data = /*input*/ ;
root.primaryNext = null;
root.secondaryNext = null;
The next step is adding a new Node at the end of the list, by pointing root.primaryNext or root.secondaryNext to a new Node while filling the "pointer" tree by levels. So I need to do something like this:
GIF of the idea.
I think that this could be done using ||, &&, |, & operators applied to each level of nodes with a recursive method. So:
How do I operate in C# like the RPN?
If I can, which would be the best way to do it? I understand recursion pretty well, but I might not do the best possible method.
Thanks.

Suggestion:
One command to push a single node onto a stack.
Another command to take the two topmost nodes from the stack, combine them and push the result back on the stack.

Related

TraversalEngine abstract class utilisation

Firstly, i am sorry but i don't speak english very well. Secondly, i have a problem with nodes which are put in a gridpane. In fact, if the focus is taken by the first one wich is located on the top left side, when i push the tab key, the focus is not taken by the other which is located on the right.
People ask me to use the traversalEngine abstract class in order to solve this problem. Nevertheless, when i try to implement an engine object, it doesn't work if i put the parameters which are shown everywhere on the web:
TraversalEngine engine = new TraversalEngine(gridPane, false) {
It ask me to remove the parameters. If i do it, i don't have access to the trav method. In fact, it is the getRoot method which appears and can be implemented :
TraversalEngine engine = new TraversalEngine() {
#Override
protected Parent getRoot() {
// TODO Auto-generated method stub
return null;
}
}
Is there something which can be make in order to solve this problem ?
Thanks you for your help
Vinz
The traversal order for focusing nodes in a parent is the order in which they occur in the child list. Assuming every child contains at most one focusable node you could simply add the children line by line or reorder the children.
This could be done programmatically of course, but adding the children in the correct order in the first place would be more efficient...
public static int getColumnIndex(Node n) {
Integer i = GridPane.getColumnIndex(n);
return i == null ? 0 : i;
}
public static int getRowIndex(Node n) {
Integer i = GridPane.getRowIndex(n);
return i == null ? 0 : i;
}
grid.getChildren().sort(Comparator.comparingInt(ContainingClass::getRowIndex).thenComparingInt(ContainingClass::getColumnIndex));

Binary Search Tree Insert Implementation

So my question is as follows:
When I run the code for this insert helper method, and I am positive my new node method is correct as it works for instantiating a Binary Search Tree, no nodes are inserted. Why can't I use this certain implementation? What's going wrong here?
I know how to use the other insert implementation where one would check for the left and right nodes of the root and whether or not they are null, but can not figure out the problem of this more elegant possibility. The answer to this will help me in creating other functions that go beyond the scope of the insert function.
btw yes I have another function calling this helper function
Thanks!!!!!
//INSERT METHODS
void BinarySearchTree::insert(int data, struct node* root) {
//If root is null make new node there
if (!root) {
root = new node(data);
}
else if (root -> data > data) {
insert(data, root -> left);
}
else {
insert(data, root -> right);
}
}
The variable root is a parameter, which only has local visibility for that one method call. Meaning root = new node(data) will indeed create a new node, but that will only be pointed to by the parameter. Your method doesn't return anything and it doesn't actually know what it is supposed to do with that new root object of yours (it is NOT the same as any class variable you might have defined that is named the same).
So you create a new node, but can't use it outside that one method call. Which results in an empty tree.
As a side note for future questions: Include a tag for the programming language you are using. A lot of people use that as a filter, so you will actually get more people looking at this if you use the right tag.

How to bind lists like an updating ForEach?

Here is a sample code:
public class Example3 {
class Point {
int x, y; // these can be properties if it matters
}
class PointRepresentation {
Point point; // this can be a property if it matters
public PointRepresentation(Point point) {
this.point = point;
}
}
Example3() {
ObservableList<Point> points = FXCollections.observableArrayList();
ObservableList<PointRepresentation> representations = FXCollections.observableArrayList();
points.forEach(point -> representations.add(new PointRepresentation(point)));
}
}
I have a data holder Point and a data representor PointRepresentation. I have a list of points and i would like that for each point in the list there would be an equivalent representation object in the second list. The code I gave works for the initialization but if there is any change later the above will not update.
What I am doing now is using a change listener to synchronize the lists (add and remove elements based on the change object) and it's OK but i am wondering if there's a simpler solution. I was looking for something like a "for each bind" that means: for each element in one list there is one in the other with the specified relation between them [in my case its that constructor]. In pseudocode:
representations.bindForEach(points, point -> new PointRepresentation(point));
Things I looked at: extractors for the list but that sends updates when a property in the objects they hold change and not when the list itself changes. So in my case if x in the point changes i can make an extractor that notifies it. Another thing I looked at is http://docs.oracle.com/javase/8/javafx/api/javafx/beans/binding/ListBinding.html, so maybe a custom binding does it but I don't know if it's simpler.
Also is there a similar solution for arrays instead of lists? i saw the http://docs.oracle.com/javase/8/javafx/api/javafx/collections/ObservableArray.html as a possibility.
The third-party library ReactFX has functionality for this. You can do
ObservableList<Point> points = FXCollections.observableArrayList();
ObservableList<PointRepresentation> representations = LiveList.map(points, PointRepresentation::new);
This will update representations automatically on add/remove etc changes to points.

Recursively turning a stack into a linked list

so I have been working on a programming assignment that involves taking a stack implementation of size ~13,000 and turning it into a linked list. The guide is basically that the stack was filled by sequentially scanning a linked list (IE tail would be the top of the stack), and you want to re create the linked list using the stack. The trick is you have to do it using a recursive method. The only methods in this stack class are pop (returns and removes the top element), and isEmpty(tells if the stack is empty). I have code that gets the job done, however it requires increasing the java stack size (otherwise I get StackOverflowError), which I feel like that isn't allowed.
That being said does anyone know a way I could possibly get this to work without increasing the java stack size.
The stack is a static field I have labeled S. Head is what should be the first node in the linked list, and steper is simply a node to be used to create every other step.
Here is the code I currently have:
public static void stackToList()
{
int x = 0;
if(S.isEmpty())
{
return;
}
x = S.pop();
stackToList();
if (head == null)
{
head = new ListNode(x, null);
steper = head;
}
else
{
steper.next = new ListNode(x, null);
steper = steper.next;
}
}
Thank you ahead of time for any help.
It is happening because you are keeping an entire list of function calls in memory stack. You start creating your linked list only after you reach to the bottom of the stack thus keeping all the previous calls to stackList waiting to be over.
You need to start creating your linked list with the first pop of stack.
A simple & non tested (not worked in Java in a very long time now) function may look like:
public static ListNode stackToList(ListNode head) {
if(S.isEmpty())
return head;
int stackValue = S.pop();
ListNode node = ListNode(stackValue, null);
node.next(head);
return stackToList(node);
}
And you call it like:
ListNode head = stackToList(null)
HTH
EDIT: Now that I posted it, I realized that my code has potentially the same issue as yours, because I remembered Java doesn't support tail-call optimization.
It's not entirely clear from your question if you're using java.util.LinkedList and java.util.Stack but since you didn't provide the code for these objects I will be using those in my example solution below:
public static void main(String[] args) {
//Create a stack
Stack<Integer> stack = new Stack<Integer>();
stack.push(0);
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
stack.push(6);
stack.push(7);
stack.push(8);
stack.push(9);
//Create a list to hold your stack elements
LinkedList<Integer> linkedList = new LinkedList<Integer>();
//Call the conversion method, which modifies both the stack and the list
convertStackToLinkedList(stack, linkedList);
//print the results
System.out.println("linkedList: "+linkedList);
}
public static void convertStackToLinkedList(Stack<Integer> stack, LinkedList<Integer> linkedList){
int topStackElement = stack.pop();
linkedList.add(0,topStackElement);
if(!stack.isEmpty())
convertStackToLinkedList(stack, linkedList);
}
I suspect you may not be using java.util.LinkedList since your code is attempting to modify the internals of the list. So, you would simply need to implement a method similar to that of the add(int index, E element) method in the java.util.LinkedList class and then use it in your recursion. I assume you can do this if you have access to the internals of the list.
EDIT:
I forgot to mention that I agree with the answer by Harsh Gupta in that the reason you're seeing StackOverflowError is that you're waiting until you reach the end of your recursion to modify your list. In some recursion you have to wait until the end but if you don't have to wait don't do it.

How do I find the length of an associative array in ActionScript 3.0?

Is there a simple way to retrieve the length of an associative array (implemented as an Object) in ActionScript 3.0?
I understand that there are two primary ways of creating associative arrays in AS3:
Use a Dictionary object; especially handy when the key does not need to be a string
Use an Object, and simply create properties for each desired element. The property name is the key, and the value is, well, the value.
My application uses approach #2 (using the Object class to represent associative arrays).
I am hoping there is something more native than my for loop, which manually counts up all the elements.
You have to count them in a for loop as you do. Of course, you could make a class and stick the for loop in that class.
For some great implmentations of Collections in AS3, check these guys.
Edit 2013 Not surprisingly, links do break after time. Try this new one: http://www.grindheadgames.com/get-the-length-of-an-object.
Doing a few tests on this has actually surprised me. Here's normal use of an Array:
var things:Array = [];
things.push("hi!");
trace(things.length);
// traces 1
trace(things);
// traces hi!
Here's if we set a value to a string:
var things:Array = [];
things["thing"] = "hi!";
trace(things.length);
// traces 0
trace(things);
// traces an empty string
trace(things["thing"]);
// traces hi!
Basically if you add things using strings you're setting properties rather than actually adding to the array. Makes me wonder why Array is dynamic in this way.
So... yeah count the items with a for ... in loop!
I think you're stuck with counting them "manually".
An option would be to wrap the whole thing in a class and keep a separate variable that you update as you add/remove.
var count:int;
var key:String;
for (key in myObject)
{
count++;
}
trace ("myObject has this many keys in it: " + count);
or, alternatively, the for-each syntax (I haven't tested to see which is faster)
for each (var o:* in myObject)
{
count++;
}

Resources