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...
the following lists the nodes in a binary tree in two different orders: Preorder : A...
the following lists the nodes in a binary tree in two different orders: Preorder : A B C D E F G H I J K L M Inorder : C E D F B A H J I K G M L Draw the binary tree
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.
Write an algorithm to determine if two binary trees are identical when the ordering of the...
Write an algorithm to determine if two binary trees are identical when the ordering of the subtrees for a node is ignored. For example, if a tree has root node with value R, left child with value A and right child with value B, this would be considered identical to another tree with root node value R, left child value B, and right child value A. Make the algorithm as efficient as you can. Analyze your algorithm’s running time. How...
Java Problem: Array lists and linked lists are both implementations of lists. Give an example of...
Java Problem: Array lists and linked lists are both implementations of lists. Give an example of a situation where an array list would be the better choice and one where a linked list would. Explain the reasons in each case. Provide example.
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.
Research the topic of binary search trees. Write a brief summary of your understanding of this....
Research the topic of binary search trees. Write a brief summary of your understanding of this. Design a simple program, using pseudocode, that performs a search of a binary search tree.. In your own words, explain how a binary search tree works using graph theory terminology. Construct a binary search tree for any alphabetically ordered list of five words. Represent it visually as a graph. Discuss any characteristics you note about the graph you created in part (d). In your...
(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++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT