Hello,
I have been working on a school project of Algorithms class. It is basically to create a C project to insert numbers into a binary search tree then order it and find the largest element. The largest number goes to the right node of the tree so it should look to the right part always. All my functions are working except the finding the largest element. I couldn't solve it and I put the not working code into comments. Can anyone find the problem and fix it? Thanks

#include <stdio.h>
#include <stdlib.h>

typedef struct tree {
    int data;
    struct tree *right, *left;
} TREE;

TREE *insert(TREE *, int);
//TREE *findmax(TREE);
void order(TREE *);


int main() {
    TREE *root = NULL; 
    int option, element, n, i;
    do {
        printf("\n       1-Create a Binary Search Tree");
        printf("\n       2-Order\n");
        scanf("%d", &option);
        switch (option) {
            case 1:
                root = NULL;
                printf("\nEnter n number of nodes\n");
                scanf("%d", &n);
                for (i = 1; i <= n; i++) {
                    printf("\nEnter a number for node %d\n", i);
                    scanf("%d", &element);
                    root = insert(root, element);
                }
                printf("\nBinary Search Tree with %d nodes created\n", n);
                break;
            case 2:
                printf("\nOrder\n");
                order(root);
                break;
            /*case 3:
                printf("\nFind Largest\n");
                findmax(TREE);
                break;*/


            default:
                printf("\nError\n");
                break;
        }
        printf("\n\n\n");

    } while (option != 4);
}

TREE *insert(TREE *root, int element) {
    if (root == NULL) {
        root = (TREE *) malloc(sizeof(TREE));
        root->left = root->right = NULL;
        root->data = element;
        return root;
    } else {
        if (element < root->data)
            root->left = insert(root->left, element);
        else if (element > root->data)
            root->right = insert(root->right, element);
        else
            printf("Enter a different element\n");

        return (root);
    }
}

/*TREE *findmax(TREE *temp)
{
    if(temp==NULL || temp->right==NULL)
        return temp;
    return findmax(temp->right);
}*/

void order(TREE *root) {
    if (root != NULL) {
       order(root->left);
        printf(" %d ", root->data);
        order(root->right);
    }
}

Rather than list out everything I did, I'll just post the code:

#include <stdio.h>
#include <stdlib.h>

typedef struct tree {
    int data;
    struct tree *right, *left;
} TREE;

TREE *insert(TREE *, int);
TREE *findmax(TREE *);
void order(TREE *);
void structure(TREE *root, int depth);

int main() {
    TREE *root = NULL; 
    int option, element, n, i;
    do {
        printf("\n       1-Create a Binary Search Tree");
        printf("\n       2-Order");
        printf("\n       3-Find Largest");
        printf("\n       4-Tree Structure");
        printf("\n       5-Quit\n");
        scanf("%d", &option);
        switch (option) {
        case 1:
            root = NULL;
            printf("\nEnter n number of nodes\n");
            scanf("%d", &n);
            for (i = 1; i <= n; i++) {
                printf("\nEnter a number for node %d\n", i);
                scanf("%d", &element);
                root = insert(root, element);
            }
            printf("\nBinary Search Tree with %d nodes created\n", n);
            break;
        case 2:
            printf("\nOrder\n");
            order(root);
            break;
        case 3:
            printf("\nFind Largest\n");
            printf("%d\n", findmax(root)->data);
            break;
        case 4:
            printf("\nTree Structure\n");
            structure(root, 0);
            break;
        case 5:
            printf("\nQuitting...\n");
            break;
        default:
            printf("\nError\n");
            break;
        }
        printf("\n\n\n");

    } while (option != 5);
}

TREE *insert(TREE *root, int element) {
    if (root == NULL) {
        root = (TREE *) malloc(sizeof(TREE));
        root->left = root->right = NULL;
        root->data = element;
        return root;
    } else {
        if (element < root->data)
            root->left = insert(root->left, element);
        else if (element > root->data)
            root->right = insert(root->right, element);

        return (root);
    }
}

TREE *findmax(TREE *temp)
{
    if(temp==NULL || temp->right==NULL)
        return temp;
    return findmax(temp->right);
}

void order(TREE *root) {
    if (root != NULL) {
        order(root->left);
        printf(" %d ", root->data);
        order(root->right);
    }
}

void structure(TREE *root, int depth) {
    int i;

    if (root == NULL) {
        for (i = 0; i < depth; i++)
            putchar('\t');
        printf("~\n");
    } else {
        structure(root->right, depth + 1);

        for (i = 0; i < depth; i++)
            putchar('\t');
        printf("%d\n", root->data);

        structure(root->left, depth + 1);
    }
}

Note that I added a structure() function and feature for looking at the actual structure of the tree. I've found this to be invaluable in both troubleshooting and wrapping my head around tree-based algorithms.

Thanks a lot, you did much more than I asked and it was very quick.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.