Question

Write a function to return an average value of floats stored in a binary tree. In...

Write a function to return an average value of floats stored in a binary tree. In C

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The below C code is working perfectly and I have created this binary tree with three nodes.
root node =1 ,root->left=2 and root->right=3.

The below program will find the sum of all nodes and also node count.
Finally, it will calculate the average and print the average result.
Output of My Program:

Total Sum : 6
Total Nodes : 3
Average : 2.00

Code:

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

struct node
{
int data;
struct node *left,*right;
};


struct node *newnode(int data)
{
   struct node *temp=(struct node*)malloc(sizeof(struct node));
   temp->data=data;
   temp->left=NULL;
   temp->right=NULL;
   return temp;
}

struct node *insert(struct node *root,int data)
{
   if(root==NULL)
   {
       return newnode(data);
   }

   if(data < root->data)
   return insert(root->left,data);

   else if(data > root->data)
   return insert(root->right,data);

}

int total_sum;
int total_nodes;
void average_func(struct node *root)
{
   if(root!=NULL)
   {
       average_func(root->left);

       total_sum=total_sum+root->data;
       total_nodes++;
    
       average_func(root->right);
   }
}

int main()
{

   struct node *root=NULL;

   root=insert(root,1);
   root->left=insert(root,2);
   root->right=insert(root,3);



   average_func(root);

   printf("\nTotal Sum : %d",total_sum);
printf("\nTotal Nodes : %d",total_nodes);

float average=(float)(total_sum/total_nodes);

printf("\nAverage : %.2f",average);

}

Add a comment
Know the answer?
Add Answer to:
Write a function to return an average value of floats stored in a binary tree. In...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a python function that takes a binary tree as its only parameter. Return the value...

    Write a python function that takes a binary tree as its only parameter. Return the value stored in the shallowest leaf, anywhere in the tree. If there is a tie, then return the "leftmost" of the leaves (that is, the one that you would encounter first in an in-order traversal). You are allowed (and encouraged) to use a helper function. You may assume that the tree nodes have public fields left,right. The code cannot import anything from the Python standard...

  • Write a recursive function that returns the minimum key value in a binary search tree of...

    Write a recursive function that returns the minimum key value in a binary search tree of distinct (positive) integers. Return -1 in case the tree is empty. (b) Write a recursive function that returns the predecessor of the key value k in a binary search tree of distinct (positive) integers. This is the key value that precedes k in an inorder traversal of the tree. If k does not exist, or if k has no predecessor, your function should return...

  • Write a recursive function (C++) that searches a binary tree that is NOT ordered like a...

    Write a recursive function (C++) that searches a binary tree that is NOT ordered like a BST. In other words, there is no ordering relationship among the parent, child or sibling node values. Use the following prototype and data structure: struct node { node *left; node *right; int val; }; // First parameter: pointer to a node // Second parameter: the value to find bool searchValue(node *, int); The function searchValue() should return true if the integer value in the...

  • Public IntBTNode find(int item) This function will attempt to find the item in the binary search ...

    write java function: public IntBTNode find(int item) This function will attempt to find the item in the binary search tree. It will return a reference to the node that contains Item if the item is stored in the tree. If the item is not in the tree, the function will return nu public IntBTNode find(int item) This function will attempt to find the item in the binary search tree. It will return a reference to the node that contains Item...

  • C++ Write a function, singleParent, that returns the number of nodes in a binary tree that...

    C++ Write a function, singleParent, that returns the number of nodes in a binary tree that have only one child. Add this function to the class binaryTreeType and create a program to test this function. (Note: First create a binary search tree.)

  • Given a binary search tree and a value k, implement a function to find the node...

    Given a binary search tree and a value k, implement a function to find the node in the binary search tree whose value is closest to k. Write the program in Java Syntax: int lookup(Node node)

  • Write a function to return a string containing the actual binary value of the mantissa of...

    Write a function to return a string containing the actual binary value of the mantissa of a float in a char array. You should call get_flt_bits_int to get the bits in an int and return the string. Example: for f = -15.375 n = 11000001011101100000000000000000 the mantissa bits are "11101100000000000000000" The function should accept a float and return a string.

  • Write a c++ function that will a. determine whether two binary trees are mirror similar. b....

    Write a c++ function that will a. determine whether two binary trees are mirror similar. b. determine whether two binary trees are isomorphic. c. replace a given value in a binary search tree with a new value keeping the tree as a binary search tree. d. create a new tree composed of words in another tree in reverse order e. create a 0-2 binary search tree based on existing non 0-2 binary search tree

  • 7) Recursion Write a recursive function max_tuple(a tree which takes a tree as a parameter and...

    7) Recursion Write a recursive function max_tuple(a tree which takes a tree as a parameter and returns the max values of the right and left subtree. You can assume that the parameter is a full binary tree (in a pyramid shape like with more than 3 nodes. Your solution should not have any iteration. You can use a helper function if needed. Your code should be indented correctly. The expected return value of max_tuple(tree) for the tree below should be(...

  • Write an algorithm that will check if a tree is a binary search tree. If a...

    Write an algorithm that will check if a tree is a binary search tree. If a tree is not a binary search tree, then it will repair it to make it one. (a) Write pseudo-code for both functions. (b) What is the running time for your algorithm?

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT