944,167 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1417
  • C++ RSS
Jul 14th, 2006
0

Trouble shooting help

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

C++ Syntax (Toggle Plain Text)
  1. // CPP btree.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. class TreeNode
  7. {
  8. public:
  9. TreeNode(string str): data(str), left(NULL), right(NULL) {} // Constructor. Make a node containing str.
  10.  
  11. string data; // The data in this node.
  12. TreeNode *left; // Pointer to left subtree.
  13. TreeNode *right; // Pointer to right subtree.
  14. };
  15.  
  16. typedef TreeNode* TreeNodePtr;
  17.  
  18. void treeInsert(TreeNodePtr& root, string newItem)
  19. {
  20. if ( root == NULL ) {
  21. root = new TreeNode( newItem );
  22. return;
  23. }
  24. else if ( newItem < root->data ) {
  25. treeInsert( root->left, newItem );
  26. }
  27. else
  28. {
  29. treeInsert( root->right, newItem );
  30. }
  31. } // end treeInsert()
  32.  
  33. void traverseinorder(TreeNode *root ) // a recursive function to print the nodes values based on an inorder traversal
  34. {
  35. if ( root != NULL ) {
  36. traverseinorder( root->left );
  37. cout << root->data << " ";
  38. traverseinorder( root->right );
  39. }
  40. } // end traverseinorder()
  41.  
  42. int size( TreeNode *root ) // the number of nodes in the tree
  43. {
  44. if ( root == NULL )
  45. return 0; // The tree is empty. It contains no nodes.
  46. else
  47. {
  48. int count = 1; // Start by counting the root.
  49. count += size(root->left); // Add the number of nodes in the left subtree.
  50.  
  51. count += size(root->right); // Add the number of nodes in the right subtree.
  52.  
  53. return count;
  54. }
  55. } // end size()
  56.  
  57. bool isleaf(TreeNode *root) //returns true if the node is a leaf
  58. {
  59. return (root->left==NULL && root->right==NULL);
  60. }
  61.  
  62. int parent(int pos) // returns the index of the parent
  63. {
  64. return (pos - 1) / 2;
  65. }
  66.  
  67. int leftchild(int pos) // returns the index of the left child
  68. {
  69. return (pos * 2) + 1;
  70. }
  71.  
  72. int rightchild(int pos) // returns the index of the right child
  73. {
  74. return (pos * 2) + 2;
  75. }
  76.  
  77. int height(TreeNode *root) // returns the height of the tree
  78. {
  79. if (root==NULL)
  80. {
  81. return(0);
  82. }
  83. else
  84. {
  85. // compute the depth of each subtree
  86. int lheight = height(root->left);
  87. int rheight = height(root->right);
  88.  
  89. // use the larger one
  90. if (lheight > rheight) return(lheight+1);
  91. else return(rheight+1);
  92. }
  93. }
  94.  
  95. int main(int argc, char* argv[])
  96. {
  97.  
  98. treeInsert[0]=5;
  99. treeInsert[1]=10;
  100. treeInsert[2]=2;
  101. treeInsert[3]=5;
  102. treeInsert[4]=7;
  103. treeInsert[5]=12;
  104. treeInsert[6]=3;
  105. treeInsert[7]=9;
  106. treeInsert[8]=8;
  107.  
  108.  
  109. cout << leftchild(3);
  110. cout << height();
  111. cout << isleaf(7);
  112. cout << isleaf(2);
  113. cout << parent(5);
  114. traverseinorder();
  115. }
Similar Threads
Reputation Points: 12
Solved Threads: 0
Light Poster
schmidty169 is offline Offline
32 posts
since Jul 2006
Jul 14th, 2006
0

Re: Trouble shooting help

treeInsert is a function, not an array...
Reputation Points: 108
Solved Threads: 14
Junior Poster in Training
GloriousEremite is offline Offline
65 posts
since Jul 2006
Jul 14th, 2006
0

Re: Trouble shooting help

Where are the variable declarations for treeInsert, leftchild and the such?
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Jul 14th, 2006
0

Re: Trouble shooting help

Ok I scraped that and started new. Not sure if it is an improvement or not.
C++ Syntax (Toggle Plain Text)
  1. // CPP btree.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. class BTree
  7. {
  8.  
  9. struct tree_node
  10. {
  11. tree_node *left; // left subtree has smaller elements
  12. tree_node *right; // right subtree has larger elements
  13. int data;
  14. };
  15. tree_node *root;
  16.  
  17. public:
  18. int treenodes[1000]; // structure to store the tree nodes values
  19.  
  20. int size; // the number of nodes in the tree
  21.  
  22. BTree(void); // the "constructor"
  23.  
  24. bool isleaf(int nodeindex); // returns true if the node is a leaf
  25. int parent(int nodeindex); // returns the index of the parent
  26. int leftchild(int nodeindex); // returns the index of the left child
  27. int rightchild(int nodeindex); // returns the index of the right child
  28.  
  29. int height(); // returns the height of the tree
  30. void traverseinorder(); // a recursive function to print the nodes values based on an inorder traversal
  31. };
  32.  
  33. BTree::BTree()
  34. {
  35. root=new tree_node;
  36. root->left=NULL;
  37. root->right=NULL;
  38. }
  39.  
  40. bool BTree::isleaf(int nodeindex) //returns true if the node is a leaf
  41. {
  42. return (nodeindex->left==NULL && nodeindex->right==NULL);
  43. }
  44.  
  45. int BTree::parent(int nodeindex) // returns the index of the parent
  46. {
  47. int rv =0;
  48.  
  49. rv = (nodeindex - 1) / 2;
  50. if (rv >= size)
  51. rv = -1 ;
  52.  
  53. return rv ;
  54. }
  55.  
  56. int BTree::leftchild(int nodeindex) // returns the index of the left child
  57. {
  58. int rv =0;
  59.  
  60. rv = (nodeindex * 2) + 1;
  61. if (rv <= size)
  62. rv = -1 ;
  63.  
  64. return rv ;
  65. }
  66.  
  67. int BTree::rightchild(int nodeindex) // returns the index of the right child
  68. {
  69. int rv =0;
  70.  
  71. rv = (nodeindex + 1) * 2;
  72. if (rv >= size)
  73. rv = -1 ;
  74.  
  75. return rv ;
  76. }
  77.  
  78. void traverseinorder(BTree *root ) // a recursive function to print the nodes values based on an inorder traversal
  79. {
  80. if ( root != NULL ) {
  81. traverseinorder( root->left );
  82. cout << root->item << " ";
  83. traverseinorder( root->right );
  84. }
  85. } // end traverseinorder()
  86.  
  87. int height(BTree *root) // returns the height of the tree
  88. {
  89. if (root==NULL)
  90. {
  91. return(0);
  92. }
  93. else
  94. {
  95. // compute the depth of each subtree
  96. int lheight = height(root->left);
  97. int rheight = height(root->right);
  98.  
  99. // use the larger one
  100. if (lheight > rheight) return(lheight+1);
  101. else return(rheight+1);
  102. }
  103. }
  104.  
  105. int main(int argc, char* argv[])
  106. {
  107. BTree tree1;
  108.  
  109. tree1.treenodes[0]=5;
  110. tree1.treenodes[1]=10;
  111. tree1.treenodes[2]=2;
  112. tree1.treenodes[3]=5;
  113. tree1.treenodes[4]=7;
  114. tree1.treenodes[5]=12;
  115. tree1.treenodes[6]=3;
  116. tree1.treenodes[7]=9;
  117. tree1.treenodes[8]=8;
  118.  
  119. //set the size here. size is a member of the bTree class so we need our object.member syntax
  120. tree1.size = 9;
  121.  
  122. cout << "The left child of node 3 is: " << tree1.leftchild(3) << endl;
  123. cout << "The height of the tree is: " << tree1.height() << endl;
  124. cout << "The node 7 is a leaf: " << tree1.isleaf(7) << endl;
  125. cout << "The node 2 is a leaf: " << tree1.isleaf(2) << endl;
  126. cout << "The parent of node 5 is: " << tree1.parent(5) << endl;
  127.  
  128. cout << "Values for inorder traversal: " << endl;
  129. tree1.traverseinorder();
  130.  
  131. return 0;
  132. }
Reputation Points: 12
Solved Threads: 0
Light Poster
schmidty169 is offline Offline
32 posts
since Jul 2006
Jul 14th, 2006
0

Re: Trouble shooting help

in the traverse I found and fixed this
C++ Syntax (Toggle Plain Text)
  1. cout << root->data << " ";
Reputation Points: 12
Solved Threads: 0
Light Poster
schmidty169 is offline Offline
32 posts
since Jul 2006
Jul 14th, 2006
0

Re: Trouble shooting help

closer if I can figure out the traverse and height
C++ Syntax (Toggle Plain Text)
  1. // CPP btree.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. class BTree
  7. {
  8. public:
  9. int treenodes[1000]; // structure to store the tree nodes values
  10.  
  11. int size; // the number of nodes in the tree
  12.  
  13. BTree(void); // the "constructor"
  14.  
  15. bool isleaf(int nodeindex); // returns true if the node is a leaf
  16. int parent(int nodeindex); // returns the index of the parent
  17. int leftchild(int nodeindex); // returns the index of the left child
  18. int rightchild(int nodeindex); // returns the index of the right child
  19.  
  20. int height(); // returns the height of the tree
  21. void traverseinorder(); // a recursive function to print the nodes values based on an inorder traversal
  22. };
  23.  
  24. bool BTree::isleaf(int nodeindex) //returns true if the node is a leaf
  25. {
  26. return (BTree::leftchild(nodeindex)==NULL && BTree::rightchild(nodeindex)==NULL);
  27. }
  28.  
  29. int BTree::parent(int nodeindex) // returns the index of the parent
  30. {
  31. int rv =0;
  32.  
  33. rv = (nodeindex - 1) / 2;
  34. if (rv >= size)
  35. rv = -1 ;
  36.  
  37. return rv ;
  38. }
  39.  
  40. int BTree::leftchild(int nodeindex) // returns the index of the left child
  41. {
  42. int rv =0;
  43.  
  44. rv = (nodeindex * 2) + 1;
  45. if (rv <= size)
  46. rv = -1 ;
  47.  
  48. return rv ;
  49. }
  50.  
  51. int BTree::rightchild(int nodeindex) // returns the index of the right child
  52. {
  53. int rv =0;
  54.  
  55. rv = (nodeindex + 1) * 2;
  56. if (rv >= size)
  57. rv = -1 ;
  58.  
  59. return rv ;
  60. }
  61.  
  62. void traverseinorder() // a recursive function to print the nodes values based on an inorder traversal
  63. {
  64. if ( size!= NULL ) {
  65.  
  66. cout << " ";
  67.  
  68. }
  69. } // end traverseinorder()
  70.  
  71. int height() // returns the height of the tree
  72. {
  73. if (size==NULL)
  74. {
  75. return(0);
  76. }
  77. else
  78. {
  79. // compute the depth of leftchild
  80. int lheight = height();
  81.  
  82. return(lheight+1);
  83. }
  84. }
  85.  
  86. int main(int argc, char* argv[])
  87. {
  88. BTree tree1;
  89.  
  90. tree1.treenodes[0]=5;
  91. tree1.treenodes[1]=10;
  92. tree1.treenodes[2]=2;
  93. tree1.treenodes[3]=5;
  94. tree1.treenodes[4]=7;
  95. tree1.treenodes[5]=12;
  96. tree1.treenodes[6]=3;
  97. tree1.treenodes[7]=9;
  98. tree1.treenodes[8]=8;
  99.  
  100. //set the size here. size is a member of the bTree class so we need our object.member syntax
  101. tree1.size = 9;
  102.  
  103. cout << "The left child of node 3 is: " << tree1.leftchild(3) << endl;
  104. cout << "The height of the tree is: " << tree1.height() << endl;
  105. cout << "The node 7 is a leaf: " << tree1.isleaf(7) << endl;
  106. cout << "The node 2 is a leaf: " << tree1.isleaf(2) << endl;
  107. cout << "The parent of node 5 is: " << tree1.parent(5) << endl;
  108.  
  109. cout << "Values for inorder traversal: " << endl;
  110. tree1.traverseinorder();
  111.  
  112. return 0;
  113. }
Reputation Points: 12
Solved Threads: 0
Light Poster
schmidty169 is offline Offline
32 posts
since Jul 2006
Jul 15th, 2006
0

Re: Trouble shooting help

Ok this is where i am now
C++ Syntax (Toggle Plain Text)
  1. // CPP btree.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. class BTree
  7. {
  8. public:
  9. int treenodes[1000]; // structure to store the tree nodes values
  10.  
  11. int size; // the number of nodes in the tree
  12.  
  13. // BTree(void); // the "constructor"
  14.  
  15. bool isleaf(int nodeindex); // returns true if the node is a leaf
  16. int parent(int nodeindex); // returns the index of the parent
  17. int leftchild(int nodeindex); // returns the index of the left child
  18. int rightchild(int nodeindex); // returns the index of the right child
  19.  
  20. int height(); // returns the height of the tree
  21. void traverseinorder(); // a recursive function to print the nodes values based on an inorder traversal
  22. };
  23.  
  24. bool BTree::isleaf(int nodeindex) //returns true if the node is a leaf
  25. {
  26. return (BTree::leftchild(nodeindex)==NULL && BTree::rightchild(nodeindex)==NULL) ;
  27. }
  28.  
  29. int BTree::parent(int nodeindex) // returns the index of the parent
  30. {
  31. int rv =0;
  32.  
  33. rv = (nodeindex - 1) / 2;
  34. if (rv >= size)
  35. rv = -1 ;
  36.  
  37. return rv ;
  38. }
  39.  
  40. int BTree::leftchild(int nodeindex) // returns the index of the left child
  41. {
  42. int rv =0;
  43.  
  44. rv = (nodeindex +1) *2 - 1;
  45. if (rv >= size)
  46. rv = -1 ;
  47.  
  48. return rv ;
  49. }
  50.  
  51. int BTree::rightchild(int nodeindex) // returns the index of the right child
  52. {
  53. int rv =0;
  54.  
  55. rv = (nodeindex +1) * 2;
  56. if (rv <= size)
  57. rv = -1 ;
  58.  
  59. return rv ;
  60. }
  61.  
  62. void BTree::traverseinorder(BTree *root) // a recursive function to print the nodes values based on an inorder traversal
  63. {
  64. if ( root!= NULL )
  65. {
  66. traverseinorder(root->leftchild));
  67. cout << root << " ";
  68. traverseinorder(root->rightchild);
  69. }
  70. } // end traverseinorder()
  71.  
  72. int BTree::height(BTree *root) // returns the height of the tree
  73. {
  74. if (root==NULL)
  75. {
  76. return(0);
  77. }
  78. else
  79. {
  80. //compute the depth of leftchild
  81. int lheight = height(root->leftchild);
  82.  
  83. return(lheight+1);
  84. }
  85. }
  86.  
  87. int main(int argc, char* argv[])
  88. {
  89. BTree tree1;
  90.  
  91. tree1.treenodes[0]=5;
  92. tree1.treenodes[1]=10;
  93. tree1.treenodes[2]=2;
  94. tree1.treenodes[3]=5;
  95. tree1.treenodes[4]=7;
  96. tree1.treenodes[5]=12;
  97. tree1.treenodes[6]=3;
  98. tree1.treenodes[7]=9;
  99. tree1.treenodes[8]=8;
  100.  
  101. //set the size here. size is a member of the bTree class so we need our object.member syntax
  102. tree1.size = 9;
  103.  
  104. cout << "The left child of node 3 is: " << tree1.leftchild(3) << endl;
  105. // cout << "The height of the tree is: " << tree1.height() << endl;
  106. cout << "The node 7 is a leaf: " << tree1.isleaf(7) << endl;
  107. cout << "The node 2 is a leaf: " << tree1.isleaf(2) << endl;
  108. cout << "The parent of node 5 is: " << tree1.parent(5) << endl;
  109.  
  110. cout << "Values for inorder traversal: " << endl;
  111. // tree1.traverseinorder();
  112.  
  113. return 0;
  114. }
Reputation Points: 12
Solved Threads: 0
Light Poster
schmidty169 is offline Offline
32 posts
since Jul 2006
Jul 15th, 2006
0

Re: Trouble shooting help

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
Reputation Points: 38
Solved Threads: 3
Junior Poster in Training
Eddy Dean is offline Offline
56 posts
since Jul 2006
Jul 16th, 2006
0

Re: Trouble shooting help

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();
Reputation Points: 12
Solved Threads: 0
Light Poster
schmidty169 is offline Offline
32 posts
since Jul 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: another "segmentation fault"
Next Thread in C++ Forum Timeline: help in improving algo ...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC