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 1 5 d g 20 6 e f 1 8 Best-fit bin packing in O(n log n) time • Object I that is to be packed requires s[i] = 4. • Root h fits, proceeds to the left subtree. h 6 b c 3 12 a i 1 5 d g 20 6 e f 1 8 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 1 5 d g 20 6 f e 1 8 Best-fit bin packing in O(n log n) time • Object I that is to be packed requires s[i] = 4. • b fails, proceeds to the right subtree of b. h 6 b c 3 12 a i 1 5 d g 20 6 f e 1 8 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 1 5 d g 20 6 f e 1 8 Best-fit bin packing in O(n log n) time • Object I that is to be packed requires s[i] = 4. h • i fits, proceeds to the 6 left subtree of i. b c 3 12 a i 1 5 d g 20 6 f e 1 • i doesn’t have any left child, it is the best candidate. 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 • h fails, proceeds to 6 the right subtree of h. b c 3 12 a i 1 5 d g 20 6 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 1 5 d g 20 6 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 • c fits, proceeds to the 6 left subtree of c. b c 3 12 a i 1 5 d g 20 6 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 1 5 d g 20 6 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 • d fails, proceeds to 6 the right subtree of d. b c 3 12 a i 1 5 d g 20 6 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 1 5 d g 20 6 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 • e fits, proceeds to the 6 left subtree of e. b c 3 12 a i 1 5 d g 20 6 f e 1 • e has no left child, so e is the 8 best candidate. 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.