| | |
Find first NULL
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2008
Posts: 92
Reputation:
Solved Threads: 0
I want to find the first null child in a tree and return that node of the tree.
Im thinking something like this but it doesnt seem to work the way I want.
and then back where is called do something like this?
Im thinking something like this but it doesnt seem to work the way I want.
C++ Syntax (Toggle Plain Text)
node *Find_Empty_Node(node* root) { int index = 0; if(root) { if(root->data == 0)//<--I want this child return tree->child[index]; for(int i = 0;i<root->child.size();i++) { Find_Empty_Node(root->child[i]); index = i; } } return tree; }
and then back where is called do something like this?
C++ Syntax (Toggle Plain Text)
node* curr = root; curr = Find_Empty_Node(node* root);
Last edited by JackDurden; Jun 13th, 2009 at 1:07 pm. Reason: code tags
c++ Syntax (Toggle Plain Text)
node *Find_Empty_Node(node* root) { int index = 0; if(root) { if(root->data == 0)//<--I want this child return tree->child[index]; for(int i = 0;i<root->child.size();i++) { index = i; return Find_Empty_Node(root->child[i]); } } return tree; }
Shouldnt this be the way to do it?
I mean return the Empty Node as this is a example of a Recursive Function.
Last edited by Sky Diploma; Jun 13th, 2009 at 1:45 pm. Reason: Added SOME TEXT
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
•
•
•
•
c++ Syntax (Toggle Plain Text)
node *Find_Empty_Node(node* root) { int index = 0; if(root) { if(root->data == 0)//<--I want this child return tree->child[index]; for(int i = 0;i<root->child.size();i++) { index = i; return Find_Empty_Node(root->child[i]); } } return tree; }
Shouldnt this be the way to do it?
I mean return the Empty Node as this is a example of a Recursive Function.
C++ Syntax (Toggle Plain Text)
for(int i = 0;i<root->child.size();i++) { node* nodeFound = Find_Empty_Node(root->child[i]); if (nodeFound != NULL && nodeFound->data == 0) return nodeFound; }
I think this function may have some other problems. In particular, I'm not sure it will ever return below the first level of children due to these lines:
C++ Syntax (Toggle Plain Text)
return tree->child[index];
and
C++ Syntax (Toggle Plain Text)
return tree;
I assume
tree is the overall root node for the entire tree. Last edited by VernonDozier; Jun 13th, 2009 at 2:19 pm. Reason: changed "null" to "NULL". Forgot that NULL is capitalized in C++.
•
•
Join Date: May 2009
Posts: 13
Reputation:
Solved Threads: 3
•
•
•
•
I want to find the first null child in a tree and return that node of the tree.
Im thinking something like this but it doesnt seem to work the way I want.
C++ Syntax (Toggle Plain Text)
node *Find_Empty_Node(node* root) { int index = 0; if(root) { if(root->data == 0)//<--I want this child return tree->child[index]; for(int i = 0;i<root->child.size();i++) { Find_Empty_Node(root->child[i]); index = i; } } return tree; }
and then back where is called do something like this?
C++ Syntax (Toggle Plain Text)
node* curr = root; curr = Find_Empty_Node(node* root);
root however if this node is empty or it's data is set to zero you are returning the first child of a node pointed to by tree . I believe you really want a statement like return root->child[index]; . If you are trying to find a node with data set to
0 do you want to return that so it can be changed to some other data. If this is the case you should actually be returning something like return root; that way that node can be changed to some other data. If you don't change the data in the first node that has data 0 your Find_Empty_Node function will always stop at the same node and over write it's first child.Also it would be helpful if we knew what results you were trying to get. I hope some of this helps you.
![]() |
Similar Threads
- Check null value in excel file (C#)
- find and replace string in a text file (C)
- IE 7.0 Installed on XP Pro SP2 - Errors! (Windows NT / 2000 / XP)
- C programming - need some help (C)
- Strings in array [I NEED HELP] C lang. (C)
- Please help with program - STUCK (C)
- "hotoffers" help me please!!! (Viruses, Spyware and other Nasties)
- reading input ... quick C++ question ... (C++)
Other Threads in the C++ Forum
- Previous Thread: Best Approach To solve this..
- Next Thread: Help with FTP uploading using winsock
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






