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.
Related
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 5 years ago.
Improve this question
I am trying to create a depth first search algorithm here, kind of. Instead of traversing and finding out how many values it had to go through, it should be finding the highest value in each row.
I know how to actually implement the search and everything but the one part I can't figure out, especially since googling it doesn't work very well is how to create an array of Sub Classes/childs.
I don't really need to know how to initialize a general array, but rather how to initialize an array when I have a class of Node with 2 int values, and then I want an array of said Nodes with the 2 values in them
e.g. right now I have
Node Node1 = new Node(1,1);
Node Node2 = new Node(2,3);
Node Node3 = new Node(2,2);
Node Node4 = new Node(3,5);
Node Node5 = new Node(3,3);
Node Node6 = new Node(3,9);
I want to use an array where it would be something like Node[0] = 1,1
or something similar to that - is there any way this could be done in Java?
Something like:
Node[] nodes = {new Node(1,1), new Node(2,3)};
Then you could say something like:
nodes[0] = new Node(5,5);
or
nodes[0].setXValue = 1 // Or whatever this method and value would be
etc. But look at the comments also as you could use an ArrayList for more flexibility or use another way of initializing an array as in the link given.
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 7 years ago.
Improve this question
I want to build a dictionary without using any database application. I also want to store word in text/binary file. How can I improve searching if a file contains millions of records.
You could use the concept of Inverted Indexing. It could go like this:
Sort the file.
For every character in the alphabet keep the first and last line that a particular character appears in a certain position.
For a certain word get all the ranges and calculate the intersection. The sequence matters. If the intersection is empty keep the previous one. If in the end the intersection is empty, the word doesn't exist.
For example:
You have calculated the range (first and last line) that character a appears in the first position of a word. The same for the second position and so on and so forth. You do the same for all the characters of the alphabet.
Let's assume you want the word "apple".
You find the range that a appears in the first position (Set A). p in the second (Set B) and third position(Set C), l in the forth (Set D) and e (Set E) in the fifth. You have 5 ranges.
You calculate the intersection of these five ranges like this:
A intersect B = K. If K is empty then K = A.
K intersect C = Y. If Y is empty then Y = K...
This leaves you a much smaller range when you hit the disk. Basically with this you have an estimation of where in the file this word is located.
This approach is safe regarding memory. The size of the structure depends on the size of the alphabet and on the size of the biggest word you have in the dictionary. Of course you will have to take into consideration that the data represented by the intersection can be bigger than the memory. In this case you will have to load the data in chunks.
It's very expensive to add or delete a word from the dictionary, because you will have to sort and recalculate the index. In order to avoid doing the calculation every time, you can save your index in another file (serialize).
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 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)
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I wrote a function in Scala to find out and return a loopy path in a directed graph. The program is as followed, one of the arguments is a graph presented in an adjacent list, and the other is a start node. It returns a pair including a loopy path by a list of nodes.
I wonder there are more elegant ways to do so. Please share your thoughts if you would like to. Thanks.
def GetACycle(start: String, maps: Map[String, List[String]]): (Boolean, List[String]) = {
def explore(node: String, visits: List[String]): (Boolean, List[String]) = {
if (visits.contains(node)) (true, (visits.+:(node)).reverse)
else {
if (maps(node).isEmpty) (false, List())
else {
val id = maps(node).indexWhere(x => explore(x, visits.+:(node))._1)
if (id.!=(-1))
explore(maps(node)(id), visits.+:(node))
else
(false, List())
}
}
}
explore(start, List())
}
I felt I had to use the indexWhere in this situation, but I suppose it would have other ways to do that.
You should use an array to check if you have already visited a node and not visits.contains(node), it would give you the answer in constant time instead of linear time.
The overall complexity of your algorithm is exponential. For instance, if you run your algorithm on this graph:
0 -> 1, 2, ..., n
1 -> 2, ..., n
...
where there are n nodes and there are edges from i to j iff i<j then the node i will be explored 2^i times.
Again you can solve this problem using an array (one array for all nodes) to ensure that each node is explored at most one time.