SlideShare a Scribd company logo
Help to implement delete_node, get_succ, get_pred, walk, and tree_search in the script
following the given structure:
#include "bst.h"
// ---------------------------------------
// Node class
// Default constructor
Node::Node() {
// TODO: Implement this
key = 0;
parent = nullptr;
left = nullptr;
right = nullptr;
}
// Constructor
Node::Node(int in) {
// TODO: Implement this
key = in;
parent = nullptr;
left = nullptr;
right = nullptr;
}
// Destructor
Node::~Node() {
// TODO: Implement this
delete left;
delete right;
}
// Add parent
void Node::add_parent(Node* in) {
// TODO: Implement this
parent = in;
}
// Add to left of current node
void Node::add_left(Node* in) {
// TODO: Implement this
left = in;
if (in != nullptr) {
in->add_parent(this);
}
}
// Add to right of current node
void Node::add_right(Node* in) {
// TODO: Implement this
right = in;
if (in != nullptr) {
in->add_parent(this);
}
}
// Get key
int Node::get_key()
{
// TODO: Implement this
return key;
}
// Get parent node
Node* Node::get_parent()
{
// TODO: Implement this
return parent;
}
// Get left node
Node* Node::get_left()
{
// TODO: Implement this
return left;
}
// Get right node
Node* Node::get_right()
{
// TODO: Implement this
return right;
}
// Print the key to ostream to
// Do not change this
void Node::print_info(ostream& to)
{
to << key << endl;
}
// ---------------------------------------
// ---------------------------------------
// BST class
// Walk the subtree from the given node
void BST::inorder_walk(Node* in, ostream& to)
{
// TODO: Implement this
if (in != nullptr) {
inorder_walk(in->get_left(), to);
in->print_info(to);
inorder_walk(in->get_right(), to);
}
}
// Constructor
BST::BST()
{
// TODO: Implement this
root = nullptr;
}
// Destructor
BST::~BST()
{
// TODO: Implement this
delete root;
}
// Insert a node to the subtree
void BST::insert_node(Node* in)
{
// TODO: Implement this
Node* curr = root;
Node* par = nullptr;
while (curr != nullptr) {
par = curr;
if (in->get_key() < curr->get_key()) {
curr = curr->get_left();
} else {
curr = curr->get_right();
}
}
in->add_parent(par);
if (par == nullptr) {
root = in;
} else if (in->get_key() < par->get_key()) {
par->add_left(in);
} else {
par->add_right(in);
}
}
// Delete a node to the subtree
void BST::delete_node(Node* out)
{
// TODO: Implement this
}
// minimum key in the BST
Node* BST::tree_min()
{
// TODO: Implement this
return get_min(root);
}
// maximum key in the BST
Node* BST::tree_max()
{
// TODO: Implement this
return get_max(root);
}
// Get the minimum node from the subtree of given node
Node* BST::get_min(Node* in)
{
// TODO: Implement this
if (in == nullptr) {
return nullptr;
}
while (in->get_left() != nullptr) {
in = in->get_left();
}
return in;
}
// Get the maximum node from the subtree of given node
Node* BST::get_max(Node* in)
{
// TODO: Implement this
if (in == nullptr) {
return nullptr;
}
while (in->get_right() != nullptr) {
in = in->get_right();
}
return in;
}
// Get successor of the given node
Node* BST::get_succ(Node* in)
{
// TODO: Implement this
}
// Get predecessor of the given node
Node* BST::get_pred(Node* in)
{
// TODO: Implement this
}
// Walk the BST from min to max
void BST::walk(ostream& to)
{
// TODO: Implement this
}
// Search the tree for a given key
Node* BST::tree_search(int search_key)
{
// TODO: Implement this
}
// ---------------------------------------
bst.h
#ifndef BST_H_
#define BST_H_
#include <iostream>
using namespace std;
class Node {
private:
int key;
Node* parent;
Node* left;
Node* right;
public:
// Default constructor
Node();
// Constructor
Node(int in);
// Destructor
// a virtual constructor is required for inheritance
virtual ~Node();
// Add to parent of current node
void add_parent(Node* in);
// Add to left of current node
void add_left(Node* in);
// Add to right of current node
void add_right(Node* in);
// Get key
int get_key();
// Get parent node
Node* get_parent();
// Get left node
Node* get_left();
// Get right node
Node* get_right();
virtual void print_info(ostream& to);
};
class BST {
private:
Node* root;
// Walk the subtree from the given node
void inorder_walk(Node* in, ostream& to);
// Get the minimum node from the subtree of given node
Node* get_min(Node* in);
// Get the maximum node from the subtree of given node
Node* get_max(Node* in);
public:
// Constructor and Destructor
BST();
~BST();
// Modify tree
void insert_node(Node* in);
void delete_node(Node* out);
// Find nodes in the tree
Node* tree_min(); // minimum key value
Node* tree_max(); // maximum key value
Node* get_succ(Node* in); // successor for a given node
Node* get_pred(Node* in); // predecessor for a given node
void walk(ostream& to); // Traverse the tree from min to max recursively
Node* tree_search(int search_key);
};
#endif

More Related Content

PDF
Help implement BST- The code must follow the instruction below as well.pdf
PDF
In C++ Follow all code styling and submission instructions a.pdf
PDF
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
PDF
In c++ format, for each function in the code, please using the comme.pdf
PDF
Tree Traversals A tree traversal is the process of visiting.pdf
PDF
Can you finish and write the int main for the code according to the in.pdf
PDF
This is a c++ binary search program I worked so far but still cant g.pdf
DOCX
Binary Tree in C++ coding in the data structure
Help implement BST- The code must follow the instruction below as well.pdf
In C++ Follow all code styling and submission instructions a.pdf
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
In c++ format, for each function in the code, please using the comme.pdf
Tree Traversals A tree traversal is the process of visiting.pdf
Can you finish and write the int main for the code according to the in.pdf
This is a c++ binary search program I worked so far but still cant g.pdf
Binary Tree in C++ coding in the data structure

Similar to Help to implement delete_node get_succ get_pred walk and.pdf (20)

PDF
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdf
PDF
MAINCPP include ltiostreamgt include ltstringgt u.pdf
PDF
I have C++ question that I do not know how to do, Can you teach me t.pdf
PDF
Please write the C++ code that would display the exact same output a.pdf
PPT
computer notes - Data Structures - 16
PDF
8 chapter4 trees_bst
PPTX
Binary Search Tree.pptxA binary search i
PPTX
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
PDF
I have a Programming is Binary Tree Search (BTS) for strings with po.pdf
DOCX
Biary search Tree.docx
PDF
Tree and binary tree
PDF
In c++ format please, for each function in the code, using the comme.pdf
PPT
17 Trees.ppt DSADSADSADSADSADSADSADSADSA
PPTX
Lecture_10 - Revised.pptx
PPT
binary search tree
PPT
1.2 operations of tree representations
PPTX
BST.pptx this is Good for data structure
PPTX
BST.pptx this isp used for learning binary search trees
PDF
Implement a function TNode copy_tree(TNode t) that creates a copy .pdf
PDF
JavaCreate a java application using the code example in the follo.pdf
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdf
MAINCPP include ltiostreamgt include ltstringgt u.pdf
I have C++ question that I do not know how to do, Can you teach me t.pdf
Please write the C++ code that would display the exact same output a.pdf
computer notes - Data Structures - 16
8 chapter4 trees_bst
Binary Search Tree.pptxA binary search i
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
I have a Programming is Binary Tree Search (BTS) for strings with po.pdf
Biary search Tree.docx
Tree and binary tree
In c++ format please, for each function in the code, using the comme.pdf
17 Trees.ppt DSADSADSADSADSADSADSADSADSA
Lecture_10 - Revised.pptx
binary search tree
1.2 operations of tree representations
BST.pptx this is Good for data structure
BST.pptx this isp used for learning binary search trees
Implement a function TNode copy_tree(TNode t) that creates a copy .pdf
JavaCreate a java application using the code example in the follo.pdf
Ad

More from contact32 (20)

PDF
From the following list of accounts calculate the quick rati.pdf
PDF
Errors are defined as An injury caused by medical managemen.pdf
PDF
ESTUDIO DE CASO SNCLAVALIN GROUP INC Por Steven L McShan.pdf
PDF
Deliverables Your deliverable in this assignment is to d.pdf
PDF
Diana and Ryan Workman were married on January 1 of last yea.pdf
PDF
6 15 points Let Ntt0 be a Poisson process with rate .pdf
PDF
Bario Corporation ha proporcionado los siguientes datos sobr.pdf
PDF
Caratinin 2 it Yoe may alos amune the itadaiand joypertin o.pdf
PDF
A function annotation also known as a type hint serves to .pdf
PDF
Beckenworth had the following purchases and sales during the.pdf
PDF
Part 1 True or False 16 marks Write True or false for th.pdf
PDF
Which of the following is the correct sequence for the germi.pdf
PDF
Oriole Corporation issues 340000 of bonds for 343400 a.pdf
PDF
You and a team of diverse scientists and astronauts have bee.pdf
PDF
You are working to your full scope as a Certificate IV gradu.pdf
PDF
Teresa has a deck of 10 cards numbered 1 through 10 She is.pdf
PDF
Tore ne to the irrit Blued fequiredb Eupeoce that Chajs.pdf
PDF
What gets said if I run the script below The variable globa.pdf
PDF
include ltiostreamgt include ltstringgt include .pdf
PDF
21 Ribozom antlamas ve dahili ribozom balangcnn ortak nokta.pdf
From the following list of accounts calculate the quick rati.pdf
Errors are defined as An injury caused by medical managemen.pdf
ESTUDIO DE CASO SNCLAVALIN GROUP INC Por Steven L McShan.pdf
Deliverables Your deliverable in this assignment is to d.pdf
Diana and Ryan Workman were married on January 1 of last yea.pdf
6 15 points Let Ntt0 be a Poisson process with rate .pdf
Bario Corporation ha proporcionado los siguientes datos sobr.pdf
Caratinin 2 it Yoe may alos amune the itadaiand joypertin o.pdf
A function annotation also known as a type hint serves to .pdf
Beckenworth had the following purchases and sales during the.pdf
Part 1 True or False 16 marks Write True or false for th.pdf
Which of the following is the correct sequence for the germi.pdf
Oriole Corporation issues 340000 of bonds for 343400 a.pdf
You and a team of diverse scientists and astronauts have bee.pdf
You are working to your full scope as a Certificate IV gradu.pdf
Teresa has a deck of 10 cards numbered 1 through 10 She is.pdf
Tore ne to the irrit Blued fequiredb Eupeoce that Chajs.pdf
What gets said if I run the script below The variable globa.pdf
include ltiostreamgt include ltstringgt include .pdf
21 Ribozom antlamas ve dahili ribozom balangcnn ortak nokta.pdf
Ad

Recently uploaded (20)

PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Pre independence Education in Inndia.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Lesson notes of climatology university.
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Computing-Curriculum for Schools in Ghana
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Institutional Correction lecture only . . .
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Basic Mud Logging Guide for educational purpose
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Sports Quiz easy sports quiz sports quiz
Module 4: Burden of Disease Tutorial Slides S2 2025
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Pre independence Education in Inndia.pdf
Cell Structure & Organelles in detailed.
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPH.pptx obstetrics and gynecology in nursing
01-Introduction-to-Information-Management.pdf
Lesson notes of climatology university.
Final Presentation General Medicine 03-08-2024.pptx
O5-L3 Freight Transport Ops (International) V1.pdf
Computing-Curriculum for Schools in Ghana
Abdominal Access Techniques with Prof. Dr. R K Mishra
Institutional Correction lecture only . . .
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Basic Mud Logging Guide for educational purpose
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx

Help to implement delete_node get_succ get_pred walk and.pdf

  • 1. Help to implement delete_node, get_succ, get_pred, walk, and tree_search in the script following the given structure: #include "bst.h" // --------------------------------------- // Node class // Default constructor Node::Node() { // TODO: Implement this key = 0; parent = nullptr; left = nullptr; right = nullptr; } // Constructor Node::Node(int in) { // TODO: Implement this key = in; parent = nullptr; left = nullptr; right = nullptr; } // Destructor Node::~Node() { // TODO: Implement this delete left; delete right; } // Add parent void Node::add_parent(Node* in) { // TODO: Implement this parent = in; } // Add to left of current node void Node::add_left(Node* in) { // TODO: Implement this left = in; if (in != nullptr) { in->add_parent(this); } } // Add to right of current node void Node::add_right(Node* in) {
  • 2. // TODO: Implement this right = in; if (in != nullptr) { in->add_parent(this); } } // Get key int Node::get_key() { // TODO: Implement this return key; } // Get parent node Node* Node::get_parent() { // TODO: Implement this return parent; } // Get left node Node* Node::get_left() { // TODO: Implement this return left; } // Get right node Node* Node::get_right() { // TODO: Implement this return right; } // Print the key to ostream to // Do not change this void Node::print_info(ostream& to) { to << key << endl; } // --------------------------------------- // --------------------------------------- // BST class // Walk the subtree from the given node void BST::inorder_walk(Node* in, ostream& to)
  • 3. { // TODO: Implement this if (in != nullptr) { inorder_walk(in->get_left(), to); in->print_info(to); inorder_walk(in->get_right(), to); } } // Constructor BST::BST() { // TODO: Implement this root = nullptr; } // Destructor BST::~BST() { // TODO: Implement this delete root; } // Insert a node to the subtree void BST::insert_node(Node* in) { // TODO: Implement this Node* curr = root; Node* par = nullptr; while (curr != nullptr) { par = curr; if (in->get_key() < curr->get_key()) { curr = curr->get_left(); } else { curr = curr->get_right(); } } in->add_parent(par); if (par == nullptr) { root = in; } else if (in->get_key() < par->get_key()) { par->add_left(in); } else { par->add_right(in); }
  • 4. } // Delete a node to the subtree void BST::delete_node(Node* out) { // TODO: Implement this } // minimum key in the BST Node* BST::tree_min() { // TODO: Implement this return get_min(root); } // maximum key in the BST Node* BST::tree_max() { // TODO: Implement this return get_max(root); } // Get the minimum node from the subtree of given node Node* BST::get_min(Node* in) { // TODO: Implement this if (in == nullptr) { return nullptr; } while (in->get_left() != nullptr) { in = in->get_left(); } return in; } // Get the maximum node from the subtree of given node Node* BST::get_max(Node* in) { // TODO: Implement this if (in == nullptr) { return nullptr; } while (in->get_right() != nullptr) { in = in->get_right(); } return in; }
  • 5. // Get successor of the given node Node* BST::get_succ(Node* in) { // TODO: Implement this } // Get predecessor of the given node Node* BST::get_pred(Node* in) { // TODO: Implement this } // Walk the BST from min to max void BST::walk(ostream& to) { // TODO: Implement this } // Search the tree for a given key Node* BST::tree_search(int search_key) { // TODO: Implement this } // --------------------------------------- bst.h #ifndef BST_H_ #define BST_H_ #include <iostream> using namespace std; class Node { private: int key; Node* parent; Node* left; Node* right; public: // Default constructor Node(); // Constructor Node(int in); // Destructor // a virtual constructor is required for inheritance virtual ~Node(); // Add to parent of current node void add_parent(Node* in);
  • 6. // Add to left of current node void add_left(Node* in); // Add to right of current node void add_right(Node* in); // Get key int get_key(); // Get parent node Node* get_parent(); // Get left node Node* get_left(); // Get right node Node* get_right(); virtual void print_info(ostream& to); }; class BST { private: Node* root; // Walk the subtree from the given node void inorder_walk(Node* in, ostream& to); // Get the minimum node from the subtree of given node Node* get_min(Node* in); // Get the maximum node from the subtree of given node Node* get_max(Node* in); public: // Constructor and Destructor BST(); ~BST(); // Modify tree void insert_node(Node* in); void delete_node(Node* out); // Find nodes in the tree Node* tree_min(); // minimum key value Node* tree_max(); // maximum key value Node* get_succ(Node* in); // successor for a given node Node* get_pred(Node* in); // predecessor for a given node void walk(ostream& to); // Traverse the tree from min to max recursively Node* tree_search(int search_key); }; #endif