This project demonstrates how binary trees work in C++ by using them to decode Morse code. Each letter in the Morse alphabet is stored in a binary tree where:
- a dot (Β·) means go left
- a dash (β) means go right
The root node starts empty, and each letter is inserted by following its Morse pattern through the tree. Decoding works by traversing from the root according to a dot/dash string.
This project is intended as a simple educational example for learning:
- dynamic memory and pointer-based tree structures
- binary tree traversal
- mapping codes to characters
- building and using a basic class in C++
.
βββ Makefile
βββ README.md
βββ src
β βββ main.cpp
β βββ decoder.cpp
β βββ decoder.hpp
βββ build/ (generated automatically)
Nodeβ represents a node in the Morse binary treeDecoderβ builds the Morse tree and provides adecode()methodmain.cppβ command-line tool that decodes Morse code arguments
Run:
makeThis compiles the source files into build/ and produces the executable:
./morseTo clean the project:
make cleanThe program expects each command-line argument (after the program name)
to be a Morse code sequence made of . and -. It decodes each argument
to a character and prints the resulting string.
./morse ".-" "-..." "-.-."Output:
ABC
If no Morse codes are provided, it prints a usage message and exits with a non-zero status:
./morseUsage: ./morse <morse_code1> <morse_code2> ...
-
Each
Nodecontains:char valueβ the decoded character (or'\0'if empty)Node* dotβ pointer to next node on.Node* dashβ pointer to next node on-
-
The
Decoderconstructor builds the entire alphabet by inserting:A β .- B β -... C β -.-. ... -
The
decode()function traverses the tree following the Morse pattern for each argument passed to./morse.
Unknown or invalid patterns return '?'.
This project is excellent for demonstrating:
- pointer-based tree construction
- memory management and destructors
- class design (
NodeandDecoder) - Makefile basics
- simple CLI tools and argument parsing
