I am using Java's Linkedlist
in my project. I have to build a delete
function that removes an element with a specified unique id
(id
is a filed in my class) in the Linkedlist
. As per the Java official document, were I to use LinkedList.remove
, the runtime would be O(n)
as the process happens in two steps, the first of which is a linear search with a runtime of O(n)
followed by the actual delete which takes O(1)
.
In an attempt to speed things up, I wanted to use a binary tree for lookup, where each node in the tree is (id, reference to the node in the linkedlist)
. I am not exactly sure how to implement this in Java. In C/C++, one could just store a pointer as reference to the node in the linkedlist
.
==
If you are wondering why I have to use LinkedList
, it's because I am building an order-matching engine for exchanges. LinkedList
offers superior runtime as far as insert
is concerned. I am also using insertion sort to keep prices in the orderbook sorted. Priority queue does not suit my needs because I have to show the sorted order book in real time.