Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. CS 240 Spring 2018
  2. Program #4 and Homework #14
  3. Deadline: Friday, April 27th 11:55 PM
  4.  
  5. Download the BinaryTreeLinked class (just the .h file) from Moodle and implement the following prototyped functions. The functions listed should be in the public section of the class.
  6. You must also implement private recursive functions that actually do the jobs as described.
  7.  
  8. void printInorder() const;
  9.  
  10. void countNodesPerLevel(int counts[], int &levels, int arraySize) const;
  11.  
  12. void countParentTypes(int counts[]) const;
  13.  
  14. Details:
  15. printInOrder(): Must print the tree’s root value (if there is one) and then print all the tree’s values using an in-order traversal. At each node, you must print the node’s value, then its left child and then its right child. If either or both of the children are nullptr, print “- -“.
  16. For example:
  17. This tree:
  18. Should print like this:
  19.  
  20. Root: 14
  21. 4 Left child: -- Right child: 5
  22. 5 Left child: -- Right child: --
  23. 14 Left child: 4 Right child: 51
  24. 51 Left child: -- Right child: 80
  25. 80 Left child: -- Right child: --
  26.  
  27. countNodesPerLevel(): This function must count the number of nodes at each level of the tree, where level one is the root node and the other levels are the nodes that are the same distance from the root. For example, in the tree above, node 14 is on level 1, nodes 4 and 51 are on level 2 and nodes 5 and 80 are on level 3. The array “counts[]” must store the number of nodes at each level, where level 1 is stored at index 0, level 2 at index 1 and so on. So each index (i) stores the number of nodes at level (i + 1). Parameter “levels” should be set to how many levels are in the tree and “arraySize” is how many integers are actually in array “counts”. You must make sure to crash the program if the number of levels ever exceeds “arraySize”.
  28. For example: if this function was called on the tree above, the results would be:
  29. counts[0] = 1
  30. counts[1] = 2
  31. counts[2] = 2
  32. levels = 3
  33.  
  34. countParentTypes(): This function must count the number of nodes in the tree based on how many children the nodes have. counts[0] must store how many nodes have 0 children, counts[1] must store how many have 1 child and counts[2] must store how many nodes have 2 children.
  35. For example: if this function was called on the tree above, the result would be:
  36. counts[0] = 2
  37. counts[1] = 2
  38. counts[2] = 1
  39.  
  40. What to turn in:
  41. The BinaryTreeLinked.h with these functions implemented.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement