Which data structure is better?

In designing algorithms, we will choose the data structure that most fulfil our needs.

Which data structure is the best when using it for both searching and insertion/deletion?

Sorted array Binary Search Tree Unsorted array Linked-list

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

3 solutions

Daniel Lim
Jul 31, 2014

Arrays

Searching

  • If it is unsorted, we need to iterate through the whole array

  • If sorted, we can use binary search

Insertion

  • If it is unsorted, we can easily append it to the back of our array

  • If it is sorted, that means we need to insert it in the right place by iterating through the whole array to find the suitable place for it. The worst case for it is when it should be inserted at the back of the array, so it is not really useful when doing insertion.

Deletion

  • If it is unsorted, we can change the value of the item we want to delete with the last item of the array then just remove the last element of the array, which will take constant time.

  • If it is sorted, we need to move all elements that are behind the element which we want to delete to replace the blank space after removing the element's value.

Linked List

Searching

  • We need to iterate through the whole link

Insertion/Deletion

  • As it is just a set of pointers pointing to their respective values, we can easily remove that pointer from its set.

Binary Search Tree

Searching

  • Searching is fast because we half the range until the desired value is found, just like binary search

Insertion

  • Fast, we can insert it to the most-bottom node which still doesn't have a child.

Deletion

  • Fast too, when deleting a node, just replace its child with its parent.

Therefore, Binary Search Tree is the best when designing an algorithm for searching and insertion/deletion.

Makes sense!!

VAIBHAV borale - 6 years, 10 months ago

Uhmm...Isn't this more of a theoretical problem than program writing..anyway good stuff i guess

Jayakumar Krishnan - 6 years, 10 months ago

Log in to reply

Before writing a program, you need to make sure that you use the best data structure to optimize your code's running time, isn't it?

You wouldn't want your code to run slowly when handling really big data. :D

Daniel Lim - 6 years, 10 months ago

Log in to reply

Yup..dat's rite...I am quite new to programming compared to you!!!

Jayakumar Krishnan - 6 years, 10 months ago
Arijit Banerjee
Aug 3, 2014

Binary Search Tree has the least complexity... O(log n) , so it is the best when using it for both searching and insertion/deletion

Aditya Gaikwad
Sep 19, 2014

Its because BST has complexity of log n where n is no. of elements while arrays has complexity of n in sorting and deleting and inserting any element in between array requires the shifting of all preceding ones

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...