What is the best data structure (container) for fast element insertion/deletion by index?

You could get O(log n) performance using a binary tree with a node structure like this: struct Node T value Node left int left_count Node right int right_count end The left_count value would hold the number of nodes in the left branch of the tree, and similarly for the right side. Lookup would be performed by starting at the top of the tree and traversing downward based on comparing the desired index value with the left and right counts. Insertion and deletion would be performed using the normal binary tree algorithms, with appropriate adjustment to the count values.

More consistent performance can be achieved by requiring that the binary tree is balanced This kind of tree probably has a name; more information appreciated!

You could get O(log n) performance using a binary tree with a node structure like this: struct Node T value Node left int left_count Node right int right_count end The left_count value would hold the number of nodes in the left branch of the tree, and similarly for the right side. Lookup would be performed by starting at the top of the tree and traversing downward based on comparing the desired index value with the left and right counts. Insertion and deletion would be performed using the normal binary tree algorithms, with appropriate adjustment to the count values.

More consistent performance can be achieved by requiring that the binary tree is balanced. This kind of tree probably has a name; more information appreciated!

I guess you mean Red-black tree – nnd Jul 23 '09 at 22:46 A red-black tree doesn't contain the left and right subtree counts. – Greg Hewgill Jul 23 '09 at 22:55.

Depends on whether or not you're talking about something in memory or on disk. Usually, on disk it is some variant of a B-tree, and usually, in memory, it is generally a linked list (if you know what node you need to insert at).

Linked list is no good since I don't know the node (there is "access by index" requirement which takes O(n) for a list) – nnd Jul 24 '09 at 9:18.

Try a hash table that dynamically resizes at fixed intervals. Assuming a pretty good uniform distribution, you should have basically constant time O(1) access time. cs.cornell.edu/courses/cs312/2006fa/lect... That link seems to give a good explanation.

I've updated my answer – I think this is a good solution. – Isaac Hodes Jul 23 '09 at 22:50.

The best data structure for fast element insertion is (without quotes):.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions