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
