| | |
Trouble shooting help
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
I'm getting error C2109: subscript requires array or pointer type
C++ Syntax (Toggle Plain Text)
// 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(); }
Ok I scraped that and started new. Not sure if it is an improvement or not.
C++ Syntax (Toggle Plain Text)
// 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; }
closer if I can figure out the traverse and height
C++ Syntax (Toggle Plain Text)
// 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
C++ Syntax (Toggle Plain Text)
// 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; }
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();
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();
![]() |
Similar Threads
- IE Trouble shooting (Web Browsers)
- Windows XP keeps restarting since a new video card (Windows NT / 2000 / XP)
- this isnt really trouble shooting but i couldnt find a better catagory (laptop) (Troubleshooting Dead Machines)
- why i can not load game on one partition but can on aother (Troubleshooting Dead Machines)
- IBM ThinkPad T22 bad cmos battery? (Motherboards, CPUs and RAM)
- Disappearing Drive in Win XP (Storage)
- IRQL_ Not_les_than_ equal, blue screens, and windows crashing (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: another "segmentation fault"
- Next Thread: help in improving algo ...
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






