Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please write in C++ code that needs to be modified: BSTree.cpp #include stdafx.h #include BSTree.h #include Node.h #include using namespace std; BSTree::BSTree(Node *r) { root

please write in C++

image text in transcribed

code that needs to be modified:

BSTree.cpp

#include "stdafx.h"

#include "BSTree.h"

#include "Node.h"

#include

using namespace std;

BSTree::BSTree(Node *r)

{

root = r;

}

bool BSTree::empty()

{

if (root == NULL)

return true;

else

return false;

}

bool BSTree::search(Node *root, int key)

{

if (root == NULL)

return false;

else

{

if (root->data == key)

return true;

else

{

if (root->data > key)

search(root->Left, key);

else

search(root->Right, key);

}

}

}

void BSTree::insert(Node *root, Node *n) //compare until you find the right place, takes 2 parameters inserted is n and other is root

{

if (root->data == n->data)

cout data

else

{

if (root->data > n->data)

{

if (root->Left == NULL)

{

root->Left = n;

n->Parent = root;

}

else

insert(root->Left, n);

}

else

{

if (root->Right == NULL)

{

root->Right = n;

n->Parent = root;

}

else

insert(root->Right, n);

}

}

}

void BSTree::remove(Node *n)

{

if ((n->Left == NULL) && (n->Right == NULL)) //leaf

{

if (n->Parent != NULL)

if (n->data > n->Parent->data)

n->Parent->Right = NULL;

else

n->Parent->Left = NULL;

}

else //root

{

root = NULL;

cout

}

if ((n->Right != NULL) && (n->Left == NULL))

{

if (n->data > n->Parent->data)

n->Parent->Right = n->Right;

else

n->Parent->Left = n->Right;

}

if ((n->Left != NULL) && (n->Right == NULL))

{

if (n->data > n->Parent->data)

n->Parent->Right = n->Left;

else

n->Parent->Left = n->Right;

}

Node *succ;

if ((n->Left != NULL) && (n->Right != NULL))

{

succ = n->Right;

while (succ->Left != NULL)

{

succ = succ->Left;

}

n->data = succ->data;

if (succ->Right != NULL)

succ->Parent->Left = succ->Right;

else

succ->Parent->Left = NULL;

}

}

void BSTree::inorder(Node *r)

{

if (r == NULL)

{

inorder(r->Left);

cout data

inorder(r->Right);

}

}

void BSTree::preorder(Node *r)

{

if (r != NULL)

{

cout data

inorder(r->Left);

preorder(r->Right);

}

}

void BSTree::postorder(Node *r)

{

if (r != NULL)

{

postorder(r->Left);

postorder(r->Right);

cout data

}

}

Node.cpp

#include "stdafx.h"

#include "Node.h"

Node::Node(int d)

{

data = d;

Parent = '\0';

Right = '\0';

Left = '\0';

}

BinarySearchTree.cpp

// BinarySearchTree.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include "BSTree.h"

#include "Node.h"

#include

using namespace std;

int main()

{

Node *n1 = new Node(46);

Node *n2 = new Node(55);

Node *n3 = new Node(63);

Node *n4 = new Node(12);

Node *n5 = new Node(2);

BSTree mytree(n1);

mytree.insert(n1, n2);

mytree.insert(n1, n3);

mytree.insert(n1, n4);

mytree.insert(n1, n5);

mytree.inorder(n1);

cout

mytree.inorder(n1);

cout

mytree.preorder(n1);

cout

mytree.postorder(n1);

cout

if (mytree.search(n1,12))

cout

else

cout

mytree.remove(n5);

mytree.inorder(n1);

return 0;

}

Node.h

#pragma once

class Node

{

public:

int data;

Node *Right;

Node *Left;

Node *Parent;

Node(int Data);

};

BSTree.h

#pragma once

#include"stdafx.h"

#include "Node.h"

class BSTree //class name and cpp should be the same

{

public:

Node *root;

BSTree(Node *);

bool empty();

bool search(Node *root, int key);

void insert(Node *root, Node *n);

void remove(Node *n);

void inorder(Node *r);

void preorder(Node *r);

void postorder(Node *r);

};

Modify the binary search tree project conducted in class so that the remove function implements the method of finding the "in-order predecessor" rather than the method of "in-order successor" 1. Modify the binary search tree project developed in (1) so that each node has two data fields: score and numher ofstudents The scores of a test are shown in the following table. The "number of students" field contains the number of students that have the same score. Review and modify all the member functions to accommodate this change (for example, the insert function and the display function). 2. 3. The driver shall (in the particular order specified below) a. Create a binary search tree based on the score; if the score to be inserted already exists, then increase the number of students. Score 76 80 95 77 659380 60 65 83 76 80 99 67 79 84 75 608195 b. c. d. Demonstrate all three traverse functions. Both score and number of students shall be displayed. Search through the BST to see whether score 65 exists, and how many students obtain that score Suppose the fourth student's score is changed from 77 to 83, modify the project to accommodate this change (e.g., you need to invoke the remove function, followed by invoking the insert function.) Display the BST using the three traverse functions afterwards

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

an element of formality in the workplace between different levels;

Answered: 1 week ago