Implementacija binarnog stabla pomocu polja





0
Date Submitted Mon. Jan. 11th, 2010 11:29 AM
Revision 1 of 1
Beginner dpoje1
Tags binarno | binary | implementacija | polje | stablo | tree
Comments 1 comments
Ovo je moja implementacija binarnog stabla pomoĉu polja


struct element{
        int label;
        int used;
};

struct btPolje {
        struct element elements[10000];
};

typedef struct btPolje* btree;

void UsedB(int n, btree tree)
{
        tree->elements[n].used = 1;
}

void UnusedB(int n, btree tree)
{
        tree->elements[n].used = 0;
}

bool IsUsedB(int n, btree tree)
{
        return tree->elements[n].used != 0;
}

int ParentB(int n, btree tree)
{
        if (n == 1)
        return 0;
        return n / 2;
}

int LeftChildB(int n, btree tree)
{
        if (!IsUsedB(n * 2, tree))
                return 0;
        return n * 2;
}

int RightChildB(int n, btree tree)
{
        if (!IsUsedB(n * 2 + 1, tree))
                return 0;
        return n * 2 + 1;
}

int LabelB(int n, btree tree)
{
        return tree->elements[n].label;
}

void ChangeLabelB(int x, int n, btree tree)
{
        tree->elements[n].label = x;
}

int RootB(btree tree)
{
        if (!IsUsedB(1, tree))
                return 0;
        return 1;
}

bool CreateLeftB(int x, int n, btree tree)
{
        if (IsUsedB(n * 2, tree))
                return false;
        UsedB(n * 2, tree);
        ChangeLabelB(x, n * 2, tree);
        return true;
}

bool CreateRightB(int x, int n, btree tree)
{
        if (IsUsedB(n * 2 + 1, tree))
                return false;
        UsedB(n * 2 + 1, tree);
        ChangeLabelB(x, n * 2 + 1, tree);
        return true;
}

void DeleteB(int n, btree tree)
{
        if (!IsUsedB(n, tree))
                return;
        DeleteB(n * 2, tree);
        DeleteB(n * 2 + 1, tree);
        UnusedB(n, tree);
}

void InitB(int label, btree tree)
{
        for (int i = 0; i < 10000; i++)
        UnusedB(i, tree);
        UsedB(1, tree);
        ChangeLabelB(label, 1, tree);
}

 

Dejan Poje

Comments

Comments Razlika
Fri. Jan. 15th, 2010 9:02 AM    Beginner dpoje1

Voting

Votes Up


Votes Down