Eroxl's Notes
Examlet Week 8 (CPSC 221)

Problem 1

Choose the appropriate running time from the list below.

The variable represents the number of items (keys, data, or key/data pairs) in the structure. In answering this question you should assume the best possible implementation given the constraints, and also assume that every array is sufficiently large to handle all items (unless otherwise stated).

Perform a post-order traversal of a Binary Tree.

  • [ ] (a)
  • [ ] (b)
  • [ ] (c)
  • [ ] (d)
  • [x] (e)

Problem 2

Recently, your TA team has had a bit of wanderlust. They've written down a few places they'd like to go, and, as all TAs do, chosen one and created a binary tree! They're too busy daydreaming to write down all of its traversals, so they need your help.

Given the following binary tree, provide all four traversals. Your solution should contain only letters -- no commas or brackets!

<div style="display: inline-flex; width: 100%; justify-content: center; align-items: center;"> <svg width="298pt" height="207pt" viewBox="0.00 0.00 298.20 206.82" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 202.8155)"><title>%0</title> <polygon fill="transparent" stroke="transparent" points="-4,4 -4,-202.8155 294.2039,-202.8155 294.2039,4 -4,4"></polygon><g id="node1" class="node"><title>4</title> <ellipse fill="none" stroke="currentColor" cx="167.1019" cy="-180.7135" rx="18.2044" ry="18.2044"></ellipse><text fill="currentColor" text-anchor="middle" x="167.1019" y="-175.3135" font-family="Times,serif" font-size="18.00" fill="currentColor">R</text> </g><g id="node2" class="node"><title>2</title> <ellipse fill="none" stroke="currentColor" cx="120.1019" cy="-126.5097" rx="18.2044" ry="18.2044"></ellipse><text fill="currentColor" text-anchor="middle" x="120.1019" y="-121.1097" font-family="Times,serif" font-size="18.00" fill="currentColor">K</text> </g><g id="edge1" class="edge"><title>4->2</title> <path fill="none" stroke="currentColor" d="M155.2418,-167.0356C149.2762,-160.1555 141.9715,-151.7313 135.5706,-144.3493"></path><polygon fill="currentColor" stroke="currentColor" points="136.7195,-143.003 132.1217,-140.3718 134.0751,-145.2959 136.7195,-143.003"></polygon></g><g id="node10" class="node"><title>6</title> <ellipse fill="none" stroke="currentColor" cx="210.1019" cy="-126.5097" rx="18.2044" ry="18.2044"></ellipse><text fill="currentColor" text-anchor="middle" x="210.1019" y="-121.1097" font-family="Times,serif" font-size="18.00" fill="currentColor">K</text> </g><g id="edge9" class="edge"><title>4->6</title> <path fill="none" stroke="currentColor" d="M178.3997,-166.4721C183.5925,-159.9263 189.8189,-152.0775 195.3737,-145.0754"></path><polygon fill="currentColor" stroke="currentColor" points="196.9939,-145.8489 198.7303,-140.8442 194.2519,-143.6737 196.9939,-145.8489"></polygon></g><g id="node3" class="node"><title>1</title> <ellipse fill="none" stroke="currentColor" cx="58.1019" cy="-72.3058" rx="18.2044" ry="18.2044"></ellipse><text fill="currentColor" text-anchor="middle" x="58.1019" y="-66.9058" font-family="Times,serif" font-size="18.00" fill="currentColor">A</text> </g><g id="edge2" class="edge"><title>2->1</title> <path fill="none" stroke="currentColor" d="M106.3421,-114.4801C97.352,-106.6204 85.5254,-96.2809 75.8323,-87.8067"></path><polygon fill="currentColor" stroke="currentColor" points="76.7394,-86.2752 71.8233,-84.3018 74.4357,-88.9102 76.7394,-86.2752"></polygon></g><g id="node8" class="node"><title>3</title> <ellipse fill="none" stroke="currentColor" cx="138.1019" cy="-72.3058" rx="18.2044" ry="18.2044"></ellipse><text fill="currentColor" text-anchor="middle" x="138.1019" y="-66.9058" font-family="Times,serif" font-size="18.00" fill="currentColor">A</text> </g><g id="edge7" class="edge"><title>2->3</title> <path fill="none" stroke="currentColor" d="M125.8963,-109.0609C127.4379,-104.4186 129.1239,-99.3417 130.7372,-94.4833"></path><polygon fill="currentColor" stroke="currentColor" points="132.4034,-95.0187 132.3184,-89.7219 129.0817,-93.9156 132.4034,-95.0187"></polygon></g><g id="node4" class="node"><title>0</title> <ellipse fill="none" stroke="currentColor" cx="18.1019" cy="-18.1019" rx="18.2044" ry="18.2044"></ellipse><text fill="currentColor" text-anchor="middle" x="18.1019" y="-12.7019" font-family="Times,serif" font-size="18.00" fill="currentColor">K</text> </g><g id="edge3" class="edge"><title>1->0</title> <path fill="none" stroke="currentColor" d="M47.1718,-57.4944C42.4978,-51.1607 36.9853,-43.6908 32.0242,-36.968"></path><polygon fill="currentColor" stroke="currentColor" points="33.3959,-35.8795 29.0189,-32.8955 30.5797,-37.9578 33.3959,-35.8795"></polygon></g><g id="node11" class="node"><title>5</title> <ellipse fill="none" stroke="currentColor" cx="192.1019" cy="-72.3058" rx="18.2044" ry="18.2044"></ellipse><text fill="currentColor" text-anchor="middle" x="192.1019" y="-66.9058" font-family="Times,serif" font-size="18.00" fill="currentColor">I</text> </g><g id="edge10" class="edge"><title>6->5</title> <path fill="none" stroke="currentColor" d="M204.3075,-109.0609C202.7659,-104.4186 201.08,-99.3417 199.4666,-94.4833"></path><polygon fill="currentColor" stroke="currentColor" points="201.1221,-93.9156 197.8855,-89.7219 197.8005,-95.0187 201.1221,-93.9156"></polygon></g><g id="node13" class="node"><title>7</title> <ellipse fill="none" stroke="currentColor" cx="272.1019" cy="-72.3058" rx="18.2044" ry="18.2044"></ellipse><text fill="currentColor" text-anchor="middle" x="272.1019" y="-66.9058" font-family="Times,serif" font-size="18.00" fill="currentColor">O</text> </g><g id="edge12" class="edge"><title>6->7</title><path fill="none" stroke="currentColor" d="M223.8617,-114.4801C232.8519,-106.6204 244.6785,-96.2809 254.3715,-87.8067"></path><polygon fill="currentColor" stroke="currentColor" points="255.7681,-88.9102 258.3806,-84.3018 253.4645,-86.2752 255.7681,-88.9102"></polygon></g></g></svg> </div>

  • Level Order: RKKAAIOK
  • In-Order: KAKARIKO
  • Post-Order: KAAKIOKR
  • Pre-Order: RKAKAKIO

Problem 3

Consider the binary tree class described in lecture where we have 1) variable root that is the treeNode pointer representing the root of the binary tree and 2) each treeNode consists of an integer data element, and two treeNode pointers called left and right.

int fun (treeNode* curr) {
	if (curr != NULL) {
		ret1 = fun(curr->right);
		ret2 = fun(curr->left);
		
		return 1 + max(ret1, ret2);
	}
	else return -1;
}

What does fun(root) return?

  • [ ] (a) fun returns the number of elements in the tree.
  • [ ] (b) fun returns the shortest distance from root to leaf.
  • [ ] (c) None of the other options is correct.
  • [x] (d) fun returns the height of the tree.
  • [ ] (e) fun returns the sum of all elements in the tree.

Problem 4

It's 1AM, and all through the house, not a creature was stirring... except for your TA. They've been given the In-Order and Post-Order traversals of a binary tree, but they're missing the Pre-Order and Level-Order traversals...

they're tired... so tired... and then they realized that they could ask their students to help them out!

Given the following In-Order and Post-Order traversals of a binary tree:

  • In-Order: K N D H L O C M
  • Post-Order: N K D L C M O H

find the Pre-Order and Level-Order traversal.

  • Pre-Order: HDKNOLMC
  • Level-Order: HDOKLMNC

Problem 5

Choose the appropriate worst-case expression from the list below.

The variable represents the number of items (keys, data, or key/data pairs) in the tree and represents the height of the tree. In answering this question you should assume the best possible implementation given the constraints, and also assume that every array is sufficiently large to handle all items (unless otherwise stated).

In a perfect binary tree, what is the length of the longest path from an arbitrary node down to a descendant leaf?

  • [ ] (a)
  • [ ] (b) None of the options is correct
  • [ ] (c)
  • [x] (d)
  • [ ] (e)

Problem 6

Consider this binary tree Node structure:

struct Node {
  int key;
  int value;
  Node *left, *right;
  Node(int key_, int value_ = 0, Node *left_ = nullptr, Node *right_ = nullptr) : 
    key(key_), value(value_), left(left_), right(right_) {}
};

Complete the function void waldo(Node *¤t) below. current is part of (and maybe the overall root of) a binary tree in which there are no duplicate keys and no duplicate values. waldo performs pre-order traversal of current's subtree, where the action in the traversal is: Check if the current node has a non-null left child whose value is smaller than the current node's value and, if so, swap the positions of the two nodes in the tree.

NOTE:

  • "Swapping the positions" of two nodes has a similar effect to just swapping the keys/values within the nodes, in that it leaves the overall shape of the tree unchanged after the swap. HOWEVER, you must accomplish the swap by manipulating the two nodes' left and right pointers and the left/right pointers of their parents (if any) and NOT by directly swapping the keys/values within the nodes.
  • We urge you to sketch an example of the swap on paper before implementing!
  • current is passed by reference. If it ends up being reassigned, be sure to update the reference!
  • For each recursive call in the traversal, use the current node's most up-to-date child pointers (even if the swapping process might cause some nodes to be visited multiple times or not visited at all).
#include <cstdlib> // in case you need the absolute value function abs
#include <cstddef> // for NULL
#include "problem.h"

void waldo(Node *&current) {
    if (current == NULL) return;
    
    if (current->left != NULL && current->left->value < current->value) {
        Node *tempLeft = current->left->left;
        Node *tempRight = current->left->right;
        
        current->left->left = current;
        current->left->right = current->right;
        
        Node *tempCurrent = current->left;
        
        current->left = tempLeft;
        current->right = tempRight;
        
        current = tempCurrent;
    }
    
    waldo(current->left);
    waldo(current->right);
}