IN C++, Using Notepad ++, Compiled in Developer Command Prompt for VS 2019. Please dont give me anything involving .hpp,
Posted: Fri Jul 01, 2022 5:42 am
IN C++, Using Notepad ++, Compiled in Developer Command Prompt for VS 2019. Please dont give me anything involving .hpp, i only need stuff for the .h, .cpp, and a main
Add the following functions to the bst.h/bst.cpp files:
Already done in class, but please include:
Insert a node given an integer into the tree.
Delete the node in the tree given an integer. If the node does not exist in the tree, then let the user know.
Print the values in the tree in ascending order.
You need to add: (Please use exact spelling and parameters so I can include it in my test program.
void printParenthetical(node* root); // Print the tree using parenthetical notation.
node* searchNode(node* root, int key); // Given an integer, return the node pointer for the node that has that integer as an attribute.
int findSmallest(node* root); // returns the smallest value in the BST.
int findLargest(node* root); // returns the smallest value in the BST.
You'll want to write your own main function to test your functions, but make sure it's in a separate file or commented out when you're done so I can include these files in my own test program. I'll write my own mainbst.cpp file that includes bst.cpp.
When you submit, I'll want the bst.h and bst.cpp files. If you write your own main program, you can submit it optionally.
bst.h code
#ifndef BST_H //BST == Binary Search Tree#define BST_H
struct node{int data;struct node* left;struct node* right;};
void insertNode(node*& root, int key);void deleteNode(node*& root, int key);void printAscending(node* root);
// Next functions are required for homework and weren't covered in videovoid printParenthetical(node* root);node* searchNode(node* root, int key);int findSmallest(node* root);int findLargest(node* root);#endif
bst.cpp code
#include "bst.h"#include <iostream>using namespace std;
void insertNode(node*& root, int key);{if(root == nullptr){root = new node;root->data = key;root->right = nullptr;root->left = nullptr;}else{if(root->data > key){insertNode(root->left, key);}else{insertNode(root->right, key);}}}
void deleteNode(node*& root, int key);{if(root == nullptr){cout << key << "Was not found in tree.\n\n";return;}else{if(key < root->data){deleteNode(root->left, key);}else if(key root->data{deleteNode(root->right, key);}else{if(root->left == nullptr){if(root->right == nullptr){delete root;root = nullptr;}else{dltPtr = root;root = root->right;delete dltPtr;}}else{if(root->right == nullptr){node* dltptr = root;root = root->left;delete dltPtr;}else{node* largestLeft = root->left;while(largestLeft->right != nullptr;){largestLeft = largestLeft->right;}root->data = largestLeft->data;deleteNode(root->left, largestLeft->data);}}}}}
void printAscending(node* root);{if(root != nullptr){printAscending(root->left);cout << root->data << " "printAscending(root->right);}}
Add the following functions to the bst.h/bst.cpp files:
Already done in class, but please include:
Insert a node given an integer into the tree.
Delete the node in the tree given an integer. If the node does not exist in the tree, then let the user know.
Print the values in the tree in ascending order.
You need to add: (Please use exact spelling and parameters so I can include it in my test program.
void printParenthetical(node* root); // Print the tree using parenthetical notation.
node* searchNode(node* root, int key); // Given an integer, return the node pointer for the node that has that integer as an attribute.
int findSmallest(node* root); // returns the smallest value in the BST.
int findLargest(node* root); // returns the smallest value in the BST.
You'll want to write your own main function to test your functions, but make sure it's in a separate file or commented out when you're done so I can include these files in my own test program. I'll write my own mainbst.cpp file that includes bst.cpp.
When you submit, I'll want the bst.h and bst.cpp files. If you write your own main program, you can submit it optionally.
bst.h code
#ifndef BST_H //BST == Binary Search Tree#define BST_H
struct node{int data;struct node* left;struct node* right;};
void insertNode(node*& root, int key);void deleteNode(node*& root, int key);void printAscending(node* root);
// Next functions are required for homework and weren't covered in videovoid printParenthetical(node* root);node* searchNode(node* root, int key);int findSmallest(node* root);int findLargest(node* root);#endif
bst.cpp code
#include "bst.h"#include <iostream>using namespace std;
void insertNode(node*& root, int key);{if(root == nullptr){root = new node;root->data = key;root->right = nullptr;root->left = nullptr;}else{if(root->data > key){insertNode(root->left, key);}else{insertNode(root->right, key);}}}
void deleteNode(node*& root, int key);{if(root == nullptr){cout << key << "Was not found in tree.\n\n";return;}else{if(key < root->data){deleteNode(root->left, key);}else if(key root->data{deleteNode(root->right, key);}else{if(root->left == nullptr){if(root->right == nullptr){delete root;root = nullptr;}else{dltPtr = root;root = root->right;delete dltPtr;}}else{if(root->right == nullptr){node* dltptr = root;root = root->left;delete dltPtr;}else{node* largestLeft = root->left;while(largestLeft->right != nullptr;){largestLeft = largestLeft->right;}root->data = largestLeft->data;deleteNode(root->left, largestLeft->data);}}}}}
void printAscending(node* root);{if(root != nullptr){printAscending(root->left);cout << root->data << " "printAscending(root->right);}}