Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include \"2170_9_7a.h\" #include <iostream> #include <stdlib.h> using namespace

ID: 3556178 • Letter: #

Question

#include "2170_9_7a.h"
#include <iostream>
#include <stdlib.h>

using namespace std;

//The constructor reads in all the integers and stores them in the
//object of type dataStructure
dataStructure::dataStructure()
{
    startPtr = NULL;
    node* ptr = startPtr;
    node* prev = startPtr;
    int data;

    while(cin >> data)
    {
        if(ptr == NULL)
        {
            startPtr = new node;
            startPtr->data = data;
            startPtr->left = NULL;
            startPtr->right = NULL;
        }
        else
        {
            while(ptr != NULL)
            {
                if(data <= ptr->data)
                {
                    prev = ptr;
                    ptr = ptr->left;
                    if(ptr == NULL)
                    {
                        ptr = new node;
                        ptr->data = data;
                        ptr->left = NULL;
                        ptr->right = NULL;
                        prev->left = ptr;
                        ptr=NULL;
                    }
                }
                else if(data > ptr->data)
                {
                    prev = ptr;
                    ptr = ptr->right;
                    if(ptr == NULL)
                    {
                        ptr = new node;
                        ptr->data = data;
                        ptr->left = NULL;
                        ptr->right = NULL;
                        prev->right = ptr;
                        ptr=NULL;
                    }
                }
            }
            
        }
        ptr = startPtr;
    }
}

//countNodes is the helper function that the client calls.
//It calls the recursive function countAll which will count the nodes.
//It passes the startPtr as the starting point for counting.
int dataStructure::countNodes()
{
    int n = 0;

    n = countAll(startPtr);

    return n;
}


// put code for the recursive function countAll(node* ptr) that will
// count the nodes within the dataStructure.
// countAll should count the node pointed at by ptr.  Then count
// all the nodes in the left data set, then count all the nodes
// in the right data set.
int dataStructure::countAll(node* ptr)
{
    int n = 0;
    if(ptr!=NULL)
    {
        n+=1;
        //insert the recursive code necessary to count the nodes in the
        //left data set and then count the nodes in the right data set
         write the recursive code here that will count the nodes in the left data set and then count the nodes in the right data set

    }
    return n;
}

Explanation / Answer

Just write :

n+=countAll(ptr->left) + countAll (ptr->right) ; // Replace this with bolded words