It's 1AM, and all through the house, not a creature was stirring... except for your TA. They've been given the Pre-Order traversal of a binary search tree, but they're missing the Level-Order and Post-Order traversals...
they're tired... so tired... and then they realized that they could ask their students to help them out!
Given the following Pre-Order traversal of a binary search tree:
Pre-Order: P E D H G V U W X
find the Level-Order and Post-Order traversals.
For reference, the English alphabet will be ordered with the following relative values:
A < B < C < D < E < F < G < H < I < J < K < L < M < N < O < P < Q < R < S < T < U < V < W < X < Y < Z
PEVDHUWGXDGHEUXWVPBuild a binary search tree by inserting all of the keys in the array below, in the order given.
The left child of the node containing 87 is:
The right child of the node containing 87 is:
Which of the following could be the sequence of keys compared in a search for 333 in a binary search tree?
Select all possible options that apply.
The Binary Search Tree below has an unknown key X. (The tree contains only keys, no values.)
<div style="display: inline-flex; width: 100%; justify-content: center; align-items: center;"> <svg width="380pt" height="260pt" viewBox="0.00 0.00 380.00 260.00" 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 256)"><title>%0</title> <polygon fill="transparent" stroke="transparent" points="-4,4 -4,-256 376,-256 376,4 -4,4"></polygon><g id="node1" class="node"><title>60</title> <ellipse fill="none" stroke="currentColor" cx="150" cy="-234" rx="18" ry="18"></ellipse><text fill="currentColor" text-anchor="middle" x="150" y="-229.8" font-family="Times,serif" font-size="14.00" fill="currentColor">60</text> </g><g id="node2" class="node"><title>20</title> <ellipse fill="none" stroke="currentColor" cx="90" cy="-180" rx="18" ry="18"></ellipse><text fill="currentColor" text-anchor="middle" x="90" y="-175.8" font-family="Times,serif" font-size="14.00" fill="currentColor">X</text> </g><g id="edge1" class="edge"><title>60->20</title> <path fill="none" stroke="currentColor" d="M136.3851,-221.7466C127.7931,-214.0138 116.6147,-203.9532 107.3844,-195.6459"></path><polygon fill="currentColor" stroke="currentColor" points="108.4482,-194.249 103.561,-192.2049 106.1068,-196.8506 108.4482,-194.249"></polygon></g><g id="node10" class="node"><title>214</title> <ellipse fill="none" stroke="currentColor" cx="200" cy="-180" rx="18" ry="18"></ellipse><text fill="currentColor" text-anchor="middle" x="200" y="-175.8" font-family="Times,serif" font-size="14.00" fill="currentColor">214</text> </g><g id="edge9" class="edge"><title>60->214</title> <path fill="none" stroke="currentColor" d="M162.3596,-220.6517C168.8012,-213.6947 176.7623,-205.0967 183.7015,-197.6024"></path><polygon fill="currentColor" stroke="currentColor" points="185.3224,-198.4276 187.4353,-193.5698 182.7542,-196.0497 185.3224,-198.4276"></polygon></g><g id="node3" class="node"><title>7</title> <ellipse fill="none" stroke="currentColor" cx="18" cy="-126" rx="18" ry="18"></ellipse><text fill="currentColor" text-anchor="middle" x="18" y="-121.8" font-family="Times,serif" font-size="14.00" fill="currentColor">7</text> </g><g id="edge2" class="edge"><title>20->7</title> <path fill="none" stroke="currentColor" d="M75.4297,-169.0723C64.3043,-160.7282 48.8858,-149.1643 36.8257,-140.1193"></path><polygon fill="currentColor" stroke="currentColor" points="37.5986,-138.5115 32.5486,-136.9115 35.4986,-141.3115 37.5986,-138.5115"></polygon></g><g id="node5" class="node"><title>43</title> <ellipse fill="none" stroke="currentColor" cx="98" cy="-126" rx="18" ry="18"></ellipse><text fill="currentColor" text-anchor="middle" x="98" y="-121.8" font-family="Times,serif" font-size="14.00" fill="currentColor">43</text> </g><g id="edge4" class="edge"><title>20->43</title> <path fill="none" stroke="currentColor" d="M92.6639,-162.0188C93.2781,-157.873 93.9397,-153.4068 94.581,-149.0779"></path><polygon fill="currentColor" stroke="currentColor" points="96.3188,-149.2893 95.3205,-144.0868 92.8565,-148.7763 96.3188,-149.2893"></polygon></g><g id="node6" class="node"><title>24</title> <ellipse fill="none" stroke="currentColor" cx="40" cy="-72" rx="18" ry="18"></ellipse><text fill="currentColor" text-anchor="middle" x="40" y="-67.8" font-family="Times,serif" font-size="14.00" fill="currentColor">24</text> </g><g id="edge5" class="edge"><title>43->24</title> <path fill="none" stroke="currentColor" d="M84.5479,-113.4756C76.4955,-105.9785 66.1725,-96.3675 57.4916,-88.2853"></path><polygon fill="currentColor" stroke="currentColor" points="58.2312,-86.5828 53.3792,-84.4565 55.8462,-89.1444 58.2312,-86.5828"></polygon></g><g id="node11" class="node"><title>174</title> <ellipse fill="none" stroke="currentColor" cx="185" cy="-126" rx="18" ry="18"></ellipse><text fill="currentColor" text-anchor="middle" x="185" y="-121.8" font-family="Times,serif" font-size="14.00" fill="currentColor">174</text> </g><g id="edge10" class="edge"><title>214->174</title> <path fill="none" stroke="currentColor" d="M195.1713,-162.6168C193.9134,-158.0884 192.5402,-153.1448 191.2214,-148.3969"></path><polygon fill="currentColor" stroke="currentColor" points="192.8441,-147.6998 189.8196,-143.3506 189.4717,-148.6366 192.8441,-147.6998"></polygon></g><g id="node22" class="node"><title>237</title> <ellipse fill="none" stroke="currentColor" cx="316" cy="-126" rx="18" ry="18"></ellipse><text fill="currentColor" text-anchor="middle" x="316" y="-121.8" font-family="Times,serif" font-size="14.00" fill="currentColor">237</text> </g><g id="edge21" class="edge"><title>214->237</title> <path fill="none" stroke="currentColor" d="M216.62,-172.2631C237.2072,-162.6794 272.308,-146.3394 294.8101,-135.8643"></path><polygon fill="currentColor" stroke="currentColor" points="295.6165,-137.4193 299.4109,-133.7225 294.1394,-134.2462 295.6165,-137.4193"></polygon></g><g id="node12" class="node"><title>112</title> <ellipse fill="none" stroke="currentColor" cx="145" cy="-72" rx="18" ry="18"></ellipse><text fill="currentColor" text-anchor="middle" x="145" y="-67.8" font-family="Times,serif" font-size="14.00" fill="currentColor">112</text> </g><g id="edge11" class="edge"><title>174->112</title> <path fill="none" stroke="currentColor" d="M174.2807,-111.529C169.528,-105.1128 163.8769,-97.4838 158.8128,-90.6473"></path><polygon fill="currentColor" stroke="currentColor" points="160.1313,-89.4871 155.7489,-86.511 157.3188,-91.5704 160.1313,-89.4871"></polygon></g><g id="node17" class="node"><title>207</title> <ellipse fill="none" stroke="currentColor" cx="232" cy="-72" rx="18" ry="18"></ellipse><text fill="currentColor" text-anchor="middle" x="232" y="-67.8" font-family="Times,serif" font-size="14.00" fill="currentColor">207</text> </g><g id="edge16" class="edge"><title>174->207</title> <path fill="none" stroke="currentColor" d="M196.8601,-112.3735C202.8258,-105.5193 210.1304,-97.1268 216.5314,-89.7725"></path><polygon fill="currentColor" stroke="currentColor" points="218.0176,-90.7304 219.9802,-85.81 215.3775,-88.4326 218.0176,-90.7304"></polygon></g><g id="node13" class="node"><title>111</title> <ellipse fill="none" stroke="currentColor" cx="65" cy="-18" rx="18" ry="18"></ellipse><text fill="currentColor" text-anchor="middle" x="65" y="-13.8" font-family="Times,serif" font-size="14.00" fill="currentColor">111</text> </g><g id="edge12" class="edge"><title>112->111</title> <path fill="none" stroke="currentColor" d="M129.9526,-61.843C116.9908,-53.0938 98.2086,-40.4158 84.172,-30.9411"></path><polygon fill="currentColor" stroke="currentColor" points="85.1033,-29.4584 79.9799,-28.1114 83.1451,-32.3593 85.1033,-29.4584"></polygon></g><g id="node15" class="node"><title>144</title> <ellipse fill="none" stroke="currentColor" cx="145" cy="-18" rx="18" ry="18"></ellipse><text fill="currentColor" text-anchor="middle" x="145" y="-13.8" font-family="Times,serif" font-size="14.00" fill="currentColor">144</text> </g><g id="edge14" class="edge"><title>112->144</title> <path fill="none" stroke="currentColor" d="M145,-53.7181C145,-49.7595 145,-45.5209 145,-41.3966"></path><polygon fill="currentColor" stroke="currentColor" points="146.7501,-41.2631 145,-36.2631 143.2501,-41.2631 146.7501,-41.2631"></polygon></g><g id="node18" class="node"><title>179</title> <ellipse fill="none" stroke="currentColor" cx="199" cy="-18" rx="18" ry="18"></ellipse><text fill="currentColor" text-anchor="middle" x="199" y="-13.8" font-family="Times,serif" font-size="14.00" fill="currentColor">179</text> </g><g id="edge17" class="edge"><title>207->179</title> <path fill="none" stroke="currentColor" d="M222.455,-56.381C218.9359,-50.6223 214.9,-44.0182 211.1794,-37.93"></path><polygon fill="currentColor" stroke="currentColor" points="212.5101,-36.7513 208.4096,-33.3975 209.5236,-38.5764 212.5101,-36.7513"></polygon></g><g id="node23" class="node"><title>229</title> <ellipse fill="none" stroke="currentColor" cx="296" cy="-72" rx="18" ry="18"></ellipse><text fill="currentColor" text-anchor="middle" x="296" y="-67.8" font-family="Times,serif" font-size="14.00" fill="currentColor">229</text> </g><g id="edge22" class="edge"><title>237->229</title> <path fill="none" stroke="currentColor" d="M309.6719,-108.9141C307.9209,-104.1865 305.9956,-98.988 304.1564,-94.0223"></path><polygon fill="currentColor" stroke="currentColor" points="305.7333,-93.2411 302.3556,-89.1601 302.4512,-94.4567 305.7333,-93.2411"></polygon></g><g id="node26" class="node"><title>231</title> <ellipse fill="none" stroke="currentColor" cx="354" cy="-18" rx="18" ry="18"></ellipse><text fill="currentColor" text-anchor="middle" x="354" y="-13.8" font-family="Times,serif" font-size="14.00" fill="currentColor">231</text> </g><g id="edge25" class="edge"><title>229->231</title><path fill="none" stroke="currentColor" d="M309.4521,-59.4756C317.5045,-51.9785 327.8275,-42.3675 336.5084,-34.2853"></path><polygon fill="currentColor" stroke="currentColor" points="338.1538,-35.1444 340.6208,-30.4565 335.7688,-32.5828 338.1538,-35.1444"></polygon></g></g></svg> </div>
Answer the following questions. Assume all keys are integers and there are no duplicate keys. (Hence X cannot be the same as any of the other keys in the tree).
What is the minimum value of X?
What is the maximum value of X?
<div style="display: inline-flex; width: 100%; justify-content: center; align-items: center;"> <svg width="463pt" height="260pt" viewBox="0.00 0.00 463.00 260.00" 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 256)"><title>%0</title> <polygon fill="transparent" stroke="transparent" points="-4,4 -4,-256 459,-256 459,4 -4,4"></polygon><g id="node1" class="node"><title>17</title> <ellipse fill="none" stroke="currentColor" cx="219.5" cy="-234" rx="18" ry="18"></ellipse><text fill="currentColor" text-anchor="middle" x="219.5" y="-229.8" font-family="Times,serif" font-size="14.00" fill="currentColor">17</text> </g><g id="node2" class="node"><title>7</title> <ellipse fill="none" stroke="currentColor" cx="167.5" cy="-180" rx="18" ry="18"></ellipse><text fill="currentColor" text-anchor="middle" x="167.5" y="-175.8" font-family="Times,serif" font-size="14.00" fill="currentColor">7</text> </g><g id="edge1" class="edge"><title>17->7</title> <path fill="none" stroke="currentColor" d="M206.9123,-220.9281C199.9896,-213.7392 191.32,-204.7362 183.876,-197.0058"></path><polygon fill="currentColor" stroke="currentColor" points="185.0619,-195.7144 180.3331,-193.3267 182.5407,-198.1422 185.0619,-195.7144"></polygon></g><g id="node19" class="node"><title>27</title> <ellipse fill="none" stroke="currentColor" cx="312.5" cy="-180" rx="18" ry="18"></ellipse><text fill="currentColor" text-anchor="middle" x="312.5" y="-175.8" font-family="Times,serif" font-size="14.00" fill="currentColor">27</text> </g><g id="edge18" class="edge"><title>17->27</title> <path fill="none" stroke="currentColor" d="M235.2762,-224.8396C250.9081,-215.7631 274.9246,-201.818 292.0207,-191.8912"></path><polygon fill="currentColor" stroke="currentColor" points="293.2012,-193.2294 296.6464,-189.2053 291.4437,-190.2026 293.2012,-193.2294"></polygon></g><g id="node3" class="node"><title>1</title> <ellipse fill="none" stroke="currentColor" cx="48.5" cy="-126" rx="18" ry="18"></ellipse><text fill="currentColor" text-anchor="middle" x="48.5" y="-121.8" font-family="Times,serif" font-size="14.00" fill="currentColor">1</text> </g><g id="edge2" class="edge"><title>7->1</title> <path fill="none" stroke="currentColor" d="M150.9536,-172.4915C129.728,-162.8598 92.8871,-146.142 69.6479,-135.5965"></path><polygon fill="currentColor" stroke="currentColor" points="70.1822,-133.9173 64.9059,-133.4447 68.7359,-137.1045 70.1822,-133.9173"></polygon></g><g id="node11" class="node"><title>13</title> <ellipse fill="none" stroke="currentColor" cx="187.5" cy="-126" rx="18" ry="18"></ellipse><text fill="currentColor" text-anchor="middle" x="187.5" y="-121.8" font-family="Times,serif" font-size="14.00" fill="currentColor">13</text> </g><g id="edge10" class="edge"><title>7->13</title> <path fill="none" stroke="currentColor" d="M173.8281,-162.9141C175.5791,-158.1865 177.5044,-152.988 179.3436,-148.0223"></path><polygon fill="currentColor" stroke="currentColor" points="181.0488,-148.4567 181.1444,-143.1601 177.7667,-147.2411 181.0488,-148.4567"></polygon></g><g id="node6" class="node"><title>3</title> <ellipse fill="none" stroke="currentColor" cx="68.5" cy="-72" rx="18" ry="18"></ellipse><text fill="currentColor" text-anchor="middle" x="68.5" y="-67.8" font-family="Times,serif" font-size="14.00" fill="currentColor">3</text> </g><g id="edge5" class="edge"><title>1->3</title> <path fill="none" stroke="currentColor" d="M54.8281,-108.9141C56.5791,-104.1865 58.5044,-98.988 60.3436,-94.0223"></path><polygon fill="currentColor" stroke="currentColor" points="62.0488,-94.4567 62.1444,-89.1601 58.7667,-93.2411 62.0488,-94.4567"></polygon></g><g id="node9" class="node"><title>4</title> <ellipse fill="none" stroke="currentColor" cx="95.5" cy="-18" rx="18" ry="18"></ellipse><text fill="currentColor" text-anchor="middle" x="95.5" y="-13.8" font-family="Times,serif" font-size="14.00" fill="currentColor">4</text> </g><g id="edge8" class="edge"><title>3->4</title> <path fill="none" stroke="currentColor" d="M76.6009,-55.7982C79.2707,-50.4587 82.2777,-44.4445 85.0948,-38.8104"></path><polygon fill="currentColor" stroke="currentColor" points="86.734,-39.4451 87.4048,-34.1903 83.6035,-37.8798 86.734,-39.4451"></polygon></g><g id="node12" class="node"><title>8</title> <ellipse fill="none" stroke="currentColor" cx="147.5" cy="-72" rx="18" ry="18"></ellipse><text fill="currentColor" text-anchor="middle" x="147.5" y="-67.8" font-family="Times,serif" font-size="14.00" fill="currentColor">8</text> </g><g id="edge11" class="edge"><title>13->8</title> <path fill="none" stroke="currentColor" d="M176.7807,-111.529C172.028,-105.1128 166.3769,-97.4838 161.3128,-90.6473"></path><polygon fill="currentColor" stroke="currentColor" points="162.6313,-89.4871 158.2489,-86.511 159.8188,-91.5704 162.6313,-89.4871"></polygon></g><g id="node17" class="node"><title>15</title> <ellipse fill="none" stroke="currentColor" cx="227.5" cy="-72" rx="18" ry="18"></ellipse><text fill="currentColor" text-anchor="middle" x="227.5" y="-67.8" font-family="Times,serif" font-size="14.00" fill="currentColor">15</text> </g><g id="edge16" class="edge"><title>13->15</title> <path fill="none" stroke="currentColor" d="M198.2193,-111.529C202.972,-105.1128 208.6231,-97.4838 213.6872,-90.6473"></path><polygon fill="currentColor" stroke="currentColor" points="215.1812,-91.5704 216.7511,-86.511 212.3687,-89.4871 215.1812,-91.5704"></polygon></g><g id="node15" class="node"><title>10</title> <ellipse fill="none" stroke="currentColor" cx="200.5" cy="-18" rx="18" ry="18"></ellipse><text fill="currentColor" text-anchor="middle" x="200.5" y="-13.8" font-family="Times,serif" font-size="14.00" fill="currentColor">10</text> </g><g id="edge14" class="edge"><title>8->10</title> <path fill="none" stroke="currentColor" d="M160.3298,-58.9281C167.4909,-51.6319 176.4861,-42.467 184.148,-34.6605"></path><polygon fill="currentColor" stroke="currentColor" points="185.5349,-35.7458 187.7883,-30.9515 183.037,-33.2941 185.5349,-35.7458"></polygon></g><g id="node20" class="node"><title>18</title> <ellipse fill="none" stroke="currentColor" cx="292.5" cy="-126" rx="18" ry="18"></ellipse><text fill="currentColor" text-anchor="middle" x="292.5" y="-121.8" font-family="Times,serif" font-size="14.00" fill="currentColor">18</text> </g><g id="edge19" class="edge"><title>27->18</title> <path fill="none" stroke="currentColor" d="M306.1719,-162.9141C304.4209,-158.1865 302.4956,-152.988 300.6564,-148.0223"></path><polygon fill="currentColor" stroke="currentColor" points="302.2333,-147.2411 298.8556,-143.1601 298.9512,-148.4567 302.2333,-147.2411"></polygon></g><g id="node28" class="node"><title>29</title> <ellipse fill="none" stroke="currentColor" cx="406.5" cy="-126" rx="18" ry="18"></ellipse><text fill="currentColor" text-anchor="middle" x="406.5" y="-121.8" font-family="Times,serif" font-size="14.00" fill="currentColor">29</text> </g><g id="edge27" class="edge"><title>27->29</title> <path fill="none" stroke="currentColor" d="M328.4458,-170.8396C344.3415,-161.708 368.8151,-147.6487 386.1139,-137.7112"></path><polygon fill="currentColor" stroke="currentColor" points="387.3259,-139.0332 390.7897,-135.0251 385.5824,-135.9983 387.3259,-139.0332"></polygon></g><g id="node23" class="node"><title>24</title> <ellipse fill="none" stroke="currentColor" cx="332.5" cy="-72" rx="18" ry="18"></ellipse><text fill="currentColor" text-anchor="middle" x="332.5" y="-67.8" font-family="Times,serif" font-size="14.00" fill="currentColor">24</text> </g><g id="edge22" class="edge"><title>18->24</title> <path fill="none" stroke="currentColor" d="M303.2193,-111.529C307.972,-105.1128 313.6231,-97.4838 318.6872,-90.6473"></path><polygon fill="currentColor" stroke="currentColor" points="320.1812,-91.5704 321.7511,-86.511 317.3687,-89.4871 320.1812,-91.5704"></polygon></g><g id="node26" class="node"><title>25</title> <ellipse fill="none" stroke="currentColor" cx="372.5" cy="-18" rx="18" ry="18"></ellipse><text fill="currentColor" text-anchor="middle" x="372.5" y="-13.8" font-family="Times,serif" font-size="14.00" fill="currentColor">25</text> </g><g id="edge25" class="edge"><title>24->25</title> <path fill="none" stroke="currentColor" d="M343.2193,-57.529C347.972,-51.1128 353.6231,-43.4838 358.6872,-36.6473"></path><polygon fill="currentColor" stroke="currentColor" points="360.1812,-37.5704 361.7511,-32.511 357.3687,-35.4871 360.1812,-37.5704"></polygon></g><g id="node29" class="node"><title>28</title> <ellipse fill="none" stroke="currentColor" cx="386.5" cy="-72" rx="18" ry="18"></ellipse><text fill="currentColor" text-anchor="middle" x="386.5" y="-67.8" font-family="Times,serif" font-size="14.00" fill="currentColor">28</text> </g><g id="edge28" class="edge"><title>29->28</title><path fill="none" stroke="currentColor" d="M400.1719,-108.9141C398.4209,-104.1865 396.4956,-98.988 394.6564,-94.0223"></path><polygon fill="currentColor" stroke="currentColor" points="396.2333,-93.2411 392.8556,-89.1601 392.9512,-94.4567 396.2333,-93.2411"></polygon></g></g></svg> </div>
Oh no! Winnie accidentally changes the key 7 in the Binary Search Tree above to 20.
Which of the following operations will fail because of Winnie's change? Assume each operation starts over from Winnie's adjusted BST. (An insert fails if it puts the key in a different spot in Winnie's BST from where it would have gone in the original BST. A find fails if it returns the wrong value. Either operation fails if it causes an error.)
Select all possible options that apply.
Choose the BEST asymptotic description for each of the following statements. Unless otherwise stated, assume we are referring to the worst-case behavior or structure, and that our algorithms (if any) are the best possible for the scenario.
Maximum number of nodes on the path from the root to the largest key in a binary search tree of
Minimum height of any binary tree with
Time to remove the root from a perfect binary search tree with
Consider a Binary Tree (not necessarily a BST) with a node definition as follows:
struct TreeNode {
int data;
TreeNode* left;
TreeNode* right;
};
Two trees can be "mirror images" of one another; that is, two trees may contain the same keys placed such that one is the result of flipping the other horizontally (around a vertical axis). You will write a function to check whether two trees are mirror images of one another.
In the editor below, complete the IsFlip function. HINT: this function should be implemented recursively.
Note: In test output, each tree's contents are output as if they have been "rotated" 90 degrees counter-clockwise so their roots are on the left. There is one node per line. Each line's indentation shows its depth in the tree. The left subtree of a node is printed below it; the right is printed above.
/*
* File: treeflip.cpp
* Description: Implementation of a binary tree manipulation for EX6
*/
#include <cstddef> // For NULL
#include "tree.h"
#include "treeisflip.h"
// Test if two trees are flips of one another (structure and data)
// returns true if nd1 and nd2 are flipped, false otherwise
// See the PrairieLearn page for examples of operation
bool IsFlip(TreeNode* nd1, TreeNode* nd2) {
if (nd1 == NULL && nd2 == NULL) return true;
if (nd1 == NULL || nd2 == NULL) return false;
if (nd1->data != nd2->data) return false;
return (
IsFlip(nd1->left, nd2->right)
&& IsFlip(nd1->right, nd2->left)
);
}