Binary Search Trees II - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Binary Search Trees II

Description:

Binary Search Trees II Morse Code * * An Application: Morse Code Table Let's create a BST to translate ASCII characters into Morse Code symbols. Use genBST1.h template. – PowerPoint PPT presentation

Number of Views:229
Avg rating:3.0/5.0
Slides: 18
Provided by: Roll73
Category:
Tags: binary | search | tree | trees

less

Transcript and Presenter's Notes

Title: Binary Search Trees II


1
Binary Search Trees II
  • Morse Code

2
An Application Morse Code Table
  • Let's create a BST to translate ASCII characters
    into Morse Code symbols.
  • Use genBST1.h template.
  • Will create a new class to be the template
    parameter.
  • New empty C project
  • ASCII_to_Morse_Code

3
Initial main.cpp
  • include ltiostreamgt
  • using namespace std
  • int main()
  • cout ltlt "Enter an ASCII string to be
    translated into Morse Code\n"
  • char buffer1000
  • cin.getline(buffer, 1000)
  • cout ltlt "You entered\n"
  • cout ltlt buffer
  • cin.get()
  • cin.get()
  • return 0

4
Initial Program Running
5
Class Morse_Code_Pair
  • Add class Morse_Code_Pair
  • Each object will specify the Morse Code symbol
    for one ASCII character.
  • We will build a BST of these objects.
  • Use the BST to translate ASCII into Morse Code.
  • Copy source files from
  • http//www.cse.usf.edu/turnerr/Data_Structures/Do
    wnloads/2011_03_07_Morse_Code_BST/

6
Morse_Code_Pair.h
  • pragma once
  • include ltiostreamgt
  • include ltstringgt
  • class Morse_Code_Pair
  • public
  • stdstring symbol // A Morse code symbol.
  • char value // The letter or digit
    represented
  • // by this symbol
  • public
  • // Default constructor
  • Morse_Code_Pair()
  • // Normal constructor
  • Morse_Code_Pair(stdstring symbol_, char
    value_)
  • // Accessor functions
  • stdstring get_symbol() const return
    symbol

7
Morse_Code_Pair.h
  • // Less than operator
  • bool operatorlt(const Morse_Code_Pair lhs,
  • const Morse_Code_Pair rhs)
  • // Equals operator
  • bool operator(const Morse_Code_Pair lhs,
  • const Morse_Code_Pair rhs)
  • // Output operator
  • stdostream operatorltlt(stdostream out,
  • const Morse_Code_Pair
    s)

8
Morse_Code_Pair.cpp
  • include "Morse_Code_Pair.h"
  • using namespace std
  • // Default constructor
  • Morse_Code_PairMorse_Code_Pair() symbol("", '
    ')
  • // Normal constructor
  • Morse_Code_PairMorse_Code_Pair(string symbol_,
    char value_)
  • symbol(symbol_), value(value_)
  • // Less then operator
  • bool operatorlt (const Morse_Code_Pair lhs,
  • const Morse_Code_Pair rhs)
  • char c_lhs lhs.get_value()
  • char c_rhs rhs.get_value()
  • return c_lhs lt c_rhs

9
Morse_Code_Pair.cpp
  • // Equals operator
  • bool operator(const Morse_Code_Pair lhs,
  • const Morse_Code_Pair rhs)
  • char c_lhs lhs.get_value()
  • char c_rhs rhs.get_value()
  • return c_lhs c_rhs

10
Morse_Code_Pair.cpp
  • ostream operatorltlt(ostream out,
  • const Morse_Code_Pair s)
  • string symbol_string s.get_symbol()
  • for (size_t i 0 i lt symbol_string.length()
    i)
  • if (symbol_stringi '_')
  • symbol_stringi '-'
  • out ltlt s.get_value() ltlt " " ltlt symbol_string
  • return out

11
main.cpp
  • include ltiostreamgt
  • include "genBST1.h"
  • include "Morse_Code_Pair.h"
  • using namespace std
  • // Conservative upper bound on length of a single
    Morse code symbol
  • const int MAX_SYMBOL_LEN 10
  • // Binary Search Tree to map Morse code symbols
    to characters
  • BSTltMorse_Code_Pairgt morse_map
  • void add_symbol_to_map(string symbol, char value)
  • Morse_Code_Pair sym(symbol, value)
  • morse_map.insert(sym)

12
build_map
  • In main.cpp
  • void build_map()
  • add_symbol_to_map("....", 'H')
  • add_symbol_to_map("___..", '8')
  • add_symbol_to_map("._.", 'R')
  • add_symbol_to_map("...__", '3')
  • add_symbol_to_map("_._.", 'C')
  • add_symbol_to_map("__", 'M')
  • add_symbol_to_map("..._", 'V')
  • add_symbol_to_map(".____", '1')
  • add_symbol_to_map(".....", '5')
  • add_symbol_to_map("._", 'A')
  • add_symbol_to_map(".", 'E')
  • add_symbol_to_map(".___", 'J')
  • add_symbol_to_map("___", 'O')
  • add_symbol_to_map("_", 'T')
  • add_symbol_to_map("_.._", 'X')

13
build_map
  • add_symbol_to_map("_____", '0')
  • add_symbol_to_map("..___", '2')
  • add_symbol_to_map("...._", '4')
  • add_symbol_to_map("_....", '6')
  • add_symbol_to_map("____.", '9')
  • add_symbol_to_map("_...", 'B')
  • add_symbol_to_map("_..", 'D')
  • add_symbol_to_map(".._.", 'F')
  • add_symbol_to_map("..", 'I')
  • add_symbol_to_map("_._", 'K')
  • add_symbol_to_map(".__.", 'P')
  • add_symbol_to_map("_.__", 'Y')
  • add_symbol_to_map("__...", '7')
  • add_symbol_to_map("__.", 'G')
  • add_symbol_to_map("._..", 'L')
  • add_symbol_to_map("_.", 'N')
  • add_symbol_to_map("__._", 'Q')
  • add_symbol_to_map("...", 'S')
  • add_symbol_to_map(".._", 'U')

14
main.cpp
  • int main()
  • build_map()
  • morse_map.display(cout)

15
The Morse_Pair BST
16
main.cpp
  • Add at end of main.cpp
  • cout ltlt "\n\nHere is the string in Morse
    Code\n"
  • for (int i 0 i lt (int) strlen(buffer) i)
  • char c toupper(bufferi)
  • Morse_Code_Pair search_target new
    Morse_Code_Pair("", c)
  • Morse_Code_Pair mcp morse_map.search(searc
    h_target)
  • if (mcp 0)
  • cout ltlt " "
  • else
  • string next_symbol mcp-gtget_symbol()
  • cout ltlt next_symbol ltlt " "

17
Program in Action
End of Presentation
Write a Comment
User Comments (0)
About PowerShow.com