Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

in python: 1 Overview In this project, you will be writing a variety of functions involving trees. Most of them will require that you write

in python: image text in transcribed
image text in transcribed
image text in transcribed
1 Overview In this project, you will be writing a variety of functions involving trees. Most of them will require that you write a function which recurses over a twee; however, two of them will require that you use loops. A few of the functions will assume that the tree is a BST; however, most will not. All of the trees will be binary trees, and will use the TreeNode class that I've provided in tree_node.py Put all of your functions into a file named tree_funcs.py. 1.1 Common Rules . Except where explicitly stated in the function descriptions below, you may choose whether to use recursion or a loop in the functions. (Note that the recursive solution will be much easier in most cases.) In this project, helper functions and default arguments) are banned. - In two of the functions in this project, you may assume that the tree we pass you is not empty. But only do this if we explicitly say so! Most of your functions must handle the empty-tree case. 2 tree.count(root) Write a function, tree_count(), which takes a tree as a parameter, and returns the number of nodes in the tree. 3 tree_count_1_child(root) Write a function, tree_count_1_child, which takes a tree as a parameter, and returns the number of nodes in the tree that have exactly one child. 4 tree sum(root) Write a function, tree-sum(), which takes a tree as a parameter, and returns the sum of all of the values in the tree. 1 You may assume that all of the values are mmerich an empty tree should return er 5 tree-print (root) Write a function, tree_print), which takes a tree as n parameter, and prints on the value stored in every node, one per line. Note that this function does not impose a particular order on you! You may print the values in any order! My testcase grading code will be smart enough to give you full points if you print all of the correct lines - no matter what order You se Do not print anything, if you are given an empty tree. 6 tree-print leaves (root) This is the same as tree-print), except that it should only print the value of Jesve D 7 bst_search_loop (root, val) Search the tree to find the value, if it exists. If it exists, then return the node which contains it. If it does not exist, return None. This function should assume that the tree is a BST. RESTRICTION: Your implementation must use a loop: recursion is forbidden in this function 8 tree.search(root, val) Search the tree to find the value, if it exists. If it exists, then return the node which contains it. If it does not exist, return None. This function should not assume anything about the order of values in the tree: it might not be a BST Feel free to use a loop or recursion, your choice, 9 bst max loop(root) Return the maximum value in the tree. You may assume that the tree is a BST. Additionally, you may assume that the tree is not empty. RESTRICTION: Your implementation must use a loop: recursion is forbidelen in this fiction, Search the tree to find the mlue, if it exists. If it exists, then return the node which contains it. If it does not exist, retum None. This function should assume that the tree is a BST RESTRICTION: Your implementation must use a loop: recursion is forbidden in this function 8 tree search (root, val) Search the tree to find the value, if it exists. If it exists, then return the node which contains it. If it does not exist, return None. This function should not assume anything about the order of values in the tree, it might not be a BST Feel free to use a loop or recursion, your choice. 9 bst max.loop(root) Return the maximum value in the tree. You may assume that the tree is a BST. Additionally, you may assume that the tree is not empty. RESTRICTION: Your implementation must use a loop: recursion is forbidden in this function 2 10 tree .max (root) Return the maximum value in the tree. You must not amine that the tree is BST. However, you may resume that the tree is not empty

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Programming The Perl DBI Database Programming With Perl

Authors: Tim Bunce, Alligator Descartes

1st Edition

1565926994, 978-1565926998

More Books

Students also viewed these Databases questions

Question

What are Decision Trees?

Answered: 1 week ago

Question

What is meant by the Term Glass Ceiling?

Answered: 1 week ago