Thread: Function which makes a simmetric visit in a Binary Search Tree

  1. #1
    Registered User
    Join Date
    Jun 2019
    Posts
    33

    Function which makes a simmetric visit in a Binary Search Tree

    Hi everyone.


    In this post I'm gonna show you just the definition of a function that I can't understand.


    Here we have the exercise.....
    Given a binary search tree:
    define a recursive function which makes a simmetric visit of the tree and which prints the value of every node.




    Two notes:
    Note number one: simmetric visit is that kind of visit which shows in order:
    1) Left sons;
    2) Root;
    3) Right sons.
    Note number two: every node of the tree is a structured data.




    Code:
    struct btree{ 
          float value; 
         struct btree * left_ptr; 
         struct btree * right_ptr; 
         }



    Here we have the definition of the function that I'm not comprehending.


    Code:
    void visit_r(struct btree * ptr){ 
    /*function which takes in input a pointer to struct btree*/
     
     
     
    if (ptr != NULL) { /*if the pointer points to something*/
     
        printf("I search the left son... "); 
     
        visit_r(ptr->left_ptr); 
    /*I can't understand what happens here. I re-use my 
    function with a new input, but I will give a new input 
    until pointer==NULL. 
    When pointer will be == NULL, I will not
     enter in the "if cycle", and I will not be able
     to print the value of my nodes!*/
     
     
        printf("\n I print the value: %f \n", ptr->value);
     
     
    printf("I search the right son... "); 
    visit_r(ptr->right_ptr);
    } printf("Let's go back... "); 
     
    /*I can't understand neither how it can go back, 
    how it can 'climb' the tree*/
    }



    when it reaches line n°10 it starts again from line n°1 with a different input.
    But I still don't understand how it can work,
    because it will do this recursive path
    until 'ptr->left_ptr' will be' == NULL'.
    But when 'ptr->left_ptr' will be == 'NULL', I will not enter in the cycle "if(...)" and I will not be able to print the value of my nodes!

    P.s. The code is written by a university teacher.



    Thanks everyone!!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You already posted about this.

    *thread closed*
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 06-28-2019, 05:40 PM
  2. Search function for binary tree
    By Jacopo in forum C Programming
    Replies: 4
    Last Post: 02-22-2018, 05:00 AM
  3. Replies: 1
    Last Post: 04-16-2011, 04:44 AM
  4. A Binary Search Tree of... Binary Search Trees...
    By SlyMaelstrom in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2005, 02:12 PM
  5. FunctionType visit and binary search trees
    By MRAB54 in forum C++ Programming
    Replies: 1
    Last Post: 05-11-2004, 05:20 AM

Tags for this Thread