0

I've come across an error in my code upon compilation, and I'm a bit stumped as to how to proceed. I don't think I'm using the most up-to-date version of Visual Studio. I am not completely new to C++, nor am I anywhere near seasoned - but I am sure that my functions are in the correct place and int main() is fine. So I'm just a little confused. If anyone can give me a second pair of eyes, and explain my error/wrong doing, and how I can do better going forward, I would greatly appreciate it!

#include <iostream>
#include <queue>
#include <fstream>
#include "binaryTreeType.h"
#include "binarySearchTree.h"
using namespace std;

struct node {

    int data;
    node* left;
    node* right;
};

node* newNode(int item) {

    node* temp = new node;
    temp->data = item;
    temp->left = NULL;
    temp->right = NULL;
    return temp;
}

//Inorder traversal of tree
void inorder(node* root) {

    if (root != NULL) {
        inorder(root->left);
        printf("%d ", root->data);
        inorder(root->right);
    }
}

//calculate height of tree
int height(node* root) {

    if (root == NULL)
        return 0;

    else
    {
        int l = height(root->left);
        int r = height(root->right);
        return max(l, r) + 1;
    }
}

node* binarySearchTree(node* head, int data) {

    if (head == NULL) return newNode(data);

    if (data < head->data)

        head->left = binarySearchTree(head->left, data);

    else if (data > head->data)

        head->right = binarySearchTree(head->right, data);

    return head;

}

//caluclate single parents of Binary Search Tree
int singleParent(node* root) {

    queue<node*> qp;
    int ans = 0;

    if (root == NULL) {

        return 0;
    }

    qp.push(root);

    while (!qp.empty()) {

        node* f = qp.front();

        qp.pop();

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

            ans++;

        }

        if (f->left != NULL) {

            qp.push(f->left);

        }

        if (f->right != NULL) {

            qp.push(f->right);

        }

    }

    return ans;
}

int main() {

    node* root = NULL;
    cout << "Please enter elements ending with -1:" << endl;
    int x;
    cin >> x;

    while (x != -1) {

        root = binarySearchTree(root, x);

        cin >> x;
    }


    //display
    cout << "The tree elements within Inorder are: " << inorder(root) << endl;
    cout << "Tree height is: " << height(root) << endl;
    cout << "Number of single parents: " << singleParent(root) << endl;

    return 0;
}
Remy Lebeau
  • 505,946
  • 29
  • 409
  • 696
sticks_
  • 13
  • 2
  • Visual Studio (VS) requires you to either create files that will be compiled in VS or add them to the project (Project->Add->New Item or something like that) or it doesn't build and link them in. – user4581301 Nov 16 '21 at 20:41

0 Answers0