A binary tree problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zahit
    New Member
    • Mar 2008
    • 10

    A binary tree problem

    Hi guys, below there is a c code which caluclates given data repetition in a binary tree.

    Code:
    typedef struct node *tree_pointer;
    typedef struct node{
    char data;
    tree pointer left_child, right_child;
    }
    
    int repetition(tree_pointer L, char x){
    if(L){
    if(L->data == x){
    return 1 + repetition(L->left_child, x) + repetition(L->right_child, x);
    }
    else{
    return repetition(L->left_child, x) + repetition(L->right_child, x);
    }
    else{
    return 0;
    }
    }
    The problem is I don't understand how it works. The all recursive and return process confused me. For example; what happens when we track the code on the tree attached(for data 'a'). If anyone can explain me step by step, I would appreciate.
    Attached Files
  • Sick0Fant
    New Member
    • Feb 2008
    • 121

    #2
    Originally posted by zahit
    Hi guys, below there is a c code which caluclates given data repetition in a binary tree.

    Code:
    typedef struct node *tree_pointer;
    typedef struct node{
    char data;
    tree pointer left_child, right_child;
    }
    
    int repetition(tree_pointer L, char x){
    if(L){
    if(L->data == x){
    return 1 + repetition(L->left_child, x) + repetition(L->right_child, x);
    }
    else{
    return repetition(L->left_child, x) + repetition(L->right_child, x);
    }
    else{
    return 0;
    }
    }
    The problem is I don't understand how it works. The all recursive and return process confused me. For example; what happens when we track the code on the tree attached(for data 'a'). If anyone can explain me step by step, I would appreciate.
    It's recursive. It calls itself with both leaves and adds 1 when the datum is repeated, otherwise, it just calls itself with its leaves

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by zahit
      The problem is I don't understand how it works. The all recursive and return process confused me. For example; what happens when we track the code on the tree attached(for data 'a'). If anyone can explain me step by step, I would appreciate.
      Suppose I have a pile of notes; each note contains a letter. Someone asks me
      to count the number of 'a's in that pile of notes. I divide that pile in two (not
      necessarily equal) parts and give those parts to two friends. I ask them the same
      thing to do and I keep one note for myself. In your example I keep an 'a' note.

      My friends L (Leo) and R (Rudolph) come up with the two totals: 1 and 1; I had
      one 'a' myself so there are 1+1+1 == 3 'a's in the pile.

      Now suppose Leo and Rudolph did the same thing I did and divided there piles
      as well ... we still can get the end result; that's what recursion is all about.

      kind regards,

      Jos

      Comment

      Working...