hi

Dependencies:   microbit

Committer:
schizzlewizzle
Date:
Mon Mar 04 22:54:07 2019 +0000
Revision:
1:52a1fd1e7193
hi

Who changed what in which revision?

UserRevisionLine numberNew contents of line
schizzlewizzle 1:52a1fd1e7193 1 #include "binTree.h"
schizzlewizzle 1:52a1fd1e7193 2 #include <string.h>
schizzlewizzle 1:52a1fd1e7193 3
schizzlewizzle 1:52a1fd1e7193 4 using std::string;
schizzlewizzle 1:52a1fd1e7193 5 //constructor that makes root a new node for insertion.
schizzlewizzle 1:52a1fd1e7193 6 binTree::binTree() {
schizzlewizzle 1:52a1fd1e7193 7 root = new node;
schizzlewizzle 1:52a1fd1e7193 8 //root.dot = NULL;
schizzlewizzle 1:52a1fd1e7193 9 //root.dash = NULL;
schizzlewizzle 1:52a1fd1e7193 10
schizzlewizzle 1:52a1fd1e7193 11
schizzlewizzle 1:52a1fd1e7193 12
schizzlewizzle 1:52a1fd1e7193 13 }
schizzlewizzle 1:52a1fd1e7193 14
schizzlewizzle 1:52a1fd1e7193 15
schizzlewizzle 1:52a1fd1e7193 16 //Function to fill the tree, public facing as developers don't need to know root.
schizzlewizzle 1:52a1fd1e7193 17 void binTree::insert(char inputC, string morse) {
schizzlewizzle 1:52a1fd1e7193 18
schizzlewizzle 1:52a1fd1e7193 19 node *r = this->root;
schizzlewizzle 1:52a1fd1e7193 20
schizzlewizzle 1:52a1fd1e7193 21
schizzlewizzle 1:52a1fd1e7193 22 //loop going through length of morse and seeing if dash or dot for where to add new nodes.
schizzlewizzle 1:52a1fd1e7193 23 for (int i = 0; i < morse.length(); i++) {
schizzlewizzle 1:52a1fd1e7193 24 // assert(r);
schizzlewizzle 1:52a1fd1e7193 25 if (morse[i] == '-') {
schizzlewizzle 1:52a1fd1e7193 26 if (r->dash == NULL) {
schizzlewizzle 1:52a1fd1e7193 27 r->dash = new node;
schizzlewizzle 1:52a1fd1e7193 28 }
schizzlewizzle 1:52a1fd1e7193 29 r = r->dash;
schizzlewizzle 1:52a1fd1e7193 30 }
schizzlewizzle 1:52a1fd1e7193 31 else if (morse[i] == '.') {
schizzlewizzle 1:52a1fd1e7193 32 if (r->dot == NULL) {
schizzlewizzle 1:52a1fd1e7193 33 r->dot = new node;
schizzlewizzle 1:52a1fd1e7193 34 }
schizzlewizzle 1:52a1fd1e7193 35 r = r->dot;
schizzlewizzle 1:52a1fd1e7193 36 }
schizzlewizzle 1:52a1fd1e7193 37
schizzlewizzle 1:52a1fd1e7193 38
schizzlewizzle 1:52a1fd1e7193 39 }
schizzlewizzle 1:52a1fd1e7193 40 insert(inputC, morse, r);
schizzlewizzle 1:52a1fd1e7193 41
schizzlewizzle 1:52a1fd1e7193 42 //else {
schizzlewizzle 1:52a1fd1e7193 43
schizzlewizzle 1:52a1fd1e7193 44 //}
schizzlewizzle 1:52a1fd1e7193 45 }
schizzlewizzle 1:52a1fd1e7193 46
schizzlewizzle 1:52a1fd1e7193 47
schizzlewizzle 1:52a1fd1e7193 48 //sets char and string in the specific node designated at r.
schizzlewizzle 1:52a1fd1e7193 49 void binTree::insert(char inputC, string morse, node * r) {
schizzlewizzle 1:52a1fd1e7193 50
schizzlewizzle 1:52a1fd1e7193 51 r->inputC = inputC;
schizzlewizzle 1:52a1fd1e7193 52 r->morse = morse;
schizzlewizzle 1:52a1fd1e7193 53 }
schizzlewizzle 1:52a1fd1e7193 54
schizzlewizzle 1:52a1fd1e7193 55 //search function that traverses down the tree till it finds the coresponding morse, then returns the alphabetical value stored in that node.
schizzlewizzle 1:52a1fd1e7193 56
schizzlewizzle 1:52a1fd1e7193 57 char binTree::tFind(node * r, string morse)
schizzlewizzle 1:52a1fd1e7193 58 {
schizzlewizzle 1:52a1fd1e7193 59 if (r != NULL){
schizzlewizzle 1:52a1fd1e7193 60 for (int i = 0; i < morse.length(); i++) {
schizzlewizzle 1:52a1fd1e7193 61 //assert(r);
schizzlewizzle 1:52a1fd1e7193 62 if (morse[i] == '-') {
schizzlewizzle 1:52a1fd1e7193 63 //these if statements check to see whether there's actually any data at that node, and if not, then it exits the function.
schizzlewizzle 1:52a1fd1e7193 64 if (r->dash == NULL){
schizzlewizzle 1:52a1fd1e7193 65 exit(0);
schizzlewizzle 1:52a1fd1e7193 66 }
schizzlewizzle 1:52a1fd1e7193 67 else {
schizzlewizzle 1:52a1fd1e7193 68 r = r->dash;
schizzlewizzle 1:52a1fd1e7193 69 }
schizzlewizzle 1:52a1fd1e7193 70
schizzlewizzle 1:52a1fd1e7193 71 }
schizzlewizzle 1:52a1fd1e7193 72 else if (morse[i] == '.') {
schizzlewizzle 1:52a1fd1e7193 73 if (r->dot == NULL){
schizzlewizzle 1:52a1fd1e7193 74 }
schizzlewizzle 1:52a1fd1e7193 75 else {
schizzlewizzle 1:52a1fd1e7193 76 r = r->dot;
schizzlewizzle 1:52a1fd1e7193 77 }
schizzlewizzle 1:52a1fd1e7193 78
schizzlewizzle 1:52a1fd1e7193 79 }
schizzlewizzle 1:52a1fd1e7193 80 }
schizzlewizzle 1:52a1fd1e7193 81 }
schizzlewizzle 1:52a1fd1e7193 82 else{
schizzlewizzle 1:52a1fd1e7193 83
schizzlewizzle 1:52a1fd1e7193 84 }
schizzlewizzle 1:52a1fd1e7193 85 return r->inputC;
schizzlewizzle 1:52a1fd1e7193 86 }