Ajout : définition d'un nœud d'arbre n-aire
This commit is contained in:
parent
0fc1f02c9f
commit
fbb1b5592c
2 changed files with 107 additions and 0 deletions
88
src/node.c
Normal file
88
src/node.c
Normal file
|
@ -0,0 +1,88 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "node.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a brand new node, initialized
|
||||||
|
*/
|
||||||
|
Node *Node_new(char letter) {
|
||||||
|
Node *node = malloc(sizeof(Node));
|
||||||
|
|
||||||
|
node->letter = letter;
|
||||||
|
node->next_sibling = NULL;
|
||||||
|
node->first_child = NULL;
|
||||||
|
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Free all nodes from a root node
|
||||||
|
*/
|
||||||
|
void Node_destroyTree(Node *root_node) {
|
||||||
|
if (root_node->first_child != NULL) {
|
||||||
|
Node_destroyTree(root_node->first_child);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (root_node->next_sibling != NULL) {
|
||||||
|
Node_destroyTree(root_node->next_sibling);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(root_node);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* With a root node, build the n-ary tree from a string
|
||||||
|
*/
|
||||||
|
void Node_addString(Node *root_node, char *letters) {
|
||||||
|
if (letters[0] == '\0') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (root_node->letter != letters[0] && root_node->next_sibling != NULL) {
|
||||||
|
root_node = root_node->next_sibling;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (root_node->letter == letters[0]) {
|
||||||
|
letters++;
|
||||||
|
if (letters[0] != '\0' && root_node->first_child == NULL) {
|
||||||
|
root_node->first_child = Node_new(letters[0]);
|
||||||
|
}
|
||||||
|
Node_addString(root_node->first_child, letters);
|
||||||
|
} else if (root_node->next_sibling == NULL) {
|
||||||
|
root_node->next_sibling = Node_new(letters[0]);
|
||||||
|
Node_addString(root_node->next_sibling, letters);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void print_tabs(unsigned int depth) {
|
||||||
|
for (unsigned int i = 0; i < depth; i++) {
|
||||||
|
printf("| ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Recursively print the content of the tree
|
||||||
|
*/
|
||||||
|
static void print_node(Node *node, unsigned int depth) {
|
||||||
|
print_tabs(depth);
|
||||||
|
printf("%c", node->letter);
|
||||||
|
|
||||||
|
if (node->first_child != NULL) {
|
||||||
|
putchar('\n');
|
||||||
|
print_node(node->first_child, depth+1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node->next_sibling != NULL) {
|
||||||
|
putchar('\n');
|
||||||
|
print_node(node->next_sibling, depth);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize the printing of a tree
|
||||||
|
*/
|
||||||
|
void Node_printTree(Node *root_node) {
|
||||||
|
print_node(root_node, 0);
|
||||||
|
putchar('\n');
|
||||||
|
}
|
19
src/node.h
Normal file
19
src/node.h
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
#ifndef NODE_H
|
||||||
|
#define NODE_H
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define the members of my n-ary tree
|
||||||
|
*/
|
||||||
|
typedef struct Node {
|
||||||
|
char letter;
|
||||||
|
struct Node *next_sibling;
|
||||||
|
struct Node *first_child;
|
||||||
|
} Node;
|
||||||
|
|
||||||
|
Node *Node_new(char letter);
|
||||||
|
void Node_destroyTree(Node *root_node);
|
||||||
|
void Node_addString(Node *root_node, char *letters);
|
||||||
|
void Node_printTree(Node *root_node);
|
||||||
|
char Node_stringToDigit(Node *root_node, char *string);
|
||||||
|
|
||||||
|
#endif /* NODE_H */
|
Reference in a new issue