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 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.
write a function that takes as input the root of a general tree and returns a...
write a function that takes as input the root of a general tree and returns a binary tree generated by the conversion process illustrated in java
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include: • Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node. • Find a node by integer value: This function takes in two...
Write a method for binary tree in Python that can determine whether a binary tree is...
Write a method for binary tree in Python that can determine whether a binary tree is a binary search tree or not. The input should be a binary tree. The output should be true or false. True= binary tree meets the criteria to be a binary search tree. False= does not meet the criteria to be a binary search tree.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT