Best-fit bin packing in O(n log n) time The search tree will contain one element for each bin which is in use and has non-zero available capacity Notice! It is possible that for two or more bins to have the same available capacity
Best-fit bin packing in O(n log n) time Example Suppose that when object i is to be packed, there are nine bins (a through i) in use. Available capacity of these bins are 1, 3, 12, 6, 8, 1, 20, 6 and 5, respectively. The nine bins may be stored in a binary tree with duplicates, using as key the available capacity of a bin.
Best-fit bin packing in O(n log n) time A possible binary search tree. h 6 b c 3 12 a i g d 1 5 6 20 8 e f 1
Best-fit bin packing in O(n log n) time Object I that is to be packed requires s[i] = 4. h Root h fits, proceeds to the left subtree. 6 b c 3 12 a i g d 1 5 6 20 8 e f 1
Best-fit bin packing in O(n log n) time Object I that is to be packed requires s[i] = 4. h 6 b c 3 12 a i g d 1 5 6 20 8 e f 1
Best-fit bin packing in O(n log n) time Object I that is to be packed requires s[i] = 4. h b fails, proceeds to the right subtree of b. 6 b c 3 12 a i g d 1 5 6 20 8 e f 1
Best-fit bin packing in O(n log n) time Object I that is to be packed requires s[i] = 4. h 6 b c 3 12 a i g d 1 5 6 20 8 e f 1
Best-fit bin packing in O(n log n) time Object I that is to be packed requires s[i] = 4. i fits, proceeds to the left subtree of i. h 6 b c 3 12 a i g d 1 5 6 20 8 e f 1 i doesn’t have any left child, it is the best candidate.
Best-fit bin packing in O(n log n) time Another example, object I that is to be packed requires s[i] = 7. h fails, proceeds to the right subtree of h. h 6 b c 3 12 a i g d 1 5 6 20 8 e f 1
Best-fit bin packing in O(n log n) time Another example, object I that is to be packed requires s[i] = 7. h 6 b c 3 12 a i g d 1 5 6 20 8 e f 1
Best-fit bin packing in O(n log n) time Another example, object I that is to be packed requires s[i] = 7. c fits, proceeds to the left subtree of c. h 6 b c 3 12 a i g d 1 5 6 20 f e 1 8
Best-fit bin packing in O(n log n) time Another example, object I that is to be packed requires s[i] = 7. h 6 b c 3 12 a i g d 1 5 6 20 f e 1 8
Best-fit bin packing in O(n log n) time Another example, object I that is to be packed requires s[i] = 7. d fails, proceeds to the right subtree of d. h 6 b c 3 12 a i g d 1 5 6 20 f e 1 8
Best-fit bin packing in O(n log n) time Another example, object I that is to be packed requires s[i] = 7. h 6 b c 3 12 a i g d 1 5 6 20 f e 1 8
Best-fit bin packing in O(n log n) time Another example, object I that is to be packed requires s[i] = 7. e fits, proceeds to the left subtree of e. h 6 b c 3 12 a i g d 1 5 6 20 f e 1 e has no left child, so e is the best candidate. 8
Best-fit bin packing in O(n log n) time When we find the best bin, we can delete it from the search tree, reduce its capacity by s[i], and reinsert it (unless its remaining capacity is zero). If we do not find a bin with enough capacity, we can start a new bin.