algorithm - Search, Insert, Delete and DeleteLast operations in logN -
what can best suited data structure supports following operations in o(log n) time:
search(x) finds element key x insert(x) insert element key x delete(x) delete element key x deletelast() removes inserted element
i know binary search tree can handle first 3 operations pretty good. how handle fourth query. if bst not solution tell can best suited data structure handling 4 queries.
credit @thomasjungblut bringing solution up.
first, build bst hold information need in leaves of tree.
in self solves search, insert & delete requirements.
address "delete inserted element" requirement add structure of each leaf prev
& next
fields, this:
struct leaf { int key; info_struct info; }
becomes this:
struct leaf { int key; info_struct info; leaf *prev; leaf *next; }
and except holding root of bst, hold leaf *last
. every time new element added, point 1 before , last
updated.
note: addition helps finding requested item, deletion takes log(n)
.
edited @alexeishestakov
Comments
Post a Comment