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
  • Hello guys can you t help me with writing a c++ program using classes to for...

    Hello guys can you t help me with writing a c++ program using classes to for doing a program about BINARY SEARCH TREES (BST) which would be capable of these things : 1.insert data to the current place in the tree with the ability to add same data many times in the tree 2.delete data from the tree 3.list the data according to *Preorder*-*Inorder*-Postorder* ways 4.count the data in the tree in two ways  , first with counting the data that...

  • Using C Please comment Part 1: BST Create a link based Binary Search tree composed of a Node and a Tree struct. You should have a header file, BST.h, with the following: o Node struct containing...

    Using C Please comment Part 1: BST Create a link based Binary Search tree composed of a Node and a Tree struct. You should have a header file, BST.h, with the following: o Node struct containing left, right, and parent pointers, in addition to holding an Data struct value Tree struct containing a pointer to the root of the tree A function declaration for a function that allocates a tree, and initializes the root to NULL o o o A...

  • can anyone help me how to write binary search program in c++ that return multiple index.For...

    can anyone help me how to write binary search program in c++ that return multiple index.For instance if i have 124564.It should give the index of both for value 4 .

  • Can you help me with python question: Implement the function isBinarySearchTree(root), which returns True if the...

    Can you help me with python question: Implement the function isBinarySearchTree(root), which returns True if the binary tree passed to it is a valid binary search tree. I've provided a simple TreeNode class, and the beginnings of isBinarySearchTree(root). class TreeNode: '''Node for a simple binary tree structure''' def __init__(self, value, left, right): self.value = value self.left = left self.right = right    def isBinarySearchTree(tree):

  • 1. In Lab 4, you developed a program to build a Max Heap, and then Heap...

    1. In Lab 4, you developed a program to build a Max Heap, and then Heap Sort. Update the program by adding two additional functions: (a) AddData(A, N, V) where V is the new value added. (b) Delete a data Delete by giving the index of the data position Make sure to display the array after calling each of the function. 2. Write a program to implement Binary Search Algorithm, which will return the index of the data searched (V)....

  • I would just like to know how these are done! If you guys can show me...

    I would just like to know how these are done! If you guys can show me a formula or an example as to how these types of reactions are done that would be great! Thanks! a)Alkylation of an Ester b)Acid Catalyzed Hydrolysis of of an Amine

  • Can you guys please help me with this Percent crystallinity may also be determined using outputs...

    Can you guys please help me with this Percent crystallinity may also be determined using outputs from DSC. a. Explain how DSC works. b. Explain how you can determine percent crystallinity from DSC. c. Using this article: http://www.perkinelmer.com/Content/appicationnotes/app thermaicrystailinitythermo plastics.pdf and the information below. Determine the percent crystallinity in another PET sample: i. Delta H_m = 10J/g ii. Delta H_c =5J/g

  • using java to write,show me the output. please write some common. You CAN NOT use inbuild...

    using java to write,show me the output. please write some common. You CAN NOT use inbuild functions for Tree ADT operations. using code below to finsih public class Main {    public static void main(String[] args) {        BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); tree.root.right.left = new Node(6); tree.root.right.right = new Node(7); tree.root.left.left.left = new Node(8); tree.root.left.left .right= new Node(9);...

  • Can you upload a program to the Arudino and then call functions you uploaded from c#? ie: if you ...

    Can you upload a program to the Arudino and then call functions you uploaded from c#? ie: if you have an LED strip with various lighting functions in your code (flashing, strobe, etc) can I just simply call upon these functions from c# code to already uploaded code on the arduino  or do i need to re-upload the function each time

  • Please help me with this C++ program, thank you!!! You are to develop some binary tree...

    Please help me with this C++ program, thank you!!! You are to develop some binary tree routines that will handle single words. The binary tree is to be maintain as an ordered tree. The routines you need are: ADD (add a new word to tree, do not allow duplicates), DELETE ( deletes a word out of the tree), SEARCH (look up a word in the tree and indicate the word is in the structure or not) TRAVERSE ( inorder, preorder,...

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