Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to implement Tree : LinkedList - Array

I am preparing for an exam, one of the questions I came across is : what is the best way to implement tree, LinkedList or Array.

Most likely: - Array uses 1 address - LinkedList use two addresses.

Using LinkedList, we can insert the value we need (we manage perfectly the memory), but most likey use O(N) to access to this element, while in Array it O(1).

How should I answer this question ? Or should I just say that is subjective.

like image 374
user3378649 Avatar asked Mar 10 '14 00:03

user3378649


People also ask

Can I implement a linked list as an array?

both using an index of -1 as the end of the list. Then you would need multiple pointers (one for each list) to determine where you are in the list. Honestly, I'm not even sure I understand the question, but wanted to throw out ideas anyway. if I implement a linked list as an array will it lead to better utilization of cache lines ? Yes, it will.

How to implement stack using linked list in Java?

To implement stack using linked list, first we need Nodes which can be implemented using a structure or a class and each node consists of a variable to store the data and pointer pointing to the next node, these nodes are used by another class stack which is used to perform all stack operations.

Why do linked lists use more memory than linked lists?

Memory efficiency: For the same number of elements, linked lists use more memory as a reference to the next node is also stored along with the data.

What is a linked list?

To conclude, linked list are a really cool way to structure data. As previously mentioned, if you would like a to understand how linked list work or when to use them, check out my other blog here.


1 Answers

For a Binary Search Tree, the answer would definitely be an array ( at least hopefully an extendable array, like a vector<> so you aren't limited to a fixed size). I'll do an analysis of the common operations, assuming the tree is balanced.

Query

In a BST, nodes need to have pointers to left and right children and is also very common to have parent pointers. In an array implementation, the "pointers" can simply be integer indexes into the array ( this would mean the array would store Node objects). Thus looking up the parent and children of a node is constant since indexing into the array is constant. O(1). A linked list implementation would probably need to also store a reference to the position as to where their ancestors/children are, thus requiring an O(N) pass through the list to get the desired references.

Search

Starting at the root, array[0], searching would be an O(log N) operation. Searching would just call/get the info of the children per node, which is O(1) amount of work, roughly O(log N) times, thus O(log N) for search in an array.

A linked list would require an O(N) pass through the data to get the required left/right pointers, and can also be done in O(log N) steps, thus producing an O(n log n) search in linked-lists.

Insert

Arrays would be similar to search, except would require additional O(1) constant time for pointer assignments. So O(log N) insert.

Linked-lists would also be similar to their search routine, except with an additional O(n) time for adjusting the pointers, so O(n log n)

Delete

Arrays would also be similar to search, except you could take more than a single O(log n) factor to delete, since you have to traverse back up the tree, but it still is O(log n).

Linked lists would too have the O(n log n) plus more O(n log n) for traversing up. So O(n log n) for linked lists as well.

Conclusion

The answer should be fairly evident by now :) Plus with arrays you'll get the benefit of better caching than linked-lists. Plus, some derivatives of binary search trees, such as heaps (usually min-heaps/max-heaps) are commonly represented as arrays,

I hope this helps :)

like image 145
Alejandro Avatar answered Nov 15 '22 10:11

Alejandro