Implement insert() in tree.cpp and show the results of inserting
2, 1, 4, 5, 9, 3, 6, 7, 10, 12, 11 into an empty Binary search
tree.
2.Start with the tree in problem 1 and implement delete_node() and
do:
2.1.delete 4, then
2.2.delete 9.
3.Start with the tree in problem 2 and implement search() and
do:
3.1.Search 12
3.2.Search 4
#include <iostream>
#include <cstddef>
using std::cout;
using std::endl;
class Node {
int value;
public:
Node* left; // left child
Node* right; // right child
Node* p; // parent
Node(int data) {
value = data;
left = NULL;
right = NULL;
p = NULL;
}
~Node() {
}
int d() {
return value;
}
void print() {
std::cout << value << std::endl;
}
};
int main(int argc, const char * argv[])
{
}
function insert(Node *insert_node, Node *tree_root){
//Your code here
}
function delete_node(int value, Node *tree_root){
//Your code here
}
function search(int value, Node *tree_root){
//Your code here
}
#include <iostream>
#include<cstdlib>
using namespace std;
class NODE
{
int data;
NODE *left, *right;
public:
// Default constructor.
NODE();
// Parameterized constructor.
NODE(int);
// Insert function.
NODE* Insert(NODE *, int);
NODE* Search(NODE *, int);
NODE* Delete(NODE *, int);
NODE* minValueNode(NODE *);
void Inorder(NODE *);
};
// Default Constructor definition.
NODE :: NODE() : data(0), left(NULL), right(NULL){}
// Parameterized Constructor definition.
NODE :: NODE(int value)
{
data = value;
left = right = NULL;
}
// Insert function definition.
NODE* NODE :: Insert(NODE *root, int value)
{
if(!root)
{
// Insert the first node, if root is NULL.
return new NODE(value);
}
// Insert data.
if(value > root->data)
{
// Insert right node data, if the 'value'
// to be inserted is greater than 'root' node data.
// Process right nodes.
root->right = Insert(root->right, value);
}
else
{
// Insert left node data, if the 'value'
// to be inserted is greater than 'root' node data.
// Process left nodes.
root->left = Insert(root->left, value);
}
// Return 'root' node, after insertion.
return root;
}
NODE* NODE :: minValueNode(NODE *node)
{
NODE *current = node;
/* loop down to find the leftmost leaf */
while (current && current->left != NULL)
current = current->left;
return current;
}
NODE* NODE :: Delete(NODE *root, int key)
{
// base case
if (root == NULL) return root;
// If the key to be deleted is smaller than the root's key,
// then it lies in left subtree
if (key < root->data)
root->left = Delete(root->left, key);
// If the key to be deleted is greater than the root's key,
// then it lies in right subtree
else if (key > root->data)
root->right = Delete(root->right, key);
// if key is same as root's key, then This is the node
// to be deleted
else
{
// node with only one child or no child
if (root->left == NULL)
{
NODE *temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL)
{
NODE *temp = root->left;
free(root);
return temp;
}
// node with two children: Get the inorder successor (smallest
// in the right subtree)
NODE *temp = minValueNode(root->right);
// Copy the inorder successor's content to this node
root->data = temp->data;
// Delete the inorder successor
root->right = Delete(root->right, temp->data);
}
return root;
}
void NODE :: Inorder(NODE *root)
{
if(!root)
{
return;
}
Inorder(root->left);
cout << root->data << endl;
Inorder(root->right);
}
NODE* NODE :: Search(NODE *root, int value)
{
// Base Cases: root is null or value is present at root
if (root == NULL || root->data == value)
return root;
// value is greater than root's value
if (root->data < value)
return Search(root->right, value);
// value is smaller than root's value
return Search(root->left, value);
}
// Driver code
int main()
{
NODE b, *root = NULL;
root = b.Insert(root, 2);
b.Insert(root, 4);
b.Inorder(root);
b.Insert(root, 5);
b.Inorder(root);
b.Insert(root, 9);
b.Inorder(root);
b.Insert(root, 3);
b.Inorder(root);
b.Insert(root, 6);
b.Inorder(root);
b.Insert(root, 7);
b.Inorder(root);
b.Insert(root, 10);
b.Inorder(root);
b.Insert(root, 12);
b.Inorder(root);
b.Insert(root, 11);
b.Inorder(root);
b.Delete(root, 4);
b.Inorder(root);
b.Delete(root, 9);
b.Inorder(root);
b.Search(root, 12);
b.Search(root, 4);
return 0;
}
Implement insert() in tree.cpp and show the results of inserting 2, 1, 4, 5, 9, 3,...
#include <iostream> #include <cstddef> using std::cout; using std::endl; class Node { int value; public: Node* left; // left child Node* right; // right child Node* p; // parent Node(int data) { value = data; left = NULL; right = NULL; p = NULL; } ~Node() { } int d() { return value; } void print() { std::cout << value << std::endl; } }; int main(int argc, const char * argv[]) { } function insert(Node *insert_node, Node *tree_root){ //Your code here...
Implement downHeap(Node *n) for a min heap implemented as an ordinary binary tree with an integer member variable "value" and left and right child pointers. Any node might have only a left child, only a right child, both, or neither. The starter code below defines a class called "Node" that has two child pointers ("left" , "right") and an integer "value" member variable. There is also a constructor Node(int val) that initializes the children to nullptr and the node's value...
C++ Binary tree, help pls #pragma once // include this library to use NULL, otherwise use nullptr instead #include <cstddef> #include <iostream> #include "node.hpp" template<class T> class BST{ public: // Constructor for the BST class, creates an empty tree BST(void); // Destructor for the BST class, destroys the tree ~BST(void); // Inserts data into the tree // param: The data to be inserted into the tree void insert(T); // Removes data from the tree // param: The data to be...
3. (Gaddis Exercises 20.4) Tree Height Write a recursive member function for the BinaryTree class that returns the height of the tree. The height of the tree is the number of levels it contains. Demonstrate the function in a driver program. CPP FILE CODE: #include "BinaryTree.h" #include <iostream> using namespace std; BinaryTree::BinaryTree() { root = NULL; } BinaryTree::~BinaryTree() { destroy(root); } bool BinaryTree::search(int data) { return search(data, root); } void BinaryTree::insert(int data) { insert(data, root); } void BinaryTree::traverseInOrder() { traverseInOrder(root);...
Having code issues wth my C++ program. My program checks if two binary trees are similar and if they're not they return false. My program is return true with different binary trees. Could use some help thanks #include <iostream> #include <string> using namespace std; //Struct of Nodes struct BinarySearchTree { int data; BinarySearchTree *left; BinarySearchTree *right; }; // Inserting nodes into BST BinarySearchTree* insert( BinarySearchTree* node, int val) { if (node == NULL) { BinarySearchTree *newNode = new BinarySearchTree(); newNode->data...
C++: PLEASE HELP~!!! ~~~~~~~~ Implement bool AVLTree::deleteNode(string ss) method. Function deleteNode() tries to delete the node containing value ss. If there is no such node, it returns false. Otherwise, it deletes the node, check the balance of the tree, rebalance the tree if it is necessary. When you delete a node, consider three different scenarios: -The node is a leaf -The node has only ONE child subtree -The node has two child subtrees Important: When you implement this project, do...
C++, implement the bst.cpp file without adding any additional methods or functions to it ======================================================================== // bst.cpp #include <iostream> #include "bst.h" using namespace std; BinarySearchTree::BinarySearchTree() { root = nullptr; } void BinarySearchTree::insert(int key, string val) { Node* new_node = new Node; new_node->key = key; new_node->val = val; new_node->left = nullptr; new_node->right = nullptr; if (root == nullptr) { root = new_node; } else { insertHelper(root, new_node); } } void BinarySearchTree::insertHelper(Node* parent, Node* new_node) { if (new_node->key < parent->key) { if...
Hi there, I am working on a binary search tree code in c++. The program must store and update students' academic records, each node includes the student name, credits attempted, credits earned and GPA. I have made some progress with the code and written most of the functions in the .cpp file (already did the .h file) but i am struggling with what to put in the main and how to put an update part in the insert function. I...
Language: C++ ○ For this question I task you with creating an Iterative Search function that works with the existing binary search tree of type string. This function will be inputted a variable of type string and will search for it within the existing and pre-declared binary tree. ○ The function appears just above the main, and is simply a placeholder. It can be altered as you wish. ○ Within the Main I have a simple program to prompt for...
Need this in C++ Goals: Your task is to implement a binary search tree of linked lists of movies. Tree nodes will contain a letter of the alphabet and a linked list. The linked list will be an alphabetically sorted list of movies which start with that letter. MovieTree() ➔ Constructor: Initialize any member variables of the class to default ~MovieTree() ➔ Destructor: Free all memory that was allocated void printMovieInventory() ➔ Print every movie in the data structure in...