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: Image Description

  1. What is the value of the blue highlighted node?
  2. What is the green node called (in relation to the blue node)?
  3. What are the pink nodes called (in relation to the blue node)?
  4. What is the green node called (in relation to the entire tree)?
  5. What are the pink nodes called (in relation to the entire tree)?

[16.3] Consider the following tree with two highlighted regions: Image Description

  1. What do we call the red and blue highlighted regions?
  2. What is the root of the blue highlighted region? What about the red?
  3. What nodes does the rightmost subtree of node 6 contain?
  4. 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)? Image Description

[16.7] Image Description

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? Image Description

[16.10] In a few words, please explain what the following code does:


def mystery1(tree):
   if tree["left"] == None and tree["right"] == None:
      return [tree["contents"] * tree["contents"]]
   else:
       leftValues = []
       if tree["left"] != None:
           leftValues = mystery1(tree["left"])
      
       rightValues = []
       if tree["right"] != None:
           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: Image Description

[16.13] Write a function called findMax(T) that returns the maximum element of a binary tree passed as a parameter.

[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 umNodes(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.