what's wrong with my code

Thread Starter

Werapon Pat

Joined Jan 14, 2018
35
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

C:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
typedef struct Node {
    int data;
    struct Node *r, *l;
}Tree;
typedef struct Node* T_ptr;
T_ptr newnode(int val) {
    T_ptr T;
    T = (T_ptr)malloc(sizeof(Tree));
    T->data = val;
    T->l = NULL;
    T->r = NULL;
    return T;
}
void insertnode(T_ptr root,int v) {
    if (root == NULL) { //base case
        root = newnode(v);
    }
    else { //recursive
        if (v >= root->data) {
            insertnode(root->r,v);
        }
        if (v <= root->data) {
            insertnode(root->l, v);
        }
    }
}
void inorder(T_ptr root) {
    if (root == NULL) return;
    printf("Inorder traverse : ");
    inorder(root->l);
    printf("%d ",root->data);
    inorder(root->r);
    printf("\n");
}
void main() {
    T_ptr root=NULL;
    int dt;
    while (true) {
        printf("please enter val : ");
        scanf("%d", &dt);
        if (root == NULL)
            root = newnode(dt);
        else
            insertnode(root, dt);
        inorder(root);
    }
}
Moderators note : used code tags
 
Last edited by a moderator:

WBahn

Joined Mar 31, 2012
30,062
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.
 

WBahn

Joined Mar 31, 2012
30,062
I figured out! All I need is just pass by reference root pointer to insertnode function
thanks!
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.
 

Thread Starter

Werapon Pat

Joined Jan 14, 2018
35
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:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
typedef struct Node {
    int data;
    struct Node *r, *l, *m;
}Tree;
typedef struct Node* T_ptr;
T_ptr newnode(int val) {
    T_ptr T;
    T = (T_ptr)malloc(sizeof(Tree));
    T->data = val;
    T->l = NULL;
    T->r = NULL;
    T->m = NULL;
    return T;
}
T_ptr find(T_ptr root,int v) {
    if (root->data == v) {
        return root;
    }
    else {
        if (v >= root->data) {
            find(root->r,v);
        }
        if (v <= root->data) {
            find(root->l,v);
        }
    }
}
void delnode(T_ptr summer,int v) {
    summer = find(summer,v);
    if (summer->r==NULL&&summer->l==NULL) {
 
    }
}
void insertnode(T_ptr* c,int v) {
    if (*c == NULL) { //base case
        *c = newnode(v);
    }
    else { //recursive
        if (v >= (*c)->data) {
            insertnode(&(*c)->r,v);
        }
        if (v <= (*c)->data) {
            insertnode(&(*c)->l, v);
        }
    }
}
void inorder(T_ptr root) {
    if (root == NULL) return;
    inorder(root->l);
    printf("%d ",root->data);
    inorder(root->r);
}
void main() {
    T_ptr root=NULL;
    int dt;
    while (true) {
        printf("please enter val : ");
        scanf("%d", &dt);
        if (root == NULL)
            root = newnode(dt);
        else {
            insertnode(&root, dt);
        }
        inorder(root);
        printf("\n");
    }
}
 

Attachments

Last edited:
Top