0

Hey I ran this program in vscode and found that the binary tree traversals orders show wrongly the orders. What's wrong in this

//Binary tree traversal: #include <stdio.h> #include <conio.h> #include <malloc.h> struct node { struct node *left; int data; struct node *right; }; void main() { void insert(struct node **,int); void inorder(struct node *); void postorder(struct node *); void preorder(struct node *); struct node *ptr; int no,i,num; ptr = NULL; ptr->data=NULL; clrscr(); printf("\nProgram for Tree Traversal\n"); printf("Enter the number of nodes to add to the tree.<BR>\n"); scanf("%d",&no); for(i=0;i<no;i++) { printf("Enter the item\n"); scanf("%d",&num); insert(&ptr,num); } //getch(); printf("\nINORDER TRAVERSAL\n"); inorder(ptr); printf("\nPREORDER TRAVERSAL\n"); preorder(ptr); printf("\nPOSTORDER TRAVERSAL\n"); postorder(ptr); getch(); } void insert(struct node **p,int num) { if((*p)==NULL) { printf("Leaf node created."); (*p)=malloc(sizeof(struct node)); (*p)->left = NULL; (*p)->right = NULL; (*p)->data = num; return; } else { if(num==(*p)->data) { printf("\nREPEATED ENTRY ERROR VALUE REJECTED\n"); return; } if(num<(*p)->d

27th Mar 2021, 12:11 PM
san sheva S
12 Respostas
+ 3
Input: 9 4 7 3 6 10 1 2 5 3 ...is outputting: INORDER TRAVERSAL Data :1 Data :2 Data :3 Data :4 Data :5 Data :6 Data :7 Data :10 PREORDER TRAVERSAL Data :4 Data :3 Data :1 Data :2 Data :7 Data :6 Data :5 Data :10 POSTORDER TRAVERSAL Data :2 Data :1 Data :3 Data :5 Data :6 Data :10 Data :7 Data :4 Where do you think this is wrong?
27th Mar 2021, 4:00 PM
Russ
Russ - avatar
+ 4
Wow can you even read that code?
27th Mar 2021, 12:17 PM
Ipang
+ 2
Thank you
27th Mar 2021, 4:15 PM
san sheva S
+ 1
I copied the code and tested run in C4Droid but I'm getting segmentation fault problem. I don't know how VSCode got to run that code at all ...
27th Mar 2021, 2:16 PM
Ipang
+ 1
I copied your code and made a couple of alterations (to get it running) and it appears to be showing the right orders. Can you give an example of where it goes wrong? https://code.sololearn.com/cRDVQVWgfqXM/?ref=app
27th Mar 2021, 3:51 PM
Russ
Russ - avatar
0
https://forgetcode.com/C/267-Binary-Tree-Traversal
27th Mar 2021, 12:22 PM
san sheva S
0
Pls see the whole coding in above link
27th Mar 2021, 12:23 PM
san sheva S
0
Sry about the mess I did above😅
27th Mar 2021, 12:23 PM
san sheva S
0
I think the code doesn't give the inorder postorder according to the rule. It just prints in same order and reverse order which I think is incorrect?
27th Mar 2021, 3:56 PM
san sheva S
0
What do you mean reverse order? What input are you submitting?
27th Mar 2021, 3:58 PM
Russ
Russ - avatar
0
If I give input:1 2 3 4 Inorder:1 2 3 4 Postorder: 4 3 2 1 But according to rule Inorder should be left subtree and root and right subtree
27th Mar 2021, 4:03 PM
san sheva S
0
There are no left subtrees if the input is 1 2 3 4
27th Mar 2021, 4:04 PM
Russ
Russ - avatar