Esempio n. 1
0
void TreePrint (Node_t * node, FILE * out)
{
    assert (TreeIsOk (node));

    fprintf (out, "(");
    if (node->left)
        TreePrint (node->left, out);
    fprintf (out, "%s", node->data);
    if (node->right)
        TreePrint (node->right, out);
    fprintf (out, ")");
}
Esempio n. 2
0
void TreePrint(Node *node, int level) {
    if (!node)
        return;

    TreePrint(node->left, level+1);
    printf("Node: level %d\n", level);
    printf("Index: %d\n", node->index);
    printf("Threshold: %f\n", node->threshold);
    printf("Class: %f", node->label);
    printf("\n");
    TreePrint(node->right, level+1);
    
    return;
}
Esempio n. 3
0
int main ()
{
    SetConsoleOutputCP(1251);
    SetConsoleCP(1251);
    const char dumpFilename[] = "dump";
    const char saveFilename[] = "save";

//    Node_t * nd = newNode ("aha",  newNode ("столовка", NULL, newNode ("ops", NULL, NULL)), NULL);
    Node_t * nd = TreeLoad (saveFilename);

    QUIZ (nd);

//    TreePrint  (nd, stdout);

    TreeDump (nd, dumpFilename);

    FILE * out = fopen (saveFilename, "w");
    assert (out);
    TreePrint (nd, out);
    fclose (out);

    TreeDel (&nd);

    return 0;

}
Esempio n. 4
0
File: test.c Progetto: JulioC/UFABC
int main(void) {
  char inorder[32] = "ZVCDJNGHI";
  char postorder[32] = "ZCJDVHIGN";
  char preorder[32] = {0};
  int written;
  Tree tree = TreeBuild(inorder, postorder, Tree_postOrder, strlen(inorder));
  
  printf("Arvore Resultante\n");
  TreePrint(tree);
  
  printf("\n");

  printf("ABB: %d\n", TreeIsBST(tree));
  
  printf("\n");
  
  written = TreeTraversal(tree, Tree_preOrder, preorder, 32);
  printf("Pre-ordem: %s\n", preorder);
  insertionSort(preorder, written);
  printf("Ordenado com insertionSort: %s\n", preorder);
  
  printf("\n");
  
  return 0;
}
Esempio n. 5
0
int main(int argc, const char * argv[]) {
    TreeNode *root1 = TreeCreate(2,1,2);
//    IntVector2DPrint(pathSum(root1, 1));
    TreeNode *root2 = TreeCreate(13, 5,4,8,11,INT_MAX,13,4,7,2,INT_MAX,INT_MAX,5,1);
    TreePrint(root2);
    IntVector2DPrint(pathSum(root2, 22));
    return 0;
}
Esempio n. 6
0
main ()
{
    tree_sTable	*tp;
    sNode		*np;
    int		key;
    int		i;
    char		s[256];
    char		*cp;
    char		c;

    tp = tree_CreateTable(sizeof(int), offsetof(sNode, key), sizeof(sNode), 100, tree_eComp_int32, NULL);

    for (i = 0; i < 1000; i += 10) {
        tree_Insert(tp, &i);
    }

    for ( ;;) {
        printf("Command: ");
        cp = gets(s);
        c = s[0];
        if (cp == NULL || c == '\0') {
            printf("\nGoodbye\n");
            return;
        }
        switch (c) {
        case 'i':
        case 'I':
            printf("Insert, Key: ");
            gets(s);
            key = atoi(s);
            if (tree_Find(tp, &key) == NULL) {
                tree_Insert(tp, &key);
            } else
                printf("\nKey allready exists!\n");
            break;
        case 'd':
        case 'D':
            printf("Delete, Key: ");
            gets(s);
            key = atoi(s);
            if ((np = tree_Find(tp, &key)) != NULL) {
                tree_Remove(tp, &key);
            } else
                printf("\nKey does not exist!\n");
            break;
        case 'f':
        case 'F':
            printf("Find, Key: ");
            gets(s);
            key = atoi(s);
            if ((np = tree_Find(tp, &key)) == NULL) {
                printf("\nKey does not exist!\n");
            } else
                printf("\nKey exists! %d\n", np->key);
            break;
        case 's':
        case 'S':
            printf("Find successor, Key: ");
            gets(s);
            key = atoi(s);
            if ((np = tree_FindSuccessor(tp, &key)) == NULL) {
                printf("\nKey does not exist!\n");
            } else
                printf("\nKey exists! %d\n", np->key);
            break;
        case 'p':
        case 'P':
            printf("Find predecessor, Key: ");
            gets(s);
            key = atoi(s);
            if ((np = tree_FindPredecessor(tp, &key)) == NULL) {
                printf("\nKey does not exist!\n");
            } else
                printf("\nKey exists! %d\n", np->key);
            break;
#if 0
        case 't':
        case 'T':
            printf("Start: ");
            gets(s);
            start = atoi(s);
            printf("Stop: ");
            gets(s);
            stop = atoi(s);
            printf("Order: ");
            gets(s);
            c = s[0];
            switch (c) {
            case 's':
            case 'S':
                printf("\navl-tree\n");
                i = start;
                j = stop;
                sts = sys$getjpiw(0, &pid, 0,item_list, 0, 0, 0);
                cpu = cputim;
                page = pageflts;

                for (; i <= j; i++) {
                    if (TreeSearch(tp,i) == tp->Null) {
                        np = TreeAlloc(tp, i);
                        TreeInsert(tp, np);
                    }
                }

                sts = sys$getjpiw(0, &pid, 0,item_list, 0, 0, 0);
                printf("Cputim: %d, Pageflts: %d\n", cputim - cpu, pageflts - page);

                i = start;
                j = stop;
                printf("\nlib$tree\n");
                sts = sys$getjpiw(0, &pid, 0,item_list, 0, 0, 0);
                cpu = cputim;
                page = pageflts;

                for (; i <= j; i++) {
                    sts = lib$lookup_tree(&ltp, i, Compare, &lnp);
                    if (!(sts & 1)) {
                        lib$insert_tree(&ltp, i, &0, Compare, Alloc, &lnp, 0);
                    }
                }

                sts = sys$getjpiw(0, &pid, 0,item_list, 0, 0, 0);
                printf("Cputim: %d, Pageflts: %d\n", cputim - cpu, pageflts - page);
                break;
            case 'd':
            case 'D':
                i = start;
                j = stop;
                sts = sys$getjpiw(0, &pid, 0,item_list, 0, 0, 0);
                cpu = cputim;
                page = pageflts;

                for (; i <= j; i++) {
                    if ((np = TreeSearch(tp,i)) != tp->Null) {
                        TreeDelete(tp, np);
                    } else {
                        printf("Could not find %d\n", i);
                    }
                }

                sts = sys$getjpiw(0, &pid, 0,item_list, 0, 0, 0);
                printf("Cputim: %d, Pageflts: %d\n", cputim - cpu, pageflts - page);
                break;
            case 'f':
            case 'F':
                printf("\navl-tree\n");
                i = start;
                j = stop;
                sts = sys$getjpiw(0, &pid, 0,item_list, 0, 0, 0);
                cpu = cputim;
                page = pageflts;

                for (; i <= j; i++) {
                    if ((np = TreeSearch(tp,i)) != tp->Null) {
                    } else {
                        printf("Could not find %d\n", i);
                    }
                }

                sts = sys$getjpiw(0, &pid, 0,item_list, 0, 0, 0);
                printf("Cputim: %d, Pageflts: %d\n", cputim - cpu, pageflts - page);

                i = start;
                j = stop;
                printf("\nlib$tree\n");
                sts = sys$getjpiw(0, &pid, 0,item_list, 0, 0, 0);
                cpu = cputim;
                page = pageflts;

                for (; i <= j; i++) {
                    sts = lib$lookup_tree(&ltp, i, Compare, &lnp);
                }

                sts = sys$getjpiw(0, &pid, 0,item_list, 0, 0, 0);
                printf("Cputim: %d, Pageflts: %d\n", cputim - cpu, pageflts - page);
                break;
            case 'b':
            case 'B':
                i = start;
                j = stop;
                sts = sys$getjpiw(0, &pid, 0,item_list, 0, 0, 0);
                cpu = cputim;
                page = pageflts;

                for (; i <= j; j--) {
                    if (TreeSearch(tp,j) == tp->Null) {
                        np = TreeAlloc(tp, j);
                        TreeInsert(tp, np);
                    }
                }

                sts = sys$getjpiw(0, &pid, 0,item_list, 0, 0, 0);
                printf("Cputim: %d, Pageflts: %d\n", cputim - cpu, pageflts - page);
                break;
            case 'r':
            case 'R':
                i = start;
                j = stop;
                sts = sys$getjpiw(0, &pid, 0,item_list, 0, 0, 0);
                cpu = cputim;
                page = pageflts;

                for (; i <= j; i++) {
                    k = 65535 & rand();
                    if (TreeSearch(tp,k) == tp->Null) {
                        np = TreeAlloc(tp, k);
                        TreeInsert(tp, np);
                    }
                }

                sts = sys$getjpiw(0, &pid, 0,item_list, 0, 0, 0);
                printf("Cputim: %d, Pageflts: %d\n", cputim - cpu, pageflts - page);
                break;
            default:
                printf("Illegal order!\n");
                break;
            }
            break;
        case 'p':
        case 'P':
            tp->ErrorCount = 0;
            tp->HZCount = 0;
            tp->HNCount = 0;
            tp->HPCount = 0;
            maxlevel = 0;
            count = 0;
            hight = 0;
            TreePrint(tp, tp->Root, NULL, NULL,0);
            TreeCheck (tp, tp->Root, &count, &maxlevel, &hight, 0);
            printf("Hight: %d\n", hight);
#if 0
            TreePrintInorder(tp, tp->Root, 0);
#endif
            sp = TreeMinimum(tp, tp->Root);
            ep = TreeMaximum(tp, tp->Root);
            op = sp;
            for (np = TreeSuccessor(tp, op); op != ep; np = TreeSuccessor(tp, np)) {
#if 0
                printf("Key: %d\n", op->Key);
#endif
                if (op->Key >= np->Key)
                    tp->ErrorCount++;
                op = np;
            }
            printf("Hight.......: %d\n", hight);
            printf("Insert......: %d\n", tp->Insert);
            printf("Search......: %d\n", tp->Search);
            printf("Delete......: %d\n", tp->Delete);
            printf("NodeCount...: %d\n", tp->NodeCount);
            printf("FreeCount...: %d\n", tp->FreeCount);
            printf("MaxDepth....: %d\n", tp->MaxDepth);
            printf("HZCount.....: %d\n", tp->HZCount);
            printf("HNCount.....: %d\n", tp->HNCount);
            printf("HPCount.....: %d\n", tp->HPCount);
            printf("LLCount.....: %d\n", tp->LLCount);
            printf("LRCount.....: %d\n", tp->LRCount);
            printf("RLCount.....: %d\n", tp->RLCount);
            printf("RRCount.....: %d\n", tp->RRCount);
            printf("AllocCount..: %d\n", tp->AllocCount);
            printf("MallocCount.: %d\n", tp->MallocCount);
            printf("ErrorCount..: %d\n", tp->ErrorCount);
            count = maxlevel = 0;
            Print(ltp, &count, &maxlevel, 0);
            printf("\nlib$tree\n");
            printf("Count.......: %d\n", count);
            printf("MaxDepth....: %d\n", maxlevel);
            break;
        case 'l':
        case 'L':
            TreePrintInorder(tp, tp->Root, 0);
            break;
#endif
        case 'q':
        case 'Q':
            printf("\nGoodbye\n");
            return;
            break;
        default:
            printf("Illegal command!\n");

            break;
        }
    }
}
Esempio n. 7
0
static void s_TEST_TreeOperations()
{
  
  typedef CTreeNode<int> TTree;
  
  TTree* orig = new TTree(0);
  TTree* orig10 = orig->AddNode(10);
  TTree* orig11 = orig->AddNode(11);
  orig10->AddNode(20);
  orig10->AddNode(21);
  orig11->AddNode(22);
  orig11->AddNode(23);
  
  TTree* corr = new TTree(11);
  corr->AddNode(22);
  corr->AddNode(23);
  TTree* corr0 = corr->AddNode(0);
  TTree* corr10 = corr0->AddNode(10);
  corr10->AddNode(20);
  corr10->AddNode(21);

  TreeReRoot(*orig10);

  cout << "After rerooting original tree by node 10, " << endl;
  cout << "the original tree and correct tree are now ";
  if(TreeCompare(*orig10, *corr, TestFunctor3)) {
    cout << "the same." << endl;
  }
  else {
    cout << "different." << endl;
  }
  cout << endl;

  TreePrint(cout, *orig10, s_IntToStr, false);
  TreePrint(cout, *corr, s_IntToStr, false);
  cout << endl;
  /*
  TTree* corr2 = new TTree(11);
  corr2->AddNode(22);
  corr2->AddNode(23);
  corr2->AddNode(0);
 
  TTree* t = orig->DetachNode(orig10);

  cout << "After removing node 10 from this tree, " << endl;
  cout << "the original tree and correct tree are now ";
  if(TreeCompare(*orig11, *corr2, TestFunctor3)) {
    cout << "the same." << endl;
  }
  else {
    cout << "different." << endl;
  }
  cout << endl;
   
  TreePrint(cout, *orig11, s_IntToStr, false);
  TreePrint(cout, *t, s_IntToStr, false);
  TreePrint(cout, *corr2, s_IntToStr, false);
  cout << endl;
  */
    delete orig->GetRoot();
    delete corr->GetRoot();
}
Esempio n. 8
0
static void s_TEST_IdTreeOperations()
{
    cout << "--------------------- s_TEST_IdTreeOperations " << endl;

    typedef CTreeNode<IdValue> TTree;

    TTree* tr = new TTree(0);

    TTree* tr10 = tr->AddNode(10);
    TTree* tr11 = tr->AddNode(11);
    TTree* tr110 = tr10->AddNode(110);
    TTree* tr1100 = tr110->AddNode(1100);

    TreePrint(cout, *tr, s_IdValueToStr);

    bm::bvector<> bv;
    TreeMakeSubNodesSet(*tr, bv.inserter());
    assert(bv.count() == 2);
    assert(bv[10]);
    assert(bv[11]);

    typedef vector<TTree*> TNodeList;
    TNodeList node_list;
    node_list.push_back(tr10);
    node_list.push_back(tr11);
    node_list.push_back(tr110);
    node_list.push_back(tr1100);

    TNodeList res_node_list;

    CTreeNonRedundantSet<TTree, bm::bvector<>, TNodeList> nr_func;
    nr_func(node_list, res_node_list);

    cout << "Non-redundant set:" << endl;
    ITERATE(TNodeList, it, res_node_list) {
        cout << (*it)->GetValue().GetId() << "; ";
    }
    cout << endl;
    assert(res_node_list.size() == 2);


    res_node_list.clear();
    node_list.clear();

    node_list.push_back(tr110);
    node_list.push_back(tr1100);

    CTreeMinimalSet<TTree, bm::bvector<>, TNodeList> min_func;
    min_func(node_list, res_node_list);

    
    cout << "Minimal set:" << endl;
    ITERATE(TNodeList, it, res_node_list) {
        cout << (*it)->GetValue().GetId() << "; ";
    }
    cout << endl;
    cout << "-----" << endl;
    assert(res_node_list.size() == 1);


    res_node_list.clear();
    node_list.clear();


    node_list.push_back(tr110);
    node_list.push_back(tr1100);
    node_list.push_back(tr11);

    min_func(node_list, res_node_list);

    
    cout << "Minimal set:" << endl;
    ITERATE(TNodeList, it, res_node_list) {
        cout << (*it)->GetValue().GetId() << "; ";
    }
    cout << endl;
    cout << "-----" << endl;
    assert(res_node_list.size() == 1);


    res_node_list.clear();
    node_list.clear();


    
    TNodeList node_list_a;
    TNodeList node_list_b;
    TNodeList node_list_c;

    node_list_a.push_back(tr10);
    node_list_a.push_back(tr11);
    node_list_a.push_back(tr110);
    node_list_a.push_back(tr1100);
    
    node_list_b.push_back(tr10);
    node_list_b.push_back(tr11);
    node_list_b.push_back(tr110);

    CTreeNodesAnd<TTree, bm::bvector<>, TNodeList> and_func;
    and_func(node_list_a, node_list_b, node_list_c);

    ITERATE(TNodeList, it, node_list_c) {
        cout << (*it)->GetValue().GetId() << "; ";
    }
    cout << endl;
    assert(node_list_c.size() == 3);


    node_list_c.clear();

    CTreeNodesOr<TTree, bm::bvector<>, TNodeList> or_func;
    or_func(node_list_a, node_list_b, node_list_c);

    ITERATE(TNodeList, it, node_list_c) {
        cout << (*it)->GetValue().GetId() << "; ";
    }
    cout << endl;
    assert(node_list_c.size() == 4);


    delete tr;

    cout << "--------------------- s_TEST_IdTreeOperations ok" << endl;
}
Esempio n. 9
0
static void s_TEST_Tree()
{
    typedef CTreeNode<int>  TTree;
    
    TTree* tr = new TTree(0);
    TTree* tr10 = tr->AddNode(10);
    tr->AddNode(11);
    tr10->AddNode(20);
    tr10->AddNode(21);
   
    TTree* sr = new TTree(0);
    sr->AddNode(10);
    sr->AddNode(11);

    TTree* ur = new TTree(0);
    ur->AddNode(10);
    ur->AddNode(11);
    ur->AddNode(20);
    ur->AddNode(21);
    

//    TreePrint(cout, *tr, (IntConvType) NStr::IntToString);
//    TreeReRoot(*tr10);
//    TreePrint(cout, *tr10, (IntConvType) NStr::IntToString);

    cout << "Testing Breadth First Traversal" << endl;
    TreeBreadthFirstTraverse(*tr, TestFunctor2);
    cout << endl;

    cout << "Testing Depth First Traversal" << endl;
    TreeDepthFirstTraverse(*tr, TestFunctor1);
    cout << endl;

    cout << "Testing Tree Comparison" << endl;
    cout << "tr and tr10 are ";
    if(!TreeCompare(*tr, *tr10, TestFunctor3)) cout << "not ";
    cout << "the same." << endl;
    cout << "tr and tr are ";
    if(!TreeCompare(*tr, *tr, TestFunctor3)) cout << "not ";
    cout << "the same." << endl;
    cout << "tr and sr are ";
    if(!TreeCompare(*tr, *sr, TestFunctor3)) cout << "not ";
    cout << "the same." << endl;
    cout << "tr and ur are ";
    if(!TreeCompare(*tr, *ur, TestFunctor3)) cout << "not ";
    cout << "the same." << endl;
    cout << "sr and ur are ";
    if(!TreeCompare(*sr, *ur, TestFunctor3)) cout << "not ";
    cout << "the same." << endl;
    cout << endl;

    {{
    unsigned int cnt;
    TTree::TNodeList_CI it = tr->SubNodeBegin();
    TTree::TNodeList_CI it_end = tr->SubNodeEnd();
    
    for (cnt = 0; it != it_end; ++it, ++cnt) {
        const TTree* t = *it;
        int v = t->GetValue();
        assert(v == 10 || v == 11);
    }
    assert(cnt == 2);
    }}
    
    {{
    TTree* tr2 = new TTree(*tr);
    unsigned int cnt;
    TTree::TNodeList_CI it = tr2->SubNodeBegin();
    TTree::TNodeList_CI it_end = tr2->SubNodeEnd();
    
    for (cnt = 0; it != it_end; ++it, ++cnt) {
        const TTree* t = *it;
        int v = t->GetValue();
        assert(v == 10 || v == 11);
    }
    assert(cnt == 2);
    delete tr2;
    }}
    
    
    {{
    TTree::TNodeList_I it = tr->SubNodeBegin();
    TTree::TNodeList_I it_end = tr->SubNodeEnd();
    
    for (; it != it_end; ++it) {
        TTree* t = *it;
        int v = t->GetValue();
        if (v == 10)
        {
            tr->RemoveNode(t);
            break;
        }
    }
    }}

    TreeDepthFirstTraverse(*tr, TestFunctor1);
    cout << endl;

    {{
    unsigned int cnt;
    TTree::TNodeList_CI it = tr->SubNodeBegin();
    TTree::TNodeList_CI it_end = tr->SubNodeEnd();
    
    for (cnt = 0; it != it_end; ++it, ++cnt) {
        const TTree* t = *it;
        int v = t->GetValue();
        assert(v == 11);
    }
    assert(cnt == 1);
    }}
    
    delete tr;

    TTree* str = tr = new TTree(0);
    
    //
    // 0 - 2 
    //       - 4
    //   - 3 
    //       - 5
    //       - 6
    //

    TTree* tr4 = tr->AddNode(2)->AddNode(4);
    tr = tr->AddNode(3);
    TTree* tr5 = tr->AddNode(5);
    TTree* tr6 = tr->AddNode(6);

    cout << "Test Tree: " << endl;

    TreeDepthFirstTraverse(*str, TestFunctor1);
    cout << endl;

    vector<const TTree*> trace_vec;
    TreeTraceToRoot(*tr6, trace_vec);

    assert(trace_vec.size() == 3);

    {{
    cout << "Trace to root: ";

    ITERATE(vector<const TTree*>, it, trace_vec) {
        cout << (*it)->GetValue() << "; ";
    }

    cout << endl;

    }}

    const TTree* parent_node = TreeFindCommonParent(*tr4, *tr6);

    assert(tr4->IsParent(*parent_node));
    assert(tr6->IsParent(*parent_node));
    assert(!tr4->IsParent(*tr6));


    assert(parent_node);

    cout << "parent: " << parent_node->GetValue() << endl;

    assert(parent_node->GetValue() == 0);

    parent_node = TreeFindCommonParent(*tr5, *tr6);
    assert(parent_node);
    assert(parent_node->GetValue() == 3);


    TreePrint(cout, *str, s_IntToStr);
    cout << endl;

    TreeReRoot(*tr5);
    TreePrint(cout, *tr5, s_IntToStr);

    tr5->MoveSubnodes(str);
    TreePrint(cout, *tr5, s_IntToStr);

    delete tr5;
    delete ur;
    delete sr;
}