Trouble shooting help

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2006
Posts: 32
Reputation: schmidty169 is an unknown quantity at this point 
Solved Threads: 0
schmidty169's Avatar
schmidty169 schmidty169 is offline Offline
Light Poster

Trouble shooting help

 
0
  #1
Jul 14th, 2006
I'm getting error C2109: subscript requires array or pointer type

  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. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 65
Reputation: GloriousEremite will become famous soon enough GloriousEremite will become famous soon enough 
Solved Threads: 14
GloriousEremite GloriousEremite is offline Offline
Junior Poster in Training

Re: Trouble shooting help

 
0
  #2
Jul 14th, 2006
treeInsert is a function, not an array...
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Trouble shooting help

 
0
  #3
Jul 14th, 2006
Where are the variable declarations for treeInsert, leftchild and the such?
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 32
Reputation: schmidty169 is an unknown quantity at this point 
Solved Threads: 0
schmidty169's Avatar
schmidty169 schmidty169 is offline Offline
Light Poster

Re: Trouble shooting help

 
0
  #4
Jul 14th, 2006
Ok I scraped that and started new. Not sure if it is an improvement or not.
  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. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 32
Reputation: schmidty169 is an unknown quantity at this point 
Solved Threads: 0
schmidty169's Avatar
schmidty169 schmidty169 is offline Offline
Light Poster

Re: Trouble shooting help

 
0
  #5
Jul 14th, 2006
in the traverse I found and fixed this
  1. cout << root->data << " ";
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 32
Reputation: schmidty169 is an unknown quantity at this point 
Solved Threads: 0
schmidty169's Avatar
schmidty169 schmidty169 is offline Offline
Light Poster

Re: Trouble shooting help

 
0
  #6
Jul 14th, 2006
closer if I can figure out the traverse and height
  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. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 32
Reputation: schmidty169 is an unknown quantity at this point 
Solved Threads: 0
schmidty169's Avatar
schmidty169 schmidty169 is offline Offline
Light Poster

Re: Trouble shooting help

 
0
  #7
Jul 15th, 2006
Ok this is where i am now
  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. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 56
Reputation: Eddy Dean is an unknown quantity at this point 
Solved Threads: 3
Eddy Dean Eddy Dean is offline Offline
Junior Poster in Training

Re: Trouble shooting help

 
0
  #8
Jul 15th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 32
Reputation: schmidty169 is an unknown quantity at this point 
Solved Threads: 0
schmidty169's Avatar
schmidty169 schmidty169 is offline Offline
Light Poster

Re: Trouble shooting help

 
0
  #9
Jul 16th, 2006
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();
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC