Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello there! ,I have this data structure below of Binary search Tree.. I want the 4 exercises bottom, solved using the data structure i have

Hello there! ,I have this data structure below of Binary search Tree.. I want the 4 exercises bottom, solved using the data structure i have supported along with the methods already built-in.. thank you :D

#include

using namespace std;

const int n=11;

template

struct node

{

T data;

node *left;

node *right;

node(){left=right=0;}

node(T item){data = item;left=right=0;}

};

template

class btree

{

node *root;

void insert(T key, node *leaf)

{

if(key < leaf->data)

{

if(leaf->left!=NULL)

insert(key, leaf->left);

else

{

leaf->left=new node(key);

leaf->left->left=NULL; //Sets the left child of the child node to null

leaf->left->right=NULL; //Sets the right child of the child node to null

}

}

else if(key>=leaf->data)

{

if(leaf->right!=NULL)

insert(key, leaf->right);

else

{

leaf->right=new node(key);

leaf->right->left=NULL; //Sets the left child of the child node to null

leaf->right->right=NULL; //Sets the right child of the child node to null

}

}

}

void destroy(node* leaf) {

if(leaf!=NULL)

{

destroy(leaf->left);

destroy(leaf->right);

delete leaf;

}

}

node *search(T key, node *leaf)

{

if(leaf!=NULL)

{

if(key==leaf->data)

return leaf;

if(keydata)

return search(key, leaf->left);

else

return search(key, leaf->right);

}

else return NULL;

}

void inorder_print(node *leaf)

{

if(leaf != NULL)

{

inorder_print(leaf->left);

cout << leaf->data << " , ";

inorder_print(leaf->right);

}

}

node * minValueNode(node* sub)

{

node* current = sub;

/* loop down to find the leftmost leaf */

while (current && current->left != NULL)

current = current->left;

return current;

}

node* deleteNode(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 = deleteNode(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 = deleteNode(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;

delete root;

return temp;

}

else if (root->right == NULL)

{

node *temp = root->left;

delete 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 = deleteNode(root->right, temp->data);

}

return root;

}

public:

btree(){ root=NULL;}

~btree(){ destroy(root);}

void inorder()

{

inorder_print(root);

}

void insert(T key)

{

if(root!=NULL)

insert(key, root);

else

{

root=new node(key);

}

}

int search(T key)

{

node*q= search(key,root);

if(q==0)

return 0;

return 1;

}

void destroy()

{

destroy(root);

}

int deleteNode(T key)

{

if(deleteNode(root,key)==0)

return 0;

return 1;

}

bool empty()

{

return(root == NULL);

}

};

int main() {

btree bt;

bt.insert(50);

bt.insert(30);

bt.insert(20);

bt.insert(40);

bt.insert(70);

bt.insert(60);

bt.inorder();

bt.destroy();

bt.insert(80);

bt.insert(45);

bt.deleteNode(70);

cout<

bt.inorder();

if(bt.search(80))

cout<<" the item is found";

else

cout<<" the item is not Exist";

}

Exercise1: write an operator overload for = operation.

Exercise 2: write a code to check if the two binary trees are the same or not

Exercise 3: write a code to flip the right subtree to be the left and the left to be the right

Exercise 4: write a code to check if the tree is a BST or not

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning C# 5.0 Databases

Authors: Vidya Vrat Agarwal

2nd Edition

1430242604, 978-1430242604

More Books

Students also viewed these Databases questions

Question

Finding and scheduling appointments with new prospective clients.

Answered: 1 week ago