I'm getting error C2109: subscript requires array or pointer type

// CPP btree.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

class TreeNode 
{
public:
         TreeNode(string str): data(str), left(NULL), right(NULL) {} // Constructor.  Make a node containing str.

         string data;       // The data in this node.
         TreeNode *left;    // Pointer to left subtree.
         TreeNode *right;   // Pointer to right subtree.
};

typedef TreeNode* TreeNodePtr;

void treeInsert(TreeNodePtr& root, string newItem) 
{
          if ( root == NULL ) {
             root = new TreeNode( newItem );
             return;
          }
          else if ( newItem < root->data ) {
             treeInsert( root->left, newItem );
          }
          else 
          {
             treeInsert( root->right, newItem );
          }
}  // end treeInsert()

void traverseinorder(TreeNode *root )  // a recursive function to print the nodes values based on an inorder traversal
    {
        if ( root != NULL ) {  
           traverseinorder( root->left );    
           cout << root->data << " ";     
           traverseinorder( root->right );   
        }
     } // end traverseinorder()

int size( TreeNode *root )  // the number of nodes in the tree
     {
        if ( root == NULL )
           return 0;  // The tree is empty.  It contains no nodes.
        else 
        {
           int count = 1;   // Start by counting the root.
           count += size(root->left);  // Add the number of nodes in the left subtree.
                                         
           count += size(root->right); // Add the number of nodes in the right subtree.
                                        
           return count;  
        }
     } // end size()

bool isleaf(TreeNode *root) //returns true if the node is a leaf
{
  return (root->left==NULL && root->right==NULL);
}
 
int parent(int pos)                                // returns the index of the parent
{
    return (pos - 1) / 2;
}

int leftchild(int pos)  // returns the index of the left child
{
return (pos * 2) + 1;
}

int rightchild(int pos)  // returns the index of the right child
{
return (pos * 2) + 2;
}
 
int height(TreeNode *root) // returns the height of the tree
{
  if (root==NULL) 
  {
    return(0);
  }
  else 
  {
    // compute the depth of each subtree
    int lheight = height(root->left);
    int rheight = height(root->right);

    // use the larger one
    if (lheight > rheight) return(lheight+1);
    else return(rheight+1);
  }
}  

int main(int argc, char* argv[])
{

treeInsert[0]=5;
treeInsert[1]=10;
treeInsert[2]=2;
treeInsert[3]=5;
treeInsert[4]=7;
treeInsert[5]=12;
treeInsert[6]=3;
treeInsert[7]=9;
treeInsert[8]=8;


cout << leftchild(3);
cout << height();
cout << isleaf(7);
cout << isleaf(2);
cout << parent(5);
traverseinorder();
}

Dani AI

Generated

The original compile error (C2109) came from treating a function like an array (for example treeInsert[0] = 5;). spotted that early. After that moved toward an array-based representation but mixed pointer-style code and integer/index-style code: that’s the root cause of most remaining errors (comparing indexes to NULL, calling root->leftchild, using tree-node pointers and treenodes[] at the same time).

Pick one model and stick to it. Since the code in main fills treenodes[], the simplest fix is to treat the tree as an implicit binary tree stored in an array (indexes 0..size-1). Important rules:

  • Left child of index i is 2i+1; right child is 2i+2.
  • A child index is invalid if it is >= size (use -1 as the “no child” sentinel).
  • The parent of index 0 does not exist (return -1); for others use (i-1)/2.
  • Don’t compare integer indexes to NULL; NULL is a pointer concept (0) and leads to confusion.

Here are compact, correct helper routines (place inside BTree as members):

int left_index(int i) const { int c = 2*i + 1; return (i<0 || i>=size || c>=size) ? -1 : c; }
int right_index(int i) const { int c = 2*i + 2; return (i<0 || i>=size || c>=size) ? -1 : c; }
int parent_index(int i) const { if (i<=0 || i>=size) return -1; return (i-1)/2; }
bool is_leaf(int i) const { return left_index(i)==-1 && right_index(i)==-1; }

void traverse_inorder(int i=0) const {
  if (i<0 || i>=size) return;
  int L = left_index(i); if (L!=-1) traverse_inorder(L);
  std::cout << treenodes[i] << ' ';
  int R = right_index(i); if (R!=-1) traverse_inorder(R);
}

int height(int i=0) const {
  if (i<0 || i>=size) return 0;
  int lh = height(left_index(i)), rh = height(right_index(i));
  return 1 + (lh>rh ? lh : rh);
}

Quick debugging tips: print indexes while testing, initialize size before using helpers, and consider std::vector<int> instead of a fixed array. ’s request for variable declarations is spot-on — ensure your class members and function signatures match the representation you choose.

Recommended Answers

All 8 Replies

treeInsert is a function, not an array...

Where are the variable declarations for treeInsert, leftchild and the such?

Ok I scraped that and started new. Not sure if it is an improvement or not.

// CPP btree.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

class BTree 
{
    
    struct tree_node 
    {
        tree_node *left;   // left subtree has smaller elements
        tree_node *right;  // right subtree has larger elements
        int data;
    };
tree_node *root;

public:
        int treenodes[1000];  // structure to store the tree nodes values
        
        int size;  // the number of nodes in the tree

        BTree(void);  // the "constructor"

        bool isleaf(int nodeindex);  // returns true if the node is a leaf
        int parent(int nodeindex);  // returns the index of the parent
        int leftchild(int nodeindex);  // returns the index of the left child
        int rightchild(int nodeindex);  // returns the index of the right child

        int height();  // returns the height of the tree
        void traverseinorder();  // a recursive function to print the nodes values based on an inorder traversal
};

BTree::BTree()
{
    root=new tree_node;
    root->left=NULL;
    root->right=NULL;
}

bool BTree::isleaf(int nodeindex) //returns true if the node is a leaf
{
  return (nodeindex->left==NULL && nodeindex->right==NULL);
}
 
int BTree::parent(int nodeindex)  // returns the index of the parent
{
        int rv =0; 

    rv = (nodeindex - 1) / 2; 
    if (rv >= size)  
        rv = -1 ;

    return rv ;
}

int BTree::leftchild(int nodeindex)  // returns the index of the left child
{
    int rv =0; 

    rv = (nodeindex * 2) + 1; 
    if (rv <= size)  
        rv = -1 ;

    return rv ;
}

int BTree::rightchild(int nodeindex)  // returns the index of the right child
{
    int rv =0; 

    rv = (nodeindex + 1) * 2; 
    if (rv >= size)  
        rv = -1 ;

    return rv ;
}

void traverseinorder(BTree *root )  // a recursive function to print the nodes values based on an inorder traversal
    {
        if ( root != NULL ) {  
           traverseinorder( root->left );    
           cout << root->item << " ";     
           traverseinorder( root->right );   
        }
     } // end traverseinorder()

int height(BTree *root) // returns the height of the tree
{
  if (root==NULL) 
  {
    return(0);
  }
  else 
  {
    // compute the depth of each subtree
    int lheight = height(root->left);
    int rheight = height(root->right);

    // use the larger one
    if (lheight > rheight) return(lheight+1);
    else return(rheight+1);
  }
}  

int main(int argc, char* argv[])
{
    BTree tree1;

        tree1.treenodes[0]=5;
        tree1.treenodes[1]=10;
        tree1.treenodes[2]=2;
        tree1.treenodes[3]=5;
        tree1.treenodes[4]=7;
        tree1.treenodes[5]=12;
        tree1.treenodes[6]=3;
        tree1.treenodes[7]=9;
        tree1.treenodes[8]=8;

        //set the size here.  size is a member of the bTree class so we need our object.member syntax
        tree1.size = 9;

        cout << "The left child of node 3 is:  " << tree1.leftchild(3) << endl;
        cout << "The height of the tree is:    " << tree1.height() << endl;
        cout << "The node 7 is a leaf:         " << tree1.isleaf(7) << endl;
        cout << "The node 2 is a leaf:         " << tree1.isleaf(2) << endl;
        cout << "The parent of node 5 is:      " << tree1.parent(5) << endl;

        cout << "Values for inorder traversal: " <<  endl;
        tree1.traverseinorder();

        return 0;
}

in the traverse I found and fixed this

cout << root->data << " ";

closer if I can figure out the traverse and height

// CPP btree.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

class BTree 
{
public:
        int treenodes[1000];  // structure to store the tree nodes values
        
        int size;  // the number of nodes in the tree

        BTree(void);  // the "constructor"

        bool isleaf(int nodeindex);  // returns true if the node is a leaf
        int parent(int nodeindex);  // returns the index of the parent
        int leftchild(int nodeindex);  // returns the index of the left child
        int rightchild(int nodeindex);  // returns the index of the right child

        int height();  // returns the height of the tree
        void traverseinorder();  // a recursive function to print the nodes values based on an inorder traversal
};

bool BTree::isleaf(int nodeindex) //returns true if the node is a leaf
{
    return (BTree::leftchild(nodeindex)==NULL && BTree::rightchild(nodeindex)==NULL);
}
 
int BTree::parent(int nodeindex)  // returns the index of the parent
{
        int rv =0; 

    rv = (nodeindex - 1) / 2; 
    if (rv >= size)  
        rv = -1 ;

    return rv ;
}

int BTree::leftchild(int nodeindex)  // returns the index of the left child
{
    int rv =0; 

    rv = (nodeindex * 2) + 1; 
    if (rv <= size)  
        rv = -1 ;

    return rv ;
}

int BTree::rightchild(int nodeindex)  // returns the index of the right child
{
    int rv =0; 

    rv = (nodeindex + 1) * 2; 
    if (rv >= size)  
        rv = -1 ;

    return rv ;
}

void traverseinorder()  // a recursive function to print the nodes values based on an inorder traversal
    {
        if ( size!= NULL ) {  
             
           cout <<   " ";     
             
        }
     } // end traverseinorder()

int height() // returns the height of the tree
{
  if (size==NULL) 
  {
    return(0);
  }
  else 
  {
    // compute the depth of leftchild
    int lheight = height();
    
    return(lheight+1);
  }
}  

int main(int argc, char* argv[])
{
    BTree tree1;

        tree1.treenodes[0]=5;
        tree1.treenodes[1]=10;
        tree1.treenodes[2]=2;
        tree1.treenodes[3]=5;
        tree1.treenodes[4]=7;
        tree1.treenodes[5]=12;
        tree1.treenodes[6]=3;
        tree1.treenodes[7]=9;
        tree1.treenodes[8]=8;

        //set the size here.  size is a member of the bTree class so we need our object.member syntax
        tree1.size = 9;

        cout << "The left child of node 3 is:  " << tree1.leftchild(3) << endl;
        cout << "The height of the tree is:    " << tree1.height() << endl;
        cout << "The node 7 is a leaf:         " << tree1.isleaf(7) << endl;
        cout << "The node 2 is a leaf:         " << tree1.isleaf(2) << endl;
        cout << "The parent of node 5 is:      " << tree1.parent(5) << endl;

        cout << "Values for inorder traversal: " <<  endl;
        tree1.traverseinorder();

        return 0;
}

Ok this is where i am now

// CPP btree.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

class BTree 
{
public:
        int treenodes[1000];  // structure to store the tree nodes values
        
        int size;  // the number of nodes in the tree

 //       BTree(void);  // the "constructor"

        bool isleaf(int nodeindex);  // returns true if the node is a leaf
        int parent(int nodeindex);  // returns the index of the parent
        int leftchild(int nodeindex);  // returns the index of the left child
        int rightchild(int nodeindex);  // returns the index of the right child

        int height();  // returns the height of the tree
        void traverseinorder();  // a recursive function to print the nodes values based on an inorder traversal
};

bool BTree::isleaf(int nodeindex) //returns true if the node is a leaf
{
    return (BTree::leftchild(nodeindex)==NULL && BTree::rightchild(nodeindex)==NULL) ;
}
 
int BTree::parent(int nodeindex)  // returns the index of the parent
{
        int rv =0; 

    rv = (nodeindex - 1) / 2; 
    if (rv >= size)  
        rv = -1 ;

    return rv ;
}

int BTree::leftchild(int nodeindex)  // returns the index of the left child
{
    int rv =0; 

    rv = (nodeindex +1) *2 - 1; 
    if (rv >= size)  
        rv = -1 ;

    return rv ;
}

int BTree::rightchild(int nodeindex)  // returns the index of the right child
{
    int rv =0; 

    rv = (nodeindex +1) * 2; 
    if (rv <= size)  
        rv = -1 ;

    return rv ;
}

void BTree::traverseinorder(BTree *root)  // a recursive function to print the nodes values based on an inorder traversal
    {
        if ( root!= NULL ) 
        {  
           traverseinorder(root->leftchild));   
           cout << root << " ";     
           traverseinorder(root->rightchild); 
        }
     } // end traverseinorder()

int BTree::height(BTree *root) // returns the height of the tree
{
 if (root==NULL) 
 {
   return(0);
  }
 else 
 {
    //compute the depth of leftchild
    int lheight = height(root->leftchild);
    
    return(lheight+1);
  }
}  

int main(int argc, char* argv[])
{
    BTree tree1;

        tree1.treenodes[0]=5;
        tree1.treenodes[1]=10;
        tree1.treenodes[2]=2;
        tree1.treenodes[3]=5;
        tree1.treenodes[4]=7;
        tree1.treenodes[5]=12;
        tree1.treenodes[6]=3;
        tree1.treenodes[7]=9;
        tree1.treenodes[8]=8;

        //set the size here.  size is a member of the bTree class so we need our object.member syntax
        tree1.size = 9;

        cout << "The left child of node 3 is:  " << tree1.leftchild(3) << endl;
 //       cout << "The height of the tree is:    " << tree1.height() << endl;
        cout << "The node 7 is a leaf:         " << tree1.isleaf(7) << endl;
        cout << "The node 2 is a leaf:         " << tree1.isleaf(2) << endl;
        cout << "The parent of node 5 is:      " << tree1.parent(5) << endl;

        cout << "Values for inorder traversal: " <<  endl;
    //    tree1.traverseinorder();

        return 0;
}

Sorry, but could you please a little explanation of what your code is supposed to do? I refuse to read all code and try to figure that out.


Thanks in advance,
Eddy

Actually if you look in the main at the end that is it. Fill the tree with the data and spit out a few results.
tree1.treenodes[0]=5;
tree1.treenodes[1]=10;
tree1.treenodes[2]=2;
tree1.treenodes[3]=5;
tree1.treenodes[4]=7;
tree1.treenodes[5]=12;
tree1.treenodes[6]=3;
tree1.treenodes[7]=9;
tree1.treenodes[8]=8;
output
cout << "The left child of node 3 is: " << tree1.leftchild(3) << endl;
cout << "The height of the tree is: " << tree1.height() << endl;
cout << "The node 7 is a leaf: " << tree1.isleaf(7) << endl;
cout << "The node 2 is a leaf: " << tree1.isleaf(2) << endl;
cout << "The parent of node 5 is: " << tree1.parent(5) << endl;

cout << "Values for inorder traversal: " << endl;
// tree1.traverseinorder();

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.