Binary Search Tree Insert Implementation - recursion

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.

Related

Dynamics AX 2012: How can I get, by code, a list of all the methods within a specific form?

Is there any way for me to get, by code, a list of all the methods in a given form in Dynamics AX 2012?
I am working in developing a small tool that will insert some comments in all of the methods of custom objects through using the Editor class. However, to do so, I need the full path of each method (ex: \Classes\MyClass\CustomMethod), but I simply can't find any way to get this working for Forms.
Thanks in advance!
Thanks for sending me suggestions. I actually just finished writing some code to get me the information. Here is the code, for anyone who may be interested:
//I used the BatchJobHistory form as a test, since I called this static method from a job
public static void findAllChildNodes(str _nodeName = "\\Forms\\BatchJobHistory", boolean _isMethod = NoYes::No)
{
TreeNode treeNode;
TreeNodeIterator treeNodeIterator;
treeNode methodsNode;
str treePath;
boolean containsMethod;
treeNode = TreeNode::findNode(_nodeName);
treeNodeIterator = treeNode.AOTiterator();
methodsNode = treeNodeIterator.next();
while(methodsNode)
{
treePath = methodsNode.treeNodePath();
containsMethod = strScan(treePath, 'Methods', 1, strLen(treePath));
if (methodsNode.AOTchildNodeCount())
{
//TestClass is the class containing this method
TestClass::findAllChildNodes(treePath, containsMethod);
}
else if (_isMethod)
{
info(strFmt("%1", treePath));
}
methodsNode = treeNodeIterator.next();
}
}
Here's a question that should point you in the right direction, albeit you'll need to expand on it.
AX2009 Loop through all the controls in the form on init
The general theme you'll probably need to work with is recursion (https://en.wikipedia.org/wiki/Recursion_(computer_science)), reflection, and use of the TreeNode and derivatives of the class to do Tree Node Traversal (https://en.wikipedia.org/wiki/Tree_traversal)
See https://learn.microsoft.com/en-us/dynamicsax-2012/developer/performing-reflection-with-the-treenode-class
I'm also imagining you'll need to use some of the layer-aware methods if you're trying to decorate methods with comments. It sounds fun what you're trying to do, but a little bit of a pain. I'd expect it to take at least half a day to do properly.

Handlebars variable inside each loop (Accessing object key value)

One of my components needs to fetch different data from a property based on a parameter I pass it. The parameterHolder is an Object with keys and parameter is the key name.
{{component-name parameterHolderBinding=parameterHolder parameterValueBinding=parameter}}
I'm trying to loop through it like so
{{#each item in parameterHolder.[{{parameter}}]}}
{{/#each}}
But it's not working. When I try,
{{parameterHolder.keyName}}
I'm getting [Object] which is correct. Could someone please point out the right way do this?
I've managed to get it working by dynamically creating new properties.
In my controller -
parameterUpdatedObserver: function ()
{
var reference = this;
$.each(parameterHolder, function (key, value) {
reference.set('parameterHolderName'+key, value);
});
}.observes('parameterHolder')
And my component -
{{component-name parameterHolderBinding=parameterHolderNameKey parameterValueBinding=parameter}}
I can probably abstract it up one more level using another component. If anyone has a better solution, please do post it.

Using Recursive References in Go

I want to contain all my commands in a map and map from the command to a function doing the job (just a standard dispatch table). I started with the following code:
package main
import "fmt"
func hello() {
fmt.Print("Hello World!")
}
func list() {
for key, _ := range whatever {
fmt.Print(key)
}
}
var whatever = map[string](func()) {
"hello": hello,
"list": list,
}
However, it fails to compile because there is a recursive reference between the function and the structure. Trying to forward-declare the function fails with an error about re-definition when it is defined, and the map is at top-level. How do you define structures like this and initialize them on top level without having to use an init() function.
I see no good explanation in the language definition.
The forward-reference that exists is for "external" functions and it does not compile when I try to forward-declare the function.
I find no way to forward-declare the variable either.
Update: I'm looking for a solution that do not require you to populate the variable explicitly when you start the program nor in an init() function. Not sure if that is possible at all, but it works in all comparable languages I know of.
Update 2: FigmentEngine suggested an approach that I gave as answer below. It can handle recursive types and also allow static initialization of the map of all commands.
As you might already have found, the Go specifications states (my emphasis):
if the initializer of A depends on B, A will be set after B. Dependency analysis does not depend on the actual values of the items being initialized, only on their appearance in the source. A depends on B if the value of A contains a mention of B, contains a value whose initializer mentions B, or mentions a function that mentions B, recursively. It is an error if such dependencies form a cycle.
So, no, it is not possible to do what you are trying to do. Issue 1817 mentions this problem, and Russ Cox does say that the approach in Go might occasionally be over-restrictive. But it is clear and well defined, and workarounds are available.
So, the way to go around it is still by using init(). Sorry.
Based on the suggestion by FigmentEngine above, it is actually possible to create a statically initialized array of commands. You have, however, to pre-declare a type that you pass to the functions. I give the re-written example below, since it is likely to be useful to others.
Let's call the new type Context. It can contain a circular reference as below.
type Context struct {
commands map[string]func(Context)
}
Once that is done, it is possible to declare the array on top level like this:
var context = Context {
commands: map[string]func(Context) {
"hello": hello,
"list": list,
},
}
Note that it is perfectly OK to refer to functions defined later in the file, so we can now introduce the functions:
func hello(ctx Context) {
fmt.Print("Hello World!")
}
func list(ctx Context) {
for key, _ := range ctx.commands {
fmt.Print(key)
}
}
With that done, we can create a main function that will call each of the functions in the declared context:
func main() {
for key, fn := range context.commands {
fmt.Printf("Calling %q\n", key)
fn(context)
}
}
Just populate the map inside a function before using list(). Like that.
Sry I did not see that you wrote "without init()": that is not possible.

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

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.

Rational Functional Tester - How can I get scripts called from a parent script to use the parent's data pool?

I'm fairly new to Rational Functional Tester (Java) but I have one large blank. I have an application that is in an agile development environment so some of the screens can flux as new interfaces are brought online.
For this reason I'm trying to modularize my test scripts. For example: I would like to have a login script, a search script, and a logout script.
I would then stitch these together (pseudo code)
Call Script components.security.Login;
Call Script components.search.Search;
//verification point
Call Script components.security.Logout;
By breaking the testing script into discrete chunks (functional units) I believe that I would be better able to adapt to change. If the login script changed, I would fix or re-record it once for every script in the application.
Then I would call that script, say, "TestSituation_001". It would have need to refer to several different data pools. In this instance a User datapool (instead of a superUser datapool) and a TestSituation_001 datapool, or possibly some other datapools as well. The verfication point would use the situational datapool for its check.
Now, this is how I would do it in an ideal world. What is bothering me at the moment is that it appears that I would need to do something entirely different in order to get the child scripts to inherit the parents.
So my questions are these:
Why don't child scripts just inherit the calling script's data pool?
How can I make them do it?
Am I making poor assumptions about the way this should work?
If #3 is true, then how can I do better?
As a side note, I don't mind hacking the heck out of some Java to make it work.
Thanks!
I solved my own problem. For those of you who are curious, check this out:
public abstract class MyTestHelper extends RationalTestScript
{
protected void useParentDataPool() {
if(this.getScriptCaller() != null) {
IDatapool dp = this.getScriptCaller().getDatapool();
IDatapoolIterator iterator = DatapoolFactory.get().open(dp, "");
if(dp != null && iterator != null) {
//if the datapool is not null, substitute it for the current data pool
this.dpInitialization(dp, iterator);
}
}
}
}
This will use the same iterator too. Happy hunting...
Actually, after some reflection, I made a method that would make any given script use the Root calling script's DataPool. Again, happy hunting to those who need it...
/*
* preconditions: there is a parent caller
* postconditions: the current script is now using the same datapool / datapool iterator as the root script
*/
protected void useRootDataPool() {
//if there is no parent, then this wouldn't work so return with no result;
if(this.getScriptCaller() == null) return;
//assume that we're at the root node to start
RationalTestScript root = this;
while(root.getScriptCaller() != null) {
root = root.getScriptCaller();
}
//if this node is the root node, no need to continue. the default attached datapool will suffice.
if(this.equals(root)) return;
//get the root's data pool (which would be the parent's parent and so on to the topmost)
IDatapool dp = root.getDatapool();
if(dp != null) {
//check to make sure that we're not trying to re-initialize with the same datapool (by name)
//if we are, then leave
if(dp.getName().equals(this.getDatapool().getName())) return;
//this basically says "give me the iterator already associated to this pool"
IDatapoolIterator iterator = DatapoolFactory.get().open(dp, "");
//if we have an iterator AND a data pool (from above), then we can initialize
if(iterator != null) {
//this method is never supposed to be run, but this works just fine.
this.dpInitialization(dp, iterator);
//log information
logInfo("Using data pool from root script: " + root.getScriptName());
}
}
}

Resources