Return to Snippet

Revision: 38681
at January 6, 2011 05:59 by tomislav_matic


Initial Code
#include "a_zadatak.h"
#include <iostream>

using namespace std;

int main(void) {
    tree T;
    cout << "Test 1 (init funkcija)" << endl;
    InitT(1, &T);
    cout << "Test 1 zavrsen" << endl;


    cout << "Test 2 (label, init, root funkcije): ispisuje 1 ako je tocno." << endl;
    cout << LabelT(RootT(&T), &T) << endl;;
    cout << "Test 2 zavrsen" << endl;

    cout << "Test 3 (create ): bez ispisa" << endl;
    CreateT(2, RootT(&T), &T);
    CreateT(3, RootT(&T), &T);
    cout << "Test 3 zavrsen" << endl;

    cout << "Test 4 (create, first child, next sibling): ispisuje 2,3" << endl;
    cout << LabelT(FirstChildT(RootT(&T), &T), &T) << endl;
    cout << LabelT(NextSiblingT(FirstChildT(RootT(&T), &T), &T), &T) << endl;    
    cout << "Test 4 zavrsen" << endl;


    cout << "Test 5 (create): bez ispisa" << endl;
    CreateT(4, FirstChildT(RootT(&T), &T), &T);
    CreateT(5, FirstChildT(RootT(&T), &T), &T);
    cout << "Test 5 zavrsen" << endl;

    cout << "Test 6 (change label): ispisuje \"42\"" << endl;
    ChangeLabelT(42, FirstChildT(RootT(&T), &T), &T);
    cout << LabelT(FirstChildT(RootT(&T), &T), &T) << endl;
    cout << "Test 6 zavrsen" << endl;

    cout << "Test 7 (delete): bez ispisa" << endl;
    DeleteT(FirstChildT(RootT(&T), &T), &T);
    cout << "Test zavrsen" << endl;


    cout << "Test 8 (delete): ispisuje 3" << endl;
    cout << LabelT(FirstChildT(RootT(&T), &T) , &T);
    cout << endl;


    return 0;

}

 BIBLIOTEKA A

struct elem {
    int label;
    int child,sibling;
    bool empty;
};

struct tree {
    elem elements[10000];
    int first;
};

typedef int node;

node ParentT(node n, tree* T) {
    for (int i = 1; i < 10000; i++) {
        if (T->elements[i].child == n)
            return i;
        else {
            if (T->elements[i].sibling == n) 
                return ParentT(i, T);
        }
    }
    return 0;
}

node FirstChildT(node n, tree*T) {
    return (T->elements[n].child);
}

node NextSiblingT(node n, tree*T) {
    return (T->elements[n].sibling);
}

int LabelT(node n, tree*T) {
    return (T->elements[n].label);
}

node RootT(tree*T) {
    return (T->first);
}

void CreateT(int x, node n, tree*T) {
 
    int i;
    for (i = 1; i < 10000; i++) {
        
        if (T->elements[i].empty) {
            T->elements[i].empty = false;
            T->elements[i].label = x;
            break;
        }
    }
    
    node Parent = n;
    node sib;
    
    if (FirstChildT(Parent, T) == 0) {
        T->elements[Parent].child = i;
    }
    else {
        sib = FirstChildT(Parent, T);
        while(2 < 5) {
            if (NextSiblingT(sib, T) != 0) {
                sib = NextSiblingT(sib, T);
            }
            else {
                break;
            }
        }
        T->elements[sib].sibling = i;
    }
}

void ChangeLabelT(int x, node n, tree *T) {
    T->elements[n].label = x;
}


void rekurzivno_brisanje (node n, tree *T) {
    if (FirstChildT(n, T))
        rekurzivno_brisanje(FirstChildT(n, T), T);
    if (NextSiblingT(n, T))
        rekurzivno_brisanje(NextSiblingT(n, T), T);
    T->elements[n].empty = true;
}

void DeleteT(node n, tree *T) {
   
    rekurzivno_brisanje(FirstChildT(n, T), T);

    node Parent = ParentT(n, T);   
    node buraz;
   
    if (FirstChildT(Parent, T) == n) {
        T->elements[Parent].child = NextSiblingT(n, T);
    }
    
    else if (NextSiblingT(n, T)) {
        buraz = FirstChildT(Parent, T);
        while (2 > -2) {
            if (NextSiblingT(buraz, T)) {
                buraz = NextSiblingT(buraz, T);
            }
            else {
                break;
            }
        }
        T->elements[buraz].sibling = T->elements[n].sibling;
    }
    
    else {
        
        buraz = FirstChildT(Parent, T);
        while (2 > -2) {
            if (NextSiblingT(buraz, T)) {
                buraz = NextSiblingT(buraz, T);
            }
            else {
                break;
            }
        }
        
        T->elements[buraz].sibling = 0;
    }
}

void InitT(int x, tree *T) {
    for (int i = 2;  i < 10000; i++) {
        T->elements[i].empty = true;
        T->elements[i].child = 0;
        T->elements[i].sibling = 0;
    }
    T->first = 1;
    T->elements[1].empty = false;
    T->elements[1].label = x;
}

Initial URL

                                

Initial Description

                                

Initial Title
opcenito stablo sa bibliotekom_a

Initial Tags

                                

Initial Language
C++