<jupyter_start><jupyter_text>Binary Tree Representation as an array<jupyter_code>%%writefile arraytree.h
#pragma once

#include <vector>
#include <functional>

using namespace std;

class ArrayTree {
    vector<float> elements;
    int len;

public:
    ArrayTree(int cap=100): elements(cap), len(0) {}

    void add(float elem) {
        elements[len++] = elem;
    }
    void inOrder(function<void(float)> process) {
        inOrder(0, process);
    }
    void preOrder(function<void(float)> process) {
        preOrder(0, process);
    }
    void postOrder(function<void(float)> process) {
        postOrder(0, process);
    }

private:
    int parent(int i)       { return (i - 1) / 2; }
    int leftChild(int i)    { return 2*i + 1; }
    int rightChild(int i)   { return 2*i + 2; }
    
    void inOrder(int i, function<void(float)> process) {
        if (i < len) {
            inOrder(leftChild(i), process);
            process(elements[i]);
            inOrder(rightChild(i), process);
        }
    }
    void preOrder(int i, function<void(float)> process) {
        if (i < len) {
            process(elements[i]);
            preOrder(leftChild(i), process);
            preOrder(rightChild(i), process);
        }
    }
    void postOrder(int i, function<void(float)> process) {
        if (i < len) {
            postOrder(leftChild(i), process);
            postOrder(rightChild(i), process);
            process(elements[i]);
        }
    }
};

%%writefile 1.cpp
#include <iostream>
#include "arraytree.h"

using namespace std;

int main() {
    ArrayTree* t = new ArrayTree();

    for (int i = 1; i <= 20; ++i) {
        t->add(i);
    }

    auto f = [](float a) { cout << a << " "; };

    t->inOrder(f); cout << endl;
    t->preOrder(f); cout << endl;
    t->postOrder(f); cout << endl;

    delete t;

    return 0;
}
!g++ -std=c++17 -O2 -Wall 1.cpp && ./a.out<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15412748171368862, 'fraction_numerical': 0.011494252873563218, 'mean_word_length': 2.377425044091711, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '103813', 'license': 'CC0-1.0', 'n_tokens_mistral': 667, 'n_tokens_neox': 644, 'n_words': 154, 'path': '20202/aed_20202_111.ipynb', 'repo_name': 'pcsilcan/aed', 'size': '1914'}