Question: #include bstSequence.h using namespace std; void fix _ size ( Node * T ) { T - > size = 1 ; if (
#include "bstSequence.h
using namespace std;
void fixsizeNode T
Tsize ;
if Tleft Tsize Tleftsize;
if Tright Tsize Trightsize;
insert key k into tree T returning a pointer to the resulting tree
Node insertNode T int k
if T nullptr return new Nodek;
if k Tkey Tleft insertTleft, k;
else Tright insertTright, k;
fixsizeT;
return T;
insert value v at rank r
Node insertNode T int v int r
if T nullptr return new Nodev; If the tree is empty, create a new node with the value v and return it
int rankofroot Tleft Tleftsize : ; Calculate the rank of the root node
if r rankofroot If the rank is less than or equal to the rank of the root, insert the value in the left subtree
Tleft insertTleft, v r;
else If the rank is greater than the rank of the root, insert the value in the right subtree
Tright insertTright, v r rankofroot ;
fixsizeT; Update the size of the tree
return T; Return the modified tree
returns a vector of key values corresponding to the inorder traversal of T ie the contents of T in sorted order
vector inordertraversalNode T
vector inorder;
vector rhs;
if T nullptr return inorder;
inorderinordertraversalTleft;
inorder.pushbackTkey;
rhsinordertraversalTright;
inorder.insertinorderend rhsbegin rhsend;
return inorder;
return pointer to node of rank r with rth largest key; eg r is the minimum
Node selectNode T int r
assertTnullptr && r && rsize;
int rankofroot Tleft Tleftsize : ;
if r rankofroot return T;
if r rankofroot return selectTleft, r;
else return selectTright, r rankofroot ;
Split tree T on rank r into tree L containing ranks r and
a tree R containing ranks r
void splitNode T int r Node L Node R
Implement void splitNode T int r Node L Node R
insert value v at rank r
Node insertrandomNode T int v int r
If vr is the Nth node inserted into T then:
with probability N insert vr at the root of T
otherwise, insertrandom vr recursively left or right of the root of T
Implement Node insertrandomNode T int v int r
Returns true if team x defeated team y
Leave this function unmodified
bool didxbeatyint x int y
assert x y;
if x y return didxbeatyyx;
unsigned long long lx x;
unsigned long long ly y;
return lx ly;
Return a BST containing a valid ordering of n teams
Node ordernteamsint n
Node T nullptr;
start by inserting the first team
T insertrandomT;
now insert the other teams...
for int i; ikey can we insert at beginning?
T insertrandomT i;
else if didxbeatyselectTTsizekey, i can we insert at end?
T insertrandomT i Tsize;
else
when inserting team i
W W W L L L W L
return T;
void printVectorvector v
for int i; i inorder;
Node T nullptr;
test insert at beginning
for int i; i; i
T insertT i;
cout "Tree should contain :
;
inorderinordertraversalT;
printVectorinorder;
test insert at end
for int i; i; i
T insertT i Tsize;
cout "Tree should contain :
;
inorderinordertraversalT;
printVectorinorder;
test insert at middle
for int i; i; i
T insertT i Tsize;
cout "Tree should contain :
;
inorderinordertraversalT;
printVectorinorder;
once insert is working, the next step is to build the
insertrandom function to test this, just change
calls to insert above to insertrandom.
int N ; this should run quickly even for very large N
Node S ordernteamsN;
if S nullptr Ssize N
cout "Size of tree returned by ordernteams is wrong
;
else
cout "Team ordering:
;
inorderinordertraversalS;
printVectori
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
