0 0 practice503 Asked: November 20, 20212021-11-20T09:10:21+05:30 2021-11-20T09:10:21+05:30In: Data Structure C Program to Find the Largest value in a Tree using Inorder Traversal. 0 0 C Program to Find the Largest value in a Tree using Inorder Traversal. Share Facebook 1 Answer Voted Oldest Recent Professor 2021-11-20T10:29:26+05:30Added an answer on November 20, 2021 at 10:29 am #include <stdio.h> #include <stdlib.h> struct node { int info; struct node *left, *right; }; struct node *createnode(int key) { struct node *newnode = (struct node*)malloc(sizeof(struct node)); newnode->info = key; newnode->left = NULL; newnode->right = NULL; return(newnode); } void inorder(struct node *root) { if(root != NULL) { inorder(root->left); printf(" %d ",root->info); inorder(root->right); } } void largest(struct node *root) { while (root != NULL && root->right != NULL) { root = root->right; } printf("\nLargest value is %d\n", root->info); } /* * Main Function */ int main() { struct node *newnode = createnode(25); newnode->left = createnode(17); newnode->right = createnode(35); newnode->left->left = createnode(13); newnode->left->right = createnode(19); newnode->right->left = createnode(27); newnode->right->right = createnode(55); /* Sample Tree 1: * 25 * / \ * 17 35 * / \ / \ * 13 19 27 55 */ printf("Inorder traversal of tree 1 :"); inorder(newnode); largest(newnode); /* Creating second Tree. */ struct node *node = createnode(1); node->right = createnode(2); node->right->right = createnode(3); node->right->right->right = createnode(4); node->right->right->right->right = createnode(5); /* Sample Tree 2: Right Skewed Tree (Unbalanced). * 1 * \ * 2 * \ * 3 * \ * 4 * \ * 5 */ printf("\nInorder traversal of tree 2 :"); inorder(node); largest(node); struct node *root = createnode(15); printf("\nInorder traversal of tree 3 :"); inorder(root); largest(root); return 0; } Leave an answerCancel replyYou must login to add an answer. Username or email* Password* Remember Me! Forgot Password?
1 Answer