#ifndef BST_H #define BST_H // Representation of an element in the tree struct N
ID: 3586401 • Letter: #
Question
#ifndef BST_H
#define BST_H
// Representation of an element in the tree
struct Node {
int val; // Value of the node
Node *left; // Pointer to the left node
Node *right; // Pointer to the right node
Node *parent; // Pointer to the parent node
};
class BST {
// Public Definitions
public:
enum TraversalOrder { InOrderTrav, PreOrderTrav, PostOrderTrav };
// Public Functions/Variables
public:
BST(); // Constructor
virtual ~BST(); // Destructor
void Insert(int toInsert);
void Delete(int toDelete);
void Print(enum TraversalOrder);
// Private Functions/Variables
private:
Node *root;
Node* Search(int toFind); // Searche for a node in the tree
Node* Successor(Node *curr); // Find the successor of the given node
Node* Minimum(Node *curr); // Find the minimum node of the given subtree
Node* Maximum(Node *curr); // Find the minimum node of the given subtree
void Transplant(Node *u, Node *v); // Replace the subtree rooted at node u with the subtree rooted at node v
void InOrder(Node *curr); // Inorder traversal
void PreOrder(Node *curr); // Preorder traversal
void PostOrder(Node *curr); // Postorder traversal
};
#endif
--------------------------------------------------------------------
cpp file:
#include <iostream>
#include <string>
#include "BST.h"
using namespace std;
/****************************************************************
* CONSTRUCTOR
* Creates an empty binary tree
****************************************************************/
BST::BST() {
}
/****************************************************************
* DESTRUCTOR
* Free all memory used by current tree
****************************************************************/
BST::~BST() {
// Write code to remove and deallocate all nodes in the tree
}
void BST::Insert(int toInsert) {
// Write your code here
}
void BST::Delete(int toDelete) {
// Write your code here
}
void BST::Transplant(Node *u, Node *v) {
// Write your code here
}
Node *BST::Successor(Node *x) {
// Write your code here
}
Node *BST::Minimum(Node *x) {
// Write your code here
}
Node *BST::Maximum(Node *x) {
// Write your code here
}
Node *BST::Search(int toFind) {
// Write your code here
}
void BST::Print(TraversalOrder Order) {
if(Order==InOrderTrav)
InOrder(root);
else if(Order==PreOrderTrav)
PreOrder(root);
else if(Order==PostOrderTrav)
PostOrder(root);
}
void BST::PreOrder(Node *x) {
// Write your code here
}
void BST::InOrder(Node *x) {
// Write your code here
}
void BST::PostOrder(Node *x) {
// Write your code here
}
-------------------------------------------
main.cpp
#include <sstream>
#include <iostream>
#include "BST.h"
using namespace std;
int main(int argc,char **argv) {
// Create an empty Binary Search Tree
BST Tree;
// Using a fixed size buffer for reading content is not always safe,
// but ok here because we know how our input has to be:
char line[100];
// Main loop
while ( std::cin.getline(line,100) ) {
string str(line);
if ( str.size()==0 ) continue;
if ( str[0]=='e' ) return 0;
// Use cerr if you want to always print to the console, because cout
// will be redirected to the output file when calling the Grade05 script:
// cerr<<line<<endl;
if ( str.substr(0,2)=="in" ) {
Tree.Print(BST::InOrderTrav);
cout << "++++++++++++++++++++" << endl;
}
else {
if ( str.substr(0,3)=="pre" ) {
Tree.Print ( BST::PreOrderTrav );
cout << "++++++++++++++++++++" << endl;
}
else {
if (str.substr(0,4) == "post") {
Tree.Print(BST::PostOrderTrav);
cout << "++++++++++++++++++++" << endl;
}
else {
int key;
stringstream convert_stm(str.substr(2, str.size()-1));
if ( !(convert_stm>>key) ) key = -1;
if (str.substr(0,1) == "i") Tree.Insert(key);
else if (str.substr(0,1) == "d") Tree.Delete(key);
}
}
}
}
return 0;
}
Description In this lab your goal is to implement standard operations on binary search trees, including insert and delete. See section 12.3 in the textbook. A sample class structure with empty methods, is given in the support code. You can either use the given class structure or create your own. In this assignment the keys are integers. You will use Grade04 to test your code. Your execution file name must be "BST.exe". Refer to the previous lab assignments for instructions on how to use the grading tool The input contains one of the following commands on a line » i key: Insert key into the BST. For example, "i 2" means "Insert key 2 into the BST" » d key: Delete key from the BST. For example, "d 2" means "Delete key 2 from the BST" Do nothing if the BST does not contain the key Output all keys in preorder » post: Output all keys in postorder in: Output all keys in inorder Finish your program Examples of input and output Input 1 i 5 i 7 d 7 d 7 pre d 1 postExplanation / Answer
I have written the complete BST implementation in a single file. Here, you won't need to include the given class.
#include <sstream>
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
struct node
{
int key;
struct node *left, *right;
};
struct node *root = NULL;
struct node *newNode(int item)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
void inorder(struct node *root)
{
if (root != NULL)
{
inorder(root->left);
printf("%d ", root->key);
inorder(root->right);
}
}
void preorder(struct node *root)
{
if (root != NULL)
{
printf("%d ", root->key);
preorder(root->left);
preorder(root->right);
}
}
void postorder(struct node *root)
{
if (root != NULL)
{
postorder(root->left);
postorder(root->right);
printf("%d ", root->key);
}
}
struct node* insertutil(struct node* node, int key)
{
if (node == NULL) return newNode(key);
if (key < node->key)
node->left = insertutil(node->left, key);
else
node->right = insertutil(node->right, key);
return node;
}
struct node* insert(int key)
{
return insertutil(root, key);
}
struct node * minValueNode(struct node* node)
{
struct node* current = node;
while (current->left != NULL)
current = current->left;
return current;
}
struct node* deleteutil(struct node* root, int key)
{
if (root == NULL) return root;
if (key < root->key)
root->left = deleteutil(root->left, key);
else if (key > root->key)
root->right = deleteutil(root->right, key);
else
{
if (root->left == NULL)
{
struct node *temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL)
{
struct node *temp = root->left;
free(root);
return temp;
}
struct node* temp = minValueNode(root->right);
root->key = temp->key;
root->right = deleteutil(root->right, temp->key);
}
return root;
}
struct node* deleteNode(int key)
{
return deleteutil(root,key);
}
int main()
{
char line[100];
while ( std::cin.getline(line,100) ) {
string str(line);
if ( str.size()==0 ) continue;
if ( str[0]=='e' ) return 0;
// Use cerr if you want to always print to the console, because cout
// will be redirected to the output file when calling the Grade05 script:
// cerr<<line<<endl;
if ( str.substr(0,2)=="in" ) {
//Tree.Print(BST::InOrderTrav);
inorder(root);
cout << "++++++++++++++++++++" << endl;
}
else {
if ( str.substr(0,3)=="pre" ) {
preorder(root);
//Tree.Print ( BST::PreOrderTrav );
cout << "++++++++++++++++++++" << endl;
}
else {
if (str.substr(0,4) == "post") {
//Tree.Print(BST::PostOrderTrav);
postorder(root);
cout << "++++++++++++++++++++" << endl;
}
else {
int key;
stringstream convert_stm(str.substr(2, str.size()-1));
if ( !(convert_stm>>key) ) key = -1;
if (str.substr(0,1) == "i") insert(key);
else if (str.substr(0,1) == "d") deleteNode(key);
}
}
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.