Can someone assist me please? (Counting the number of nodes with the character 'A') | Sololearn: Learn to code for FREE!
+ 2

Can someone assist me please? (Counting the number of nodes with the character 'A')

So I have a tester1 class with a method countA ( countA takes as input a BinaryTree and returns as result an integer; the result should be a number of nodes in the input tree which contain the character 'A'.) I keep getting an error: 'cannot find symbol'. Can someone please help me with this? I would really appreciate it. https://code.sololearn.com/cAbsb4wsnH81/?ref=app

29th Oct 2022, 6:07 PM
Triz
Triz - avatar
4 Antworten
+ 3
You kind of stumbled over your own feet when you chose the tree to be a node and the node having the subtrees. By this, your getRoot() is designed not to return the root but its content. The method getData() does not work on the left and right child elements because they return trees not nodes. You ought to reevaluate your chosen tree design.
29th Oct 2022, 7:11 PM
Ani Jona ­čĽŐ
Ani Jona ­čĽŐ - avatar
+ 2
A start would be to have getRoot() return the root as Node and fix all errors you see by inserting a getRoot() or removing getData() depending on what it is you compare. But actually your BinaryTree ought to be generic in the Node type and branch off in itself, and not in the Node. But since it is clear that the node is a char type, you could put the char data and branches into the tree.
30th Oct 2022, 3:45 AM
Ani Jona ­čĽŐ
Ani Jona ­čĽŐ - avatar
+ 1
Here is an example. Sorry for the delay, I was out of town... The principle I follow is called Open-Closed-Principle. Having the BinTree as just the a data container ensures that the won't be any further modification. Any kind of extension, such as operations on the BinTree comes in form of classes that operate on the binary tree instead of being methods the BinTree provides (which would inevitably lead to later modifications of the class, which would be undesireable). In this case it is an accumulation function in an in-order tree walk... https://code.sololearn.com/ct395b1hVdlW
31st Oct 2022, 5:21 PM
Ani Jona ­čĽŐ
Ani Jona ­čĽŐ - avatar
0
Ani Jona ­čĽŐ how would I go about fixing it because IÔÇÖm kind of confuse?
29th Oct 2022, 7:53 PM
Triz
Triz - avatar