Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Algorithm which finds biggest n nodes in a tree

And a standard heap selection algorithm won't work?

The basic algorithm is (assuming that k is the number of items you want to select)

create an empty min-heap
for each node (depth-first search)
    if heap.count < k
    else if node.Value < heap.Peek.Value()

When the for loop is done, your heap contains the k largest values. You can obtain them in ascending order with:

while heap.count > 0
    output (heap.RemoveSmallest().Value)

If you want them in ascending order, remove them from the heap as above into an array, and then reverse the array.

This algorithm is O(n log k), where n is the number of nodes in the tree, and k is the number of items you want.

Categories : Algorithm

Related to : Algorithm which finds biggest n nodes in a tree
Proving optimality for a new algorithm that finds minimum spanning tree
Yes, IMO the algorithm outputs a Minimum Spanning Tree. Informal Proof: At every iteration, we remove only that edge which is the most expensive edge on a cycle. Such an edge can never be included in a MST (by exchange argument). Thus we always exclude those edges which can never be a part of the MST. Also, the output of the algorithm is always a spanning tree because we are deleting edges only

Categories : Algorithm
google maps web finds an address/place, but neither places API nor geocoding API finds it
The "Oahu" is confusing the geocoder (and the places API). If I use "526+Kawailoa+Road,Kailua,HI+96734" with the geocoder, I get a result If I use that address with the places API, I also get a result

Categories : Google Maps
Pass a parameter from one PHP page to another - it always gets the biggest (MySQL)
AND `manufacturer ID` LIKE '%$by_manufacturer_id%' change this to AND `manufacturer ID` LIKE '$by_manufacturer_id%' I edited my answer according to your feedback. This will search for only starts with id. For example assume to ids 123456 and 999123456 if you search 123 it will return 123456 because, we remove % sign.

Categories : PHP
How to loop through XML nodes and child nodes using JavaScript?
Attaching the root of your loop on employee rather than name would be better for nested loops (which is what this solution will use): var employees = xmlDoc.getElementsByTagName("employee"); //outputs array of employees for (employeeIndex = 0; employeeIndex < employees.length; employeeIndex++) { var empDetails = employees[employeeIndex].children; //outputs array of details for (deta

Categories : Javascript
Python: can't merge sort two sorted lists in O(n) starting biggest first
You're not keeping 'x' in sync with the positions in the two lists. Which suggests that, with this code, you need two indices, one for each list. A good way to understand why your code isn't working is to grab a deck of cards, create two sorted mini-decks of, say, 10 cards, and walk through your algorithm. The old and new decks cover l1, l2, and new, so you'll need pencil and paper to track the

Categories : Python
Recently Add
Proving optimality for a new algorithm that finds minimum spanning tree
why this assembly piece of code do jmp forever
Find out if segment is fully inside of polygon
Algorithm for coloring a hexagon tile map with minimum distance (3) for reoccurring colors
Sort pairs to be more consecutive
To find three unique numbers whose number of occurrence is even
Dealing with duplication between unit and integration tests
reflection and symmetry in back tracking queens
Big O analysis for method with multiple parameters
Divide Huge Array of Numbers in Buckets
Algorithm to find adjacent cells in a matrix
Why this code gives WA for Petersen Graph(codechef)?
Complexity of this prime number search algorithm
How to detect if a file has changed?
Given string x,y and z. Determine if z is a shuffle
Basic decryption for simple encryption algorithm
An efficient way to assign user_ids to huge dataset under certain conditions
What's a more efficient implementation of this puzzle?
Generating prime numbers in poly-time
What if I do not use G transpose in calculating Strongly Connected Components?
Dividing an array into optimum no of equal sum sublists
Counting derangements
How to iterate through all cases when partitioning objects
Algorithm: How to find closest element, having coordinates and dimension
Developing player rankings with ELO
How to transform two set of discrete points ( vectors ) to help plotting them on a common scale
Heap Sort Space Complexity
complex root finding algorithm
Every possible combination algorithm
RSA Cryptosystem - Retrieve m
© Copyright 2017 Publishing Limited. All rights reserved.