1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
#include <stdio.h>
//#include<mmsystem.h> WHY SYSTEM HEADERS?
//#include<thread>
#include<stdlib.h>
#include<string.h>
#define DEBUG_INFO
// replacement for system("pause>0");
void waitForUser(){
printf("Press ENTER to continue...\n");
while((getchar() != '\n') && !feof(stdin))
; /* Empty loop */
}
struct node
{
char data[1000];
struct node* left;
struct node* right;
};
void printArray(char ints[], int len)
{
int i;
for (i = 0; i<len; i++) {
//printf("%d ", ints[i]);
printf("%c", ints[i]);
}
#ifdef DEBUG_INFO
// for debugging, o/p length, too
printf(" (%d)");
#endif
printf("\n");
}
void printPathsRecur(struct node* node, char path[], int pathLen)
{
if (node == NULL)
return;
//strcpy_s(&path[pathLen], 1000, node->data);
strncpy(&path[pathLen], node->data, 1000 - pathLen); // added - pathLen
//pathLen++;
pathLen+=strlen(node->data);
// add space
strncpy(&path[pathLen], " ", 1000 - pathLen); // added - pathLen
pathLen++;
if (node->left == NULL && node->right == NULL)
{
printArray(path, pathLen);
}
else
{
printPathsRecur(node->left, path, pathLen);
printPathsRecur(node->right, path, pathLen);
}
}
void printPaths(struct node* node)
{
char path[1000];
printPathsRecur(node, path, 0);
}
struct node* newNode(char *data)
{
struct node* node = (struct node*)malloc(sizeof(struct node));
//strcpy_s(node->data, data);
strncpy(node->data, data, 1000);
node->left = NULL;
node->right = NULL;
return node;
}
int main()
{
struct node *root = newNode("Hi");
root->left = newNode("Hello");
root->right = newNode("How are you");
root->left->left = newNode("I'm good");
root->left->right = newNode("lel");
printPaths(root);
// SHOULD REALLY FREE ALL MEMORY YOU ALLOCATED BEFORE EXITING!
//system("pause>0");
waitForUser();
return 0;
}
|