Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

implement a Queue using a BST

How do i implement a Queue using a BST.

Is this the way to do it, keep on inserting the nodes in the tree while maintaining a count value associated with each and every node,but while deletion BST should work like queue(FIFO) so start deleting from BST with the node having lowest count value in the tree.

Did i get the question and solution right? If not,then please explain me the question.

like image 299
Sumit Kumar Saha Avatar asked Oct 17 '25 02:10

Sumit Kumar Saha


1 Answers

A BST is really an inappropriate data structure to use to back a queue. You really ought to use a linked list instead, because it would be way faster, less complicated, and plain old better.

However, if you insist on using a BST...

You would use the BST as a priority queue, and define a wrapper type that also holds a 'queue index', which is what the items would be sorted by. You would have to define the comparison to take into account the current queue index though, because otherwise you could only ever add as many items as the difference between the highest and lowest values of your index type.

like image 143
AJMansfield Avatar answered Oct 18 '25 18:10

AJMansfield



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!