Hello Friends, its me again....back with another problem.
I am trying to write a method which goes through my tree, finds the element given to the function and gives back the path (nodes) I traversed while I was searching for that element/ object.
My Idea was to work with a few helper-methods to solve the problem.
1. Write a method to check if there is a path from each child of my current node.(There is always only one child-node option that works at each level)
2. If there is a path I want to save the node and check the same thing for the child. I don't care about the nodes that don't have a path.
My questions now:
- Is there a smooth way to store the my paths? Thought about an ArrayList where I add the [i] that leads to the correct path.
- Does my pathExist function work? To be honest I am not even sure if i get the correct result that I want...
Hope this is not to vague and someone can maybe give me a Tip or send me a link to check a concept?
I only find Binary-Tree solutions.
@Carey Brown: That might be true, but I am unfortunately just the poor student working on the tasks, restricted by the cruelty of our education system spending 10 hours on a Saturday to implement three methods.
So I came up with the following approach for my size() Method:
To be honest, I don't really think it is working because the count variable should reset every time when I call the method.
I have no idea how I can include the variable and keep it in the recursion and the loop without using another method.
Thank you very much for the reply!
Correct, I want to pass the count back up the chain.
My problem is, that I saw a solution like your multiple times when I search the web but I don't want to count every node.
I only want to count the node if there is a penguin inside so: penguin != null .
How do I add my case to the count += child.size() ?
Sorry, my bad.
I just think it is always better to provide some code to questions so people can see how I tried to deal with the problem.
Thank you very much for the quick reply btw.
My problem is, that I understand how you can use recursion for Binary-Trees when you basically just have a right-left option.
In my case there is the probability of 26 nodes afterwards each representing a character in the alphabet (children = array with a length of 26).
I don't find a nice way to iterate through all these options and check if there is an object stored in them and check the next options again...hope this makes somehow sense.
So I can store the variable in a parameter call but aren't there ways where I can do it without it?
I am working on a task for my University Project and I am super stuck a this point.
The task contains a tree in which we need to build a string with char concatenation.
My problem is that we need to write a method which counts the names we have build in this tree via recursive method calls.
What I don't understand is how I can save my count or add-up to this count during my recursive calls, I don't want to store the count in a parameter which gets called by a method.
It is about implementing an Iterator which returns digit sequences as a String.
We need to implement an Instanz of Iterator which goes through all the possible digit sequences of the given password length with the following conditions.
1. Digit Sequence of same number 0000
2. Digit Sequence rising 1234
3. Digit Sequence falling 4321
4. All the rest