Search code examples
crecursionprintfbinary-treeinorder

Recursive function to represent binary tree in C


Problem

I want to print the nodes of a binary tree inorder and would like to have the nodes printed with as many dashes as the height they are in and then it's data.

Research done

I have found other posts like this one or this one but I'm still clueless on how I can represent my binary tree the way I want, as it differs to the ones stated on those questions.

Example

So lets say I insert nodes with data in this manner:

5,4,2,3,9,8

The output I would expect when representing the binary tree would be:

-9
--8
5
-4
---3
--2

Toy example code

So far I was able to print the nodes in the correct order. But after days I'm still clueless on how to implement a recursive function to get the correct representation. I also tried loops but found it's even messier and wasn't getting the correct result either.

The problem is that the amount of dashes is not the correct one and I'm unsure on where I need to append the dashes within the recursion. I'm thinking I may need to rewrite the whole printBinaryTreeRecurrsively code.

Below you can see the toy example code which can be compiled as a whole:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct repBinaryTree *BinaryTree;

struct repBinaryTree {
  int data;
  BinaryTree left;
  BinaryTree right;
};

BinaryTree newNode() {
  BinaryTree b = new repBinaryTree;
  b->data = NULL;
  b->left = b->right = NULL;
  return b;
}

BinaryTree insertNode(int i, BinaryTree b) {
  if (b==NULL) {
    b = newNode();
    b->data = i;
  } else if ( i < b->data ) {
    if (b->left == NULL) {
      b->left = newNode();
      b->left->data = i;
    } else {
      insertNode(i, b->left);
    }
  } else if ( i > b->data ) {
    if (b->right == NULL) {
      b->right = newNode();
      b->right->data = i;
    } else {
      insertNode(i, b->right);
    }
  }
  return b;
}

char* printBinaryTreeRecurrsively(BinaryTree b, char level[]) {
  if (b == NULL) {
    printf("\n");
    return level;
  } else {
    level = printBinaryTreeRecurrsively(b->right, level);
    printf("%s%d",level,b->data);
    level = printBinaryTreeRecurrsively(b->left, level);
  }
  strcat(level,"-");
  return level;
}

int main () {
  BinaryTree b = insertNode(5,NULL);
  b = insertNode(4, b);
  b = insertNode(2, b);
  b = insertNode(3, b);
  b = insertNode(9, b);
  b = insertNode(8, b);
  printf("Recursive BinaryTree print:");
  char level0[] = "";
  printBinaryTreeRecurrsively(b, level0);
  printf("Expected BinaryTree print:");
  printf("\n-9\n--8\n5\n-4\n---3\n--2\n");
}

The output I get after compiling and running the program from command line is as follows:

cedric@multivac:~$ g++ printBinaryTree.cpp -o printBinaryTree
cedric@multivac:~$ ./printBinaryTree 
Recursive BinaryTree print:
9
8
--5
--4
--3
---2
Expected BinaryTree print:
-9
--8
5
-4
---3
--2

Question

How should I rewrite my printBinaryTreeRecurrsively function code so as I get the correct output?


Solution

  • Modify the function to this instead

    void printBinaryTreeRecurrsively(BinaryTree b, int level) {
      if (b == NULL) {
        printf("\n");
      } else {
        printBinaryTreeRecurrsively(b->right, level+1);
        for (int i = 0; i < level; i++) {
            printf("-");
        }
        printf("%d",b->data);
        printBinaryTreeRecurrsively(b->left, level+1);
      }
    }
    

    and call in main() as

    printBinaryTreeRecurrsively(b, 0);
    

    This method is much simpler than worrying about string concatenation etc. Just keep track of which level you're on with an int, print the correct number of -, and tell the levels below to print with one more -.