#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
}
//C ++ program
#include<iostream>
using namespace std;
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;
}
void setValue(int v){
value = v;
}
};
Node* insert(Node *insert_node, Node *tree_root){
if (tree_root == NULL) return insert_node;
if (insert_node->d() < tree_root->d())
tree_root->left = insert(insert_node,tree_root->left);
else if (insert_node->d() > tree_root->d())
tree_root->right = insert(insert_node,tree_root->right);
return tree_root;
}
Node*findmin(Node*tree_root){
while(tree_root&&tree_root->left){
tree_root=tree_root->left;
}
return tree_root;
}
Node* delete_node(int value, Node *tree_root){
Node*temp;
if(!tree_root)return NULL;
else
if(tree_root->d()<value)tree_root->right=delete_node(value,tree_root->right);
else
if(tree_root->d()>value)tree_root->left=delete_node(value,tree_root->left);
else if(tree_root->d()==value){
if(!tree_root->left){
temp=tree_root->right;
delete(tree_root);
return
temp;
}
if(!tree_root->right){
temp=tree_root->left;
delete(tree_root);
return
temp;
}
temp=findmin(tree_root->right);
tree_root->setValue(temp->d());
tree_root->right=delete_node(temp->d(),tree_root->right);
}
else
cout<<"key not
found\n";
return tree_root;
}
Node* search(int value, Node *tree_root){
if (tree_root == NULL || tree_root->d() == value)
return tree_root;
// Key is greater than root's key
if (tree_root->d() < value)
return search(value,tree_root->right);
// Key is smaller than root's key
return search(value,tree_root->left);
}
void inorder(Node*tree_root){
if(tree_root==NULL)return;
inorder(tree_root->left);
tree_root->print();
inorder(tree_root->right);
}
int main(int argc, const char * argv[])
{
int arr[] = {2, 1, 4, 5, 9, 3, 6, 7, 10, 12, 11};
int n = sizeof(arr)/sizeof(arr[0]);
Node*tree = NULL;
for(int i=0;i<n;i++){
tree = insert(new Node(arr[i]),tree);
}
cout<<"Inorder Traversal :\n";
inorder(tree);
tree = delete_node(4,tree);
cout<<"\nInorder Traversal after deleting
4:\n";
inorder(tree);
tree = delete_node(9,tree);
cout<<"\nInorder Traversal after deleting
9:\n";
inorder(tree);
if(search(12,tree))cout<<"\n12 found in
tree\n";
else cout<<"\n12 not found in tree\n";
return 0;
}
//sample output

#include <iostream> #include <cstddef> using std::cout; using std::endl; class Node { int value; public: Node* left;...
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*...
#include <iostream> #include <string> using std::string; using std::cout; using std::endl; void testAnswer(string testname, int answer, int expected) { if (answer == expected) cout << "PASSED: " << testname << " expected and returned " << answer << "\n"; else cout << "FAILED: " << testname << " returned " << answer << " but expected " << expected << "\n"; } // Implement printArray here void printArray(int array[],int b) { for(int i = 0; i<b;i++) { std::cout<< array[i] << "...
#include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0; i<expr.length(); i++){ //Insert code here to push each character onto the stack } cout << "My stack is popped in this order" << endl; while(!myStack.empty()){ //Insert code here to cout the top of the stack one by one //Pop each one after it’s printed out } cout << endl; } void printFromQueue(string expr){ queue<char> myQueue; //Insert code here to push each character onto the...
#include <iostream> using namespace std; class BinarySearchTree{ public: int size; int *array; BinarySearchTree(int size){ int newSize = extendSize(size); array = new int[newSize]; for(int x = 0; x < size; ++x){ array[x] = NULL; } } } Given the code above implement the following: 5. Write a function that displays all...
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...
Using the following implementation of Tree class Node { public int iData; // data item (key) public double dData; // data item public Node leftChild; // this node's left child public Node rightChild; // this node's right child public void displayNode() // display ourself { System.out.print('{'); System.out.print(iData); System.out.print(", "); System.out.print(dData); System.out.print("} "); } } // end class Node //------------------------------------------------------------------ import java.io.IOException; import java.util.Stack; public class Tree { private Node root; // first node of tree // ------------------------------------------------------------- public Tree() // constructor { root = null; }...
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);...
#include <iostream> #include <vector> #include <fstream> #include <time.h> #include <chrono> #include <sstream> #include <algorithm> class Clock { private: std::chrono::high_resolution_clock::time_point start; public: void Reset() { start = std::chrono::high_resolution_clock::now(); } double CurrentTime() { auto end = std::chrono::high_resolution_clock::now(); double elapsed_us = std::chrono::duration std::micro>(end - start).count(); return elapsed_us; } }; class books{ private: std::string type; int ISBN; public: void setIsbn(int x) { ISBN = x; } void setType(std::string y) { type = y; } int putIsbn() { return ISBN; } std::string putType() { return...
C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public: Node(int val); int value; Node* next; }; Node::Node(int val){ value = val; } class List { public: List(); // Uncomment the line below once you're ready List(List &other); void push_front(int value); bool pop_front(int &value); void push_back(int value); bool pop_back(int &value); int at(int index); void insert_at(int index, int value); void remove_at(int index); int size(); private: // other members you may have used Node* head; Node*...