Consider the binary tree traversal algorithm below. Algorithm stackTraversal(tre
ID: 3865046 • Letter: C
Question
Consider the binary tree traversal algorithm below. Algorithm stackTraversal(treeNode n) Input: a treeNode n Output: Prints the value of each node in the binary tree rooted at n Stack s leftarrow new Stack(); s.push(n); while (! s.empty()) do x leftarrow s.pop(); print x.getValue(); if (x.getRightChild() != null) then s.push(x.getRightChild()); if (x.getLeftChild() != null) then s.push(x.getLeftChild()); Write the output being printed when stackTraversal(root) is executed. This is the equivalent of what traversal method seen previously in class?Explanation / Answer
If We have a Tree Like
1
/
2 3
/ /
4 5 6 7
Then we will get The PRE-ORDER TRAVERSAL OF BINARY TREE
Output will be : 1 2 4 5 3 6 7
Represents The PreOrder Traversal
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.