A
andrew browning
this program is a binary tree using nodes. it should do an in-order
print, which it does. however, it inserts a garbage value at the end
of the print. ex: cin >> 1 2 3 4 5 prints 1 2 3 4 5 7884788. can
anyone help? value_type is an integer.
constructors:
tree():data(value_type()),left(NULL), right(NULL){}
tree(value_type vt, tree* l = NULL, tree* r = NULL):
data(vt), left(l), right(r) {};
insert function:
tree::tree* tree::insert(tree* node, value_type data){
if (node == NULL){
return(new tree(data));
}else
if(data <= node->data){
node->left = insert(node->left, data);
}else
if(data >= node->data){
node->right = insert(node->right, data);
}
return node;
}
driver:
int main(){
tree t1;
int user_input;
tree* tree_ptr = new tree(user_input);
while(cin >> user_input && cin.peek() != EOF){
t1.insert(tree_ptr, user_input);
}
t1.inorder_print(tree_ptr);
return 0;
}
print, which it does. however, it inserts a garbage value at the end
of the print. ex: cin >> 1 2 3 4 5 prints 1 2 3 4 5 7884788. can
anyone help? value_type is an integer.
constructors:
tree():data(value_type()),left(NULL), right(NULL){}
tree(value_type vt, tree* l = NULL, tree* r = NULL):
data(vt), left(l), right(r) {};
insert function:
tree::tree* tree::insert(tree* node, value_type data){
if (node == NULL){
return(new tree(data));
}else
if(data <= node->data){
node->left = insert(node->left, data);
}else
if(data >= node->data){
node->right = insert(node->right, data);
}
return node;
}
driver:
int main(){
tree t1;
int user_input;
tree* tree_ptr = new tree(user_input);
while(cin >> user_input && cin.peek() != EOF){
t1.insert(tree_ptr, user_input);
}
t1.inorder_print(tree_ptr);
return 0;
}