Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

Printing from a binary tree in C by matching leaf data

So i am trying to finish up a project and i am stuck. I have made almost everything and from my perspective the function i have made should be working fine but it does not. First of all i am reading IPs from a file in a decimal form and making them in a binary form based on the slash for different prefixes. For example if I read 8.0.0.0/8 means that i have to insert in my tree 00001000.

After that the user gives me an IP and then I have to search the tree one by one the leaves to match if I have to go left, that means 0, or if i have to go right, that means 1. And that's where I am stuck because I have made a print/traverse method that starts alright but breaks without letting me understand why. I am 99% sure that my insert method is correct because i have tested it with many debugging printfs so i am providing the structure of the struct I am using and the code of the print & insert functions.

typedef struct TREE{
    int data;
    struct TREE* left;
    struct TREE* right;
}TREE;

TREE* insert(TREE* root, int level, int tmpData[], int *slash) {
    
    TREE *tmp = root;
    TREE *newNode = NULL;
    int i;
    for(i = 0; i < slash[4]; i++) {

        if(tmpData[i] == 0) {
            //printf("We go LEFT.
");
            printf("0");
            if(tmp -> left == NULL) {
                newNode = (TREE*)malloc(sizeof(TREE));
                if (newNode == NULL) {
                    printf("33[0;31mFailed to allocate memory for a node.33[0m
");
                    exit(1);
                }
                newNode -> data = tmpData[i];
                newNode -> left = NULL;
                newNode -> right = NULL;
                tmp -> left = newNode;
            }
            tmp = tmp -> left;
        } else if(tmpData[i] == 1) {
            //printf("We go RIGHT.
");
            printf("1");
            if(tmp -> left == NULL) {
                newNode = (TREE*)malloc(sizeof(TREE));
                if (newNode == NULL) {
                    printf("33[0;31mFailed to allocate memory for a node.33[0m
");
                    exit(1);
                }
                newNode -> data = tmpData[i];
                newNode -> left = NULL;
                newNode -> right = NULL;
                tmp -> right = newNode;
            }
            tmp = tmp -> right;
        }
    }
}

void print(TREE* root, int max[], int tmpData[], int slash, int level){
    
    int i = 0;
    TREE *tmp = root;
    
    if (tmpData[0] == 0 && root -> left != NULL){
        tmp = root -> left;
    }
    else if (tmpData[0] == 1 && root -> right != NULL){
        tmp = root -> right;
    }
    else {
        return;
    }
    
    while(i != binarySIZE){
        if (tmp == NULL){
            printf("We BREAK.
");
            break;
        }
        
        printf("
%d
", tmp -> data);
        if (tmpData[i] == 0){
            printf("We go LEFT.
");
            max[i] = tmp -> data;
            printf("%d
", tmp -> data);
            tmp = tmp -> left;
        }
        else if (tmpData[i] == 1){
            printf("We go RIGHT.
");
            max[i] = tmp -> data;
            printf("%d
", tmp -> data);
            tmp = tmp -> right;
        }
        i++;
    }
}

Also i am initiating the tree with a root number 32 and the two pointer to NULL

root = (TREE*)malloc(sizeof(TREE));
root->data = 32;
root->left = NULL;
root->right = NULL;

And because of this initiation i have to use this perimeters at the start of the print so it does not check the root number 32.

if (tmpData[0] == 0 && root -> left != NULL){
    tmp = root -> left;
}
else if (tmpData[0] == 1 && root -> right != NULL){
    tmp = root -> right;
}
else {
    return;
}

Any help is really appreciated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...