Return to Snippet

Revision: 38653
at January 6, 2011 04:05 by sanzivode


Initial Code
#include <iostream>
using namespace std;

struct element {
    int label;
    bool used;
};

struct bt {
    element elements[10000];
};

int ParentB(int n, bt* T) {
    if (n == 1) return 0;
    if (n%2) n--;
    return n/2;
}

int LeftChildB(int n, bt* T) {
    if (T->elements[n].used == 0) return 0;
    if (T->elements[2*n].used == 1) return 2*n;
    return 0;
}

int RightChildB(int n, bt* T) {
    if (T->elements[n].used == 0) return 0;
    if (T->elements[2*n+1].used == 1) return 2*n+1;
    return 0;
}

int LabelB(int n, bt* T) {
    return T->elements[n].label;
}

void ChangeLabelB(int x, int n, bt* T) {
    if(T->elements[n].used == 1)
        T->elements[n].label = x;
}

int RootB(bt* T) {
    if(T->elements[1].used == 0) return 0;
    return 1;
}

void CreateLeftB(int x, int n, bt* T) {
	if(T->elements[n].used == 0 || T->elements[2*n].used == 1) {
        cout << "Poruka greske" << endl;
        return;
    }
    T->elements[2*n].used = 1;
    T->elements[2*n].label = x;
}

void CreateRightB(int x, int n, bt* T) {
	if(T->elements[n].used == 0 || T->elements[2*n+1].used == 1) {
        cout << "Poruka greske" << endl;
        return;
    }
    T->elements[2*n+1].used = 1;
    T->elements[2*n+1].label = x;
}

void DeleteB(int n, bt* T) {
    T->elements[n].used = 0;
	if (LeftChildB(n,T)) DeleteB(LeftChildB(n,T),T);
	if (RightChildB(n,T)) DeleteB(RightChildB(n,T),T);
}

bt* InitB(int x, bt* T) {
    T = new bt;
    T->elements[1].label = x;
    T->elements[1].used = 1;
    for(int i = 2; i < 10000; i++)
        T->elements[i].used = 0;
    return T;
}

Initial URL

                                

Initial Description

                                

Initial Title
Binarno stablo_polje

Initial Tags

                                

Initial Language
C++