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)
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
For example, Let T be a binary tree:
Is it possible for the inorder traversal of T to be identical to the postorder traversal of T? if "yes" can you please give an example. And if "No" could you please explain why it cannot occur?
Also, Is it possible for the inorder traversal of T to be identical to the preorder traversal of T?
Thank you so much in advance.
Is it possible? Yes. Consider the inorder, preorder, and postorder traversal of the following tree
A
...that is, the tree consisting of exactly one node and no children.
This tree has the following traversal in inorder, preorder, and postorder: [A].
In general, the inorder traversal is equivalent to the postorder traversal if there are only left children, and the inorder traversal is equivalent to the preorder traversal if there are only right children.
Is it possible for the inorder traversal of T to be identical to the
postorder traversal of T?
Yes, when the tree only has a left child at every node:
a
/ \
b null
/ \
c null
/ \
...
Is it possible for the inorder traversal of T to be identical to the
preorder traversal of T?
Yes, when the tree only has a right child at every node:
a
/ \
null b
/ \
null c
/ \
...
Look here for a more detailed explanation.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am working on Huffman coding in Java. I am not getting the point of how and where to store the tree. I know how to make the tree, and then save it as a binary file, but for decoding purpose we will again need that same tree to decode. How can I save that tree in a file to attach it with binary file?
Write the tree as a series of bits: 0 represents a leaf, 1 represents an internal node. The output for a binary tree (Huffman or otherwise) with N leaf nodes and N-1 internal nodes will be a sequence of 2N-1 bits. (You can actually save two bits, since you know that the first and last nodes in the tree will be leaf nodes, but it's probably not worth complicating the algorithm to save two bits.)
Perhaps easiest is to arrange the bits in pre-order:
function write_tree (top_node) {
if is_leaf(top_node) {
write "0"
// optionally, write any date associated with the leaf node
// although in practice it's easier to write the leaf data
// to a separate output stream. That lets this stream contain
// actual bits rather than the characters "0"/"1"
}
else {
write "1"
write_tree (top_node.left)
write_tree (top_node.right) }}
function read_tree (bit_stream) -> returns tree
next_bit = bit_stream.read()
if next_bit = "0" {
root = new leaf
// optionally read data associated with the leaf node
}
else {
root = new internal node
root.left = read_tree (bit_stream)
root.right = read_tree (bit_stream) }
return root }
I didn't notice at first that you mentioned Java, so I wrote the above in pseudo-code, which I'm sure you'll have no trouble re-writing in Java.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
First time poster, sorry if I break any etiquette.
I'm studying for my Data structures and algorithms midterm and I have a practice problem I don't really understand.
Suppose you are given a sorted list of N elements
followed by f(N) randomly-order elements.
How would you sort the entire list if f(N) = O(1)?
How would you sort the entire list if
f(N) = O(log N)?
We have gone over lots of sorting algorithms but the exam focuses on Insertion, Quick and Merge Sort. I don't really understand what it means by if F(N) = O(log N). Is that using Big oh notation to say that the most amount of random elements on the end would be either a constant or log(N) in each respect case.
Thanks for any insight.
Edited: Fixed my obvious mistake in terms of Big Oh notation, not sure where to go from here though.
In the first example you are given a problem, where a constant number of non-ordered elements follow the sorted sequence. In essence this means that you may implement an algorithm to insert a single non-ordered element and then repeat it several times. The overall complexity of inserting all f(N) = O(1) elements will be the same. One of the algorithms you mention is best to perform this operation.
In the second case you have number of elements to be inserted in the order of log(n). In this case you can not ignore this number as it is dependent on the input size. Thus you need to think of a smarter way to merge it with the remaining part that is already sorted. (TIP: maybe the operation you need to perform will help you choose an algorithm?)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am writing a method to traverse a binary search tree recursively. It will visit every node and as it does so, append it to an ArrayList. So by the time all nodes are visited, they're going to be in the ArrayList.
I've written the following, but it's not working and I don't understand why.
Any help here would be much appreciated.
Method as follows:
/**
* Traverse a Binary Search Tree and append each node to an ArrayList.
*
* #param n - this.root of the Binary Search Tree
* #return - a ListArray of ALL nodes in the calling Binary Search Tree
*/
private List<TreeNode> traverse( TreeNode n ) {
List<TreeNode> listOfNodes = new ArrayList<TreeNode>();
if (n == null)
return listOfNodes;
listOfNodes.add(n);
traverse(n.left);
traverse(n.right);
return listOfNodes;
}
Many thanks.
In any case, you almost got the solution, but you do not use the result of your recursive calls. When you call traverse(n.left) and traverse(n.right) you should add the result of these calls to your listOfNodes
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 an ArrayList<Edge> in which I have to remove duplicate values.
Edge class is as follows
class Edge
{
int srcNode;
int destNode;
double edgeWeight;
}
I wan to calculate Minimum Spanning Tree of a Graph to implement Krushkal's algorithm. To calculate that I will have to remove all the duplicate Edges from a list. So which algorithm would be best suitable to remove duplicate values from this kind of Data Structure with srcNode, destNode and edgeWeight.
You can create a binary search tree of Edges on the basis of edgeWeight. While inserting in BST keep one thing in mind is to check if Edge with edgeWeight is already present in BST (compare srcNode and destNode too), if it is found then discard it else add it to BST. In this way you will be having all unique Edges in BST.
searching in BST will take only log(n) time (n=number of element in BST).
if number of edges are 'm' then complexity would be m log(n)
PS: I'm assuming from srcNode '1' to destNode '2' will have duplicate Edges with same edgeWeight. if you have multiple edges with different edgeWeight on two node then you can choose the minimum one and replace it in BST while inserting if it already present. In this way you will have lowest edgeWeight for that Edge
like #zapl has said, you can use Set.
anyway, you have to sort the edges, why don't you consider implement compareTo, then sort the edges, then if one edge equals the previous one, skip it.
see: http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/
btw, Krushkal doesn't require the edges to be unique, the second time an edge is processed, fa[u] would equal fa[v] and this edge would be ignored.