| | |
Insert Tree
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2008
Posts: 92
Reputation:
Solved Threads: 0
Would someone be so kind as to help me with this insert. The function insert takes two numbers, num and num2. num is the number to insert in the tree and num2 is the number of children to the parent node where num is inserted. I am getting errors for root->children = NULL. Here is what I have.
this is my tree base:
And my insert function:
this is my tree base:
C++ Syntax (Toggle Plain Text)
struct node { node *parent; vector <node*> children; int item; }; node* root;
C++ Syntax (Toggle Plain Text)
void Tree::insert(int num, int num2) { if(isEmpty()) { root = new node; root->children = NULL; root->item = num; } else if(root->children == NULL) { root->children = num; for(int i = 0;i<num2;i++) { children = root; root->children = NULL; } } else root->children = num; }
•
•
Join Date: Jun 2008
Posts: 92
Reputation:
Solved Threads: 0
Ah yeah that was just a blind man that didnt notice that. But now, how would I check for an open node?
C++ Syntax (Toggle Plain Text)
void Tree::insert(int num, int num2) { if(isEmpty()) { root = new node; root->children.push_back(NULL); root->item = num; } else if(root->children == NULL)//here { root->children.push_back(num); for(int i = 0;i<num2;i++) { children = root; root->children.push_back(NULL); } } else root->children.push_back(num); }
•
•
Join Date: Jun 2008
Posts: 92
Reputation:
Solved Threads: 0
Something like this?
C++ Syntax (Toggle Plain Text)
void Tree::insert(int num, int num2) { node* insert = new node; insert->item = num; if(isEmpty()) { root = insert; for(int i = 0;i<num2;i++) { root->children.push_back(0); } } else { } }
•
•
Join Date: Jan 2008
Posts: 3,829
Reputation:
Solved Threads: 501
•
•
•
•
Something like this?
C++ Syntax (Toggle Plain Text)
void Tree::insert(int num, int num2) { node* insert = new node; insert->item = num; if(isEmpty()) { root = insert; for(int i = 0;i<num2;i++) { root->children.push_back(0); } } else { } }
Well, 0 means NULL and NULL is a pointer, so there's no error there. But I prefer to use the word "NULL" so it's obvious that it's being used as a pointer, not as the integer 0.
This is confusing.
void Tree::insert(int num, int num2) { node* insert = new node; insert->item = num;
I'd pick a different name for your new node. It's the same as your function's name. How about
newNode ?void Tree::insert(int num, int num2)
{
node* newNode = new node;
newNode->item = num;The whole function looks fairly odd to me. I think I'd have to see the larger Tree class and the main function to get a better feel for how you're trying to use it.
•
•
Join Date: Jun 2008
Posts: 92
Reputation:
Solved Threads: 0
Here is the whole thing...
i want to do something like this for my insert function:
-open node?
if no
make new node and insert num to node
create children to that node based on num2
if yes
find open node, insert num to that node
create children to that node
It looks confusing because im not sure how to do it.
C++ Syntax (Toggle Plain Text)
class Tree { public: Tree() { root = NULL; } bool isEmpty() const { return root==NULL; } void insert(int, int); private: struct node { node *parent; vector <node*> children; int item; }; node* root; }; void Tree::insert(int num, int num2) { node* current = new node; current->item = num; if(isEmpty()) { root = current; for(int i = 0;i<num2;i++) { root->children.push_back(NULL); } } else { } } int main() { Tree myTree; myTree.insert(1, 3); return 0; }
i want to do something like this for my insert function:
-open node?
if no
make new node and insert num to node
create children to that node based on num2
if yes
find open node, insert num to that node
create children to that node
It looks confusing because im not sure how to do it.
Well vectors can be handled in majorly in 2 ways. One is to use an index as we do to an array and the other to use iterators.
for example assuming that the for loop in your insert function creates Null pointers which are to be filled here is what you can do.
for example assuming that the for loop in your insert function creates Null pointers which are to be filled here is what you can do.
C++ Syntax (Toggle Plain Text)
for(int x=0;x<root->children.size();x++) { if(root->children[x]==NULL) { //Assign a value } }
•
•
Join Date: Jan 2008
Posts: 3,829
Reputation:
Solved Threads: 501
My strong advice is to draw it out step-by step. Your tree is going to have children of children of children. Sky Diploma is on to something with his traversal idea, but you need to go further. In particular, dealing with children of children, make sure you are cautious about hard-coding
You need to be able to check children of children and children of children of children and children of children of children of children, etc., which this cannot do. So at some point you may want to have a variable called
root in the code.for(int x=0;x<root->children.size();x++) { if(root->children[x]==NULL) { //Assign a value } }
You need to be able to check children of children and children of children of children and children of children of children of children, etc., which this cannot do. So at some point you may want to have a variable called
parentNode (intentionally not named parent to avoid confusion with one of your node members), so you may want to have something like what Sky Diploma has, but replace root with parentNode . parentNode may START OFF being root , but it should potentially also be able to NOT point to root and instead point to some node that is lower in the tree. ![]() |
Similar Threads
- how to insert a struct record into a binary search tree (C++)
- Ruby Binary Search Tree (Ruby)
- Recursive Binary Search Tree Header File (C++)
- Avl tree and Bst tree (C++)
- parsing a tree (C)
- Help in Binary Tree in C (C)
- binary tree class (C++)
- searching and inserting node in a binary search tree (C)
Other Threads in the C++ Forum
- Previous Thread: Exam Q: Using Enumerations
- Next Thread: Issue with returning an int from an array
| Thread Tools | Search this Thread |
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






