-
recursive calls
if i want to recursivly print out a binary tree from lowest to highest order should i recursivly go to max first then work it to the lowest and end with a cout<<leaf->blah;
i'm having a little trouble with this heh.
in the binary tree tutorials the delete_tree(node *leaf) function i thought would do it if i changed it to print ---
if(leaf!=NULL)
{
print_tree(leaf->left);
print_tree(leaf->right);
cout<<endl<<leaf->string;
}
but it prints out garbage :P
-
updated code
ok here is the implementation file i have i changed things around.
i don't know why this is not working however.
#include <iostream.h>
#include <stdio.h>
#include <cstring>
#include "btree.h"
void binary_tree_node::print_btree()
{
print_btree(root);
}
binary_tree_node::~binary_tree_node()
{
destroy_tree();
}
void binary_tree_node::destroy_tree(node *leaf)
{
if(leaf!=NULL)
{
destroy_tree(leaf->left);
destroy_tree(leaf->right);
delete leaf;
}
}
void binary_tree_node::insert(char string[],const int MAX,node *leaf)
{
if(strcmp(string, leaf->nstring) < 0)
{
if(leaf->left!=NULL)
insert(string,MAX,leaf->left);
else
{
leaf->left=new node;
strcpy(leaf->left->nstring, string);
leaf->left->left=NULL;
leaf->left->right=NULL;
total_leaf++;
cout<<"\n"<<leaf->left;
}
}
else if(strcmp(string,leaf->nstring) > 0)
{
if(leaf->right!=NULL)
insert(string,MAX,leaf->right);
else
{
leaf->right=new node;
strcpy(leaf->right->nstring,string);
leaf->right->left=NULL;
leaf->right->right=NULL;
total_leaf++;
cout<<"\n"<<leaf->right;
}
}
}
void binary_tree_node::insert(char string[],const int MAX)
{
if(root!=NULL)
insert(string,MAX,root);
else
{
root=new node;
strcpy(root->nstring, string);
root->left=NULL;
root->right=NULL;
total_leaf++;
cout<<"root =: "<<root->nstring;
}
}
void binary_tree_node::destroy_tree()
{
destroy_tree(root);
}
void binary_tree_node::print_btree(node* leaf)
{
if(leaf!=NULL)
{
print_btree(leaf->left);
cout<<leaf->nstring<<endl;
print_btree(leaf->right);
}
}
[EOB]
****yes i used them cout statements for debugging and this is the output i got when i filled my tree
root =: this
0x14000a830
0x14000a860
0x14000a890
0x14000a8c0
0x14000a8f0
0x14000a920
0x14000a950
0x14000a980
my print_btree() function is also printing garbage and only the root . am i doing something wrong? to me it looks right (just not the output):P
-
heh ok i made stupid mistake of printing leaf->right instead of printing leaf->right->nstring *smacks self* so my insert works ok after testing now however print_btree function still prints this:
Words in the text file in order.
------------------------------------
PΘ
ÇΘ
░Θ
`Φ
└Φ
ÉΦ
αΘ
Θ
≡Φ
this
0Φ
do i convert this to english or something :P