Answered step by step
Verified Expert Solution
Question
1 Approved Answer
This in MATLAB function root = TreeGenerator(input_list) %% create a BST from a sorted list if isempty(input_list) root = []; return end mid = ceil(length(input_list)
This in MATLAB
function root = TreeGenerator(input_list) %% create a BST from a sorted list if isempty(input_list) root = []; return end mid = ceil(length(input_list) / 2); root = TreeNode; root.Val = input_list(mid); root.Left = TreeGenerator(input_list(1:mid - 1)); root.Right = TreeGenerator(input_list(mid + 1:end)); return end
classdef TreeNode properties Left Right Val end end
In computer sclence, binary search trees (BST), sometimes called ordered or sorted binary trees, are a particular type of container data structures that store "items" (such as numbers, names etc.) in memory A binary search trae is a rooted binary tree, whose i store a kay (and optionally, an associated value) and each have two distinguished sub-trees, commonly denotad left and right. The tree additionally satisfies the binary search property, which states that the key in each node must ba greater than or equal to any kay stored in the left sub-tree, and less than or equal to any key stored in the right sub-tree. The leaves (final nodes) of the tres contain no key and have no structure to distinguish them from one another As lar a balanced BST, the constraint is generally applied recursively lo every sublree That is, the tree is anly balanced i internal nodes each 1. The left and right subtrees' heights differ by at most one, AND 2 The lell subtree is balanced, AND 3. The right subtree is balanced There are twa scripls that doing the fallowing TreeNode: Dafine a binary trea node. It has three values left, right and val that left points to the left sub-tres, right points to the right sub-tree and val is the node's valus TreeGenerator: Given a sorted list A of distinctive integers, return a balanced binary search tree that each node stores a vaue from A. Note: the node of a tree has been defined in TreeNode class e.g. input: A-[1, 2, 3, 4,5,6, 7] retu: build the tee and rturn the root af the tree provided python version o these wo s ts in p script py and matlab verson o hese two scripts n m sc ptszip You can choose either ona depending on which anguage you are most comfortable with solving h Input A- [1.2.3.4,5,6,7) to the TreeGenerator function and it will return the root of a Tree. Novw your task is to in-order traverse the tree to retum the Input of TreeGenerator function, e.g. [1.2.3,4.5.6.7 In-class activity 4 Given the root of the tree built by eeGenerator function, can you traversal the tree and return the sorted list A o o ng problem Hint: inorder travarsal (Left, Root, Right) eg , input roof (the roof from part 1) relum. A 1, 2, 3, 4,5,6, 4,5,6,7
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