Question

Given a pointer to a binary tree write a routine which will traverse the tree and return th number of nodes in the tree who have just a right son or just a left son (do not include nodes tha have both right and left sons as well as do not include any nodes that do not have any sons at all). IV
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

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

btree* addNode(int val)
{
   btree* newNode = new btree;
   newNode->data = val;
   newNode->left = NULL;
   newNode->right = NULL;
   return newNode;
}

int countSingleChild(btree* root)
{
   if(root == NULL)
       return 0;
   else if(root->left == NULL && root->right != NULL)
       return 1 + countSingleChild(root->right);
   else if(root->left != NULL && root->right == NULL)
       return 1 + countSingleChild(root->left);
   else
       return countSingleChild(root->left) + countSingleChild(root->right);
}

int main()
{
  
   btree* root = addNode(1);
   root->left = addNode(2);
   root->right = addNode(3);
   root->left->left = addNode(4);
   root->left->right = addNode(5);
   root->left->right->right = addNode(6);
   root->right->right = addNode(7);
   root->right->right->right = addNode(8);
   root->right->right->right->left = addNode(9);

   cout << "Number of single childs are : " << countSingleChild(root) << endl;
   return 0;
}

Terminal OpenF splitList.cpp LL1.0pp treeSinglechild.cpp #1nclude <tos trees Lnclude <stdlib. h using nanespace std; typedef

Add a comment
Know the answer?
Add Answer to:
Given a pointer to a binary tree write a routine which will traverse the tree 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
  • java please IV. Given a pointer to a binary tree write a routine which will traverse...

    java please IV. Given a pointer to a binary tree write a routine which will traverse the tree and return the 59 and 112 number of nodes in the tree whose information field which is an integer is between In addition return the value of the node with the largest integer and as well return a count or the number of nodes that have no sons. For the 3rd part do not include nodes that have both right and left...

  • in java please thanks! Given a pointer to a binary tree write a routine which will...

    in java please thanks! Given a pointer to a binary tree write a routine which will traverse the tree and return the number of nodes in the tree whose information field which is an integer is between 59 and 112 In addition return the value of the node with the largest integer and as well return a count of the number of nodes that have no sons. For the 3rd part do not include nodes that have both right and...

  • in javascrift please Given a pointer to the root of a binary tree write a routine...

    in javascrift please Given a pointer to the root of a binary tree write a routine that will delete every node in the tree that is currently a leaf (has no sons), and when finished tell you how many nodes were deleted as well as how many nodes are left in the tree.

  • CODE IN JAVA** V. Given a pointer to the root of a binary tree and a...

    CODE IN JAVA** V. Given a pointer to the root of a binary tree and a pointer ‘p’ to a given node in the tree and a second pointer ‘q’ to another node in the tree write a routine which will return the total number of nodes in the tree that are on level ‘p’ and ‘q’. If they are on the same level you should multiply the answer by 3.

  • In C++ Given a pointer to the root of a binary search tree (has left, right,...

    In C++ Given a pointer to the root of a binary search tree (has left, right, and parent pointers as well as a data section ) write a function (or functions) which will return an STL list (you should not define this class, it’s already included) with all of the values from the tree in sorted order. Your code should run in theta(N) time. for the second part,.given a pointer to the first node of a linked list, you are...

  • Create a binary search tree in C++, where it will have an add_node(int i) function which...

    Create a binary search tree in C++, where it will have an add_node(int i) function which when called in the main (i.e. add_node(5)) it will add it to the tree. This tree will also place the nodes added in the correct order (if the head node is 40, all the nodes that are added that are less than 40 go to the left of the tree, and all the nodes larger than 40 go to the right) Also add a...

  • Write a Java function named smallcount that, given the pointer to the root of a Binary...

    Write a Java function named smallcount that, given the pointer to the root of a Binary Search Tree (BST) and a key K, returns the number of nodes having key values less than or equal to K. Function smallcount should visit as few nodes in the BST as possible.

  • A balanced binary tree is a binary tree structure in which the left and right subtrees of every node differ

    Data structures C++1- A balanced binary tree is a binary tree structure in which the left and right subtrees of every node differ in height by no more than 1 Out of the following choices, which is the minimum set of nodes, if removed, will make the BST balanced?2- Which of the following is true for Red-Black Trees ? Select all choices that apply! Select one or more: a. For each node in the tree, all paths from that node to any leaf nodes contain...

  • Trees Traversals Please write a Java program to traverse a binary tree in in-order and pre-order,...

    Trees Traversals Please write a Java program to traverse a binary tree in in-order and pre-order, and to plot a binary tree into a 2-dimensional array. You may write many recursive methods for this project. You are not allowed to use any existing Java classes such as ArrayList or Vector or Tree. Please stop your program if the user enters 0 as the tree selection. Your program must run the following Test Case 1 plus two more test cases to...

  • 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...

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