Menu
Coddy logo textTech

Node Class

Lesson 3 of 13 in Coddy's Binary Tree - Data Structures Series #3 course.

Let's start by building a Node — the basic building block of a binary tree.

challenge icon

Challenge

Easy

Write a class Node that has:

  • Constructor that gets no input and initializes the node value to 0.
  • getValue method which gets no input and returns the node's value.
  • setValue method that gets an integer and sets the node's value to it.

Try it yourself

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "node.h"

int main() {
    Node n;
    Node_init(&n);
    char line[256];
    while (fgets(line, sizeof(line), stdin)) {
        line[strcspn(line, "\r\n")] = '\0';
        char* cmd = strtok(line, " \t");
        if (!cmd) continue;
        if (strcmp(cmd, "setValue") == 0) {
            char* arg = strtok(NULL, " \t");
            if (arg) setValue(&n, atoi(arg));
        }
        if (strcmp(cmd, "getValue") == 0) {
            printf("%d\n", getValue(&n));
        }
    }
    return 0;
}

All lessons in Binary Tree - Data Structures Series #3