Answered step by step
Verified Expert Solution
Question
1 Approved Answer
PYTHON Write the function bstSort(L) that receives a singly linked list L, sorts L using a binary search tree and returns a sorted version of
PYTHON
Write the function bstSort(L) that receives a singly linked list L, sorts L using a binary search tree and returns a sorted version of L. You should insert the items in L to a binary search tree, then traverse the tree to extract its elements into a List object. Items must be inserted and retrieved directly for the List object without using intermediate storage (for example, extracting the items from the List or the binary search tree to a Python list is not allowed).
2. Write the function isBST(t) that receives a binary tree t and determines if t is a binary search tree. For example, your function should return True if it receives the tree on the left and False if it receives the tree on the right (since key 68 is on the left subtree of key 66). Do this by extracting the in-order traversal of the tree to a Python list and checking to see if the list is sorted. Your function must run in time O(n) (no credit will be given if it takes longer than that).
3. Write the function isBST2(t) that receives a binary tree t and determines if t is a binary search tree. Your function should give the same results as the one in the previous question, but now you are not allowed to use extra storage (that is, you cannot extract the elements of the tree to a list). Your function must run in time O(n) (no credit will be given if it takes longer than that). Hint: use recursion; every recursive call should receive the root and the minimum and maximum key values allowed in each sub-tree.
4. Write the function equalBSTs(t1,t2) that receives binary search trees t1 and t2 and determines if they are identical (that is, they have the same keys and the same shape).
5. Write the function extractBSTs(t1,t2) that receives binary search trees t1 and t2 and returns a sorted singly linked list L that contains the combined elements of t1 and t2 sorted in ascending order. For exam- ple, if t1 and t2 are the binary search trees in the image, your function should return the singly linked list L containing [1,2,3,4,5,7,7,8,10,13,14,15,15,16,17,18,24]. Your function must run in time O(n) (no credit will be given if it takes longer than that).
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