Imagine you have a binary tree and wants to get all the nodes that will be visible when seen from the top of the tree. How do you print all such nodes? Final output for this tree should be 7, 13, 23, 44, 51, 65. A similar problem about printing right view is given in the previous post about Right View of Binary Tree without Recursion
Imagine you have a binary tree and wants to get all the nodes that will be visible when seen from the right side of the tree. How do you print all such nodes? Final output for this tree should be 44, 51, 65, 26. In other words, the first nodes we touch upon if we draw horizontal lines from right side of the tree. Read on to find the solution.
There have been many times, we get doubts about how a certain algorithms work. There is no better way than being able to visualise that algorithm. For example check this page where you can see how each sort works on same array input.
These are created by David Galles, an Associate Professor of department of Computer Science University of San Francisco.
You will be able to see lot more visualizations by going to this page.
Before we begin, let me show you the final output we are going to get. Click Run Pen button if you want to see the collapsible panel in action
The panel can be minimized or maximized by clicking on the arrow button. It can also be removed completely by clicking close button. How do we achieve this? I have used KnockoutJS, OracleJET and jQuery to achieve the result. RequireJS is also used but only to get the required libraries from CDN. However, OracleJet is predomantly is used for styling alone. Rest of the bindings can be achieved by regular KnockoutJS and jQuery. Read forward to learn how to get the above result.
Imagine you have a binary tree where as shown above. You may be aware of InOrder traversal where you follow a scheme of visiting left subtree and then visit root node and finally visit right subtree. With small variations in order same is done in pre-order as well as post-order traversal. How do you do a breadth first traversal? It is slightly more tricky. Doing it non-recursively is even more difficult at first sight. Let me first explain what is breadthfirst traversal.
Different traversals produce different output as shown below
1 12 23 46 89 445 461 678 789
1 12 89 46 23 461 789 678 445
445 23 12 1 46 89 678 461 789
445 23 678 12 46 461 789 1 89