Question

guys can you help me with finding a good Scenario of a c++ program which works...

guys can you help me with finding a good Scenario of a c++ program which works by using a BST (binary search tree) in scenario i would just call the functions of the BTC functions like adding deleting ,,, etc

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program

#include <iostream>
using namespace std;

//structure structure
struct TreeNode
{
int value;
TreeNode *left;
TreeNode *right;
};

//BinarySearchTree class
class BinarySearchTree
{
private:
TreeNode *root;
void insert(TreeNode *&, TreeNode *&);
void destroySubTree(TreeNode *);
void deleteNode(int, TreeNode *&);
void makeDeletion(TreeNode *&);
void printInOrder(TreeNode *);
void printPreOrder(TreeNode *);
void printPostOrder(TreeNode *);

public:
// Constructor
BinarySearchTree()
{ root = NULL; }
// Destructor
~BinarySearchTree()
{ destroySubTree(root); }
void insertNode(int);
bool searchNode(int);
void remove(int);
void printInOrder()
{ printInOrder(root); }
void printPreOrder()
{ printPreOrder(root); }
void printPostOrder()
{ printPostOrder(root); }
};

/* insert a node - helping function*/
void BinarySearchTree::insert(TreeNode *&ptr, TreeNode *&newNode)
{
if (ptr == NULL)
// Insert the node.
ptr = newNode;
else if (newNode->value < ptr->value)
// Search the left branch
insert(ptr->left, newNode);
else
// Search the right branch
insert(ptr->right, newNode);
}

/* insert a node*/
void BinarySearchTree::insertNode(int num)
{
TreeNode *newNode = NULL; // Pointer to a new node.

// Create a new node and store num in it.
newNode = new TreeNode;
newNode->value = num;
newNode->left = newNode->right = NULL;

// Insert the node.
insert(root, newNode);
}

/* delete the - helping function of destructor */
void BinarySearchTree::destroySubTree(TreeNode *ptr)
{
if (ptr->left)
destroySubTree(ptr->left);
if (ptr->right)
destroySubTree(ptr->right);
delete ptr;
}

/* search a node value num */
bool BinarySearchTree::searchNode(int num)
{
bool status = false;
TreeNode *ptr = root;

while (ptr){
if (ptr->value == num)
status = true;
else if (num < ptr->value)
ptr = ptr->left;
else
ptr = ptr->right;
}
return status;
}


/* deletes a node */
void BinarySearchTree::remove(int num)
{
deleteNode(num, root);
}

/* deletes a node - helping function*/
void BinarySearchTree::deleteNode(int num, TreeNode *&ptr)
{
if (num < ptr->value)
deleteNode(num, ptr->left);
else if (num > ptr->value)
deleteNode(num, ptr->right);
else
makeDeletion(ptr);
}


/* deletes the node - helping function */
void BinarySearchTree::makeDeletion(TreeNode *&ptr)
{
// Temporary pointer, used in reattaching the left subtree.
TreeNode *tempptr = NULL;

if (ptr == NULL)
cout << "Cannot delete empty node.\n";
else if (ptr->right == NULL)
{
tempptr = ptr;
ptr = ptr->left; // Reattach the left child
delete tempptr;
}
else if (ptr->left == NULL)
{
tempptr = ptr;
ptr = ptr->right; // Reattach the right child
delete tempptr;
}
// If the node has two children.
else
{
// Move one node the right.
tempptr = ptr->right;

// Go to the end left node.
while (tempptr->left)
{
tempptr = tempptr->left;
}

// Reattach the left subtree.
tempptr->left = ptr->left;
tempptr = ptr;

// Reattach the right subtree.
ptr = ptr->right;
delete tempptr;
}
}


/* inorder traversal */
void BinarySearchTree::printInOrder(TreeNode *ptr)
{
if (ptr)
{
printInOrder(ptr->left);
cout << ptr->value << " ";
printInOrder(ptr->right);
}
}


/* preorder traversal */
void BinarySearchTree::printPreOrder(TreeNode *ptr)
{
if (ptr)
{
cout << ptr->value << endl;
printPreOrder(ptr->left);
printPreOrder(ptr->right);
}
}

/* postorder traversal */
void BinarySearchTree::printPostOrder(TreeNode *ptr)
{
if (ptr)
{
printPostOrder(ptr->left);
printPostOrder(ptr->right);
cout << ptr->value << " ";
}
}


//main function
int main()
{
BinarySearchTree bt;

cout<<"Inserting nodes with 5, 8, 3, 12, 9, and 2."<<endl;

bt.insertNode(5);
bt.insertNode(8);
bt.insertNode(3);
bt.insertNode(12);
bt.insertNode(9);
bt.insertNode(2);

cout<<"\nHere are the values in the tree in order: "<<endl;

bt.printInOrder();


cout<<"\nNow deleting 8 from the tree:"<<endl;
bt.remove(8);

cout<<"\nNow deleting 12 from the tree:"<<endl;
bt.remove(12);

cout<<endl;

cout<<"Here are the values in the tree in order: "<<endl;

bt.printInOrder();

cout<<"\nHere are the values in the tree post order: "<<endl;

bt.printPostOrder();

return 0;
}

Output:

Inserting nodes with 5, 8, 3, 12, 9, and 2.

Here are the values in the tree in order:
2 3 5 8 9 12
Now deleting 8 from the tree:

Now deleting 12 from the tree:

Here are the values in the tree in order:
2 3 5 9
Here are the values in the tree post order:
2 3 9 5

Add a comment
Know the answer?
Add Answer to:
guys can you help me with finding a good Scenario of a c++ program which works...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT