This week's lab assignment proved to be quite tricky (my partner and I worked our way through various problems, but never got it working quite perfectly). While Binary Trees and Linked Lists are similar in function (a root element with up to two children, extending into as many levels as is necessary), it was trickier than anticipated to convert a Binary Tree into a Linked List in the "inorder" traversal.
Our problem was that the recursive algorithm that we wrote worked for a "Standard" sub-tree (one parent, two children), but didn't know what to do with asymmetrical sub-trees and just sort of skipped elements from them. I'll go back and work through that one to make sure I know how to figure it out.
Our "helper" functions would be called when the "children" were reached (one left helper and one right), and the main return statement would place the sub-tree in the proper Linked List format. This is why it skipped the elements that weren't symmetrical; if you pass one run of the algorithm where the elements don't match, it's going to try and figure out a way to keep going based on the "if/else" statements you've created.
It's always helpful to know why a program doesn't work the way you want it to. By identifying the problems, one can create new methods (no pun intended) to either replace or assist the elements that don't work.
No comments:
Post a Comment