Answered step by step
Verified Expert Solution
Question
1 Approved Answer
2. This question involves working with binary trees (trees where a node may 0, 1, or 2 children) Recall the conventions we have adopted in
2. This question involves working with binary trees (trees where a node may 0, 1, or 2 children) Recall the conventions we have adopted in class for maintaining trees. We represent the empty tree with the empty list (); a nonempty tree is represented as a list of three objects (value left-subtree right-subtree) where value is the value stored at the root of the tree, and left-subtree and right-subtree are the two subtrees. We introduced some standardized functions for maintaining and accessing this structure, which you should use in your solutions below (define (make-tree value left right) produces a tree with value at the root, and left and right as its subtrees. (list value left right)) (define (value tree) (car tree)) (define (left tree) (cadr tree)) (define (right tree) (caddr tree) Notably, you will need to use these functions to produce some test trees to test the following functions. For example, to produce the simple tree in Figure 1, you could use the following code (define testtree (make-tree 1 (make-tree 3 (make-tree 7'0')) (make-tree 9'() ' ())) (make-tree 5 ()))) You will probably want to make some more complex trees than that, but you get the idea. Finally, for all of these, figure out how to define the function recursively, as that should map fairly directly to code (a) A non-empty tree is made up of nodes: the root, and all of the nodes in its subtrees. Define a Scheme procedure (tree-node-count t) that calculates the number of nodes in tree t It should also work for empty trees. (tree-node-count testtree) would be 5 (b) Each node in a tree has a value. Define a Scheme procedure (tree-node-sum t) that calculates the sum of the values of the nodes in nodes in tree t. It should work for any binary tree whose node values are numbers, as well as for empty trees. (tree-node-sum testtree) would be 25 (c) The height of a node in a tree is the length of the longest path from that node to a leaf we can consider the height of a tree to be the height of its root. The height of the empty
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