# ifdef unix
# define cfree free
calloc(m,n)
{
int storep;
storep = alloc(m*n);
if (storep == -1)
	{write(2, "memory space exceeded\n",22); exit();}
return(storep);
}
# endif
aabalance(ru,s,len,ex)
	char *s;
{
struct node *p, *op, **pp, *r, *st;
	char *cp;
	pp = &(aaroot[ru]);
 while (*pp)
	{
	p  = *pp;
	switch (aacomp(s, p->aakey))
		{
		case '>':
			pp= &(p->rp);
			break;
		case '<':
			pp= &(p->lp);
			break;
		case '=':
			if (ex >= 0)
				return(p->aadata);
			cfree (p->aadata, len, 1);
			cfree(p->aakey, aaclen(p->aakey), 1);
			if (p->rp == 0)
				*pp = p->lp;
			else
			{
			r = p->rp;
			if (r->lp == 0)
				{
				r->lp = p->lp;
				*pp = r;
				}
			else
			{
			st = r->lp;
			while (st->lp != 0)
				{
				r = st;
				st = r->lp;
				}
			st->lp = p->lp;
			r->lp = st->rp;
			st->rp = p->rp;
			*pp = st;
			}
			}
			cfree (p, sizeof(**aaroot), 1);
			return(0);
		}
	op=p;
	}
/* not there */
if (ex != 0) return(0);
p = *pp= calloc(1, sizeof(**aaroot));
p->aakey = calloc(aaclen(s),1);
cp = p->aakey;
while (*cp++ = *s++);
p->aadata = calloc(len,1);
cp = p->aadata;
while (len--)
  *cp++ = 0;
p->rp = p->lp = 0;
return(p->aadata);
}
aaclen(s)
	char *s;
{
int aak;
	for(aak=1; *s++; aak++);
return(aak);
}
aacomp(s,t)
	char *s, *t;
{
	int c,d;
while ( (c= *s++) == (d= *t++) )
	if (c== 0)
		return('=');
return(c>d ? '>' : '<');
}
btod(n, s)
	char *s;
{
	int digs[15], *dpt;
dpt = digs;
if (n >= 0)
	n = -n;
else
	*s++ = '-';
for (; n != 0; n = n/10)
	*dpt++ = n%10;
if (dpt == digs)
	*dpt++ = 0;
while (dpt != digs)
	*s++ = '0' - *--dpt;
*s++ = '|';
return(s);
}
