Skip to content

This project demonstrates how to build and traverse a binary tree in C++ by decoding Morse code sequences into letters using a simple command-line tool.

Notifications You must be signed in to change notification settings

DukeGDDI/MorseCodeCPP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Morse Code Decoder (C++ Binary Tree Example)

Binary Tree representation of Morse Code

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++

πŸ“ Project Structure

.
β”œβ”€β”€ Makefile
β”œβ”€β”€ README.md
β”œβ”€β”€ src
β”‚   β”œβ”€β”€ main.cpp
β”‚   β”œβ”€β”€ decoder.cpp
β”‚   └── decoder.hpp
└── build/        (generated automatically)
  • Node β€” represents a node in the Morse binary tree
  • Decoder β€” builds the Morse tree and provides a decode() method
  • main.cpp β€” command-line tool that decodes Morse code arguments

πŸ”§ Building

Run:

make

This compiles the source files into build/ and produces the executable:

./morse

To clean the project:

make clean

▢️ Usage

The 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.

Example

./morse ".-" "-..." "-.-."

Output:

ABC

If no Morse codes are provided, it prints a usage message and exits with a non-zero status:

./morse
Usage: ./morse <morse_code1> <morse_code2> ...

🧠 How the Decoder Works

  • Each Node contains:

    • char value β€” the decoded character (or '\0' if empty)
    • Node* dot β€” pointer to next node on .
    • Node* dash β€” pointer to next node on -
  • The Decoder constructor 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 '?'.


πŸ“˜ Educational Use

This project is excellent for demonstrating:

  • pointer-based tree construction
  • memory management and destructors
  • class design (Node and Decoder)
  • Makefile basics
  • simple CLI tools and argument parsing

About

This project demonstrates how to build and traverse a binary tree in C++ by decoding Morse code sequences into letters using a simple command-line tool.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors