Find the largest number in a binary tree
Answers were Sorted based on User's Feedback
Answer / guest
Well..it's not a binary search tree.So we need to traverse
entire binary tree and check with the all node elements and
find the max value.
struct node
{
int data;
struct node *l;
struct node *r;
};
typedef struct node *nd;
int maximum(nd root)
{
static int max;
nd cur = root;
if(cur!=NULL)
{
if(cur->data>max)
max=cur->data;
maximum(root->l);
maximum(root->r);
}
return max;
}
Is This Answer Correct ? | 13 Yes | 11 No |
Answer / tomás senart
There is a difference between a binary tree and a binary search tree.
A binary tree isn't organized in any sense. The values of it's nodes can be random and have no relationship to each other.
Here is a method for finding the biggest node on a binary tree.
typedef struct node {
int value;
struct tree *right;
struct tree *left;
} Node;
int biggest_node(Node *node)
{
int biggest_left, biggest_right;
biggest_left = node->left ? biggest_node(node->left) : node->value;
biggest_right = node->right ? biggest_node(node->right) : node->value;
if(node->value < biggest_left && node->value < biggest_right)
return biggest_left > biggest_right ? biggest_left : biggest_right;
else if(node->value < biggest_right)
return biggest_right;
else if(node->value < biggest_left)
return biggest_left;
else
return node->value;
}
Is This Answer Correct ? | 7 Yes | 6 No |
Answer / jiabul sk
int maxOfTree(tree * t)
{
//if tree is empty then it will return -99
if(t==null){return -99;}
else
{
int temp ;
temp=max( maxOfTree(t->left), maxOfTree(t->right));
return( max( t->info , temp ) );
}
}
Is This Answer Correct ? | 4 Yes | 3 No |
Answer / c++ genie
<code>
struct node
{
int data;
struct node *l;
struct node *r;
};
typedef struct node *nd;
int maximum(nd root)
{
static int max;
nd cur = root;
if(cur!=NULL)
{
int temp = maximum(root->l);
if(temp > max)
max = temp;
int temp2 = maximum(root->r);
if(temp2 > max)
max = temp;
}
return max;
}
</code>
Is This Answer Correct ? | 2 Yes | 4 No |
Answer / raghuram.a
Well..it's not a binary search tree.So we need to traverse
entire binary tree and check with the all node elements and
find the max value.
struct node
{
int data;
struct node *l;
struct node *r;
};
typedef struct node *nd;
int maximum(nd root)
{
static int max;
nd cur = root;
if(cur!=NULL)
{
if(cur->data>max)
max=cur->data;
maximum(root->l);
maximum(root->r);
}
return max;
}
Is This Answer Correct ? | 4 Yes | 10 No |
Answer / bharat pandey
The Largest Node in the Binary tree is the Rightmost node
of the tree.
Hence we would traverse the Tree Till The Rightmost child
of the node is traversed.
the code is as follows:
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *left,*right;
}*tree;
struct node* MAX(struct node* q)
{
struct node* temp;
while(q->right!=NULL)
{
temp=q;
q=q->right;
}
return temp;
}
this algorithm will find the largest element of the tree in
o(log n).
Is This Answer Correct ? | 6 Yes | 23 No |
What is the subtle error in the following code segment? void fun(int n, int arr[]) { int *p=0; int i=0; while(i++<n) p = &arr[i]; *p = 0; }
could you please send the program code for multiplying sparse matrix in c????
abcdedcba abc cba ab ba a a
C program to print magic square of order n where n > 3 and n is odd
main() { static char names[5][20]={"pascal","ada","cobol","fortran","perl"}; int i; char *t; t=names[3]; names[3]=names[4]; names[4]=t; for (i=0;i<=4;i++) printf("%s",names[i]); }
main() { int i=5; printf("%d%d%d%d%d%d",i++,i--,++i,--i,i); }
write a c program to Create a registration form application by taking the details like username, address, phone number, email along with password and confirm password (should be same as password).Ensure that the password is of 8 characters with only numbers and alphabets. Take such details for 5 users and display the details. In place of password display “****”. (Use Structures).
0 Answers CDAC, College School Exams Tests,
There is a lucky draw held every day. if there is a winning number eg 1876,then all possible numbers like 1867,1687,1768 etc are the numbers that match irrespective of the position of the digit. Thus all these numbers qualify fr the lucky draw prize Assume there is no zero digit in any numbers. write a program to show all the possible winning numbers if a "winning number"is passed as an arguments to the function.
main() { float me = 1.1; double you = 1.1; if(me==you) printf("I love U"); else printf("I hate U"); }
posted by surbhi just now main() { float a = 5.375; char *p; int i; p=(char*)&a; for(i=0;i<=3;i++) printf("%02x",(unsigned char) p[i]); } how is the output of this program is :: 0000ac40 please let me know y this output has come
What is data _null_? ,Explain with code when u need to use it in data step programming ?
what will be the position of the file marker? a: fseek(ptr,0,SEEK_SET); b: fseek(ptr,0,SEEK_CUR);