Click here to Skip to main content
15,304,161 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
/* Program to find singles in a given binary tree */
#include <bits stdc++.h="">
using namespace std;

// A Binary Tree Node
struct node
{
struct node *left, *right;
int key;
};

// Utility function to create a new tree node
node* newNode(int key)
{
node *temp = new node;
temp->key = key;
temp->left = temp->right = NULL;
return temp;
}

// Function to print all non-root nodes
// that don't have a sibling
void printSingles(struct node *root)
{
// Base case
if (root == NULL)
return;

// If this is an internal node, recur for left
// and right subtrees
if (root->left != NULL && root->right != NULL)
{
printSingles(root->left);
printSingles(root->right);
}

// If left child is NULL and right is not,
// print right child
// and recur for right child
else if (root->right != NULL)
{
cout << root->right->key << " ";
printSingles(root->right);
}

// If right child is NULL and left is
// not, print left child
// and recur for left child
else if (root->left != NULL)
{
cout << root->left->key << " ";
printSingles(root->left);
}
}

// Driver program to test above functions
int main()
{
// Let us create binary tree
// given in the above example
node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->right = newNode(4);
root->right->left = newNode(5);
root->right->left->left = newNode(6);
printSingles(root);
return 0;
}

What I have tried:

/* Program to find singles in a given binary tree */
#include <stdio.h>
#include <stdlib.h>


// A Binary Tree Node
struct node
{
    struct node *left, *right;
    int key;
};

// Utility function to create a new tree node
struct node* newNode(int key)
{
    struct node * newNode(struct node* key)
    temp->key = key;
    temp->left = temp->right = NULL;
    return temp;
}

// Function to print all non-root nodes
// that don't have a sibling
void printSingles(struct node *root);
{
    // Base case
    if (root == NULL)
      return;

    // If this is an internal node, recur for left
    // and right subtrees
    if (root->left != NULL && root->right != NULL)
    {
        printSingles(root->left);
        printSingles(root->right);
    }

    // If left child is NULL and right is not,
    // print right child
    // and recur for right child
    else if (root->right != NULL)
    {
        cout << root->right->key << " ";
        printSingles(root->right);
    }

    // If right child is NULL and left is
    // not, print left child
    // and recur for left child
    else if (root->left != NULL)
    {
       printf( "root->left->key" ) ;
        printSingles(root->left);
    }
}

// Driver program to test above functions
int main()
{
    // Let us create binary tree
    // given in the above example
    node *root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->right = newNode(4);
    root->right->left = newNode(5);
    root->right->left->left = newNode(6);
    printSingles(root);
    return 0;
}
Posted
Updated 6-Jan-22 19:22pm
Comments
_Asif_ 7-Jan-22 0:31am
   
Where have you been stuck> what error you are getting?

C++
// Utility function to create a new tree node
struct node* newNode(int key)
{
    struct node * newNode(struct node* key)
    temp->key = key;
    temp->left = temp->right = NULL;
    return temp;
}

What is temp in this code ?
The first line of function is weird to say the least. What is it supposed to do ?
   
v2
There are a couple of things you need to understand here:
1) C and C++ are very different languages - C is ancient history and C++ is a superset of C. That means that C++ code works in a very different way to C code, and contains all sorts of concepts that aren't available at all in C. It's like saying "a Model T Ford is a Car, and so is a Tesla Model S, so the engine from the Tesla will fit perfectly into the Ford with no effort whatsoever".
Obviously, it won't: the two are so far apart in time that they don't share a single component, or even many concepts beyond "personal transportation with four wheels".
It's probably possible to do it - and I'm sure some lunatic will try the Tesla / Ford hybrid - but it'll involve a lot of effort and what you get in the end isn't going to be any good as either of them.

Converting C++ to C is the same deal: well written C++ doesn't translate into well written C for the same reasons: C has no concept of classes, of vectors, streams, strings, or pretty much anything other than the basic "four wheels" idea.

And now we come to the code you want to translate - and that isn't good C++, it's somebody's homework and while I'm sure they were proud of it when they handed it in, it's pretty clear that they had little idea what they were doing.

2) Homework is set for a reason, and it's not to annoy you! The idea is that coding is a skill, a mindset, and you need experience to learn how to do it. Just like when you were learning to ride a bike you can't just jump on and zoom off into the distance because you don't have the reflexes built up to keep you and the bike upright and until you develop those, you will fall over a lot. Software is the same: if you don't write it yourself, you never learn how to write it - and that means that the next exercise (which will be built on what you learned from this one) will be much harder for you because you don't learn by looking at code andy more than you learn to ride a bike by watching the Tour de France!

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]

Will it translate? Yes. If you know both languages, it's a fairly trivial job with that badly written chunk. Is it a good idea to do it? No. So stop trying to cheat your homework, and give it a try - it's a simple enough task that it shouldn't take you long at all and it'll make your life a whole lot easier in thw future!
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900