Design algorithms for the following operations for a binary tree T: ? preorderNe
ID: 3529104 • Letter: D
Question
Design algorithms for the following operations for a binary tree T: ? preorderNext(v): return the node visited after node v in a preorder traversal of T ? inorderNext(v): return the node visited after node v in an inorder traversal of T ? postorderNext(v): return the node visited after node v in a postorder traversal of T What are the worst-case running times of your algorithms?Explanation / Answer
Please rate with 5 stars :) void postOrderTraversalIterative(BinaryTree *root) { if (!root) return; stack s; s.push(root); BinaryTree *prev = NULL; while (!s.empty()) { BinaryTree *curr = s.top(); if (!prev || prev->left == curr || prev->right == curr) { if (curr->left) s.push(curr->left); else if (curr->right) s.push(curr->right); } else if (curr->left == prev) { if (curr->right) s.push(curr->right); } else { coutRelated 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.