recursive calls

This is a discussion on recursive calls within the C++ Programming forums, part of the General Programming Boards category; if i want to recursivly print out a binary tree from lowest to highest order should i recursivly go to ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    11

    Question 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

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    11

    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:rint_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:rint_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
    ...

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    11
    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.
    ------------------------------------

    Θ

    ░Θ

    └Φ
    Φ
    αΘ
    Θ
    ≡Φ
    this



    do i convert this to english or something :P
    ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. recursive function
    By technosavvy in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 04:42 AM
  3. difference between recursive and iterative
    By Micko in forum C Programming
    Replies: 33
    Last Post: 07-06-2004, 09:34 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21