Commit 6b1987d8 authored by Ollie Rhodes's avatar Ollie Rhodes

Upload New File

parent da1f3c8c
#include <stdio.h>
#include <stdlib.h>
FILE* file;
char info[];
struct node{
int info;
struct node* left;
struct node* right;
};
//typedef void (*callback)(node*);
int main() {
//read("pg03data.txt");
//write("output.txt");
//bool first = true;
struct node *root = NULL;
file = fopen("pg03data.txt" , "r");
while (fgets(info, 100, file) != NULL){
// if (first == true){
// root = insert(root, info);
// first = false;
// }else{
insert_node(root,info);
// }
}
fclose(file);
file = fopen("output.txt", "w");
traverse(root);
fclose(file);
return 0;
}
/*
int read(char fileName[]){
//bool first = true;
struct node *root = NULL;
file = fopen(fileName , "r");
while (fgets(info, 100, file) != NULL){
// if (first == true){
// root = insert(root, info);
// first = false;
// }else{
insert_node(root,info);
// }
}
fclose(file);
}
*/
/*
int write(char newFileName[]){
file = fopen(newFileName, "w");
traverse(root);
fclose(file);
}
*/
struct node* insert_node(struct node *root, int data){
if(root == NULL)
{
root = create_node(info);
}
else
{
int is_left = 0;
int r = 0;
struct node* current = root;
struct node* prev = NULL;
while(current != NULL)
{
r = compare(data,current->info);
prev = current;
if(r < 0)
{
is_left = 1;
current = current->left;
}
else if(r > 0)
{
is_left = 0;
current = current->right;
}
}
if(is_left)
prev->left = create_node(info);
else
prev->right = create_node(info);
}
return root;
}
int compare(int left,int right)
{
if(strcmp(left, right) < 0)
return 1;
if(strcmp(left, right) > 0)
return -1;
return 0;
}
struct node* create_node(char lineInput[60]){
struct node *new_node = (node*)malloc(sizeof(struct node));
if(new_node == NULL)
{
fprintf (stderr, "Out of memory!!! (create_node)\n");
exit(1);
}
new_node->info = lineInput;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
}
void traverse(struct node *root){
node *current, *pre;
if(root == NULL)
return;
current = root;
while(current != NULL)
{
if(current->left != NULL)
{
cb(current);
current = current->right;
}
else
{
pre = current->left;
while(pre->right != NULL && pre->right != current)
pre = pre->right;
if (pre->right != NULL)
{
pre->right = current;
current = current->left;
}
else
{
pre->right = NULL;
cb(current);
current = current->right;
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment