Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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. The class TernaryTree will additionally contain two recursive methods build_tree(L) and traverse_LMRW(). The recursive method build_tree(L), where L is a list of integers: the method build_tree(L) works according to the following rules: L[0] is always taken to be a root node. 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 it 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. An example. If we build a tree using L = [4,1,2,2,3,1,0,4,6,5,6,4], the method traverse_LMRW() would return: 0 1 2 3 2 1 4 4 5 6 6 4
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