示例#1
0
//��������������ʵ��
//������������ʾ�Ķ�����T
int BiSearchT::CreateSubTree(BiTree &t,int *all,int i)
{if((all[i]==0)||i>16)
  {t=NULL;
   return 1;}
 t=(BiNode *)malloc(sizeof(BiNode));
 if(t==NULL) return 0;
 t->data=all[i];
 CreateSubTree(t->l,all,2*i);
 CreateSubTree(t->r,all,2*i+1);
}
示例#2
0
void QuadTree::InsertSubTree(Sprite *obj){
	QuadPosition pos = SubTreeThatContains( obj->GetWorldPosition() );
	if(subtrees[pos]==NULL)
		CreateSubTree(pos);
	assert(subtrees[pos]!=NULL);
	subtrees[pos]->Insert(obj);
}
示例#3
0
void
TreeAddKline(struct ServerBan *kptr)

{
  char **hostv;
  char hostname[HOSTLEN + 1];
  int hostpieces;

  assert(kptr != NULL);

  /*
   * So we don't destroy kptr->hostname, use another buffer
   */
  memset(hostname, 0, sizeof(hostname));
  strncpy_irc(hostname, kptr->hostname, HOSTLEN);

  hostpieces = BreakupHost(hostname, &hostv);

  if (IsSortable(hostpieces, hostv))
    CreateSubTree(&KlineTree, NULL, kptr, hostpieces, hostv);
  else
  {
    fprintf(stderr, "HOSTNAME [%s] IS NOT SORTABLE\n",
      kptr->hostname);
    AddUnsortableKline(kptr);
  }

  MyFree(hostv);
} /* TreeAddKline() */
示例#4
0
static void
CreateSubTree(struct Level **level, struct Level *parent,
              void *typeptr, int hostc, char **hostv)

{
  struct Level *tmpnode;
  char *ch;

  if (hostc == 0)
    return;

  /*
   * First search the current level for an exact match
   * of hostv[hostc - 1]. - if found proceed to the next
   * level down from there.
   * We can't use IsOnLevel() here because we need an
   * exact match (strcasecmp) - we don't want stuff like
   * "c?m" and "com" being put in the same node.
   */
  for (tmpnode = *level; tmpnode; tmpnode = tmpnode->nextpiece)
  {
    if (!strcasecmp(tmpnode->name, hostv[hostc - 1]))
    {
      /*
       * We have found a matching host piece on this
       * level - no need to allocate a new level
       * structure. Now we will recursively call
       * CreateSubTree() again using tmpnode->nextlevel
       * as the level pointer, indicating that we want
       * to see if the next lower index of hostv
       * (hostv[hostc - 2]) is in the next level. If so,
       * again recursively call CreateSubTree(). Eventually
       * we might reach a level that does not contain
       * the corresponding index of hostv[]. When that
       * happens, the loop will fail and we will
       * drop below to allocate a new level structure.
       * If that does not happen, we have an exact duplicate
       * of a previous I/K line, in which case hostc will
       * be 1, and we add typeptr to the node's list of
       * pointers (the username may differ).
       */

      if (hostc == 1)
        LinkStructPointer(typeptr, &tmpnode);

      CreateSubTree(&tmpnode->nextlevel, tmpnode, typeptr, hostc - 1, hostv);

      return;
    }
  }

  /*
   * If we reach this point, one of two conditions must
   * be true.
   *  1) *level is NULL, which means we must initialize it
   *     and then add our hostv[] index to it.
   *  2) The host piece hostv[hostc - 1] was not found
   *     on this level - allocate a new structure for it.
   */

  tmpnode = (struct Level *) MyMalloc(sizeof(struct Level));
  memset(tmpnode, 0, sizeof(struct Level));

  tmpnode->name = MyStrdup(hostv[hostc - 1]);
  if (parent)
  {
    tmpnode->prevlevel = parent;
    ++parent->SubNodes;
    ++parent->UnsearchedSubNodes;
  }

  for (ch = tmpnode->name; *ch; ++ch)
  {
    if (IsWild(*ch))
    {
      tmpnode->flags |= LV_WILDCARD;
      break;
    }
  }

  if (hostc == 1)
  {
    /*
     * Since hostc is 1, this is the very last hostname piece
     * we need to add to the sub tree. This is the piece that
     * will contain a pointer to the corresponding Iline or
     * Kline structure, so SearchSubTree() will know when to
     * stop.
     * Now, it is quite possible that later on we will need to
     * add more host pieces past this current piece. For example,
     * suppose our tree looks like this after this call:
     *
     *     com
     *      |
     *    varner -> [struct Iline *iptr (for @varner.com)]
     *
     * Then, suppose later we wish to add an Iline for
     * @koruna.varner.com. Our tree should then look like this:
     *
     *     com
     *      |
     *    varner -> [struct Iline (for @varner.com)]
     *      |
     *    koruna -> [struct Iline (for @koruna.varner.com)]
     *
     * SearchSubTree() will then know that both levels are a
     * complete I/K line, and depending on how big the hostname
     * it is looking for, will know how deep to go.
     */

    LinkStructPointer(typeptr, &tmpnode);
  }

  if (*level == NULL)
  {
    /*
     * Set the level to our newly allocated structure
     */
    *level = tmpnode;
  }
  else
  {
    /*
     * The level already exists, and possibly has some
     * host pieces on it - add our new piece after
     * *level. For example, if the level originally looked
     * like:
     *
     *  ...
     *   |
     * "com" --> "net" --> "org" --> NULL
     *
     * It will now look like:
     *
     *  ...
     *   |
     * "com" --> tmpnode->name --> "net" --> "org" --> NULL
     */
    tmpnode->nextpiece = (*level)->nextpiece;
    (*level)->nextpiece = tmpnode;
  }

  /*
   * We've just added hostv[hostc - 1] to the correct level,
   * but as long as hostc != 0, there are more host pieces
   * to add. Recursively call CreateSubTree() until there
   * are no more pieces to add.
   */
  CreateSubTree(&tmpnode->nextlevel, tmpnode, typeptr, hostc - 1, hostv);
} /* CreateSubTree() */