Hello to every body

I hope every one keep in best state.

I want to help me in solve this problem in my Sheet

IN TREE structuring
If I use pointer to root of tree, it make run time error .

I need to this pointer in 4 function

int countNodes( TreeNode * root )
	 
	 {
        if ( root == NULL )
           return 0; 
        else {
           int count = 1;   
           count += countNodes(root->left); 
           count += countNodes(root->right); 
           return count;
        }
     } // end countNodes()

// in main

cout <<countNodes (ObjectTree.ReturnRoot()  );

// in Tree class

TreeNode* ReturnRoot ()
		  {

			  return root ;
		  }

the rest functom is tha same this :

void preorder( TreeNode *root ) 
{
        if ( root != NULL ) {  // (Otherwise, there's nothing to print.)
           cout << root->item << " ";     
           preorder( root->left );    
           preorder( root->right );          }
}

.
.
.

I think that I need to make the function is a global function not a member function because I think the efficiency solution to this problem is recursive


I hooope to help me

The countNodes() returns one too many nodes -- Initialize count to 0, not 1.

Initialize count to 0, not 1.

int countNodes( TreeNode * root )
	 
	 {
        if ( root == NULL )
           return 0; 
        else {
           int count = 0;   
           count += countNodes(root->left); 
           count += countNodes(root->right); 
           return count;
        }
     } // end countNodes()

I made it .
but the error remaining. don't change ;

Check the tree and make sure left and right both have NULL pointers at the last node.

I Check the tree and make the correction.

thank you

it remaining the run time error in the destructor .


I make :

~Tree (void)
{
	TreeNode  *deletePtr;
	TreeNode  *deletePtr2;
	TreeNode  *deletePtr3;
	TreeNode  *deletePtr4;
	TreeNode  *deletePtr5;
	TreeNode  *deletePtr6;
	TreeNode  *deletePtr7;
	TreeNode  *deletePtr8;
	TreeNode  *deletePtr9;
	TreeNode  *deletePtr0;

deletePtr = root;
delete  deletePtr;
deletePtr= NULL;


deletePtr2 = root -> left;
delete  deletePtr2;
deletePtr2= NULL;


deletePtr3 = root ->right;
delete  deletePtr3;
deletePtr3= NULL;

deletePtr4 =  (root -> left)->left;
delete  deletePtr4;
deletePtr4= NULL;

deletePtr5 = (root -> left)->right;
delete  deletePtr5;
deletePtr5= NULL;

deletePtr0 = ((root -> left)->right)->left;
delete  deletePtr0;
deletePtr0= NULL;

deletePtr6 = ((root -> left)->right)->right;
delete  deletePtr6;
deletePtr6= NULL;

deletePtr7 = (root ->right)->left ;
delete  deletePtr7;
deletePtr7= NULL;

deletePtr8 = (root ->right)->right;
delete  deletePtr8;
deletePtr8= NULL;
 deletePtr9 = ((root ->right)->right)->left;
delete  deletePtr9;
deletePtr9= NULL;
}

because the insert in the same way .

the node in tree has ( data , *right , * left )
do not has next .

please,
What do you mean with (next) ?

I mmmm


given the chart in the question.
I insert in the tree depended on it .
(the program only organizational this chart )
so , i put the (left) or (right) depended on it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.