algorithm - Find n largest nodes in an arbitrary tree -


given arbitrary tree (not binary tree), each node labeled integer. how can find n largest nodes in tree? e.g. if tree contains {43, 253, 48, 62, 91, 641}, , asked 3 largest nodes, algorithm should return <641, 253, 91>.

all c++ (or language) standard library functions/data structures allowed. allowed add fields nodes, long constant space usage. like, can add field each node let point largest child, cannot have arraylist store of children in sorted order.

as new programmer, have spent days on question. simple graph search algorithm (bfs, dfs) work , easy implement, not fast enough because doing exhaustive search on entire tree.

can please me find correct , fast(er) solution problem?

as new programmer, have spent days on question. simple graph search algorithm (bfs, dfs) work , easy implement, not fast enough because doing exhaustive search on entire tree.

since tree not binary tree, examining node not yield additional information child nodes. therefore, not possible implement algorithm produces k highest values without exhaustive search of entire tree. in other words, don't better performance you'd unordered array of arbitrary values.

to k values in o(n * log k) time maintain priority queue of k elements traverse arbitrary tree.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -