A comprehensive repository of individual Pascal programs, each implementing a different data structure or algorithmic approach – from fundamentals to advanced.
Ideal for students and enthusiasts interested in Pascal and classic CS topics.
Below is a list of all included Pascal programs.
Each entry links to an individual writeup and usage instructions.
- AVLTree.pas: AVL (Height-Balanced Self-Balancing) Binary Search Tree
- BTree.pas: B-Tree (Multiway, Balanced Search Tree)
- CNN.pas: Convolutional Neural Network (Deep Learning)
- DatastructureTest.pas: Comprehensive Data Structure Tester
- DatastructureTestResults.txt: Example Test Output
- FacadeCNN.pas: CNN (Convolutional Neural Network) Facade
- FacadeGNN.pas: GNN (Graph Neural Network) Facade
- FacadeMLP.pas: MLP (Multi Layer Perceptron) Facade
- FacadeRNN.pas: RNN (Recurrent Neural Network) Facade
- FacadeTransformer.pas: Transformer Model Introspection and Manipulation Facade
- HeapBinaryTree.pas: Binary Tree-based Heap Data Structure
- HeapBinaryTreeNode.pas: Node Class for Binary Tree-based Heap
- HeapDoubleLinkedList.pas: Heap Using a Doubly Linked List
- HeapNode.pas: Node Class for Heap Implemented via Linked List
- MLP.pas: MultiLayer Perceptron (Feedforward Neural Network)
- RNN.pas: Advanced Recurrent Neural Network
- RedBlackTree.pas: Red-Black Self-Balancing Binary Search Tree
- SkipList.pas: Probabilistic Skip List Data Structure
- Stack.pas: Classic Stack (Array-based) Implementation
- StackBinaryTree.pas: Stack Implemented using a Binary Tree
- StackDoubleLinkedList.pas: Stack Using a Doubly Linked List
- StackLinkedList.pas: Stack Using a Linked List
- Transformer.pas: Minimal Pascal Transformer (Attention-based Model)
- Trie.pas: Trie (Prefix Tree / Digital Tree)
- UnionFind.pas: Disjoint Set / Union-Find Data Structure
File: AVLTree.pas
Category: Data Structures / Trees / Self-Balancing BST
Implements an AVL Tree, a classic height-balanced self-balancing binary search tree, in Pascal.
AVL trees guarantee O(log n) insertion, deletion, and lookup by maintaining the height difference (balance factor) between every node’s left and right subtrees at most one, after every modification.
Features:
- Fully dynamic insertions with automatic balancing ("fixup")
- Pointer-based node structure with explicit
data,height,parent,left, andright - Efficient left and right rotations, single and double
- Real-time balance and height calculation; maintains
heightproperty on all updates inorderTraversalmethod prints each value, height, and balance factor for inspection
Data Model:
- Each node stores:
data: the integer value in the nodeheight: cached subtree height for O(1) balance checksparent,left,right: classic BST pointers
Requirements:
- Free Pascal Compiler (FPC), version 3.x or later
Usage Example:
uses AVLTree;
var
tree: TAVLTree;
root: treeNode;
begin
tree.create;
tree.insert(root, 10);
tree.insert(root, 8);
tree.insert(root, 13);
tree.insert(root, 6);
tree.inorderTraversal(root);
end.To Compile:
fpc AVLTree.pas
# ...plus your main or test driver- All balancing and rotation logic is handled seamlessly in
insertFixup, so insertion always produces a balanced BST. - Traversal prints each node along with its height and balance for confidence in structure.
File: BTree.pas
Category: Data Structures / Trees / Multiway / Balanced Search
A full implementation of a B-Tree, the classic multiway, height-balanced search tree ideal for large datasets and external memory (disk) indexing.
B-trees maintain sorted data and allow efficient O(log n) search, insert, and sequential access, and are the backbone for databases, filesystems, and big indexes.
Features:
- All node and pointer management in explicit Pascal pointer/array logic
- Configurable minimum degree (3 by default) for branching and storage
- Node splitting during insert (handles overflow automatically)
- Efficient binary search for inserts and retrievals
- Keeps nodes "mostly full" for minimal tree height and efficient traversal
- Printing in-order traversals at any time
Data Model:
- Each node comprises:
keys: array of integer valueschildren: array of child pointersnumKeys: current key count (≤ max per node)isLeaf: booleanparent: for upward traversal/structural logic
Requirements:
- Free Pascal Compiler (FPC), version 3.x or newer
Usage Example:
uses BTree;
var
tree: TBTree;
root: treeNode;
begin
tree.create;
tree.insert(root, 10);
tree.insert(root, 15);
tree.insert(root, 25);
tree.inorderTraversal(root);
end.To Compile:
fpc BTree.pas
# ...plus your main or demonstration program- Designed for in-memory operation, but logic maps directly to disk/large datasets.
- For best educational value, step through the split/insert semantics (see
splitChild). - This classic implementation is the foundation for exploring filesystems and database internals.
File: CNN.pas
Category: Machine Learning / Deep Learning
A fully self-contained Pascal implementation of a modern Convolutional Neural Network (CNN) from scratch.
Features include:
- Multiple convolutional and pooling layers
- Fully connected layers
- ReLU activation, softmax + cross-entropy loss
- Adam optimizer with bias correction
- Dropout regularization
- Numerically stable softmax, clipping, and error handling
- Model save/load
- Modular design using object-oriented free Pascal (
{$mode objfpc})
This is a teaching/research-oriented example: no external libraries are required for the core functionality, and the code exposes internal states for hands-on learning.
Requirements:
- Free Pascal Compiler (FPC), version 3.x or later recommended
- 64-bit system recommended for large arrays (due to memory use)
Compile:
fpc CNN.pasRun:
./CNNtestNote: The program block in this file is named CNNtest.
To Use as a Library:
You can also uses the CNN class in other Pascal files or units for custom experiments or integration.
- The program/demo provided in the main block of
CNN.pascan be modified to load your own image data, set training targets, and configure network parameters. - Model hyperparameters (number of layers, filters, etc.) are set in the constructor of
TConvolutionalNeuralNetwork. - For real datasets: you will need to implement (or adapt) input conversion from image files to the expected
TImageDataformat.
var
cnn: TConvolutionalNeuralNetwork;
begin
cnn := TConvolutionalNeuralNetwork.Create(
28, // input width
28, // input height
1, // channels (e.g. grayscale)
[8,16],// Conv filters per layer
[3,3], // Kernel sizes
[2,2], // Pool sizes
[64], // FC layer sizes
10, // output classes
0.001, // learning rate
0.25 // dropout rate
);
// Now use cnn.Predict(...) and cnn.TrainStep(...)
end.Model Saving/Loading:
- Call
cnn.SaveCNNModel('my_model.bin')andcnn.LoadCNNModel('my_model.bin')as needed.
File: DatastructureTest.pas
Category: Data Structure Testing / Demonstration
A Pascal program that serves as a unified tester for several classic data structure implementations, such as linked lists, double-linked lists, binary-tree-based stacks, and heaps.
It is designed to automatically create each structure, add elements, perform standard operations (insert, delete, retrieve), and print the process/results step-by-step.
The code provides a hands-on, procedural demonstration of each supported module, driven by verbose output via writeln.
Included Modules:
StackLinkedList.pasStackDoubleLinkedList.pasStackBinaryTree.pasHeapLinkedList.pasHeapDoubleLinkedList.pas
Requirements:
- Free Pascal Compiler (FPC), version 3.x or later recommended
Compile:
fpc DatastructureTest.pasRun:
./DataStructureTestWhat it does:
- Sequentially instantiates each data structure (e.g., linked list, double-linked, heap)
- Demonstrates: node insertion, deletion (first, last, by index), data extraction, and search
- Prints each action with a descriptive output for simple tracking and validation
- This program is primarily for learning and testing the functionality of the included data structure units.
- To add your own tests, append more operations in the
begin ... end.block after the existing demonstrations. - The program output is intended to match the sample found in
DatastructureTestResults.txt.
File: DatastructureTestResults.txt
Category: Output Sample / Reference
A plain-text file capturing a real output log from running DatastructureTest.pas.
It documents every step, action, and change of state performed on the data structures during the test run.
You can use this file to:
- Verify expected output for successful test runs
- Compare changes when you modify the test program
- Understand the normal "flow" of each data structure’s use and manipulation
- Compile and run
DatastructureTest.pasas described above - Compare your terminal output to this file to ensure correct operation
- Use differences to help debug or enhance your structures
File: FacadeCNN.pas
Category: Machine Learning Utilities / Deep Learning Helper
A comprehensive Pascal unit (unit CNNFacade) providing a facade (i.e., a simplified interface) for deep introspection, manipulation, and analysis of Convolutional Neural Networks (CNNs).
This unit is designed to enhance your ability to debug, analyze, and extend CNNs implemented by the author’s other Pascal modules (see CNN.pas), by exposing detailed accessors and tools for reading and adjusting internal model state.
Key Capabilities:
- Detailed access to convolutional and fully connected layer parameters (weights, biases)
- Structured types for feature maps, kernels, neuron parameters, batch norm, etc.
- Utilities for extracting statistics (means, stdev, min/max) for any layer
- Read/write access to layer configurations and attributes
- Support for batch normalization parameters, filter attributes, and receptive field calculations
- All code is pure Pascal (
{$mode objfpc}), designed to be integrated alongside core CNN code
This unit is particularly useful for:
- Educational visualization of neural networks
- Research into layer behavior and transformations
- Custom training loops, fine-tuning, and explainable AI
- Model inspection or serialization
Requirements:
- Free Pascal Compiler (FPC), version 3.x or later recommended
- Should be used in concert with a core CNN implementation (such as
CNN.pas), by addingFacadeCNNto yourusesclause
To Integrate:
- Place
FacadeCNN.pasin your project directory - In your main program or unit, add it to the
usesclause:uses FacadeCNN, CNN; - Instantiate and use the
TCNNFacadeclass for advanced access, e.g.:var fcnn: TCNNFacade; begin fcnn := TCNNFacade.Create( ...layers/params... ); // ... CNN usage ... // Example: read the feature map of Conv Layer 1, Filter 0 var fmap := fcnn.GetFeatureMap(1, 0); end;
- Use the provided accessor and mutator functions to:
- Retrieve or set kernel weights
- Access feature maps, preactivations, biases
- Gather per-layer statistics for analysis or visualization
- Modify filter attributes or apply batch normalization parameters
- This facade is not a standalone program, but a utility class/unit to use with compatible neural network models.
- Can be used for in-depth experiment logging, debugging, and research.
- Extend or customize the unit for your own CNN architectures or for integrations with mathematical/statistical analysis tools.
File: FacadeGNN.pas
Category: Machine Learning Utilities / Graph Learning
A comprehensive Pascal unit (unit GNNFacade) that provides a facade (simplified interface) as well as extensive introspection, manipulation, and analysis utilities for Graph Neural Networks (GNNs).
This unit is intended to support advanced GNN architectures, training, and experimentation in Pascal, equipping researchers and students to:
- Build, train, and inspect Graph Neural Networks for node, edge, or whole-graph learning tasks
- Access and manipulate all aspects of network state: layers, embeddings, weights, activations, gradients, edge features, adjacency structures, etc.
- Run and debug message passing, backpropagation, loss calculation, and architecture configuration
- Support for various activation and loss types, batch embeddings, custom optimizers, and flexible graph configurations (undirected, self-loops, edge deduplication)
Core Features:
- Modular layer and neuron types for message, update, readout, and output computations
- Deep access to node and edge features, graph topology, and learned representations
- Built-in support for gradient clipping and diagnostic metric tracking
- Numerous utility routines for copying, concatenating, and handling arrays/graphs
- Can be extended for your own GNN flavors: GCN, GAT, MPNN, etc.
All code is Object Pascal ({$mode objfpc}), and is compatible with modern Free Pascal.
Requirements:
- Free Pascal Compiler (FPC), version 3.x or later recommended
- Designed to import as a unit in your GNN projects
To Integrate:
- Place
FacadeGNN.pasin your project directory. - Add the unit to the
usesclause in your Pascal project:uses FacadeGNN; - You can now define and create
TGraphNeuralNetworkobjects, configure layers, and operate onTGraphstructures. - Use the methods and properties to:
- Initialize architectures with standard or custom parameters
- Train/run GNNs stepwise or on batches
- Inspect internal states (embeddings, weights, activations, gradients)
- Export, analyze, or manipulate models in-depth for research or teaching
- This unit is not a standalone executable, but a reusable module for advanced GNN engineering and exploration.
- Consult the inline documentation and type declarations for extending to your own needs (e.g., new aggregation strategies, custom metrics).
- For introductory usage, build a main program that includes this unit and demonstrates node classification or graph regression.
File: FacadeMLP.pas
Category: Machine Learning Utilities / Feedforward Neural Nets
A thoroughly-featured Object Pascal unit (unit MLPFacade) acting as a facade for multi-layer perceptrons.
This module is engineered to provide detailed, externally accessible control and introspection over the implementation of a classic feedforward neural network (MLP), especially designed for experimentation, research, and educational uses.
Core Functions Exposed:
- Full access to MLP architecture (input/hidden/output layers)
- Neuron and layer-wise accessors: weights, biases, pre-activations, outputs, error gradients
- Batch normalization, dropout settings, optimizer state (SGD, Adam, RMSProp)
- L2-regularization and per-neuron attributes
- Network topology modification (add/remove layers & neurons dynamically)
- Batch/epoch training statistics, histogram features for diagnostics
All types, records, and methods are presented in {$mode objfpc}/Free Pascal style for seamless advanced integration.
This unit is ideal for:
- Introspecting/tracing MLP activations and gradients at every stage
- Modifying/training networks on the fly (e.g., for autoML or ablation studies)
- Logging/visualizing inner state for teaching and debugging
- Custom research where standard black-box neural nets aren’t enough
Requirements:
- Free Pascal Compiler (FPC), version 3.x or later recommended
- Designed for use together with an MLP definition/implementation compatible with this facade (such as
TMultiLayerPerceptronshown in the source)
Integration Steps:
- Place
FacadeMLP.pasin your project directory. - Add it to your
usesclause in your main program or an analysis tool:uses FacadeMLP; - Instantiate your core
TMultiLayerPerceptronmodel, then wrap it with theTMLPFacade:var mlp: TMultiLayerPerceptron; facade: TMLPFacade; begin mlp := TMultiLayerPerceptron.Create( ... ); facade := TMLPFacade.Create(mlp); // Now inspect/set neurons, layers, weights, etc. end;
- Use the extensive API:
- Query any neuron's weights, error, dropout, batchnorm stats
- Adjust learning rates, regularization, optimizer state
- Add/remove neurons/layers dynamically for research
- Collect/bucketize outputs/histograms for diagnostics
- This is not a standalone runnable file, but a powerful utility unit for hands-on control and analysis of MLPs in Pascal.
- Designed for deep ML experimentation, explainability, and teaching.
- For further details, see type declarations and implementation in the code; customize/extend as you wish for your own research!
File: FacadeRNN.pas
Category: Machine Learning Utilities / Recurrent Neural Networks
A powerful Object Pascal unit (unit RNNFacade) providing a unified facade (API/class) for deep introspection, manipulation, and research on Recurrent Neural Networks (RNNs) of various kinds—including vanilla/SimpleRNN, LSTM, and GRU architectures.
This module is suitable for:
- Inspecting, extracting, or modifying all weights, gates, gradients, optimizer states, activations, and dropout at every layer and timestep
- Supporting multiple RNN cell types, loss/activation functions, and output layers
- Collecting histograms and diagnostic statistics (gate saturation, gradient scales) for debugging/visualization
- Accessing time-step caches, running states, and normalization/regularization properties
- Facilitating advanced research into sequence modeling, ablation studies, and explainability in deep learning
All code is Free Pascal ({$mode objfpc}) with modern types/conventions.
Requirements:
- Free Pascal Compiler (FPC), version 3.x or above recommended
- Designed for use inside a program that instantiates/interacts with RNNs, LSTMs, or GRUs compatible with this facade API
Integration Steps:
- Place
FacadeRNN.pasin your project directory. - Include it in your
usesclause in your main program or research tool:uses FacadeRNN; - Depending on your architecture, create the relevant cell wrappers or the
TRNNFacadeobject. Example:var rnn: TRNNFacade; begin rnn := TRNNFacade.Create(...); // Now use RNN API to read/write activations, gates, optimizer states, etc. end.
- Use the extensive API to:
- Inspect any gate (LSTM/GRU/Simple), activation, or error variable for any cell and timestep
- Access/modify weights, gradients, dropout masks, normalization statistics
- Run chained training/forward/backward passes and gather in-depth logs, stats, or visualize diagnostics
- This is a reusable unit for advanced RNN experimentation and explainability—not a runnable standalone program.
- Designed for deep ML research, saliency inspection, and educational tracing of sequence architectures.
- For direct code/API walkthrough, consult inline type and class definitions.
File: FacadeTransformer.pas
Category: Machine Learning / Transformers / Introspection Utilities
A powerful Pascal unit providing an advanced facade for introspection, inspection, and manipulation of transformer models loaded from Transformer.pas.
This class is intended for research, educational, and debugging purposes—letting users deeply inspect attention, embeddings, parameters, internal network states, and even dynamically mutate the transformer architecture at runtime.
Features:
- Inspect internal model state:
- Per-layer and per-head hidden activations, Q/K/V vectors
- All attention logits and softmax weights for fine-grained attention analysis
- Access embeddings (token and positional) and model hyperparameters
- Dump weights, check structural layout, or mutate dimensions (add/remove layers/heads)
- Manipulate weights, positions, or intermediate activations
- Access/adjust key-value cache (for attention/memory states)
- Retrieve residual, layer norm, and FFN outputs per token and layer
- Run forward passes with full memory of activations for explainability
- Generate text or run prompts with fully visible intermediate state
Intended Use Cases:
- Explainability, visualization, and attribution in transformer models
- Fine-tuning, ablation studies, and architectural research
- Debugging/diagnostics at any stage in the model
Requirements:
- Free Pascal Compiler (FPC), version 3.x or above
- Load with a transformer model trained/exported to compatible GGUF format and paired tokenizer
Integration Example:
uses FacadeTransformer;
var
facade: TTransformerFacade;
begin
facade := TTransformerFacade.Create;
facade.LoadModel('model.gguf');
facade.LoadTokenizer('tokenizer.json');
// Forward a prompt, then inspect attention, QKV, activations, etc.
end.Workflow:
- Run your prompt or batch, then retrieve desired state using accessors (e.g.,
GetAttentionWeights,GetQKV,GetHiddenState,GetLogits, etc).
- Most useful as a "probe" or spike-in tool for model understanding and interpretability—pair it with visualizations or research loops.
- For basic model use or inference, use only
Transformer.pas. - All major model architectural statistics and activations are accessible through the dedicated API.
File: HeapBinaryTree.pas
Category: Data Structures / Heaps / Trees
Implements a classic binary tree-based heap in Pascal, including all fundamental operations: insertion, deletion, pre-order/in-order/post-order traversal, and node search.
Used as an educational/reference example for building a heap structure using explicit node pointers (THeapBinaryTreeNode) and emphasizing binary search tree-style data relationships.
Key Features:
- Pure Object Pascal implementation (
{$mode objfpc}) - Provides:
insertData(inputData: integer)– insert a new value into the heap/treedeleteNode(key: integer): boolean– remove a node by its valuecountNodes(): integer – total nodes via pre-order traversalprintTree()– prints the tree with indented, pre-order formattingfindNodeNumber(key: integer): integer– lookup node position for a value
- Modular, with all logic separated from the interactive or application-level I/O
Underlying nodes are managed with the companion HeapBinaryTreeNode.pas unit.
Requirements:
- Free Pascal Compiler (FPC), version 3.x or later
HeapBinaryTreeNode.pasmust be in the same directory for compilation
Compile (as part of a program or unit):
-
To use as a component of a main program:
uses HeapBinaryTree;Compile/link with all required files.
-
To interactively test, add method calls to a program using this class:
var tree: THeapBinaryTree; begin tree := THeapBinaryTree.create; tree.insertData(5); tree.insertData(3); tree.insertData(8); tree.printTree; end.
Or, compile with a provided test harness if present:
fpc HeapBinaryTree.pas
# ...plus a test main program- Traversal methods (pre/in/post-order) can be extended or called directly for custom processing.
printTreeuses indentation to show tree structure—helpful for diagnostics or demos.- Error/debug comments reflect development timeline and humor.
- All node management follows classic binary search tree rules.
File: HeapBinaryTreeNode.pas
Category: Data Structures / Heaps / Trees (Internal Node)
This unit defines the THeapBinaryTreeNode class, the node data structure used internally by HeapBinaryTree.pas for representing a binary tree-based heap.
It encapsulates basic node fields and getter/setter methods for use in binary tree and heap algorithms, keeping node logic clearly separated from the main heap operations.
Class Features:
- Fields for:
data: integer value contained in the nodenodeNumber: supporting sequential or logical enumeration of nodesleftChild,rightChild: pointers to left and right children
- Methods for:
- Setting/getting data, node number
- Assigning/returning left and right children
- Constructor initializes all pointers to
nil(empty node)
This tight encapsulation makes it easy to modify or extend the underlying data model (e.g., for balancing or additional attributes).
Requirements:
- Free Pascal Compiler (FPC), version 3.x or later
- Used automatically by
HeapBinaryTree.pas, not commonly run or compiled directly
Integration Steps:
- Place
HeapBinaryTreeNode.pasin your project directory. - Reference it in your heap or tree unit:
uses HeapBinaryTreeNode; - Use
THeapBinaryTreeNode.createto spawn new nodes as needed in your own structures. - Access and modify node properties with the provided getters and setters.
Typical Usage Example (with HeapBinaryTree):
var
node: THeapBinaryTreeNode;
begin
node := THeapBinaryTreeNode.create;
node.setData(10);
node.setNodeNumber(1);
// Link into tree, as managed by HeapBinaryTree
end.- You generally do not need to interact with nodes directly—work at the heap/tree level unless implementing or extending the structure.
- This design promotes reusability and clarity in larger object-oriented Pascal projects.
File: HeapDoubleLinkedList.pas
Category: Data Structures / Heaps / Linked Lists
Implements a heap-like data structure using a doubly linked list in Pascal for educational demonstration and practical applications that require ordering and bidirectional traversal.
Key Features:
-
Object Pascal (
{$mode objfpc}) using the companionHeapDoubleNode.pas -
Supports:
- Insertion at the head (
insertFirst) and tail (insertLast) - Deletion from the head (
deleteFirst) and tail (deleteLast) - Deletion of the first node containing a specific value
- Insertion after a node with a specific value (
insertAfter) - Node data lookup by position (
returnSpecificNodesData) - Counting total nodes (
countNodes) - Cleanup helper (
destroyNodes)
- Insertion at the head (
-
All logic is encapsulated in the class and separated from program I/O
Requirements:
- Free Pascal Compiler (FPC), version 3.x or later
HeapDoubleNode.pasplaced in the same directory
Usage in a Program:
-
Reference the unit and use the heap list as follows:
uses HeapDoubleLinkedList; var heap: THeapDoubleLinkedList; begin heap := THeapDoubleLinkedList.create; heap.insertFirst(10); heap.insertLast(20); heap.deleteFirst; heap.deleteLast; // Add further list operations as desired end.
-
Or, compile as part of a testing suite or with a main program.
To Compile:
fpc HeapDoubleLinkedList.pas
# ...plus a test or demo main program- All node linkage is via the
HeapDoubleNodeclass—never manage pointers directly at the application level. - Suits problems needing both ordered data and efficient insert/delete from both ends.
File: HeapNode.pas
Category: Data Structures / Heap / Linked Lists (Internal Node)
This unit provides the single-node implementation for a heap (or any singly linked list-style structure).
It is commonly used as the underlying node in linked-list based heaps (and similar structures), and is kept very simple for maximum clarity and extensibility.
Class Features:
- Fields:
data: integer value stored in this heap nodenext: pointer to the nextTHeapNodein the list/heap structure
- Methods:
setData(inputData: integer)andgetData: integerfor value assignment and retrievalsetNext(inputNode: THeapNode)andgetNext: THeapNodefor pointer manipulation
- Constructor starts nodes with
next := nil
This is perfect for basic heap/stack/queue/list exercises requiring your own node definitions.
Requirements:
- Free Pascal Compiler (FPC), version 3.x or later
- Used by heaps and lists which rely on their own node class
General Steps:
- Place
HeapNode.pasin your working directory. - Reference it in a unit or program:
uses HeapNode; - Create and connect nodes:
var node1, node2: THeapNode; begin node1 := THeapNode.create; node1.setData(5); node2 := THeapNode.create; node1.setNext(node2); // ... build up chain ... end.
- In normal usage, higher-level data structure classes handle managing these nodes.
- This low-dependency node design is very flexible for learning, modifying, or extending your own linked data structures.
File: MLP.pas
Category: Machine Learning / Neural Networks
A modern, full-featured MultiLayer Perceptron (MLP) neural network implementation in Object Pascal ({$mode objfpc}).
This self-contained program demonstrates the creation, configuration, training, and prediction of classic feedforward neural networks—making it valuable for educational use, algorithm benchmarking, or direct integration in simple ML pipelines.
Features:
- Multiple hidden layers (
FHiddenLayers), flexible layer sizes - Choice of activation functions: Sigmoid, Tanh, ReLU, Softmax
- Choice of optimizers: SGD, Adam, RMSProp
- Implements dropout, L2 regularization, Xavier/He initialization
- Learning rate decay and early stopping
- Batch and online training, data normalization for stability
- Compact test harness:
program MLPtest
Core types & objects:
TMultiLayerPerceptronclass with all major NN operations (forward, backward, optimizers, batch training)TNeuron,TLayer,TDataPointrecord types- Helper functions for transfer functions and data array management
Requirements:
- Free Pascal Compiler (FPC), version 3.x or above
Compile:
fpc MLP.pasRun:
./MLPtestNote: Program entry point is named MLPtest.
Editing and Experimentation:
- Adjust layer/activation/optimizer configs by editing the
TMultiLayerPerceptron.Createand field assignments in code. - Train/test data can be embedded, generated, or supplied as needed by extending the
mainblock.
- Designed for students and researchers needing a readable, modifiable Pascal neural net.
- Strong numerical stability and regularization included out of the box.
- For research, couple this file with the
FacadeMLP.pasfor advanced introspection, training/weight logging, and architecture debugging.
File: RNN.pas
Category: Machine Learning / Recurrent Neural Networks
A comprehensive, advanced implementation of modern Recurrent Neural Networks (RNNs) in Object Pascal, including full support for classic SimpleRNN, LSTM, and GRU cell types.
The code is structured for both research and education, featuring:
- Complete forward and backward pass logic for sequence learning (BPTT)
- Support for multiple cell types:
- Simple Vanila RNN
- Long Short-Term Memory (LSTM)
- Gated Recurrent Units (GRU)
- Customizable activation and loss function types
- Batch sequence and mini-batch training
- Gradient clipping for stabilizing deep training
- Modular, extensible classes for neuron cells, layers, and utility routines
- Includes layer and cell wrappers for easier experimentation and extension
- In-built random initialization, Xavier/He support, and utility normalization methods
The provided main program (program AdvancedRNN;) features demonstration of forward/backward/training logic and utility routines for initializing/testing the architecture.
Requirements:
- Free Pascal Compiler (FPC), version 3.x or newer
Compile:
fpc RNN.pasRun:
./AdvancedRNNNote: The program’s entry point is named AdvancedRNN.
Customization:
- All cell, layer, and training settings can be configured at construction time with the constructors for each class (
TAdvancedRNN, etc.). - Extend or change the main block to load/generate different sequence data.
- Perfect for education, prototyping, or algorithmic benchmarking of classic RNNs, LSTM, and GRU.
- To perform advanced introspection on RNN activations/gradients/gates, combine with
FacadeRNN.pas.
File: RedBlackTree.pas
Category: Data Structures / Trees / Self-Balancing BST
Implements a Red-Black Tree, a classic self-balancing binary search tree (BST) variant, in Pascal.
Red-black trees guarantee logarithmic time for insertion, deletion, and lookup by enforcing strict color and rotation rules after every modification.
Features:
- Fully dynamic insertions with automatic rebalancing ("fixup")
- Node and tree balancing through color assignments and tree rotations (left/right)
- Object Pascal (
{$mode objfpc}) style with in-memory pointer operations - Traversal routine (
inorderTraversal) demonstrates the result and coloring of nodes - Construction is straightforward—suitable for both learning and practical use
- Simple and extendable, making it a strong starting point for exploring other BST variants
Data Model:
-
Each node stores:
data: integer value in the nodecolor: either red or blackleft,right,parent: pointers allowing bi-directional traversal and ancestry checks
-
Helper functions:
grandparent,uncle,sibling: classic BST family accessors
Requirements:
- Free Pascal Compiler (FPC), version 3.x or newer
Integration / Usage Example:
- Place
RedBlackTree.pasin your working directory. - Reference in your main program:
uses RedBlackTree; var tree: TRedBlackTree; root: treeNode; begin tree.create; tree.insert(root, 10); tree.insert(root, 20); tree.insert(root, 15); tree.inorderTraversal(root); end.
To Compile:
fpc RedBlackTree.pas
# ...plus a main/test program- The supplied object makes the main tree manipulation interface very clean and Pascal-esque.
- All balancing, fixing, and coloring are handled automatically in
insertandinsertFixup. - For deletion, you'll need to extend the implementation (only insertion provided).
inorderTraversalprints each node in order and notes its color, offering an easy sanity check.
File: SkipList.pas
Category: Data Structures / Skip List / Probabilistic
Implements a Skip List—a fast, probabilistic, multi-level linked list that achieves O(log n) average time for search, insertion, and deletion.
Skip lists are an alternative to balanced trees, storing sorted data through multiple levels of forward pointers with randomized height per node.
Features:
- Object Pascal, pointer-based implementation for learning and performance
- Constants for max level and promotion probability (tunable)
- Node insertion, search, and deletion all provided
- Can print the skip list linearly (
printList) or show each level (printAllLevels) - Each node contains data, its level, and an array of forward pointers
- Keeps list balanced statistically—higher-level nodes are less frequent
How It Works:
- When inserting, a node may be promoted to higher levels (randomized coin-flip logic)
- Most operations run in logarithmic time, making skip lists suitable for high-performance in-memory indexes
Requirements:
- Free Pascal Compiler (FPC), version 3.x or newer
Example Usage:
uses SkipList;
var
sl: TSkipList;
begin
sl.create;
sl.insert(10); sl.insert(20); sl.insert(15);
sl.printList;
sl.printAllLevels;
sl.delete(20);
sl.printList;
end.To Compile:
fpc SkipList.pas
# ...plus your test or demonstration program- Perfect for illustrating probabilistic data structures and alternative ordered lists.
- The customizable level and probability allow experimentation with speed/space trade-offs.
- Useful for database internals and concurrency-friendly in-memory structures.
File: Stack.pas
Category: Data Structures / Stack
Implements a classic, fixed-size array-based stack in Object Pascal.
This example demonstrates all the conventional stack operations—push, pop, peek, and checks for full or empty results—using a dynamically allocated array and a TStack object wrapper.
Features:
- Array-based storage (
stackArray) for integers - Dynamic max size set at creation (
TStack.create(maxSizeInput)) - Standard stack operations:
push(inputNumber: integer)pop(): integerpeek(): integer(view top without popping)isEmpty(): booleanisFull(): boolean
- Simple, readable implementation for both educational use and real-world stack needs
Data Model:
- Stack size and top index are managed globally within the unit for all
TStackobjects (typical in teaching examples) - The interface can easily be extended for generic type support or encapsulation
Requirements:
- Free Pascal Compiler (FPC), version 3.x or later
Usage Examples:
-
Using as a unit in a program:
uses Stack; var stk: TStack; begin stk.create(10); stk.push(5); stk.push(9); writeln('Top is: ', stk.peek); stk.pop; end.
-
Or, compile together with a test main program:
fpc Stack.pas # ...plus your main/test code
- Array bounds and top index initialization (
top := 0) may differ from some conventions: adapt for 0- or 1-based stacks as desired. - For greater safety or flexibility in larger projects, wrap the stack state in records or use class-based design.
- For linked-list based stacks, see associated or companion units in the repo.
File: StackBinaryTree.pas
Category: Data Structures / Stack / Trees
Implements a stack structure using a binary tree as its storage model in Pascal.
This approach demonstrates both binary search tree construction and how stack-like access may be mapped onto tree structures, making it useful both for illustrating traversal and for exploring hybrid data structures.
Features:
-
Explicit pointer-based binary tree node definition (with
data,nodeNumber, and left/right child pointers) -
Stack operations provided through a custom tree-based logic
-
Core routines include:
insertData(inputData: integer)– insert a node following BST rulesdeleteNode(key: integer): boolean– remove a node by its valuecountNodes(): integer– total nodes using pre-order scanfindNodeNumber(key: integer): integer– returns the logical “stack position” of a value- Print and traverse tree visually with
printTree, plus support for in/pre/post-order traversals
-
Also includes an array-based object stack for trees (
TtreeStack)- Allows for mixed array/tree approaches in algorithms that require both
Data Model:
- Tree is managed by root/global pointers for simplicity (educational)
- Nodes allocated/deallocated with
newand direct pointer manipulation
Requirements:
- Free Pascal Compiler (FPC), version 3.x or later
Example Usage:
uses StackBinaryTree;
var
tree: TStackBinaryTree;
begin
tree.create;
tree.insertData(7); tree.insertData(2); tree.insertData(9);
tree.printTree;
end.Or compile with your own main/test code:
fpc StackBinaryTree.pas
# ...plus a test/demonstration program- Standard traversal methods are provided as customizable stubs: fill with your own processing for in/pre/post-order walks.
- Useful for teaching/research or for implementing exotic data structures that combine traversal and stack-like behavior.
- For pure stacks, see Stack.pas.
File: StackDoubleLinkedList.pas
Category: Data Structures / Stack / Linked Lists
Implements a stack using a doubly linked list as its underlying storage, in Object Pascal.
This file demonstrates all standard stack-like and list-like operations, with each node pointing both forwards and backwards, allowing flexible insertion and removal from either end.
Features:
- Explicit node structure (
doubleNode) containing value, previous and next pointers - Provides core doubly-linked list methods:
insertFirst(inputData: integer)– add value to the headinsertLast(inputData: integer)– add value to the taildeleteFirst()/deleteLast()– remove item from front or enddeleteNodeForFirstInstanceOfData(key: integer)– removes the first matching valueinsertAfter(key, inputData: integer)– insert after node with specific valuereturnSpecificNodesData(nodeNumber: integer): get data at nth node/positioncountNodes(): total elements in the list
Data Model:
- Global
headandtailpointers (classic Pascal teaching pattern) - All links managed via pointer assignment—demonstrates how stacks/lists work at a pointer level
Requirements:
- Free Pascal Compiler (FPC), version 3.x or above
Example Usage:
uses StackDoubleLinkedList;
var
stack: TStackDoubleLinkedList;
begin
stack.create;
stack.insertFirst(10);
stack.insertLast(20);
writeln('Count: ', stack.countNodes());
stack.deleteFirst;
end.Or compile with a separate test/demo main program:
fpc StackDoubleLinkedList.pas
# ...plus main/test code- This structure allows both stack (LIFO) and queue (FIFO) behaviors, plus flexible node operations.
- For pure stack use, focus on
insertFirst/deleteFirstfor LIFO behavior. - For simple, array-based stacks, see Stack.pas.
File: StackLinkedList.pas
Category: Data Structures / Stack / Linked List
Implements a simple stack using a classic singly linked list as the underlying structure, in Object Pascal ({$mode objfpc}).
Each node points to the next node, with the top of the stack corresponding to the head of the list—ideal for both stack and linear list teaching cases.
Features:
- Explicit pointer-based node structure (
data,next) - Provides core stack/list methods:
addNode(inputData: integer)– push value onto the end (tail) of the listdeleteFirstNode()/deleteLastNode()– remove node from head/taildeleteSpecificNode(nodeNumber: integer)– remove nth node in listcountNodes()– returns total nodes presentreturnSpecificNodesData(nodeNumber: integer)– retrieve the data at position nreturnHeadsData(),returnTailsData()– convenience methods for head/tail datareturnNodeNumberOfFirstInstanceOfData(inputData: integer)– find the index of first matching data
Data Model:
- Global
headpointer; all node allocation/deallocation with explicitnewand pointer assignment - All methods are implemented with direct pointer manipulation and looping, in pure Pascal
Requirements:
- Free Pascal Compiler (FPC), version 3.x or above
Example Usage:
uses StackLinkedList;
var
stack: TStackLinkedList;
begin
stack.create;
stack.addNode(1); stack.addNode(2); stack.addNode(3);
writeln('Count: ', stack.countNodes());
stack.deleteFirstNode;
end.Or compile with a test main program:
fpc StackLinkedList.pas
# ...main/demo/test code- Suits teaching nodes, pointers, singly linked lists, and basic stack (LIFO) logic.
- For a doubly linked list or array stack, see other relevant units (StackDoubleLinkedList.pas, Stack.pas).
File: Transformer.pas
Category: Machine Learning / Deep Learning / Transformers
A compact and modern Pascal implementation of a Transformer-based neural network model, including self-attention and GGUF (GPT-style) model file loading/parsing.
Designed as both a reference implementation and a working CLI demo for anyone seeking to understand or work with transformer networks in Pascal.
Features:
- Loads GGUF-format model weights for GPT and similar transformer architectures
- Implements fast, efficient tokenization with JSON support
- Full forward propagation for multi-layer transformers :
- Token embedding, multi-head self-attention, feed-forward, layer norm, GELU, and softmax
- End-to-end text generation ("prompting") via attention and autoregressive decoding
- Custom
TTokenizer,TGGUFLoader, andTTransformerModelclasses for clear separation of parsing, weights, and computation - All code written in idiomatic object-oriented Pascal (
{$mode objfpc}with advanced records)
How to Run
Requirements:
- Free Pascal Compiler (FPC), version 3.x or newer
- GGUF model and compatible tokenizer JSON
Compile:
fpc Transformer.pasRun:
./TransformerUsage:
- To prompt/generate text, load a GGUF model and compatible tokenizer file, then call
Generate(prompt, maxTokens)from your Pascal code or from the command line (if adequately wired up).
Package Structure:
TTokenizer: Loads and encodes/decodes text using JSON vocabulary fileTGGUFLoader: Loads transformer layers, weights, and embeds from GGUF formatTTransformerModel: Runs inference, generation, and handles all forward propagation
- Not a full-featured LLM shell, but a robust Pascal starting point for experimenting with transformer networks, or for extending to educational or research use.
- You may adapt this for BERT, GPT, or other attention-based models by adjusting the forward pass or loader logic.
- Absolutely minimal external dependencies: only JSON/FPJSON components already in standard FPC.
File: Trie.pas
Category: Data Structures / Tries / String Algorithms
Implements a Trie (prefix tree), a tree-based data structure well-suited for fast string storage, retrieval, and prefix-based search. Tries are used in autocomplete, spelling correction, dictionaries, IP routing, and countless fast search applications.
This Pascal implementation efficiently supports insertion, lookup, prefix checking, and deletion.
Features:
- Node structure: each node (record) holds a character, a parent pointer, end-of-word flag, and fixed alphabet-size array of children (default 26, 'a'-'z')
- Operations include:
insertfor building the triesearchfor exact word existencestartsWithfor prefix queriesdeletewith cleanup of unused nodes- Printing all words (
printAllWords) found in the tree
- All pointer-based, classic Pascal style for easy learning and adaptation
Data Model:
- Only lower-case alphabetic words supported by default. Tune
ALPHABET_SIZEand conversion logic for other alphabets. - All pointer management is explicit, and nodes are properly deallocated when unused.
Requirements:
- Free Pascal Compiler (FPC), version 3.x or newer
Example Usage:
uses Trie;
var
trie: TTrie;
root: trieNode;
begin
trie.create;
trie.insert(root, 'cat');
trie.insert(root, 'dog'); trie.insert(root, 'cart');
writeln('cat? ', trie.search(root, 'cat'));
writeln('ca*? ', trie.startsWith(root, 'ca'));
trie.delete(root, 'cat');
trie.printAllWords(root, '');
end.To Compile:
fpc Trie.pas
# ...plus your main or demonstration program- This implementation is best used for lower-case English strings by default.
- You can adapt the alphabet size and char-index mapping for case-insensitive or unicode operations.
- For pure educational value, follow the explicit pointer, node, and recursion logic step-by-step.
File: UnionFind.pas
Category: Data Structures / Disjoint Sets / Union-Find
Implements the classic Union-Find (disjoint set forest) data structure with path compression and union by rank—vital for efficient partitioning, connected components, and Kruskal’s/graph algorithms.
This lets you dynamically group and merge sets of elements, answering queries like: “Are elements A and B in the same set?” in nearly-constant amortized time.
Features:
- Fast find and union operations with path compression and union by rank
- Each setNode points to its parent and records its rank; root nodes are set representatives
- Simple API: add (“makeSet”), union, connected, and find-by-data
- Maintains all elements in an internal array for quick node access and iteration
- Prints set representatives, element membership, and total number of sets
Data Model:
- Supports up to 1000 elements by default (constant
MAX_ELEMENTS) - Each item is a pointer, track of its value, rank, and parent
- All pointer and array operations handled explicitly (classic Pascal style)
Requirements:
- Free Pascal Compiler (FPC), version 3.x or later
Example Usage:
uses UnionFind;
var
uf: TUnionFind;
nodeA, nodeB: setNode;
begin
uf.create;
nodeA := uf.makeSet(1);
nodeB := uf.makeSet(2);
uf.union(nodeA, nodeB);
writeln('Are 1 and 2 connected? ', uf.connected(nodeA, nodeB));
uf.printSets;
end.To Compile:
fpc UnionFind.pas
# ...plus your main or demonstration program- For Kruskal’s and graph connected component problems; forms the backbone of many graph/cluster/puzzle algorithms.
- Extend
MAX_ELEMENTSas required for large tasks. - Follows classical pointer- and array-based style for educational and practical value.
Attribution:
Created by Matthew James Abbott, 2025