Return to Snippet

Revision: 38667
at January 6, 2011 04:30 by sam085


Initial Code
#include <stdlib.h>
#define ARRAYSIZE 1000000

struct element 
{
    int label;
    int used;
};
 
typedef struct bt 
{
    struct element elements[ARRAYSIZE];
} *btree;
 
typedef int node;
 
btree init(int x)
{
    btree myBTree;
    int i;
 
    myBTree = (btree)malloc(sizeof(struct bt));
    for(i = 1; i < ARRAYSIZE; i++){
        if(i == 1)
        {
                myBTree->elements[i].label = x;
                myBTree->elements[i].used = 1;
        }
        else
        {
            myBTree->elements[i].used = 0;
        }
    }
 
    return myBTree;
}
 
node root(btree T)
{
    return 1;
}
 
node parrent(node n, btree T)
{
    if(n == 1)
    {
        printf("Roditelj korjenskog cvora ne postoji\n");
        return -1;
    }
 
    if(T->elements[n].used == 0)
    {
        printf("Ne postoji\n");
        exit(1);
    }
 
    return (n/2);
}
 
node leftChild(node n, btree T)
{
    node child;
    child = n * 2;
    if (child > ARRAYSIZE-1)
    {
        printf("\noverflow\n");
        return -1;
        //exit(1);
    }
 
    if(T->elements[child].used == 0)
    {
        return -1;
    }
    else
    {
        return child;
    }
}
 
node rightChild(node n, btree T)
{
    node child;
    child = (n * 2) + 1;
    if (child > ARRAYSIZE - 1)
    {
        printf("\noverflow\n");
        return -1;
        //exit(1);
    }
 
    if(T->elements[child].used == 0)
    {
        return -1;
    }
    else
    {
        return child;
    }
}
 
int label(node n, btree T)
{
    if(T->elements[n].used == 0)
    {
        printf("ne postoji\n");
        return -1;
    }
    else
    {
        return T->elements[n].label;
    }
}
 
void changeLabel(int x, node n, btree T)
{
    if(T->elements[n].used == 0)
    {
        printf("ne postoji\n");
        return;
    }
    else
    {
        T->elements[n].label = x;
    }
}
 
void createLeft(int x, node n, btree T)
{
    node child;
    child = n * 2;
    if (child > ARRAYSIZE - 1)
    {
        printf("\noverflow\n");
        return;

    }
 
    if(T->elements[child].used == 1){
        printf("lijevi vec postoji\n");
        return;
    }
    else
    {
        T->elements[child].used = 1;
        T->elements[child].label = x;
    }
}
 
void createRight(int x, node n, btree T)
{
    node child;
    child = (n * 2) + 1;
    if (child > ARRAYSIZE - 1)
    {
        printf("\noverflow\n");
        return;
    }
    if(T->elements[child].used == 1)
    {
        printf("desno vec postoji\n");
        return;
    }
    else
    {
        T->elements[child].used = 1;
        T->elements[child].label = x;
    }
}
 
void deleteN(node n, btree T){
    if(n == -1)
        return;
 
    deleteN(leftChild(n, T), T);
 
    T->elements[n].used=0;
 
    deleteN(rightChild(n, T), T);
}
 
void deleteNode(node n, btree T ){
    if(parrent(n, T) == -1){
        printf("Korjen se nemoze obrisat\n");
        return;
    }
 
    deleteN(n,T);
}

Initial URL

                                

Initial Description

                                

Initial Title
binarno stablo - polje

Initial Tags

                                

Initial Language
C++