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();
}

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.