Build Tree
Lesson 6 of 13 in Coddy's Binary Tree - Data Structures Series #3 course.
Challenge
EasyAdd to BinaryTree a method buildTree that gets a recursive string representation of the tree ([value, leftSubtree, rightSubtree] with null for empty) and builds the tree.
Try it yourself
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "binarytree.h"
void rtl(Node* n) {
if (n == NULL) return;
printf("%d\n", Node_getValue(n));
rtl(Node_getRight(n));
rtl(Node_getLeft(n));
}
int main() {
char buf[4096];
if (!fgets(buf, sizeof(buf), stdin)) buf[0] = '\0';
buf[strcspn(buf, "\r\n")] = '\0';
BinaryTree bt;
BinaryTree_init(&bt);
BinaryTree_buildTree(&bt, buf);
rtl(BinaryTree_getRoot(&bt));
return 0;
}
All lessons in Binary Tree - Data Structures Series #3
2Binary Tree Project
Node ClassNode with Sons