Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I need to design a voting system on a high level that associates voters with their decision in sorted order by name.
I understand that I should implement a sorted map, and it seems like we need a map that performs best with random insertions. So I was wondering which of the above data structures would work best.
If you are sorting the objects based purely by name, I would say that a Binary Search Tree would work well.
If you are particularly worried about search time complexity, you can implement a balanced tree, such as an AVL tree or a splay tree. Doing this would get your search time complexity towards logarithmic, which is what you're after!
A heap or a BST. Trie linked list and array would have a higher search complexity.
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
As it's stated in wikipedia there are multiple algorithms for traversing a tree data structure. But some of the algorithms are kind of combinations of the others, like Bidirectional search which is almost useful for other graphs rather than trees. But with a tree we almost have no idea of the end of the tree and we can only start from the root or from its children.
In this case we might be able to incorporate the multiprocessing or multithreading in search process. But I couldn't find any comprehensive approach that's described this.
Now my question is that basically what's the most optimized way of traversing a tree when we don't have access to the whole data structure (to be able to index them, etc. like a file directory)?
The most optimized algorithm is usually the one optimized for specific usecase and platform.
It does not matter whether you do inorder, preorder or postorder. Or whether you do DFS or BFS.
What matters is:
How big is the tree? Does it fit into memory?
How deep is the tree? Can you use recursion, or do you have to use explicit separate stack?
How do you find children of the node. Do you have to access harddrive/network?
What do you want to do with the node after you find in traversal. If this operation is long enough, optimizing traversal is not worth it.
How do you share data between threads?
How are the nodes in the tree distributed? Does it resemble even distribution, or are there some very long and some very short branches?
How big are the node keys (this influences data locality and how much data you can fit into one L1/L2 cache line)?
Try in order traversing of Binary Search tree. The complexity of search is nlogn and traverse is n, which is considered to be the best.
Ex: http://www.geeksforgeeks.org/binary-search-tree-set-1-search-and-insertion/
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
I'm a mostly self-taught programmer, I'm in my freshman year of college going towards a BS in CompSci. Last year I would do some of the homework for the AP CompSci kids, and when they got to sorting algorithms, I understood what they did, but my question was what is a case where one is used? I know this may seem like a horrible, or ridiculous question, but other than a few cases I can think of, I don't understand when one would use a sorting algorithm. I understand that they are essential to know, and that they are foundational algorithms. But in the day to day, when are they used?
Sorting algorithm is an algorithm that arrange the list of elements in certain order. You can use such algorithms when you want the elements in some order.
For example:
Sorting strings on basis of lexicographical order. This makes several computation easier (like searching, insertion, deletion provided appropiate data structure is used)
Sorting integers as part of preprocessing of some algorithms. Suppose you have lot of queries in data base to find an integer, you will want to apply binary search. For it to be applicable, input must be sorted.
In many computational geometry algorithms (like convex hull), sorting the co-ordinates is the first step you do.
So, basically, if you want some ordering, you resort to sorting algorithms!
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
Do all algorithms that use the divide and conquer approach use recursive functions or not necessarily?
Binary search is an application of the D&C paradigm. (As follows: split in two halves and continue into the half that may contain the key.)
It can be implemented both recursively or non-recursively.
Recursion is handy when you need to keep both "halves" of a split and queue them for later processing. A common situation is called tail recursion, when you only queue one of the halves and process the other immediately. In binary search, you just drop one of the halves.
In a very broad sense, D&C is the father of all algorithms when stated as "break the problem into easier subproblems of the same kind". This definition also encompasses iterative solutions, often implemented without recursion.
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
How will you rebuild a given BST into AVL which contains exactly the same keys?
The algorithm running time should be O(n) and its allowed to use O(n) additional space. Any ideas?
The whole pseudo-code is not necessary, any idea or suggestion would be appreciated!
Thanks!
Extract all keys to sorted array (O(n) space) with suitable traversal method (O(n) time)
Build perfectly balanced tree from sorted array (O(n) time) (simultaneously filling AVL balance factors for all nodes)
I 've omitted the details for your own research
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 9 years ago.
Improve this question
Technically I know what is Fibonacci Heaps and B-Tree are. But I want to know the use of the Fibonacci Heaps and B-tree data structure. How much useful these data structure are and where we can find the real use of these data structure? Thanks.
B-Tree s are commonly used to store large sets of data which need to be accessed quickly and updated often.
Perhaps the most pervasive use is indexing tables in the commonly used relational databases.
I'm not sure about fibonacci heaps but a b-tree is often used in database indexing and is good for ranged queries. I will try to find something on fib heaps
Edit: it seems like fibonacci heaps are basically just more optimized heaps. Heaps are very good for finding min max and median elements. Heaps are also used to implement priority queues