예제 #1
0
파일: bst.c 프로젝트: zhengguan/15122
/******** lookup related functions *****/
elem tree_lookup(tree* T, elem x, bst B)
{
  REQUIRES(is_tree(T, B));

  if (T == NULL) return NULL;
  int r = B->elem_compare(x, T->data);
  if (r == 0) {
    return T->data;
  } else if (r < 0) {
    return tree_lookup(T->left, x, B);
  } else {
    return tree_lookup(T->right, x, B);
  }
}
예제 #2
0
파일: bst.c 프로젝트: zhengguan/15122
elem bst_lookup(bst B, elem x)
{
  REQUIRES(is_bst(B) && x != NULL);

  elem r = tree_lookup(B->root, x, B);

  ENSURES(r == NULL || B->elem_compare(r, x) == 0);
  return r;
}
예제 #3
0
파일: tree.c 프로젝트: WhireCrow/templates
static int tree_lookup(node_t *node, char target)
{
    if (!node)
        return 1;

    if (node->data == target)
        return 0;
        
    return tree_lookup(target < node->data ? node->left : node->right, target);
}
예제 #4
0
/**
 * Constructor.
 *
 * Compressed suffix tree is a self-index so text is not needed after construction.
 * Text can be deleted during construction to save some construction space.
 *
 * Parameter <filename> is a prefix for filenames used for IO operation.
 * Filename suffixes are ".bwt", ".lcp" and ".bp".
 *
 * @param text a pointer to the text.
 * @param n number of symbols in the text.
 * @param deletetext delete text as soon as possible.
 * @param samplerate sample rate for Compressed suffix array.
 * @param action IO action for <filename>. Defaults to no operation.
 * @param filename Prefix for the filenames used.
 */
SSTree::SSTree(uchar *text, ulong n, bool deletetext, unsigned samplerate, io_action IOaction, const char *filename)
{
    #ifdef SSTREE_HEAPPROFILE
        ulong heapCon;
    #endif
    this->n = n;
    unsigned floorLog2n = Tools::FloorLog2(n);
    if (floorLog2n < 4)
        floorLog2n = 4;
    unsigned rmqSampleRate = floorLog2n / 2;
    if (samplerate != 0)
        floorLog2n = samplerate; // Samplerate override, affects only CSA

    #ifdef SSTREE_TIMER
    #ifdef SSTREE_HEAPPROFILE
        std::cout << "--> HeapProfiler: " << HeapProfiler::GetHeapConsumption() << ", " << HeapProfiler::GetMaxHeapConsumption() << std::endl;
        heapCon = HeapProfiler::GetHeapConsumption();
    #endif

        printf("Creating CSA with samplerate %u\n", floorLog2n);
        fflush(stdout);
        Tools::StartTimer();
    #endif
    if (IOaction == load_from && filename != 0)
        sa = new CSA(text, n, floorLog2n, (string(filename)+".csa").c_str());
    else if (IOaction == save_to && filename != 0)
        sa = new CSA(text, n, floorLog2n, 0, (string(filename)+".csa").c_str());
    else // No IO operation
        sa = new CSA(text, n, floorLog2n);

    #ifdef SSTREE_TIMER
        printf("CSA created in %.0f seconds.\n", Tools::GetTime());

    #ifdef SSTREE_HEAPPROFILE
        std::cout << "--> HeapProfiler: " << HeapProfiler::GetHeapConsumption() - heapCon << ", " << HeapProfiler::GetMaxHeapConsumption() << std::endl;
        heapCon = HeapProfiler::GetHeapConsumption();
    #endif

        printf("Creating CHgtArray\n");
        fflush(stdout);
        Tools::StartTimer();
    #endif
    if (IOaction == load_from && filename != 0)
        hgt = new CHgtArray(sa, (string(filename)+".lcp").c_str());
    else
        hgt = new CHgtArray(sa, text, n);

    if (IOaction == save_to && filename != 0)
        hgt->SaveToFile((string(filename)+".lcp").c_str());
    #ifdef SSTREE_TIMER
        printf("CHgtArray created in %.0f seconds.\n", Tools::GetTime());

    #ifdef SSTREE_HEAPPROFILE
        std::cout << "--> HeapProfiler: " << HeapProfiler::GetHeapConsumption() - heapCon << ", " << HeapProfiler::GetMaxHeapConsumption() << std::endl;
        heapCon = HeapProfiler::GetHeapConsumption();
    #endif

        printf("Creating parentheses sequence (LcpToParentheses)\n");
        fflush(stdout);
        Tools::StartTimer();
    #endif
    if (deletetext)
       delete [] text;

    #ifdef SSTREE_HEAPPROFILE
        heapCon = HeapProfiler::GetHeapConsumption();
    #endif

    ulong bitsInP;
    if (IOaction == load_from && filename != 0)
        P = LcpToParentheses::GetBalancedParentheses((string(filename)+".bp").c_str(), bitsInP);
    else
        P = LcpToParentheses::GetBalancedParentheses(hgt, n, bitsInP);

    if (IOaction == save_to && filename != 0)
        LcpToParentheses::SaveToFile((string(filename)+".bp").c_str(), P, bitsInP);
    #ifdef SSTREE_TIMER
        printf("Parentheses sequence created in %.0f seconds.\n", Tools::GetTime());

    #ifdef SSTREE_HEAPPROFILE
        std::cout << "--> HeapProfiler: " << HeapProfiler::GetHeapConsumption() - heapCon << ", " << HeapProfiler::GetMaxHeapConsumption() << std::endl;
        heapCon = HeapProfiler::GetHeapConsumption();
    #endif

        //printf("Creating CSA with sample rate %d\n", floorLog2n);
        //Tools::StartTimer();
    #endif
    //delete sa;
    //sa = new CSA(text, n, floorLog2n);
    //hgt->SetSA(sa);  // Update SA pointer
    #ifdef SSTREE_TIMER
        //printf("CSA created in %f seconds.\n", Tools::GetTime());

        printf("Creating BitRank\n");
        fflush(stdout);
        Tools::StartTimer();
    #endif
    br = new BitRank(P, bitsInP, false);
    #ifdef SSTREE_TIMER
        printf("BitRank created in %.0f seconds.\n", Tools::GetTime());

    #ifdef SSTREE_HEAPPROFILE
        std::cout << "--> HeapProfiler: " << HeapProfiler::GetHeapConsumption() - heapCon << ", " << HeapProfiler::GetMaxHeapConsumption() << std::endl;
        heapCon = HeapProfiler::GetHeapConsumption();
    #endif

        printf("Creating Parentheses\n");
        fflush(stdout);
        Tools::StartTimer();
    #endif
    Pr = new Parentheses(P, bitsInP, true, br);
    #ifdef SSTREE_TIMER
        printf("Parentheses created in %.0f seconds.\n", Tools::GetTime());

    #ifdef SSTREE_HEAPPROFILE
        std::cout << "--> HeapProfiler: " << HeapProfiler::GetHeapConsumption() - heapCon << ", " << HeapProfiler::GetMaxHeapConsumption() << std::endl;
        heapCon = HeapProfiler::GetHeapConsumption();
    #endif

        printf("Creating ReplacePatterns\n");
        fflush(stdout);
        Tools::StartTimer();
    #endif
    rpLeaf = new ReplacePattern(1, 8);
    rpSibling = new ReplacePattern(0, 8);
    #ifdef SSTREE_TIMER
        printf("ReplacePatterns created in %.0f seconds.\n", Tools::GetTime());

    #ifdef SSTREE_HEAPPROFILE
        std::cout << "--> HeapProfiler: " << HeapProfiler::GetHeapConsumption() - heapCon << ", " << HeapProfiler::GetMaxHeapConsumption() << std::endl;
        heapCon = HeapProfiler::GetHeapConsumption();
    #endif

        printf("Creating BitRanks\n");
        fflush(stdout);
        Tools::StartTimer();
    #endif
    brLeaf = new BitRank(P, bitsInP, false, rpLeaf);       //for ()
    brSibling = new BitRank(P, bitsInP, false, rpSibling); //for )(

    if (rmqSampleRate < 4)
        rmqSampleRate = 4;
    #ifdef SSTREE_TIMER
        printf("BitRanks created in %.0f seconds.\n", Tools::GetTime());
//         printf("<enter>\n");
//         std::cin.get();

    #ifdef SSTREE_HEAPPROFILE
        std::cout << "--> HeapProfiler: " << HeapProfiler::GetHeapConsumption() - heapCon << ", " << HeapProfiler::GetMaxHeapConsumption() << std::endl;
        heapCon = HeapProfiler::GetHeapConsumption();
    #endif

        printf("Creating CRMQ with sample rates %d, %d and %d\n", rmqSampleRate * rmqSampleRate * rmqSampleRate, rmqSampleRate * rmqSampleRate, rmqSampleRate);
        Tools::StartTimer();
        fflush(stdout);
    #endif
    rmq = new CRMQ(br, P, bitsInP, rmqSampleRate * rmqSampleRate * rmqSampleRate, rmqSampleRate * rmqSampleRate, rmqSampleRate);
    #ifdef SSTREE_TIMER
    #ifdef SSTREE_HEAPPROFILE
        std::cout << "--> HeapProfiler: " << HeapProfiler::GetHeapConsumption() - heapCon << ", " << HeapProfiler::GetMaxHeapConsumption() << std::endl;
    #endif
        printf("CRMQ created in %.0f seconds.\n", Tools::GetTime());
        fflush(stdout);
    #endif


    /*
     * adicionado por Jean para criar o vetor sa-1
     * preparação para o LCE. A idéia é trocar alguns processamentos O(logn) por O(1)
     */
    ulong len = numberofnodes(0);
    select_br =  new int[n+1];
    _findclose = new int[len * 4];
    //_depth =     new int[len * 4];
    _enclose  =  new int[len * 4];

    ulong x, w;
    for(int v = 0; v <= n; v++){
       x = sa->inverse(v);
       w = brLeaf->select(x + 1);
       select_br[v] = w;
       _findclose[w] = Pr->findclose(w);
    }
    tree_lookup(0, _enclose);
}
예제 #5
0
void SSTree::tree_lookup(ulong v, int *_enclose){
  if (v != 0) add_enclose(v, _enclose);
  if(!(isleaf(v))) tree_lookup(v + 1, _enclose);
  v = sibling(v);
  if (v != 0) tree_lookup(v, _enclose);
}