Question: #3. Tree Traversals 8 points In class you wrote a recursive procedure for traversal of a binary tree in O(n) time, printing out the keys

#3. Tree Traversals 8 points In class you wrote a recursive procedure for traversal of a binary tree in O(n) time, printing out the keys of the nodes. Here you write two other tree traversal procedures. The first is a variation of what you wrote in class; the second is on a different kind of tree that you read about pages 248-249 and in the lecture notes and screencast. (a) Write an O(n)-time non-recursive procedure that, given an n-node binary tree, prints out the key of each node of the tree in preorder. Assume that trees consist T.not | of vertices of class TreeNode with instance variables parent, left, right, and key. Your procedure takes a TreeNode as its argument (the root of the tree). Use a stack as an auxiliary data structure. printBinaryTreeNodes (TreeNode root) f...! (b) Explain why your solution works and is O(n
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
