0

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);
}
}
```