You are on page 1of 6

Declaration 

:
struct node {
int val;
struct node* LChild;
struct node* RChild;
};
typedef struct node* Tree;

initialization:
T = NULL;

Add an element to BST:


Tree addTree (Tree R, int val)
{
    struct node* New=NULL;
    if (R==NULL) {
        New = malloc(sizeof(struct node));
        if (New==NULL) {printf("ERROR.\n");}
        else {
            New->val = val;
            New->LChild = NULL;
            New->RChild = NULL;
            R = New;
        }
    }
    else if (val <= R->val)
    {R->LChild = addTree(R->LChild, val);}
    else {R->RChild = addTree(R->RChild, val);}
    return R;
}

Tree addval (Tree R)


{
    int val, Re;
    do { printf("Please enter the value to add:\n");
    scanf("%d", &val);
    R = addTree (R, val);
    do {
    printf("Please enter 1 to add another value and 0 to quit.\n");
    scanf("%d", &Re);
    } while ((Re!=0) && (Re!=1));
    } while (Re!=0);
    return R;
}
Tree initialize(student S)
{
    Tree New=NULL;
    New = malloc(sizeof(struct node));
    if (New==NULL) {printf("ERROR.\n");}
    else {
        New->S = S;
        New->RChild = NULL;
        New->LChild = NULL;
    }
    return New;
}

Tree insertTree (Tree T, student S)


{
    if (T==NULL) {T = initialize(S);}
    else if (S.average <= T->S.average) {T->LChild = insertTree (T->LChild,
S);}
    else {T->RChild = insertTree (T->RChild, S);}
    return T;
}

Display BST in preorder (Root – Left – Right)


void preorderTraversal (Tree R)
{
    if (R==NULL) return;
    else {
        printf("%d\t", R->val);
        preorderTraversal(R->LChild);
        preorderTraversal(R->RChild);
    }
}

Display BST in Inorder (Left – Root – Right)


void inorderTraversal (Tree R)
{
    if (R==NULL) return;
    else {
        inorderTraversal (R->LChild);
        printf("%d\t", R->val);
        inorderTraversal (R->RChild);
    }
}
Display BST in postorder (Left– Right– Root)
void postorderTraversal (Tree R)
{
    if (R==NULL) return;
    else {
        postorderTraversal (R->LChild);
        postorderTraversal (R->RChild);
        printf("%d\t", R->val);
    }
}

Display the values of BST in ascending order


void displayascending (Tree R)
{
    if (R==NULL) return;
    else { displayascending(R->LChild);
    printf("%d\t", R->val);
    displayascending(R->RChild);
    }
}

Display the values of BST in descending order


void displaydescending (Tree R)
{
    if (R==NULL) return;
    else { displaydescending (R->RChild);
    printf("%d\t", R->val);
    displaydescending (R->LChild);
    }
}

Calculate the sum of the values of a BST:


int Sum (Tree R)
{
    if (R==NULL) return 0;
    else {
        return R->val + Sum(R->LChild) + Sum (R->RChild);
    }
}

Calculate number of nodes in a BST:


int calculateNumber(Tree T)
{
    if (T==NULL) return 0;
    else {
        return 1 + calculateNumber(T->LChild) + calculateNumber(T->RChild);
    }
}

Calculate the number of nodes with a condition


int calculatenumberp (BST T, char dest [])
{
    if (T==NULL) return 0;
    else { if (strcmp(T->p.destination, dest) == 0) {return 1 +
calculatenumberp(T->LChild, dest) + calculatenumberp(T->RChild, dest);}
        else {return calculatenumberp(T->LChild, dest) + calculatenumberp(T-
>RChild, dest);}
    }
}

Compare between two BST:


int CompareTree (Tree R, Tree R1)
{
    if ((R==NULL) && (R1==NULL)) return 1;
    else if ((R==NULL) && (R1!=NULL)) return 0;
    else if ((R1==NULL) && (R!=NULL)) return 0;
    else {if ((R1->val == R->val) && (CompareTree(R->LChild, R1->LChild)) &&
(CompareTree(R->RChild, R1->RChild))) return 1;
    else return 0;}
}

Search for a value in a BST and return 1 or 0:


int searchtree (Tree R, int val)
{
    if (R==NULL) return 0;
    else if (R->val == val) {return 1;}
    else if (val <= R->val)
        {return searchtree(R->LChild, val);}
        else {return searchtree(R->RChild, val);}
}

Search for a value in a BST and return the NODE or NULL:


BST search_Subscriber(BST R, int num)
{
    if (R==NULL) return NULL;
    else if (R->S.id == num) {return R;}
    else if (num <= R->S.id)
        {return search_Subscriber(R->LChild, num);}
        else {return search_Subscriber(R->RChild, num);}
}

Create a tree from an existing text file:


Tree buildTree (Tree T, char fileName[])
{
    FILE* f=NULL;
    student s;
    f = fopen(fileName, "r");
    if (f==NULL) {printf("ERROR FILE.\n");}
    else { while (!feof(f))
            {
              fscanf(f, "%s %s %s %d %f", s.id, s.name, s.fname, &s.level,
&s.average);
              T = insertTree (T, s);
            }
         }
    fclose(f);
    return T;
}

Create 3 DLLs from a tree according to a certain criterion:


void buildList(Tree T, DLL *L, float LL, float UL)
{
    if (T==NULL) return;
    else {
        if (T->S.average > LL) {buildList(T->LChild, L, LL, UL);}
        if (T->S.average <= UL) {buildList(T->RChild, L, LL, UL);}
        if ((T->S.average < UL) && (T->S.average >= LL))
        {insert_top(L, T->S);}
    }
}

void insert_top(DLL *L, student S)


{
    struct Node* New;
    New = malloc(sizeof(struct Node));
    if (New==NULL) {printf("ERROR NODE.\n");}
    else {
        New->S = S;
        New->next = L->first;
        New->prev = NULL;
        if ((L->first == NULL) && (L->last==NULL)) {L->last = New;}
        else {L->first->prev = New;}
        L->first = New;
    }
}

Display the nodes of a certain condition and save them in a binary file
at the same time
void displayandsave (BST T, char filename [])
{
    FILE* f=NULL;
    f = fopen(filename, "a+b");
    if (T==NULL) return;
    else {
        printf("Paquet with id %d and source %s and destination %s.\n", T-
>p.id, T->p.source, T->p.destination);
        fwrite(&(T->p), sizeof(paquet),1, f);
        displayandsave(T->LChild, filename);
        displayandsave(T->RChild, filename);}
    fclose(f);
}

You might also like