hi

Dependencies:   microbit

binTree.cpp

Committer:
schizzlewizzle
Date:
2019-03-04
Revision:
1:52a1fd1e7193

File content as of revision 1:52a1fd1e7193:

#include "binTree.h"
#include <string.h>

using std::string;
//constructor that makes root a new node for insertion.
binTree::binTree() {
    root = new node;
//root.dot = NULL;
//root.dash = NULL;
    


}


//Function to fill the tree, public facing as developers don't need to know root.
void binTree::insert(char inputC, string morse) {

    node *r = this->root;
    
    
    //loop going through length of morse and seeing if dash or dot for where to add new nodes.
    for (int i = 0; i < morse.length(); i++) {
    // assert(r);
        if (morse[i] == '-') {
            if (r->dash == NULL) {
                r->dash = new node;
            }
        r = r->dash;
        }
        else if (morse[i] == '.') {
            if (r->dot == NULL) {
                r->dot = new node;
            }
        r = r->dot;
        }
    
    
        }
    insert(inputC, morse, r);
    
    //else {
    
    //}
}


//sets char and string in the specific node designated at r.
void binTree::insert(char inputC, string morse, node * r) {

    r->inputC = inputC;
    r->morse = morse;
}

//search function that traverses down the tree till it finds the coresponding morse, then returns the alphabetical value stored in that node.

char binTree::tFind(node * r, string morse)
{
    if (r != NULL){
        for (int i = 0; i < morse.length(); i++) {
//assert(r);
            if (morse[i] == '-') {
    //these if statements check to see whether there's actually any data at that node, and if not, then it exits the function.
                if (r->dash == NULL){
                    exit(0);
                }
                else {
                    r = r->dash;
                }

            }
            else if (morse[i] == '.') {
                if (r->dot == NULL){
            }
                else {
                    r = r->dot;
                    }

            }
        }
    }
    else{
    
    }
    return r->inputC;
}