Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
for my project i have to create a huffman tree project, but my lecturer has said that i cannot use priority queues to build it?
But i dunno how to implement that.
Are there any other ways i can create a huffman tree without using priority queues?
This is an example of a huffman tree but it is using priority queues
enter image description here
enter image description here
There's a trick that is often used to build Huffman trees in practice:
Create a list of your symbols with probabilities, and sort it in ascending order
Create an initially empty list for combined symbols. This will remain sorted as we work.
While there is more than one symbol in the lists:
Remove the two smallest symbols from the beginnings of two lists
Combine them and add them to the end of the combined list. Because the new symbol has a higher combined probability than any combined symbol created before, this list remains sorted.
After the initial sorting, the smallest probability symbol will always be the first one of one of the two lists, so no priority queues or searching is required to find it.
This technique is quite clever, and your lecturer would not expect you to think of it yourself, so it was probably taught or referenced in class.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have 200 folders with up to 20 files in each folder. Total the dataset is 2gb. I tried parsing all at once and put each line into a list and sort them but i get out of memory.
What approach could I use to sort the multiple files into on single file?
File-based merge-sort:
Sort content of each file.
Merge sort the 20 files of each folder to get one sorted file per folder.
Merge sort the 200 folder-files to get final result.
If you don't want to do a 200-way merge sort, you can split #3 into multiple merge-sorts and then merge-sort the results of those, to as many levels as needed.
What sorting algorithm do you use? Because I think the problem lies with the algorithm; you need to take a look for a more efficient algorithm to do the sorting. I believe that for large inputs, Merge-Sort is the best (albeit with a few modifications for that size).
Here is a very similar question, take a look at the top two answers. They should help you solve the problem.
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 6 years ago.
Improve this question
In order to implement a circular queue do you use a single linked list or double linked list or array?when and why?
What I am basically trying to say is
Array vs linked list fixed size more memory for pointers yes I understand
But when do you use double link list to implement a circular queue over a single link list and vice versa??
A doubly linked list requires extra storage and CPU work to maintain the back links, when compared to a singly linked list. A singly linked list requires extra storage and CPU to maintain the forward links, when compared to an array. The numerous small objects for a linked list requires extra storage (each object has some storage overhead) and garbage collector work, when compared to an array. So using an array has superior performance.
However, using an array requires some extra logic, which can be tricky for a beginner, to make a linear array behave like a circular structure. So you might want to write an inefficient linked list implementation first, then modify that to be efficient.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm in need of some guidance on the following exercise.
I have a custom list in which it has cities, each city knows to which city it connects this is the route, the route has a time which it take you to get between cities. What im asked to do is: given a city and sometime X:
public IList citiesWithRange(City c, int timeMinutes);
I should return to all the cities I can reach within that time frame. IList is a list made by me. Any help would be appreciated.
Thanks.
What you need is an implementation of an algorithm which returns the paths between nodes in a graph.
Take a look at Dijkstra's algorithm here:
https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
This can be helpful too:
https://www.ics.uci.edu/~eppstein/161/960208.html
You can attempt to solve this problem with a few easier steps in your recursive function
This function should be called on the starting city and be given a stack to add valid cities too, as well as an amount of time
Exit if time remaining is negative
Log current city to a stack
Loop through all connecting cities, calling the recursive function on each of them with the stack and time remaining minus travel time
At the end of this, the stack will contain all valid cities and may have duplicates
Remove the duplicates and you have all of the possible cities you can travel to
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
For practice I want to make my own lists and maps (like ArrayList, HashMap, HashSet etc.).
My goal is to have it as small and flexible as possible while still maintaining good performance. (long road...)
I have some questions:
1)
Unlike the sun, I don't have to take backwards compatibility into account.
So the first thing I wonder, is there any good reason to keep add and put?
Why not just one?
If I would name put > add would this give problems / complexity / unclearness down the road?
2)
Are there any languages known to have really good data structures? (For example, they could be really smart to avoid a concurrency exception).
3)
As last more a request then a question, if you have any tips our vision of how things could be done different then please post them.
There is no duplicated methods, Collection's have add method that returns a boolean, Map's have put method that returns type associated to Map.
There are plenty of examples of data structure, the point is, ¿what you need your data stucture do best? Avoid concurrency? sort? be fast? store securely?
The examples you need are directly in Java source code:
SOURCES
List
ArrayList
HashMap
and so on....
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
Hie guys.
I am very new to stack exchange and I am currently doing a research on graph theory.
The set of questions I'm going to ask are very introductory since I'm a beginner level programmer (not acquainted with hashing, buckets, vectors etc data structure wise).
My idea is to take in a dataset of the form (timestamp t, node i, node j) which says that there is an edge between i and j at time t. The idea is to search the neighborhood set of each nodes and hash them. If their "vectors" (I don't understand what that is) hash into the same bucket - they are candidates for cluster formation.
But he problem is I want to do experiments and try to run it. But have no idea how do I implement a hash function, and then bucket them together.
I'm not saying help me out with the code. But a pointer (pseudo code) would be very helpful. Like telling me to initialize a hash table etc etc
A hash code is an integer which is calculated from the properties of whatever it is you want to hash. That number is then used as an index into an array.
In this case it seems that you want to use the N dimensions of your vector to calculate this hash code. It's up to you to write a function that calculates that hash codes in a way that vectors that should be clustered all get the same hash code.
Language specific details about hash tables in Java or Python is very easy to find with a web search.