static int count_below(tree_node *node) { int nleft, nright; if (node == NULL) return 0; nleft = count_below(node->left); nright = count_below(node->right); node->balance = (nleft > nright)? 1 : ((nright > nleft)? 2 : 0); return 1 + ((nleft > nright)? nleft : nright); }
static BOOL read_nonrecipients_tree(tree_node **connect, FILE *f, uschar *buffer, int buffer_size) { tree_node *node; int n = Ustrlen(buffer); BOOL right = buffer[1] == 'Y'; if (n < 5) return FALSE; /* malformed line */ buffer[n-1] = 0; /* Remove \n */ node = store_get(sizeof(tree_node) + n - 3); *connect = node; Ustrcpy(node->name, buffer + 3); node->data.ptr = NULL; if (buffer[0] == 'Y') { if (Ufgets(buffer, buffer_size, f) == NULL || !read_nonrecipients_tree(&node->left, f, buffer, buffer_size)) return FALSE; } else node->left = NULL; if (right) { if (Ufgets(buffer, buffer_size, f) == NULL || !read_nonrecipients_tree(&node->right, f, buffer, buffer_size)) return FALSE; } else node->right = NULL; (void) count_below(*connect); return TRUE; }
/* *Perform a binary search for the threshold x, such that <= count elements are less *x */ double find_threshold(int count, double t[256][256], int i_lo , int i_hi, int j_lo, int j_hi){ int i, j; double min = 1E6, max = 0; for(i = i_lo; i <= i_hi; i++) for(j = j_lo; j <= j_hi; j++){ if(t[i][j] < min) min = t[i][j]; if(t[i][j] > max) max = t[i][j]; } double l = min - 1; double r = max + 1; while(l < r - 1E-5){ double x = (l+r) / 2; int d = count_below(x, t, i_lo, i_hi, j_lo, j_hi); if(d == count) return x; if(d < count) l = x; else r = x; } printf("Problem in \"Find Threshold\"\n"); return l; }