如何在二叉树中更改字符串的值?

时间:2018-01-13 22:43:42

标签: c file tree

此函数update()应该假设,它会扫描文件流upd中的信息,PC的编号及其新的IP地址。它应扫描树,查找具有相同red_brs_red_br的PC,并在找到该PC时,应使用新的IP地址(s_ip_adress)更新其IP地址。

red_brs_red_br是PC的号码; ip_adresa是PC的当前IP地址; s_ip_adresa是一个新的IP地址。

RACUNAR来自:

typedef struct racunar_st {
    int red_br;
    char naziv[MAX_NAZIV];
    char ip_adresa[MAX_IP];
    int brzina;
    struct racunar_st *left;
    struct racunar_st *right;
} RACUNAR;



void update(FILE *upd, FILE *out, RACUNAR *root){
    int s_red_br;   
    char s_ip_adresa[MAX_IP];
    while(fscanf(upd, "%d %s", &s_red_br, s_ip_adresa) == 2){
         int temp = root->red_br;          
         char temp1 = *root->ip_adresa;                             
         if (temp == s_red_br){  
             temp1 = *s_ip_adresa; 
         }
    }
}

我知道我不会穿过整棵树;我只是想测试它,我甚至无法改变根目录。

在这里你完整的程序:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define MAX_NAZIV 20
#define MAX_VEL 10
#define MAX_IP 14+1

typedef struct racunar_st {
    int red_br;
    char naziv[MAX_NAZIV];
    char ip_adresa[MAX_IP];
    int brzina;
    struct racunar_st *left;
    struct racunar_st *right;
} RACUNAR;


void init_tree(RACUNAR **root){
    *root = NULL;
}

void add_to_tree(RACUNAR *new, RACUNAR **root){
    if(*root == NULL){
        *root = new;
    } else if(new->brzina <= (*root)->brzina){
        add_to_tree(new, &((*root)->left));
    } else if(new->brzina > (*root)->brzina) {
        add_to_tree(new, &((*root)->right));
    }
}

RACUNAR *create_new_item(int red_br, char naziv[], char ip_adresa[], int brzina){
    RACUNAR *new = (RACUNAR *)malloc(sizeof(RACUNAR));
    if(new == NULL){
        printf("Nema dovoljno RAM-a!!!\n");
        exit(21);
    }

    new->red_br = red_br;
    strcpy(new->naziv, naziv);
    strcpy(new->ip_adresa, ip_adresa);
    new->brzina = brzina;

    new->left = NULL;
    new->right = NULL;

    return new;
}

void read_tree_from(FILE *in, RACUNAR **root){
    int tmp_red_br; 
    char tmp_naziv[MAX_NAZIV];
    char tmp_ip_adresa[MAX_IP];
    int tmp_brzina;
    while(fscanf(in, "%d %s %s %d", &tmp_red_br, tmp_naziv, tmp_ip_adresa, &tmp_brzina) != EOF){
        RACUNAR *new = create_new_item(tmp_red_br, tmp_naziv, tmp_ip_adresa, tmp_brzina);
        add_to_tree(new, root);
    }
}

void save_item_to(FILE *out, RACUNAR *x){
    fprintf(
        out, "%d %s %s %d\n",
        x->red_br, x->naziv, x->ip_adresa, x->brzina
        );
}

void save_tree_to(FILE *out, RACUNAR *root){
    if(root != NULL){
        save_tree_to(out, root->left);
        save_item_to(out, root);
        save_tree_to(out, root->right);
    }
}

void destroy_tree(RACUNAR **root){
    if(*root != NULL){
        destroy_tree(&((*root)->left));
        destroy_tree(&((*root)->right));
        free(*root);
        *root = NULL;
    }
}

FILE *safe_fopen(char *filename, char *mode, int error_code){
    FILE *fp = fopen(filename, mode);
    if(fp == NULL){
        printf("Nije moguce otvoriti fajl '%s'\n", filename);
        exit(error_code);
    }

    return fp;
}

void update(FILE *upd, FILE *out, RACUNAR *root){
    int s_red_br;   
    char s_ip_adresa[MAX_IP];
    while(fscanf(upd, "%d %s", &s_red_br, s_ip_adresa) == 2){
         int temp = root->red_br;
            printf("%d\n", temp);   // just testing to see if the function read data from file
                printf("%d\n", s_red_br);    
                    printf("%s\n", s_ip_adresa);   
                if ( temp == s_red_br){  
                     *root->ip_adresa = *s_ip_adresa; 
                }
    }   printf("%s\n", root->ip_adresa); // checking if that ip adress has been changed
}

int main(int arg_num, char *args[]) {
    if(arg_num != 4){
        printf("Neispravan poziv programa!\n Pozvati sa: FILENAME_IN FILENAME_UPDATE FILENAME_OUT\n");
        exit(1);
    }

    char *in_filename = args[1];
    char *update_filename = args[2];
    char *out_filename = args[3];

    FILE *in = safe_fopen(in_filename, "r", 1);
    FILE *upd = safe_fopen(update_filename, "r", 2);
    FILE *out = safe_fopen(out_filename, "w", 3);

    RACUNAR *root;
    init_tree(&root);

    read_tree_from(in, &root);
    update(upd, out, root);
    save_tree_to(out, root);

    destroy_tree(&root);

    fclose(in);
    fclose(upd);
    fclose(out);

return 0;
}

1 个答案:

答案 0 :(得分:1)

第二次尝试:

您的update(upd, out, root);获取输入文件,输出文件和指向内存中二叉树的指针。您的save_tree_to(out, root);获取输出文件和指向root的指针。下定决心将这两个函数中的哪一个写入文件。假设我们不需要update的输出文件,并且只需在根数据结构中进行更改,那么:

void update( FILE *upd, RACUNAR *root )
{
    int s_red_br;
    char s_ip_adresa[MAX_IP];
    while ( fscanf( upd, "%d %s", &s_red_br, s_ip_adresa ) == 2 )
    {
        RACUNAR *node = find( s_red_br, root );
        if ( NULL != node )
        {
            strncpy(node->ip_adresa, s_ip_adresa, MAX_IP);
        }
        else
        {
            // Either insert new data or throw some kind of error here.
        }
    }
}

您真正的问题是您的数据结构未按主键排序。给定red_br值,如果不搜索整个树,则无法找到匹配的节点。

RACUNAR* find( int red_br, RACUNAR *root )
{
    RACUNAR *node = root;
    while ( NULL != node )
    {
        if ( red_br == node->red_br )
        {
            return node;
        }

        // TODO: Need search code here, basically requires a tree sorted by red_br values.

        node = NULL;
    }
    return node;
}