This question already has answers here:
How to print binary tree diagram in Java?
(22 answers)
Closed 7 years ago.
I've created an AVL Tree, with working Add and Remove methods. However, I need to print out the tree in a visual format. something like this:
Image
is there a relatively short way of doing so?
(You can assume that i have the height of every node).
I doubt there's any easy way to display the data in an AVL tree besides inorder, postorder, and preorder list representations.
However, I did find this solution on an old Stack Overflow question.
Related
This question already has answers here:
What do 3 dots next to a parameter type mean in Java?
(9 answers)
Closed 7 years ago.
I have this AVL tree with a balance method:
private void setBalance(Node... nodes) {
for (Node n : nodes)
n.height = height(n.right) - height(n.left);
}
It uses a (...) syntax I have not encountered before. I can't find it on google or SO. It seems to be some type of list syntax, or array. Looks like something I would find in ruby.
Could someone knowledgeable in Java's syntax explain this code to me, and perhaps show me a version without the ... syntax?
Thanks.
These are varargs . Basically same are arrays but can be zero to many.
More info that might also assist.
This question already has answers here:
Find an array inside another larger array
(15 answers)
Closed 8 years ago.
Is there a way to find an array within another array like
a=[1,2,3,4,5,6,7]
b=[2,3,4]
c=[2,4,5]
// b is child of a, but c is NOT child of a.
Well I know that using Brute-force approach I can find the array within another array. But I want to know that is there any algo that can help me ... or (as I am using JAVA so) is there any built-in feature in JAVA that can help me ?
As already mentioned here :
https://stackoverflow.com/a/3940684/351861:
public static int findArray(Integer[] array, Integer[] subArray)
{
return Collections.indexOfSubList(Arrays.asList(array), Arrays.asList(subArray));
}
Java has builting features for that, apparently.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a tree which is not a binary tree and each node has more that 2 children, I am looking for an algorithm traverse the tree, I am really new in learning data structure, I know how to traverse a binary tree but I get lost when it comes to traverse a non-binary tree .
Could any one gives me a hint ?
In a non-binary tree, there will be a Vector or some other structure that has references to all the children. Make a recursive method as so:
public void traverse(Node child){ // post order traversal
for(Node each : child.getChildren()){
traverse(each);
}
this.printData();
}
Something along those lines.
Well, when traversing a binary tree, in preorder, you visit the parent node, then recursively traverse the left subtree then recursively traverse the right subtree. With a tree with more than two children, you recursively traverse the subtrees headed by each children. You would do the recursive calls in a for loop.
You'll need to use recursion since you can't determine how many children are at each node (breadth) or how far the tree goes (depth). Depending on how you want to traverse, you can use Breadth-first-search or Depth-first-search.
There is a ton of information on this topic, so give it an attempt to implement one of these recursive methods and please come back if you have trouble along the way!
The algorithm for pre-post order is the same as a binary tree.
There is no such thing as in-order traversal for a general tree i.e. does not make sense (unless you define some order)
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How would I Evaluate a certain formula?
How would I split this formula into an array of characters each having their own number in the array:
a1+a2+a5*((a1+a6)*a3)
one I have added the spaces I am going to get column 1 because a1 will indicate column one and it will contain a number than I will add that to column 2. I am not allowed to use a tree or any of those other things just stacks and I have been asking that. But people keep telling me to use libraries and trees I am only in a 200 level course !
You need a grammar and a parser to do this in a general way. Something like this.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Generating all permutations of a given string
I have an array of arbitrary length in Java, and I would like to generate all possible permutations of them. The easy way to do this for a fixed length would be a series of nested for loops, but because the array is of unknown length, that is not an option here. Is there a straightforward way to accomplish this in Java?
Use a recursive function, instead of loops. Each time you call the method should be on a smaller portion of the array and stop when length = 0. This link should help you design your function.
It may or may not be optimal as far as performance goes, but if you're looking for a way to do it with writing relatively little code and having it be clear and maintainable, you want a recursive method rather than nested loops.