Question: A ternary tree is a tree with at most three child nodes (as opposed to a binary tree which has at most two child nodes).

A ternary tree is a tree with at most three child nodes (as opposed to a binary tree which has at most two child nodes). Write a Python class TernaryTree which allows us to generate a ternary tree and traverse it recursively. The data for the tree construction will be integers contained in a list L. Here is an example of what your main function should look like and what kind of output it should produce when input is L = [4,1,2,2,3,1,0,4,6,5,6,4]. def main(): L = [4,1,2,2,3,1,0,4,6,5,6,4] T = TernaryTree(L[0]) T.build_tree(L) T.traverse_LMRW() main() 0 1 2 3 2 1 4 4 5 6 6 4 The method build_tree(L) works according to the following rules: L[0] is always taken to be a root node during the initial tree construction, i.e. T = TernaryTree(L[0]). Then, any subsequent L[i] goes to the left of the root node if L[i] < root node; it goes into the middle of the root node if L[i] = root node; otherwise L[i] goes right if L[i] > root node; The method traverse_LMRW() traverses an instance of the object TernaryTree recursively. The recursion rules LMRW stand for go (traverse) Left, go (traverse) Middle, go (traverse) Right, Write node value (print to screen). That is, we traverse the tree on the left until until we cannot go any further, then we go to the middle until we cannot go any further, then we go right until we cannot go any further. When these have been exhausted, we print the node value to screen.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!