Liam McNamara / MicroBitReceiver

Dependencies:   microbit

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers binTree.cpp Source File

binTree.cpp

00001 #include "binTree.h"
00002 #include <string.h>
00003 
00004 using std::string;
00005 //constructor that makes root a new node for insertion.
00006 binTree::binTree() {
00007     root = new node;
00008 //root.dot = NULL;
00009 //root.dash = NULL;
00010     
00011 
00012 
00013 }
00014 
00015 
00016 //Function to fill the tree, public facing as developers don't need to know root.
00017 void binTree::insert(char inputC, string morse) {
00018 
00019     node *r = this->root;
00020     
00021     
00022     //loop going through length of morse and seeing if dash or dot for where to add new nodes.
00023     for (int i = 0; i < morse.length(); i++) {
00024     // assert(r);
00025         if (morse[i] == '-') {
00026             if (r->dash == NULL) {
00027                 r->dash = new node;
00028             }
00029         r = r->dash;
00030         }
00031         else if (morse[i] == '.') {
00032             if (r->dot == NULL) {
00033                 r->dot = new node;
00034             }
00035         r = r->dot;
00036         }
00037     
00038     
00039         }
00040     insert(inputC, morse, r);
00041     
00042     //else {
00043     
00044     //}
00045 }
00046 
00047 
00048 //sets char and string in the specific node designated at r.
00049 void binTree::insert(char inputC, string morse, node * r) {
00050 
00051     r->inputC = inputC;
00052     r->morse = morse;
00053 }
00054 
00055 //search function that traverses down the tree till it finds the coresponding morse, then returns the alphabetical value stored in that node.
00056 
00057 char binTree::tFind(node * r, string morse)
00058 {
00059     if (r != NULL){
00060         for (int i = 0; i < morse.length(); i++) {
00061 //assert(r);
00062             if (morse[i] == '-') {
00063     //these if statements check to see whether there's actually any data at that node, and if not, then it exits the function.
00064                 if (r->dash == NULL){
00065                     exit(0);
00066                 }
00067                 else {
00068                     r = r->dash;
00069                 }
00070 
00071             }
00072             else if (morse[i] == '.') {
00073                 if (r->dot == NULL){
00074             }
00075                 else {
00076                     r = r->dot;
00077                     }
00078 
00079             }
00080         }
00081     }
00082     else{
00083     
00084     }
00085     return r->inputC;
00086 }