Code : Binary Search Tree C++

Click For Summary

Discussion Overview

The discussion revolves around a C++ implementation of a binary search tree, focusing on code structure, syntax errors, and best practices in C++. Participants address issues related to the declaration of the main function, the embedding of functions, and the use of C versus C++ conventions.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • Some participants note that the main function should return an int, not void, as per C and C++ standards.
  • There is a suggestion to avoid embedding function definitions within other functions, which is not allowed in C++.
  • One participant recommends using C++ I/O and memory management instead of C-style functions like malloc and printf.
  • Another participant points out that the OP's issues are more fundamental than just the void main declaration, emphasizing the incorrect structure of the code.
  • Some participants express concern that the OP may need to revisit basic programming concepts, suggesting that the errors indicate a lack of foundational knowledge.
  • There is a counterpoint that void main functions are common on Windows machines, suggesting a more lenient view on this practice.
  • A participant comments that the code is actually C rather than C++, indicating a potential misunderstanding of the language being used.

Areas of Agreement / Disagreement

Participants generally agree that there are significant issues with the OP's code, particularly regarding the structure and syntax. However, there is disagreement on the severity of the issues and the appropriate approach to correcting them.

Contextual Notes

Participants highlight various limitations in the OP's code, including the incorrect declaration of the main function, the embedding of functions, and the use of outdated C-style I/O and memory management. These issues may hinder the code's functionality and adherence to modern C++ standards.

Hamonic
Messages
1
Reaction score
0
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
void main()
{
struct znode // binary search tree node
{
int data; // data type is integer
struct znode * left; // left subtree
struct znode * right; // right subtree
};

struct znode * root = NULL; // root node, initially NULL

void print_tree_inorder(struct znode* tree) // print tree in inorder (left -> parent -> right)
{
if (tree == NULL)
printf("Tree is empty!\n");
else
{
if (tree->left != NULL)
print_tree_inorder(tree->left);
printf("%d ", tree->data);
if (tree->right != NULL)
print_tree_inorder(tree->right);
};
}

struct znode* insert(int insert_data, struct znode* tree) // node insert
{
if (tree == NULL) // if null, make new node
{
tree = (struct znode *)malloc(sizeof(struct znode));
tree->data = insert_data;
tree->left = NULL;
tree->right = NULL;
}
else if (insert_data < tree->data) // insert data in left subtree
tree->left = insert(insert_data, tree->left);
else if (insert_data > tree->data)
tree->right = insert(insert_data, tree->right); // insert data in right subtree
else
printf("Insert Error : item duplicated!\n"); // when the item already exists

return tree;
}

struct znode* zremove_min(struct znode* tree) // delete minimum-valued node
{
struct znode* temp = tree;

if (tree == NULL)
printf("remove Minimum Error : null tree!\n");
else if (tree->left != NULL)
tree->left = zremove_min(tree->left); // traverse to left and left again
else
{
tree = tree->right; // attach right subtree(or NULL) to parent
free(temp); // and remove the old parent
}

return tree;
}

struct znode* zremove(int remove_data, struct znode* tree) // delete node
{
struct znode* temp = tree;

if (tree == NULL)
printf("remove Error : item not found!\n");
else if (remove_data < tree->data) // delete data in left subtree
tree->left = zremove(remove_data, tree->left);
else if (remove_data > tree->data)
tree->right = zremove(remove_data, tree->right); // delete data in right subtree
else if (tree->left != NULL && tree->right != NULL)
// if this has both left and rihgt subtree
{
temp = temp->right;
while (temp->left != NULL)
temp = temp->left; // find the minimum data of right subtree
tree->data = temp->data; // and set it to the parent
tree->right = zremove_min(tree->right); // and remove it in right subtree
}
else
{
tree = (tree->left != NULL) ? tree->left : tree->right;
// attach one of two children to parent (tree or NULL)
free(temp); // free the old parent
}

return tree;
}

int main()
{
FILE* fp;
char buffer[11];

fp = fopen("index.txt", "r");
while (fgets(buffer, 10, fp) != NULL) // read one line from index.txt
{
printf("\ninsert %d : ", atoi(buffer));
root = insert(atoi(buffer), root); // insert it
print_tree_inorder(root); // and print the current tree shape
}
fclose(fp);

fp = fopen("index.txt", "r"); // close and reopen
while (fgets(buffer, 10, fp) != NULL)
{
printf("\ndelete %d : ", atoi(buffer));
root = zremove(atoi(buffer), root); // delete a node
print_tree_inorder(root); // and print
}
fclose(fp);

getch();
};
};

Compiling 0001.CPP:
Error 0001.CPP 16: Declaration syntax error in function main()
Warning 0001.CPP 119: 'root' is assigned a value that is never used in function main()



Help Me !


Edit and modifier
 
Technology news on Phys.org
In the future, please embed your code in code tags so that we can see the structure of your code.

kind of like this:
Code:
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
void main()
{
struct znode                        // binary search tree node
{
       int data;                        // data type is integer
       struct znode * left;                        // left subtree
       struct znode * right;                        // right subtree
};
...
int main()
{
};
};
Why are you declaring main at the very top and wrapping all functions inside of main? You cannot embed a function within a function in C++.

For a start, get rid of that initial "void main(){". I also suggest you switch to using C++ I/O rather than C I/O and C++ new/delete rather than malloc.
 
main() is never void; it returns an int. Always. That is the C and C++ standard.

Plus, if this isn't an assignment, consider the standard library function bsearch().

The STL aslo has a binary search.
 
Last edited:
The OP has problems much more basic than "void main" versus "int main". That that "void main" is pedantically incorrect is secondary to the fact that it is completely incorrect. The biggest problem, embedding one function definition inside another, is much worse than going against the standard. Many compilers will compile a program that declares main as void. I don't know any C compiler that will compile code in which a function is defined internal to some outer function (and if some compiler does do that the language is no longer C/C++).

Besides, the OP does define a function "int main" (internal to the erroneous "void main" declaration).
 
I agree, DH, the OP's code has lots of problems. However start with what you learn on day one. Then go ahead from there.

That one thing "pedantic" thing speaks volumes about how wrell the OP is learning or being taught to code. I didn't get past the void main().

So in that sense I REALLY disagree with you. IMO, the OP may need to go back to Hello World. I think the the OP missed day one in class.. Your correcting what he posted will not help much, except maybe to get his coding assignment done. Wrong. There is nothing more basic than int main(). Period the end. YMMV.
 
Void main functions are actually pretty common on Windows machines. Loosen your grip a little jim.

The real problem here is that the OP is trying to put functions inside one another, when they should be peers.

- Warren
 
bu kod c++ değil c aga
 
chroot said:
Void main functions are actually pretty common on Windows machines. Loosen your grip a little jim.

The real problem here is that the OP is trying to put functions inside one another, when they should be peers.

- Warren
Amen to that Warren and D H.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
Replies
14
Views
3K
  • · Replies 20 ·
Replies
20
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 11 ·
Replies
11
Views
3K
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K