0

I made a binary tree int the following way :

typedef struct BstNode {
   int data;
   struct BstNode *left;
   struct BstNode *right;
}Tnode;

typedef Tnode *BST;

BST InsertNode(BST tree,int info){
   if(tree==NULL){
       tree=MakeNode(info);
       return tree;
   }
   else if (InfoGreater(info,tree->data)==0){
       tree->left= InsertNode(tree->left,info);
   }
   else {
       tree->right=InsertNode(tree->right,info);
   }
   tree;
}

//info greater only checks what value is bigger and returns 1 if info is greater 0 in other cases 

and i wanna print it . I really have no idea about how to do that but i wish i could print it like

               5
        3            7
    2       4    6       8

Also it is necessary that the fucntion calls only the generic tree root. like

void print_BTS(BTS tree){
...
}

I googled around and on this site but i really can't find anything or what i found it's wayyy tooo complicated ways to implement it . I found something but not in the C lang only c# or java and so on .

  • 2
    https://www.geeksforgeeks.org/print-binary-tree-2-dimensions/ – Robert Harvey Mar 16 '20 at 16:21
  • You need to find the depth of your graph, from the depth you can compute the padding of each level. Here you have a solution https://stackoverflow.com/questions/4965335/how-to-print-binary-tree-diagram – Ôrel Mar 16 '20 at 16:22
  • This feels like it's a homework question - if it's not you may want to show *something*, anything, that you've attempted to write for this – Jeeter Mar 16 '20 at 16:25
  • @Jeeter i tried to do something but in a non recursive way and it i felt ashamed to publish here in which there's only good programmers xD . – Per i Giochi Mar 16 '20 at 16:27
  • 3
    @PeriGiochi Don't feel ashamed to show your possibly bad code. You might learn more if you allow us to see your level of knowledge and to improve your code than by getting a complete solution. BTW: https://stackoverflow.com/q/801740/10622916 – Bodo Mar 16 '20 at 16:35
  • @PeriGiochi: We're all learning, even the "good" programmers. – Fred Larson Mar 16 '20 at 16:36
  • Beware of code that works well when the numbers are all single digits if your data has, for sake of example, 3 or 4 or 5 digits (especially an even number of digits like 4), or a variable number of digits. – Jonathan Leffler Mar 16 '20 at 16:53

0 Answers0