OSU CSE 1223 - Homework BinaryTree and Recursion II
Pages 2

Unformatted text preview:

Homework BinaryTree and Recursion II 1 Write a recursive body for the following static generic method that returns a String 1 2 3 4 5 6 7 8 9 10 11 12 representation of a given BinaryTree T You cannot use the BinaryTree toString method in your solution Note that the BinaryTree must be restored i e its outgoing value must be the same as its incoming value Returns the code String prefix representation of the given code BinaryTree T param T the type of the code BinaryTree node labels param t the code BinaryTree to convert to a code String return the prefix representation of code t ensures treeToString the String prefix representation of t public static T String treeToString BinaryTree T t String s if t size 0 s else BinaryTree T left t newInstance BinaryTree T right t newInstance T root t disassemble left right s root treeToString left treeToString right return s The prefix representation of the empty tree is and the prefix representation of a non empty BinaryTree T is the string concatenation of the root followed by then by the prefix representation of the left subtree the prefix representation of the right subtree and finally Here are five examples of prefix representations of some BinaryTree Character s See if you can figure out and draw the binary trees being described a a b c a b a b c 2 Write a recursive body for the following static method that copies and returns a given BinaryTree Integer Note that the given BinaryTree must be restored i e its outgoing value must be the same as its incoming value 1 2 3 4 5 6 7 8 9 Returns a copy of the the given code BinaryTree param t the code BinaryTree to copy return a copy of the given code BinaryTree ensures copy t public static BinaryTree Integer copy BinaryTree Integer t BinaryTree Integer left t newInstance BinaryTree Integer right t newInstance int root t disassemble left right t assemble root left right return t

View Full Document

# OSU CSE 1223 - Homework BinaryTree and Recursion II

Pages: 2
Documents in this Course

3 pages

2 pages

4 pages

40 pages

31 pages

47 pages

44 pages

13 pages

11 pages

18 pages