In c++ I am working on two trees, 1 is alphabetical a-z with nums and characters 0-9 , . ?
The other tree is the equivalent of those characters in Morse code. I have to have the different trees in text files that should already be in the correct order for insert. In my normal alphabet, I worked out my balanced text file for preorder traversal looks like
P
H
D
B
A
C
F
E
G
L
J
I
K
N
M
O
2
X
T
R
Q
S
V
U
W
0
Y
Z
1
9
5
4
3
7
6
8
,
.
?
This text file prints out preorder traversal
,
.
0
1
2
3
4
5
6
7
8
9
?
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
The problem I am having is with the Morse code textfile. I understand that the characters for Morse code are not the same for the normal alphabet. From lest to greatest, this is Morse code
- T
-- M
--- O
----- 0
----. 9
---.. 8
--. G
--.- Q
--.. Z
--..-- ,
--... 7
-. N
-.- K
-.-- Y
-.-. C
-.. D
-..- X
-... B
-.... 6
. E
.- A
.-- W
.--- J
.---- 1
.--. P
.-. R
.-.. L
.. I
..- U
..--- 2
..--.. ?
..-. F
... S
...- V
...-- 3
.... H
....- 4
..... 5
applying the same formula for the tree (so it has the same sequence as the alphabetical one above, my text file looks like
-.. D
--.- Q
----- 0
-- M
- T
--- O
---.. 8
----. 9
--. G
-. N
--..-- ,
--.. Z
--... 7
-.-- Y
-.- K
-.-. C
.. I
.---- 1
. E
-... B
-..- X
-.... 6
.-- W
.- A
.--- J
.-.-.- .
.--. P
.-. R
.-.. L
...-- 3
..--.. ?
..--- 2
..- U
... S
..-. F
...- V
....- 4
.... H
..... 5
But this also does not print out the tree in alphabetical order for Morse code for preorder.
This is how I am inserting into the tree
void BST::insert(BSTNode *&pTree, string morse)
{
if (pTree == NULL)
{
BSTNode *pMem = new BSTNode(morse);
pTree = pMem;
}
else if (morse < (pTree)->getMorse())
{
insert((pTree)->getLeft(), morse);
}
else if (morse > (pTree)->getMorse())
{
insert((pTree)->getRight(), morse);
}
}
And this is how I am printing out the results
void BST::print(BSTNode *&pTree, string id)
{
if (pTree != nullptr)
{
//cout << pTree->getMorse() << endl; // processed
print(pTree->getLeft(), id);
cout << pTree->getMorse() << endl; // processed
print(pTree->getRight(), id);
}
}
(the same code is for the alphabet except it uses chars and getLetter() but other than that it is identical)
If I am just approaching this incorrectly, I could use any help in the right direction.
Did you notice that your
insert()
method does not handle the case of "key-collision" (due to the missingelse
branch for the lastif
). This can be used to detect if a key shall be inserted which is already in tree. As it is, such duplicated inserts are simply ignored (which is IMHO not the worst possible behavior).In my sample below, I decided for a different option: let
insert()
return a boolean value which reports about success of insertion.Unfortunately, you didn't provide an MCVE.
Thus, I filled the gaps with own code where (for my personal joy) I involved templates. Hopefully, this won't bring in too much confusion.
However, after playing around with your sample data, I came to the conclusion that your code does probably the right – may be not, what you expected. Eventually, you have a false expectation. Or, you correctly solved the false problem. I re-read your question multiple times but didn't get I clear idea about this...
I used your last sample file as input (to build up the binary search tree) and let my application output preorder and inorder:
Preorder output corresponds to your last file.
Inorder output corresponds to your 3rd file.
The inorder output provides a sorted output according to the defined order of the tree – in this case the lexicographical order of Morse sequences (where
-
precedes.
due to their respective ASCII values).Hmmm. If you expected an alphabetical order, do you mean an order which considers the alpha-numerical characters (to which the Morse codes map)?
If so, this can hardly done by such a tree (as I cannot see how a possible order of Morse codes corresponds to a possible order of alpha-numerics). I.e. to achieve "Morse codes sorted by the associated alpha-numerics", the obvious (and IMHO only) way is to build a tree for the reverse mapping. You can easily built up another tree (e.g. from the first) where the assigned alpha-numeric values are used as key instead. (Actually, you already did first a binary search tree for alpha-numerics.)
This leaves me somehow puzzled. May be, I missed something and didn't get your actual problem...
However, below is the result of my fiddling:
Compile and run:
Note:
I just recognized that the "reversed" tree is not well-balanced anymore. Thus, the optimal worst-case time-complexity of O(ld(n)) cannot be achieved.