what's wrong with my code

Discussion in 'Programmer's Corner' started by Werapon Pat, Apr 2, 2018.

  1. Werapon Pat

    Thread Starter Member

    Jan 14, 2018
    35
    0
    here I wrote my own binary search tree using C++, but when I tried to insert left or right node it doesn't work at all..
    what's wrong with it. I can't figured out :(


    upload_2018-4-3_0-46-4.png

    Code (C):
    1. #include <stdio.h>
    2. #include <conio.h>
    3. #include <stdlib.h>
    4. typedef struct Node {
    5.     int data;
    6.     struct Node *r, *l;
    7. }Tree;
    8. typedef struct Node* T_ptr;
    9. T_ptr newnode(int val) {
    10.     T_ptr T;
    11.     T = (T_ptr)malloc(sizeof(Tree));
    12.     T->data = val;
    13.     T->l = NULL;
    14.     T->r = NULL;
    15.     return T;
    16. }
    17. void insertnode(T_ptr root,int v) {
    18.     if (root == NULL) { //base case
    19.         root = newnode(v);
    20.     }
    21.     else { //recursive
    22.         if (v >= root->data) {
    23.             insertnode(root->r,v);
    24.         }
    25.         if (v <= root->data) {
    26.             insertnode(root->l, v);
    27.         }
    28.     }
    29. }
    30. void inorder(T_ptr root) {
    31.     if (root == NULL) return;
    32.     printf("Inorder traverse : ");
    33.     inorder(root->l);
    34.     printf("%d ",root->data);
    35.     inorder(root->r);
    36.     printf("\n");
    37. }
    38. void main() {
    39.     T_ptr root=NULL;
    40.     int dt;
    41.     while (true) {
    42.         printf("please enter val : ");
    43.         scanf("%d", &dt);
    44.         if (root == NULL)
    45.             root = newnode(dt);
    46.         else
    47.             insertnode(root, dt);
    48.         inorder(root);
    49.     }
    50. }
    Moderators note : used code tags
     
    Last edited by a moderator: Apr 2, 2018
  2. WBahn

    Moderator

    Mar 31, 2012
    24,076
    7,473
    Have you walked through the code by hand (sometimes referred to as "desk checking") to see exactly what it IS doing, as compared to what you WANT it to do? If not, do so. But be careful that you actually do what the code says to do, not what you intended for it to do.
     
    MrSoftware likes this.
  3. Werapon Pat

    Thread Starter Member

    Jan 14, 2018
    35
    0
    I figured out! All I need is just pass by reference root pointer to insertnode function
    thanks!
     
  4. WBahn

    Moderator

    Mar 31, 2012
    24,076
    7,473
    I don't know if you fixed the problem, or just masked the symptom.

    Post your new code and let's take a look.

    You are also doing something pretty sloppily. If you are still doing them that way in your updated code I'll point them out to you.
     
  5. Werapon Pat

    Thread Starter Member

    Jan 14, 2018
    35
    0
    it works properly

    upload_2018-4-4_14-12-20.png

    I don't know how to use code tag so I'll just leave the code here
    Code (Text):
    1.  
    2. #include <stdio.h>
    3. #include <conio.h>
    4. #include <stdlib.h>
    5. typedef struct Node {
    6.     int data;
    7.     struct Node *r, *l, *m;
    8. }Tree;
    9. typedef struct Node* T_ptr;
    10. T_ptr newnode(int val) {
    11.     T_ptr T;
    12.     T = (T_ptr)malloc(sizeof(Tree));
    13.     T->data = val;
    14.     T->l = NULL;
    15.     T->r = NULL;
    16.     T->m = NULL;
    17.     return T;
    18. }
    19. T_ptr find(T_ptr root,int v) {
    20.     if (root->data == v) {
    21.         return root;
    22.     }
    23.     else {
    24.         if (v >= root->data) {
    25.             find(root->r,v);
    26.         }
    27.         if (v <= root->data) {
    28.             find(root->l,v);
    29.         }
    30.     }
    31. }
    32. void delnode(T_ptr summer,int v) {
    33.     summer = find(summer,v);
    34.     if (summer->r==NULL&&summer->l==NULL) {
    35.  
    36.     }
    37. }
    38. void insertnode(T_ptr* c,int v) {
    39.     if (*c == NULL) { //base case
    40.         *c = newnode(v);
    41.     }
    42.     else { //recursive
    43.         if (v >= (*c)->data) {
    44.             insertnode(&(*c)->r,v);
    45.         }
    46.         if (v <= (*c)->data) {
    47.             insertnode(&(*c)->l, v);
    48.         }
    49.     }
    50. }
    51. void inorder(T_ptr root) {
    52.     if (root == NULL) return;
    53.     inorder(root->l);
    54.     printf("%d ",root->data);
    55.     inorder(root->r);
    56. }
    57. void main() {
    58.     T_ptr root=NULL;
    59.     int dt;
    60.     while (true) {
    61.         printf("please enter val : ");
    62.         scanf("%d", &dt);
    63.         if (root == NULL)
    64.             root = newnode(dt);
    65.         else {
    66.             insertnode(&root, dt);
    67.         }
    68.         inorder(root);
    69.         printf("\n");
    70.     }
    71. }
    72.  
     
    Last edited: Apr 4, 2018
Loading...