Question

In: Computer Science

(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the...

(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the nodes in a binary tree that have only one child. Convert it to an iterative version. in C++

Solutions

Expert Solution

Solution:

Recursive implementation of the function singleParent:

// C++ program to count number of the nodes in a binary tree that have only one child

#include <bits/stdc++.h>

using namespace std;

//node structure that have data, left and right pointer

struct Node

{

int data;

struct Node* left, *right;

};

// Recursive function to get the count of half Nodes

unsigned int singleParent(struct Node* root)

{

if (root == NULL)

return 0;

int res = 0;

if ((root->left == NULL && root->right != NULL) ||

(root->left != NULL && root->right == NULL))

res++;

res += (singleParent(root->left) + singleParent(root->right));

return res;

}

struct Node* newNode(int data)

{

struct Node* node = new Node;

node->data = data;

node->left = node->right = NULL;

return (node);

}

// Driver program

int main(void)

{

struct Node *root = newNode(2);

root->left = newNode(7);

root->right = newNode(5);

root->left->right = newNode(6);

root->left->right->left = newNode(1);

root->left->right->right = newNode(11);

root->right->right = newNode(9);

root->right->right->left = newNode(4);

cout << singleParent(root);

return 0;

}

Iterative implementation of the function singleParent:

// C++ program to count number of the nodes in a binary tree that have only one child

#include <bits/stdc++.h>

using namespace std;

//node structure that have data, left and right pointer

struct Node

{

int data;

struct Node* left, *right;

};

// recursive function to count number of the nodes in a binary tree that have only one child

unsigned int singleParent(struct Node* node)

{

// If tree is empty

if (!node)

return 0;

int count = 0; // Initialize count of half nodes

// Do level order traversal starting from root

queue<Node *> q;

q.push(node);

while (!q.empty())

{

struct Node *temp = q.front();

q.pop();

if ((!temp->left && temp->right) || (temp->left && !temp->right))

count++;

if (temp->left != NULL)

q.push(temp->left);

if (temp->right != NULL)

q.push(temp->right);

}

return count;

}

struct Node* newNode(int data)

{

struct Node* node = new Node;

node->data = data;

node->left = node->right = NULL;

return (node);

}

// Driver program

int main(void)

{

//create bianry tree

struct Node *root = newNode(2);

root->left = newNode(7);

root->right = newNode(5);

root->left->right = newNode(6);

root->left->right->left = newNode(1);

root->left->right->right = newNode(11);

root->right->right = newNode(9);

root->right->right->left = newNode(4);

cout << singleParent(root);

return 0;

}

Please give thumbsup, if you like it. Thanks.


Related Solutions

a) Based on the binary tree implementation from the Python program below  write a recursive method that...
a) Based on the binary tree implementation from the Python program below  write a recursive method that calculates the number of leaf nodes in the tree. class Binaertre: def __init__(self, datatobjekt): self.data = datatobjekt self.forelder = None self.venstre_barn = None self.hoyre_barn = None @property def venstre_barn(self): return self.__venstre_barn @venstre_barn.setter def venstre_barn(self, node): self.__venstre_barn = node if node is not None: node.forelder = self @property def hoyre_barn(self): return self.__hoyre_barn @hoyre_barn.setter def hoyre_barn(self, node): self.__hoyre_barn = node if node is not None: node.forelder...
Write a boolean function that is given a binary tree and returns true if and only...
Write a boolean function that is given a binary tree and returns true if and only if the tree has an odd number of nodes. An empty tree is considered to have an even number of nodes. Notes: The function should have just one argument, a pointer to the root. No global variables may be used. No additional functions may be defined. You may not count the number of nodes.
Write a recursive implementation of insertion sort. Draw a tree of recursive calls for your prototype...
Write a recursive implementation of insertion sort. Draw a tree of recursive calls for your prototype random dataset and compare its big-O efficiency to that of iterative insertion sort. Run your prototype datasets (best, worst, random) from homework 1 and compare the results to the ones for iterative insertion sort. Which version is better? Make sure to present the results in the table form used in homework 1. Include the data for both iterative and recursive versions.
Write a solution to return the count of the number of nodes in a binary tree....
Write a solution to return the count of the number of nodes in a binary tree. Your method will be passed one parameter, a copy of a pointer to the root node of the tree (Node *) and will return an int that is the count of nodes. If the tree is empty, return 0 (this is the recursive base case).
Write an implementation of the set class, with associated iterators using a binary search tree. Add...
Write an implementation of the set class, with associated iterators using a binary search tree. Add to each node a link to the parent node. (C++)
Write a modification of the recursive binary search algorithm that always returns the smallest index whose...
Write a modification of the recursive binary search algorithm that always returns the smallest index whose element matches the search element. Your algorithm should still guarantee logarithmic runtime. Give a brief discussion of the best- and worst-case runtimes for this new algorithm as they compare to the original. NOTE: You do not have to re-write the entire algorithm. You just need to indicate any changes you would make and show the pseudocode for any portions that are changed. Example: Given...
Write a O(n) method valuesInLevelOrder() that returns a list of the nodes of a binary tree...
Write a O(n) method valuesInLevelOrder() that returns a list of the nodes of a binary tree in level-order. That is, the method should return the root, then the nodes at depth 1, followed by the nodes at depth 2, and so on. Your algorithm should begin by putting the tree root on an initially empty queue. Then dequeue a node, add it to the output, and enqueue its left and right children (if they exist). Repeat until the queue is...
Create an array-based implementation of a binary tree. (WRITE IN JAVA) DON'T FORGET TO INCLUDE PSEUDOCODE...
Create an array-based implementation of a binary tree. (WRITE IN JAVA) DON'T FORGET TO INCLUDE PSEUDOCODE AND UML DIAGRAM
In C++: Write a binary search tree and include the functions to find the number of...
In C++: Write a binary search tree and include the functions to find the number of nodes and the height of the tree. Test your code in main. Print the post-order, in-order and pre-order traversal.
Write a binary search tree and include the functions to find the number of nodes and...
Write a binary search tree and include the functions to find the number of nodes and the height of the tree. Test your code in main. Print the post-order, in-order and pre-order traversal. in c++ please.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT