multithreading - Any connection between mirroring a binary tree and blocking queue in Java -


i believe mirroring binary tree straight-forward in java like:

 http://stackoverflow.com/questions/4366251/mirror-image-of-a-binary-tree 

however, met interview question relates problem multithreading in java,i.e., blocking queue(java.util.concurrent)

to best of knowledge, don't see apparent connection between 2 concepts, give me hint might missing something? thanks!

in general

the recursive structure of tree mirroring reveals solution. first given root node, swap left child right child on main thread. apply mirror on left node , mirror on right node. mirroring left node independent of mirroring right node, can them on separate threads.

keep following rules in order avoid having many threads. current thread swaps left , right child of node operating on. delegates mirroring of new left child new thread, , proceeds handle mirroring of right node.

note each node encounter spawning new thread. if tree unbalanced , has left children, spawn o(n) threads n number of nodes in tree. however, can maintain thread pool once thread reaches leaf node returns pool. if tree balanced, every thread terminates in o(log n) steps, have on order of o(n / log n) threads (i think?).

use of blocking queue

you can use blocking queue in conjunction thread pool idea, placing left , right nodes once swapped queue, workers can take them out , process them, , subsequently return new left , right children of subnode queue thread handle. once leaf node hit, there no left , right children, nothing put in queue. when queue empty, mirroring complete.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -