Question

In: Computer Science

Consider an implementation of binary trees with Scheme lists, as in the following example:


Consider an implementation of binary trees with Scheme lists, as in the following example:

image.png

 Before proceeding, it may be useful to define three auxiliary functions (left T), (right T) and (val T) which return the left subtree, the right subtree, and the value in the root of tree T, respectively.

 (a) Write a recursive function (n-nodes T), which returns the number of nodes in the tree T. The following example illustrates the use of this function:

 > (n-nodes T)

8

 (b) Write a recursive function (n-leaves T), which returns the number of leaves

 in the tree T. The following example illustrates the use of this function:

 > (n-leaves T)

4

 (c) (15 pts) The height of a tree is defined as the maximum number of nodes on a path from the root to a leaf. Write a recursive function (height T), which returns the height of the tree T. The following example illustrates the use of this function:

 > (height T)

4

 (d) (15 pts) Write a recursive function (postorder T), which returns the list of all elements in the tree T corresponding to a postorder traversal of the tree. The following example illustrates the use of this function:

 >

 (postorder T)

 (1 9 8 5 17 25 22 13)


Solutions

Expert Solution

(define T
   '(13
       (5
           (1 () ())
           (8 ()
               (9 () ())
           )
       )
       (22
           (17 () ())
           (25 () ())
       )
   )
)

(define (val T) (car T))
(define (left T) (cadr T))
(define (right T) (caddr T))

(define (n-nodes T)
   (cond ((null? T) 0)
       (else (+ 1 (n-nodes (left T)) (n-nodes (right T))))))

(define (n-leaves T)
   (cond ((null? T) 0)
       ((and (null? (left T)) (null? (right T))) 1)
       (else (+ (n-leaves (left T)) (n-leaves (right T))))))


(define (height T)
(cond ((null? T) 0)
(else (+ 1 (max (height (left T)) (height (right T)))))))


(define (postorder T)
   (cond ((null? T) '())
(else (append (postorder (left T)) (postorder (right T))
   (list (val T))))))


Related Solutions

Consider the following type of binary trees: data Tree a = Leaf a | Node (Tree...
Consider the following type of binary trees: data Tree a = Leaf a | Node (Tree a) (Tree a) A tree is balanced if the number of leaves in the left and right subtree of every node differ by at most one. Write a Haskell function balanced that returns whether a tree is balanced or not. balanced :: Tree a -> Bool
In C++ Consider the binary search tree implementation in file bintree.cp a)Add to the TreeNode class...
In C++ Consider the binary search tree implementation in file bintree.cp a)Add to the TreeNode class a pointer to the parent node. Modify the insert and erase functions to properly set those pointers. b)Then define a TreeIterator class that contains a pointer to a TreeNode and has member functions get and next. i)The iterator’s get member function should return the data value of the node to which it points. ii)The iterator’s next member function should find the next element in...
Decision Trees: Consider a data set described by n binary features. Does n place a limit...
Decision Trees: Consider a data set described by n binary features. Does n place a limit on the depth of the tree? Justify your answer.
Need in JAVA. You are to use Binary Trees to do this Program. Write a complete...
Need in JAVA. You are to use Binary Trees to do this Program. Write a complete program, which will process several sets of numbers: For each set of numbers you should: 1. Create a binary tree. 2. Print the tree using “inorder”, “preorder”, and “postorder”. 3. Call a method Count which counts the number of nodes in the tree. 4. Call a method Children which prints the number of children each node has. 5. Inset and delete several nodes according...
Derive a recurrence for the number of degenerate binary search trees that can be generated from...
Derive a recurrence for the number of degenerate binary search trees that can be generated from a given sequence of n distinct elements. Recall that degenerate binary search tree contains no branches and thus is structurally similar to a linked list. You must make an argument to justify each component of your recurrence. Remember that all recurrences have base cases so do not forget to include a base case.
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the...
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the nodes in a binary tree that have only one child. Convert it to an iterative version. in C++
Write a function in Python that adds two Binary Trees together. The inputs of your function...
Write a function in Python that adds two Binary Trees together. The inputs of your function should be two binary trees (or their roots, depending on how you plan on coding this), and the output will be one Binary Tree that is the sum of the two inputted. To add Binary Trees together: Add the values of nodes with the same position in both trees together, this will be the value assigned to the node of the same position in...
Q1. Assume the complete binary tree numbering scheme used by Heapsort and apply the Heapsort algorithm...
Q1. Assume the complete binary tree numbering scheme used by Heapsort and apply the Heapsort algorithm to the following key sequence (3,25,9, 35,10,13,1,7). The first element index is equal 1. What value is in location 5 of the initial HEAP? After a single deletion (of the parameter at the heap root) and tree restructuring, what value is in location 5 of the new HEAP?
Write an implementation of the set class, with associated iterators using a binary search tree. Add...
Write an implementation of the set class, with associated iterators using a binary search tree. Add to each node a link to the parent node. (C++)
a) Based on the binary tree implementation from the Python program below  write a recursive method that...
a) Based on the binary tree implementation from the Python program below  write a recursive method that calculates the number of leaf nodes in the tree. class Binaertre: def __init__(self, datatobjekt): self.data = datatobjekt self.forelder = None self.venstre_barn = None self.hoyre_barn = None @property def venstre_barn(self): return self.__venstre_barn @venstre_barn.setter def venstre_barn(self, node): self.__venstre_barn = node if node is not None: node.forelder = self @property def hoyre_barn(self): return self.__hoyre_barn @hoyre_barn.setter def hoyre_barn(self, node): self.__hoyre_barn = node if node is not None: node.forelder...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT