<div style="display: inline-flex; width: 100%; justify-content: center; align-items: center;"> <svg width="265pt" height="261pt" viewBox="0.00 0.00 264.60 261.02" 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 257.0193)"><title>%0</title> <polygon fill="transparent" stroke="transparent" points="-4,4 -4,-257.0193 260.6019,-257.0193 260.6019,4 -4,4"></polygon><g id="node1" class="node"><title>49</title> <ellipse fill="none" stroke="currentColor" cx="177.1019" cy="-234.9174" rx="18.2044" ry="18.2044"></ellipse><text fill="currentColor" text-anchor="middle" x="177.1019" y="-229.5174" font-family="Times,serif" font-size="18.00" fill="currentColor">49</text> </g><g id="node2" class="node"><title>42</title> <ellipse fill="none" stroke="currentColor" cx="131.1019" cy="-180.7135" rx="18.2044" ry="18.2044"></ellipse><text fill="currentColor" text-anchor="middle" x="131.1019" y="-175.3135" font-family="Times,serif" font-size="18.00" fill="currentColor">42</text> </g><g id="edge1" class="edge"><title>49->42</title> <path fill="none" stroke="currentColor" d="M165.2558,-220.9586C159.5059,-214.1832 152.5288,-205.9617 146.3833,-198.7203"></path><polygon fill="currentColor" stroke="currentColor" points="147.6374,-197.4934 143.0678,-194.8135 144.9688,-199.7581 147.6374,-197.4934"></polygon></g><g id="node13" class="node"><title>97</title> <ellipse fill="none" stroke="currentColor" cx="220.1019" cy="-180.7135" rx="18.2044" ry="18.2044"></ellipse><text fill="currentColor" text-anchor="middle" x="220.1019" y="-175.3135" font-family="Times,serif" font-size="18.00" fill="currentColor">97</text> </g><g id="edge12" class="edge"><title>49->97</title> <path fill="none" stroke="currentColor" d="M188.3997,-220.676C193.5925,-214.1302 199.8189,-206.2814 205.3737,-199.2792"></path><polygon fill="currentColor" stroke="currentColor" points="206.9939,-200.0528 208.7303,-195.048 204.2519,-197.8776 206.9939,-200.0528"></polygon></g><g id="node3" class="node"><title>8</title> <ellipse fill="none" stroke="currentColor" cx="72.1019" cy="-126.5097" rx="18.2044" ry="18.2044"></ellipse><text fill="currentColor" text-anchor="middle" x="72.1019" y="-121.1097" font-family="Times,serif" font-size="18.00" fill="currentColor">8</text> </g><g id="edge2" class="edge"><title>42->8</title> <path fill="none" stroke="currentColor" d="M117.7139,-168.4138C109.2652,-160.6519 98.273,-150.5533 89.1966,-142.2147"></path><polygon fill="currentColor" stroke="currentColor" points="90.303,-140.8547 85.437,-138.7607 87.9351,-143.4321 90.303,-140.8547"></polygon></g><g id="node4" class="node"><title>3</title> <ellipse fill="none" stroke="currentColor" cx="18.1019" cy="-72.3058" rx="18.2044" ry="18.2044"></ellipse><text fill="currentColor" text-anchor="middle" x="18.1019" y="-66.9058" font-family="Times,serif" font-size="18.00" fill="currentColor">3</text> </g><g id="edge3" class="edge"><title>8->3</title> <path fill="none" stroke="currentColor" d="M59.3047,-113.6641C52.0144,-106.3463 42.7928,-97.0899 34.9229,-89.1903"></path><polygon fill="currentColor" stroke="currentColor" points="35.9511,-87.7428 31.1824,-85.4357 33.4716,-90.213 35.9511,-87.7428"></polygon></g><g id="node6" class="node"><title>19</title> <ellipse fill="none" stroke="currentColor" cx="98.1019" cy="-72.3058" rx="18.2044" ry="18.2044"></ellipse><text fill="currentColor" text-anchor="middle" x="98.1019" y="-66.9058" font-family="Times,serif" font-size="18.00" fill="currentColor">19</text> </g><g id="edge5" class="edge"><title>8->19</title> <path fill="none" stroke="currentColor" d="M80.0441,-109.9522C82.5642,-104.6984 85.3847,-98.8183 88.0338,-93.2955"></path><polygon fill="currentColor" stroke="currentColor" points="89.6238,-94.027 90.2084,-88.7619 86.4681,-92.5132 89.6238,-94.027"></polygon></g><g id="node7" class="node"><title>17</title> <ellipse fill="none" stroke="currentColor" cx="58.1019" cy="-18.1019" rx="18.2044" ry="18.2044"></ellipse><text fill="currentColor" text-anchor="middle" x="58.1019" y="-12.7019" font-family="Times,serif" font-size="18.00" fill="currentColor">17</text> </g><g id="edge6" class="edge"><title>19->17</title> <path fill="none" stroke="currentColor" d="M87.1718,-57.4944C82.4978,-51.1607 76.9853,-43.6908 72.0242,-36.968"></path><polygon fill="currentColor" stroke="currentColor" points="73.3959,-35.8795 69.0189,-32.8955 70.5797,-37.9578 73.3959,-35.8795"></polygon></g><g id="node14" class="node"><title>67</title> <ellipse fill="none" stroke="currentColor" cx="188.1019" cy="-126.5097" rx="18.2044" ry="18.2044"></ellipse><text fill="currentColor" text-anchor="middle" x="188.1019" y="-121.1097" font-family="Times,serif" font-size="18.00" fill="currentColor">67</text> </g><g id="edge13" class="edge"><title>97->67</title> <path fill="none" stroke="currentColor" d="M210.8462,-165.0355C207.4925,-159.3548 203.6549,-152.8544 200.0992,-146.8315"></path><polygon fill="currentColor" stroke="currentColor" points="201.4967,-145.7562 197.4477,-142.3402 198.4827,-147.5356 201.4967,-145.7562"></polygon></g><g id="node15" class="node"><title>53</title> <ellipse fill="none" stroke="currentColor" cx="152.1019" cy="-72.3058" rx="18.2044" ry="18.2044"></ellipse><text fill="currentColor" text-anchor="middle" x="152.1019" y="-66.9058" font-family="Times,serif" font-size="18.00" fill="currentColor">53</text> </g><g id="edge14" class="edge"><title>67->53</title> <path fill="none" stroke="currentColor" d="M178.0739,-111.4109C174.0846,-105.4043 169.4406,-98.4119 165.1917,-92.0146"></path><polygon fill="currentColor" stroke="currentColor" points="166.5504,-90.8971 162.3263,-87.7003 163.6348,-92.8335 166.5504,-90.8971"></polygon></g><g id="node17" class="node"><title>88</title> <ellipse fill="none" stroke="currentColor" cx="232.1019" cy="-72.3058" rx="18.2044" ry="18.2044"></ellipse><text fill="currentColor" text-anchor="middle" x="232.1019" y="-66.9058" font-family="Times,serif" font-size="18.00" fill="currentColor">88</text> </g><g id="edge16" class="edge"><title>67->88</title><path fill="none" stroke="currentColor" d="M199.6624,-112.2682C204.976,-105.7224 211.3472,-97.8736 217.0312,-90.8715"></path><polygon fill="currentColor" stroke="currentColor" points="218.6734,-91.6253 220.4659,-86.6403 215.9559,-89.4194 218.6734,-91.6253"></polygon></g></g></svg> </div>
After completing all three removals, the parent of the node containing 97 is:
Your friend is distraught. They need an inOrder traversal of their binary search tree, but they can't find it anywhere. All they have is the history of transactions: they built the tree by inserting the following keys, in the order given:
Then, they removed the following keys, in the order given:
Can you help your friend? Give the inOrder traversal of your friend's BST by placing the keys in the boxes:
We have inserted a new node into the AVL Tree below, but we have not yet restored its balance.
<div style="display: inline-flex; width: 100%; justify-content: center; align-items: center;"> <svg width="374pt" height="261pt" viewBox="0.00 0.00 373.60 261.02" 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 257.0193)"><title>%0</title> <polygon fill="transparent" stroke="transparent" points="-4,4 -4,-257.0193 369.6019,-257.0193 369.6019,4 -4,4"></polygon><g id="node1" class="node"><title>42</title> <ellipse fill="none" stroke="currentColor" cx="192.1019" cy="-234.9174" rx="18.2044" ry="18.2044"></ellipse><text fill="currentColor" text-anchor="middle" x="192.1019" y="-229.5174" font-family="Times,serif" font-size="18.00" fill="currentColor">42</text> </g><g id="node2" class="node"><title>29</title> <ellipse fill="none" stroke="currentColor" cx="145.1019" cy="-180.7135" rx="18.2044" ry="18.2044"></ellipse><text fill="currentColor" text-anchor="middle" x="145.1019" y="-175.3135" font-family="Times,serif" font-size="18.00" fill="currentColor">29</text> </g><g id="edge1" class="edge"><title>42->29</title> <path fill="none" stroke="currentColor" d="M180.2418,-221.2395C174.2762,-214.3594 166.9715,-205.9352 160.5706,-198.5531"></path><polygon fill="currentColor" stroke="currentColor" points="161.7195,-197.2068 157.1217,-194.5756 159.0751,-199.4998 161.7195,-197.2068"></polygon></g><g id="node13" class="node"><title>68</title> <ellipse fill="none" stroke="currentColor" cx="235.1019" cy="-180.7135" rx="18.2044" ry="18.2044"></ellipse><text fill="currentColor" text-anchor="middle" x="235.1019" y="-175.3135" font-family="Times,serif" font-size="18.00" fill="currentColor">68</text> </g><g id="edge12" class="edge"><title>42->68</title> <path fill="none" stroke="currentColor" d="M203.3997,-220.676C208.5925,-214.1302 214.8189,-206.2814 220.3737,-199.2792"></path><polygon fill="currentColor" stroke="currentColor" points="221.9939,-200.0528 223.7303,-195.048 219.2519,-197.8776 221.9939,-200.0528"></polygon></g><g id="node3" class="node"><title>21</title> <ellipse fill="none" stroke="currentColor" cx="71.1019" cy="-126.5097" rx="18.2044" ry="18.2044"></ellipse><text fill="currentColor" text-anchor="middle" x="71.1019" y="-121.1097" font-family="Times,serif" font-size="18.00" fill="currentColor">21</text> </g><g id="edge2" class="edge"><title>29->21</title> <path fill="none" stroke="currentColor" d="M130.4819,-170.0046C118.902,-161.5225 102.6373,-149.6088 90.0577,-140.3944"></path><polygon fill="currentColor" stroke="currentColor" points="91.0127,-138.9247 85.9449,-137.3819 88.9444,-141.7483 91.0127,-138.9247"></polygon></g><g id="node8" class="node"><title>33</title> <ellipse fill="none" stroke="currentColor" cx="163.1019" cy="-126.5097" rx="18.2044" ry="18.2044"></ellipse><text fill="currentColor" text-anchor="middle" x="163.1019" y="-121.1097" font-family="Times,serif" font-size="18.00" fill="currentColor">33</text> </g><g id="edge7" class="edge"><title>29->33</title> <path fill="none" stroke="currentColor" d="M150.8963,-163.2647C152.4379,-158.6225 154.1239,-153.5456 155.7372,-148.6872"></path><polygon fill="currentColor" stroke="currentColor" points="157.4034,-149.2225 157.3184,-143.9258 154.0817,-148.1194 157.4034,-149.2225"></polygon></g><g id="node4" class="node"><title>19</title> <ellipse fill="none" stroke="currentColor" cx="18.1019" cy="-72.3058" rx="18.2044" ry="18.2044"></ellipse><text fill="currentColor" text-anchor="middle" x="18.1019" y="-66.9058" font-family="Times,serif" font-size="18.00" fill="currentColor">19</text> </g><g id="edge3" class="edge"><title>21->19</title> <path fill="none" stroke="currentColor" d="M58.2721,-113.3884C51.111,-106.0647 42.1159,-96.8652 34.4539,-89.0292"></path><polygon fill="currentColor" stroke="currentColor" points="35.5605,-87.6578 30.8136,-85.3062 33.058,-90.1047 35.5605,-87.6578"></polygon></g><g id="node6" class="node"><title>25</title> <ellipse fill="none" stroke="currentColor" cx="98.1019" cy="-72.3058" rx="18.2044" ry="18.2044"></ellipse><text fill="currentColor" text-anchor="middle" x="98.1019" y="-66.9058" font-family="Times,serif" font-size="18.00" fill="currentColor">25</text> </g><g id="edge5" class="edge"><title>21->25</title> <path fill="none" stroke="currentColor" d="M79.2028,-110.2467C81.8726,-104.887 84.8797,-98.8502 87.6967,-93.1948"></path><polygon fill="currentColor" stroke="currentColor" points="89.3438,-93.8131 90.0068,-88.5573 86.2109,-92.2525 89.3438,-93.8131"></polygon></g><g id="node11" class="node"><title>36</title> <ellipse fill="none" stroke="currentColor" cx="203.1019" cy="-72.3058" rx="18.2044" ry="18.2044"></ellipse><text fill="currentColor" text-anchor="middle" x="203.1019" y="-66.9058" font-family="Times,serif" font-size="18.00" fill="currentColor">36</text> </g><g id="edge10" class="edge"><title>33->36</title> <path fill="none" stroke="currentColor" d="M174.0321,-111.6982C178.7061,-105.3645 184.2185,-97.8946 189.1796,-91.1718"></path><polygon fill="currentColor" stroke="currentColor" points="190.6241,-92.1616 192.185,-87.0993 187.8079,-90.0834 190.6241,-92.1616"></polygon></g><g id="node14" class="node"><title>59</title> <ellipse fill="none" stroke="currentColor" cx="217.1019" cy="-126.5097" rx="18.2044" ry="18.2044"></ellipse><text fill="currentColor" text-anchor="middle" x="217.1019" y="-121.1097" font-family="Times,serif" font-size="18.00" fill="currentColor">59</text> </g><g id="edge13" class="edge"><title>68->59</title> <path fill="none" stroke="currentColor" d="M229.3075,-163.2647C227.7659,-158.6225 226.08,-153.5456 224.4666,-148.6872"></path><polygon fill="currentColor" stroke="currentColor" points="226.1221,-148.1194 222.8855,-143.9258 222.8005,-149.2225 226.1221,-148.1194"></polygon></g><g id="node16" class="node"><title>77</title> <ellipse fill="none" stroke="currentColor" cx="297.1019" cy="-126.5097" rx="18.2044" ry="18.2044"></ellipse><text fill="currentColor" text-anchor="middle" x="297.1019" y="-121.1097" font-family="Times,serif" font-size="18.00" fill="currentColor">77</text> </g><g id="edge15" class="edge"><title>68->77</title> <path fill="none" stroke="currentColor" d="M248.8617,-168.6839C257.8519,-160.8242 269.6785,-150.4848 279.3715,-142.0106"></path><polygon fill="currentColor" stroke="currentColor" points="280.7681,-143.1141 283.3806,-138.5056 278.4645,-140.4791 280.7681,-143.1141"></polygon></g><g id="node17" class="node"><title>72</title> <ellipse fill="none" stroke="currentColor" cx="257.1019" cy="-72.3058" rx="18.2044" ry="18.2044"></ellipse><text fill="currentColor" text-anchor="middle" x="257.1019" y="-66.9058" font-family="Times,serif" font-size="18.00" fill="currentColor">72</text> </g><g id="edge16" class="edge"><title>77->72</title> <path fill="none" stroke="currentColor" d="M286.1718,-111.6982C281.4978,-105.3645 275.9853,-97.8946 271.0242,-91.1718"></path><polygon fill="currentColor" stroke="currentColor" points="272.3959,-90.0834 268.0189,-87.0993 269.5797,-92.1616 272.3959,-90.0834"></polygon></g><g id="node19" class="node"><title>92</title> <ellipse fill="none" stroke="currentColor" cx="337.1019" cy="-72.3058" rx="18.2044" ry="18.2044"></ellipse><text fill="currentColor" text-anchor="middle" x="337.1019" y="-66.9058" font-family="Times,serif" font-size="18.00" fill="currentColor">92</text> </g><g id="edge18" class="edge"><title>77->92</title> <path fill="none" stroke="currentColor" d="M308.0321,-111.6982C312.7061,-105.3645 318.2185,-97.8946 323.1796,-91.1718"></path><polygon fill="currentColor" stroke="currentColor" points="324.6241,-92.1616 326.185,-87.0993 321.8079,-90.0834 324.6241,-92.1616"></polygon></g><g id="node20" class="node"><title>90</title> <ellipse fill="none" stroke="currentColor" cx="297.1019" cy="-18.1019" rx="18.2044" ry="18.2044"></ellipse><text fill="currentColor" text-anchor="middle" x="297.1019" y="-12.7019" font-family="Times,serif" font-size="18.00" fill="currentColor">90</text> </g><g id="edge19" class="edge"><title>92->90</title><path fill="none" stroke="currentColor" d="M326.1718,-57.4944C321.4978,-51.1607 315.9853,-43.6908 311.0242,-36.968"></path><polygon fill="currentColor" stroke="currentColor" points="312.3959,-35.8795 308.0189,-32.8955 309.5797,-37.9578 312.3959,-35.8795"></polygon></g></g></svg> </div>
Please answer the following questions, noting that the names of the options with double rotations describe the order in which the rotations are performed as described in class:
Which node was inserted? 90
Which is the lowest critically unbalanced node? 68
Which rotation will repair the imbalance?
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.
Options:
What is the runtime to determine if a perfect tree of
Time to determine the number of keys in an AVL tree with height
Maximum number of leaves in an AVL tree of height
Maximum number of nodes in an AVL tree of height
Suppose the root of an AVL tree of height 8 has a height balance of 1. What is the maximum number of nodes in the shortest subtree which is a child of the root?
In class we have learned that rotation can be applied to binary (search) trees to change their structure while preserving the in-order key sequence.
In this question, we explore a way to transform any binary search tree into a very specific shape that we call a spine.
Consider a binary search tree with a node definition below:
class BSTNode {
public:
int data;
BSTNode* left;
BSTNode* right;
// and a constructor - you do not need to know the details
};
A BST class using the node definition above has a single private member attribute: root.
class BST {
private:
BSTNode* root;
// and private and public member functions
};
A left spine is a binary tree in which there is a single leaf node, and every non-leaf node has only one left child. In this part of the question, complete the MakeLeftSpine function, which receives as a parameter a BSTNode pointer representing the root of some subtree, and transforms the subtree rooted at the parameter node into a left spine and returns the root of the transformed subtree.
To support your implementation, you have at your disposal the following private BST member functions:
// Performs a right rotation around nd and returns the root of the rotated subtree
BSTNode* RotateRight(BSTNode* nd);
// Performs a left rotation around nd and returns the root of the rotated subtree
BSTNode* RotateLeft(BSTNode* nd);
Both recursive and iterative solutions exist without additional helper functions. You are free to choose either implementation.
If your test cases show "NO OUTPUT" or incomplete output, it is likely that your code is producing a segmentation fault. Trace your code on a very small tree to identify where your error may be occurring.
The entire tree is rotated counter-clockwise by 90 degrees, so that the root is on the left, and its left subtree is towards the bottom and its right subtree is towards the top. There is one key printed on each line, and each key's amount of indentation indicates the key's depth.
Once again, significant partial credit can be obtained by passing the first test cases: empty, one node, two nodes, three nodes.
For interest (but don't waste too much time reading this) - Since any binary search tree containing
/**
* File: makeleftspine.cpp
* Description: Contains student-implemented functions for transforming
* a BST into a left spine
*/
#include <cstddef> // for NULL, if needed
#include "bst.h"
/*** NO OTHER INCLUDES ARE ALLOWED ***/
// Transforms the subtree rooted at nd into a left spine and returns the root of the left spine
BSTNode* BST::MakeLeftSpine(BSTNode* nd) {
if (nd == NULL) return nd;
if (nd->right != NULL) {
return MakeLeftSpine(RotateLeft(nd));
}
if (nd->left == NULL) return nd;
nd->left = MakeLeftSpine(nd->left);
return nd;
}