Question

Data Structures Using C++: Using C++: Write the definition of the function, nodeCount, that returns the...

Data Structures Using C++:

Using C++:

Write the definition of the function, nodeCount, that returns the number of nodes in a binary tree. Add this function to the class binaryTreeType and create a program to test this function. Read the tree definitions from files.

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

Following is the Algorithmic definition of the nodeCount function

int nodeCount( TreeNode *root ) {

           // Count the nodes in the binary tree to which

           // root points, and return the answer.

        if ( root == NULL )

           return 0; // The tree is empty. It contains no nodes.

        else {

           int count = 1;   // Start by counting the root.

           count += countNodes(root->left); // Add the number of nodes

                                            //     in the left subtree.

           count += countNodes(root->right); // Add the number of nodes

                                            //    in the right subtree.

           return count; // Return the total.

        }

     } // end nodeCount()

In the programm I have used the preorder method to count the number of nodes in the binary tree...

I have executed it under codeblocks IDE.

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

/*
* Structure of node
*/
struct btnode
{
int value;
struct btnode *l;
struct btnode *r;
};
typedef struct btnode node;
node *ptr, *root = NULL;

void createbinary();
void preorder(node *);
int count(node*);
node* add(int);


int main()
{
int c;

createbinary();
preorder(root);
c = count(root);
cout << "\nNumber of nodes in binary tree are: " << c << endl;
return 0;
}
/*
* constructing the following binary tree
* 50
* / \
* 20 30
* / \
* 70 80
* / \ \
*10 40 60
*/
void createbinary()
{
root = add(50);
root->l = add(20);
root->r = add(30);
root->l->l = add(70);
root->l->r = add(80);
root->l->l->l = add(10);
root->l->l->r = add(40);
root->l->r->r = add(60);
}

/*
* Add the node to binary tree
*/
node* add(int val)
{
ptr = (node*)malloc(sizeof(node));
if (ptr == NULL)
{
cout << "Memory was not allocated" << endl;
exit(0);
}
ptr->value = val;
ptr->l = NULL;
ptr->r = NULL;
return ptr;
}

/*
* counting the number of nodes in a tree
*/
int count(node *n)
{
int c = 1;

if (n == NULL)
return 0;
else
{
c += count(n->l);
c += count(n->r);
return c;
}
}

/*
* Displaying the nodes of tree in preorder
*/
void preorder(node *t)
{
if (t != NULL)
{
printf("%d->", t->value);
preorder(t->l);
preorder(t->r);
}
}

CAUsers KanthlDesktop babulPlacement binary TreeTypelbinlDebuglbinary TreeType.exe 50-28-7e-18->48->80->60->30- Number of nod

Add a comment
Know the answer?
Add Answer to:
Data Structures Using C++: Using C++: Write the definition of the function, nodeCount, that returns the...
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
  • 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.)

  • 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Yo...

    please explain each line of code! ( in python ) 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Your function should take one parameter, root node. You may assume that the tree only contains integers. You may not call any methods from the LinkedBinaryTree class. Specifically, you should traverse the tree in your function def binary tree even sum (root): Returns the sum of al1 even integers in the binary tree 2....

  • Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary...

    Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary tree. Add this function to the class binaryTreeType and create a program to test this function. #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct nodeType { elemType info; nodeType<elemType> *lLink; nodeType<elemType> *rLink; }; //Definition of the class template <class elemType> class binaryTreeType { public: //Overload the assignment operator. const binaryTreeType<elemType>& operator=(const binaryTreeType<elemType>&) { if (this != &otherTree) //avoid self-copy...

  • 1. Given the two binary trees below: 14 16 Write a method called swapSubtrees, which swaps all of...

    in java ..write all complete program from a- e 1. Given the two binary trees below: 14 16 Write a method called swapSubtrees, which swaps all of the left and right subtrees in the above binary trees. Add this method to the class BinaryTree and create a program to test this method for these 2 trees. Show the original trees and the resulting trees. Note: To test your algorithm, first create a binary search tree. Write a method called singleParent,...

  • You are going to implement Treesort algorithm in C++ to sort string data. Here are the...

    You are going to implement Treesort algorithm in C++ to sort string data. Here are the steps to complete the homework 1) Use the following class definition for binary search tree nodes. Its constructor is incomplete you should first complete the constructor. class TreeNode t public: string data; / this is the string stored in the node TreeNode left: TreeNode right; TreeNode (string element, TreeNode 1t, TreeNode rt //your code here 2) Write a function that will insert a string...

  • Data Structures and Algorithms. (C++ Language) 1. Write the definition code for a function that p...

    Data Structures and Algorithms. (C++ Language) 1. Write the definition code for a function that passes in a stack and returns (using a return statement) the number of items in the stack (the stack size). a. assume the this function is to be toolkit function in the implementation of the ADT stack. b. assume that this function is not a toolkit function. 2. Given the declaration: s = stack i = item struct STACK { INFO_RC i; int top; }...

  • C++ Vectors and Binary Search Trees • Write a program that takes from the user n...

    C++ Vectors and Binary Search Trees • Write a program that takes from the user n integers and stores them a vector of int. Then, create a function insert After that takes first Value and second Value. This function searches for each occurrence of first Value in the vector and insert the second Value after it in the same vector. The first and second values are taken from the user. • Create another function that creates a Binary Search Tree...

  • Data Structures, algorithm, c++. Please explain if possible. Thank you! Complete the following function that returns...

    Data Structures, algorithm, c++. Please explain if possible. Thank you! Complete the following function that returns the largest element of the binary tree rooted at root. template <typename T> T max (Node<T>root) if (root nullptr) return TO; T max1 = root->x; T max2 root->x; if (root->left != nullptr) maxlmax (root->left); if (root->right != nullptr) max2 max ( root->right); - if (root->x >= max1 && root->x >= max2) return __.-; if (max1 >= root->x && max1 >= max2) return-_-> return___--> Let...

  • C# prograaming language 1. write a program that generates 10,000 random integers in the range of ...

    c# prograaming language 1. write a program that generates 10,000 random integers in the range of 0-9 and them in binary search tree. 2. use any sort algorithm (insertion, bubble, quick...) to display a list of each of the integers and how many times they appear. 3. at last, add ruction to the BinarySearchTree class that count the number of edges in a tree. Write a program that generates 10,000 random integers in the range of 0-9 and store them...

  • WİTH C LANGUAGE Write a program that counts the leaves of a given binary tree. Hint: You will make a small (and smar...

    WİTH 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 function we discussed in class. a) 125 pointsl Create the following binary trees by creating the nodes (malloc) and setting the related pointers (leftChild, rightChild). Make content random. 60 50 40 60 70 30 45 42 b) 125 points Display the trees on screen the way we did in slide 9 using listAll...

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