Unit 2
Trees
Conceptual Questions
[16.1] In the lecture, we discussed how trees are great for storing hierarchical data (data that occurs in connected layers). What are two contexts in which using a Tree may be helpful?
[16.2] Consider the following tree with a highlighted node in blue:
- What is the value of the blue highlighted node?
- What is the green node called (in relation to the blue node)?
- What are the pink nodes called (in relation to the blue node)?
- What is the green node called (in relation to the entire tree)?
- What are the pink nodes called (in relation to the entire tree)?
[16.3] Consider the following tree with two highlighted regions:
- What do we call the red and blue highlighted regions?
- What is the root of the blue highlighted region? What about the red?
- What nodes does the rightmost subtree of node 6 contain?
- What are the leaf nodes of the above tree?
[16.4] What condition must binary trees satisfy?
[16.5] Consider that we represent a sports tournament bracket as a tree. What would the root represent? What about the leaves?
[16.6] Which of the following trees is a Binary Search Tree (BST)?
[16.7]
Code Reading & Writing
[16.8] Consider the following code meant to represent a tree:
t = { "contents": 5,
"left": { "contents":3,
"left": { "contents": 1,
"left": None,
"right": None },
"right": { "contents": 4,
"left": None,
"right": None } },
"right": { "contents": 13,
"left": { "contents": 7,
"left": None,
"right": None },
"right": { "contents": 15,
"left": None,
"right": None } }
}
Draw out what this tree would look like. Is there anything special about it?
[16.9] Consider the following code meant to represent a tree:
def oddValuesTree(tree):
if tree == None:
return []
else:
if tree["contents"] % 2 == 0:
return oddValuesTree(tree["left"]) + oddValuesTree(tree["right"])
else:
return [tree["contents"]] + oddValuesTree(tree["left"]) + oddValuesTree(tree["right"])
What should the function return when given the tree below?
[16.10] In a few words, please explain what the following code does:
def mystery1(tree):
if tree == None:
return []
else:
leftValues = mystery1(tree["left"])
rightValues = mystery1(tree["right"])
return leftValues + [tree["contents"] * tree["contents"]] + rightValues
[16.11] Write a recursive program printValues(T) that recursively prints the values of a given binary tree T.
[16.12] Convert this tree to a Python representation:
[16.13] Write a function called findMax(T) that returns the maximum element of a binary tree passed as a parameter. Assume all nodes in the tree are positive, and if the tree is empty we return -1.
[16.14] Write a function findChildren(T, node) which returns a list containing the children of the given node within a given binary tree T.
[16.15] Write a function countNodes(t) that takes a tree of values and counts the number of nodes in the given tree.
[16.16] Write a function sumNodes(t) that takes a tree of values and adds all the values of the nodes in the tree.
[16.17] Write the function listValues(t), which takes a tree and returns a list of all the values in the tree. The values can be in any order, but try to put them in left-to-right order if possible.
[16.18] Write a function findBSTMax(t) to find the maximum element in a Binary Search Tree.
[16.19] Write a function findBSTMin(t) to find the minimum element in a Binary Search Tree.