Answered step by step
Verified Expert Solution
Question
1 Approved Answer
#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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started