node* insert(node* start,int n) { if(start==NULL) { start=(node*)malloc(sizeof(node)); start->left=NULL; start->right=NULL; start->num=n; } else if(start->num > n) { start->left=insert(start->left,n); if((he(start->left)) - (he(start->right))==2) { if(start->left->num>n) start=single_left(start); else start=double_left(start); } } else if(start->num < n) { start->right=insert(start->right,n); if((he(start->right)) - (he(start->left))==2) { if(start->right->num < n) start=single_right(start); else start=double_right(start); } } start->height=he(start); return start; }
static dbtype_t balance_right(pgctx_t *ctx, dbtype_t left, dbtype_t right, dbtype_t orig) { dbval_t *l = dbptr(ctx, left); uint64_t lln = bonsai_size(ctx, l->left); uint64_t lrn = bonsai_size(ctx, l->right); return (lrn < lln) ? single_right(ctx, left, right, orig) : double_right(ctx, left, right, orig) ; }
node* double_right(node* start) { start->right=single_left(start->left); return single_right(start); }