Question
IN C LANGUAGE
Write a program that counts the leaves of a given binary tree. Hint: You will make a small (and smart) update to the add func


Directed Graph-An Example The graph G=(V.E) has 5 vertices and 6 edges V1,2,3,4,5) E{1,2).(1,4),(2,5).(4,5).,(3,1).(4,3)} Adj
Write a program that counts the leaves of a given binary tree. Hint: You will make a small (and smart) update to the add function we discussed in class. a) 25 points Create the following binary trees by creating the nodes (malloc) and setting the related pointers (leftChild, right Child). Make content random. 50 40 60 100 70 45 30 120 47 b) 25 points Display the trees on screen the way we did in slide 9 using listAll function You will of course modify it slightly as we do not deal with directories here. e) 150 points Count the leaves and print the result. Your code will be tested with additional trees as wel
Directed Graph-An Example The graph G=(V.E) has 5 vertices and 6 edges V1,2,3,4,5) E{1,2).(1,4),(2,5).(4,5).,(3,1).(4,3)} Adjacent 2 is adjacent to 1, but 1 is NOT adjacent to 2 Path: 1,2.5 (a directed path). Cycle 1,43,1 (a directed cycle), Weighted Graph We can label the edges of a graph with numeric values, the graph is called a weighted graph Weighted (Undirected) Graph 10 Weighted Directed Graph 10 Graph Implementations The two most common implementations of a graph are: Adjacency Matrix A two dimensional array Adjacency List
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>


#include <stdlib.h>

struct node

{

int data;

struct node *leftChild;

struct node *rightChild;

};

struct node * getNewNode(int data)

{

struct node *newNode = (struct node *)malloc(sizeof(struct node));

newNode->data = data;

newNode->leftChild = NULL;

newNode->rightChild = NULL;

return newNode;

}

struct node * insert(struct node * root, int data)

{

if (root == NULL)

{

root = getNewNode(data);

}

else if (data <= root->data)

{

root->leftChild = insert(root->leftChild, data);

}

else

{

root->rightChild = insert(root->rightChild, data);

}

return root;

}

int countLeaves(struct node * root){

if (root == NULL)

return 0;

else{

if(root->leftChild == NULL && root->rightChild==NULL)

return 1;

else {

return countLeaves(root->leftChild) + countLeaves(root->rightChild);

}

}

}

int main()

{

struct node *root = NULL;

root = getNewNode(50);

root->leftChild = getNewNode(40);

root->rightChild = getNewNode(60);

root->leftChild->leftChild = getNewNode(30);

root->leftChild->rightChild = getNewNode(45);

root->rightChild->rightChild = getNewNode(70);

root->leftChild->rightChild->leftChild = getNewNode(42);


printf("Number of leaves: %d ",countLeaves(root));

return 0;

}




==================================================================
SEE OUTPUT

saved main.c Files http://WigglyThirdViruses. rahulkumar29.repl.run 77 clang version 7.0.0-3-ubuntu0, 18.04.1 (to main.c 78 a


Thanks, PLEASE COMMENT if there is any concern.

Add a comment
Know the answer?
Add Answer to:
IN C LANGUAGE Write a program that counts the leaves of a given binary tree. Hint: You will make a small (and...
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
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
ADVERTISEMENT