Browse Source

Bad state. Save state of tree before being nuked

master
stew3254 2 years ago
parent
commit
b6663a9fcc
  1. 55
      jabber.c
  2. 41
      tree.c
  3. 20
      tree.h

55
jabber.c

@ -0,0 +1,55 @@
#include "stdlib.h"
#include "stdbool.h"
#include "string.h"
#include "stdio.h"
#include "tree.h"
//Define node on tree
typedef struct Node {
struct Node *zero;
struct Node *one;
int index;
} node;
//Define tree
typedef struct Tree {
node *root;
int size;
} tree;
int main() {
assert(argc == 3);
struct stat sbuf = {};
if (stat(argv[1], &sbuf) != 0) {
fprintf(stderr, "Error finding file\n");
return -1;
}
//Assume we can read the file
char *buff = (char*) malloc(sbuf.st_size * sizeof(char));
FILE *f = fopen(argv[1], "r");
if (f == NULL) {
fprintf(stderr, "Error opening input file\n");
return -1;
}
if (fread(buff, sizeof(char), sbuf.st_size, f) != sbuf.st_size) {
fclose(f);
fprintf(stderr, "Error reading file\n");
return -1;
}
//Close file when done reading
fclose(f);
f = fopen(argv[2], "w");
if (f == NULL) {
fprintf(stderr, "Error opening output file\n");
return -1;
}
tree *t = newt();
close();
return 0;
}

41
tree.c

@ -1,48 +1,49 @@
#include "stdlib.h"
#include "stdbool.h"
#include "string.h"
#include "tree.h"
static inline int isleaf(node *n) {return (n->left == NULL && n->right == NULL);}
static inline int isleaf(node *n) {return (n->zero == NULL && n->one == NULL);}
node *newn(char bit, int index) {
node *newn(int index) {
node *n = (node*) malloc(sizeof(node));
n->left = NULL;
n->right = NULL;
n->bit = bit;
n->zero = NULL;
n->one = NULL;
n->index = index;
}
int tadd(tree *t, char *tag, int index) {
node *current = t->root, *prev = NULL;
int tadd(tree *t, char *tag) {
node *current = t->root;
int i;
for (i = 0; i < strlen(tag); ++i) {
if (current == NULL) {
if (isleaf(current)) {
++t->size;
if (t->root == NULL) {
t->root = newn(tag[i], 1);
return t-size;
if (current == t->root) {
if (tag[i] == '0')
t->root->zero = newn(t->size);
else
t->root->one = newn(t->size);
return t->size;
}
else {
if (tag[i] == '0')
prev->left = newn(tag[i], t->size);
current->zero = newn(t->size);
else
prev->right = newn(tag[i], t->size);
current->one = newn(t->size);
}
}
else if (tag[i] == '0') {
prev = current;
current = current->left;
current = current->zero;
}
else {
prev = current;
current = current->right;
current = current->one;
}
}
if (current == NULL) {
if (isleaf(current)) {
if (tag[i] == '0')
prev->left = newn(tag[i], t->size);
current->zero = newn(t->size);
else
prev->right = newn(tag[i], t->size);
current->one = newn(t->size);
return i;
}
else {

20
tree.h

@ -1,7 +1,8 @@
#include "stdbool.h"
typedef struct Node {
struct Node *left;
struct Node *right;
char bit;
struct Node *zero;
struct Node *one;
int index;
} node;
@ -10,8 +11,13 @@ typedef struct Tree {
int size;
} tree;
char *tadd(tree *t, char *tag, int index);
node *newn(int index);
static inline bool tisempty(tree *t) {return (t->root->zero == NULL && t->root->one == NULL);}
static inline int tsize(tree *t) {return t->size;}
static inline tree *newt() {return (tree*) calloc(1, sizeof(tree))}
static inline tree *tisempty(tree *t) {return (t->root->left == NULL && t->root->right == NULL);}
static inline tree *tsize(tree *t) {return t->root->size;}
static inline tree *newt() {
tree *t = (tree*) malloc(sizeof(tree));
t->root = newn(0);
return t;
}
Loading…
Cancel
Save