Review
Well written API, but I am not happy with this one:
traverse({ preorder, postorder, levelorder })
preorder and postorder can be combined, but they are always mutually exclusive with levelorder.
traverse({ preorder, postorder, levelorder }) { if (levelorder) { // levelorder .. } else { // preorder conditional and/or postorder conditional .. } return this; }
Instead, use 2 separate methods, each doing their own well known type of traversal.
traverseDepthFirst({ preorder, postorder })
traverseBreadthFirst({ levelorder })
Some general thoughts:
- should you allow cyclic graphs?
- should you want to traverse up the ancestors?