Question

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 is repeated and second with out counting the data that’s is repeated

5. check the balance of the tree

6. display the tree structure of the program on the screen

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

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <math.h>

using namespace std;

struct Node{
int key;
Node* left, *right;
};

class BinarySearchTree{
public:
BinarySearchTree(){
    root = nullptr;
}

~BinarySearchTree(){
    deleteTree(&root);
}

void insert(int key){
    doInsert(&root, key);
}

void display(){
    cout<<"Preorder: ";
    preOrder(root);
    cout<<endl;
    cout<<"Inorder: ";
    inOrder(root);
    cout<<endl;
    cout<<"Postorder: ";
    postOrder(root);
    cout<<endl;
}

int countWithRepeated(){
    return doCountWithRepeated(root);
}

int countWithNotRepeated(){
    vector<int> keys;
    doCountWithNotRepeated(root, keys);
    return keys.size();
}

bool isBalanced(){
    return isBalanced(root);
}

void printTreeStructure(){
    printTreeStructure("", root, false);
}

private:
Node* getNewNode(int key){
    Node* newnode = new Node;
    newnode->key = key;
    newnode->left = nullptr;
    newnode->right = nullptr;
    return newnode;
}

void doInsert(Node** r, int key){
    if(*r == nullptr){
      *r = getNewNode(key);
      return;
    }
    if(key <= (*r)->key){
      if((*r)->left == nullptr){
        (*r)->left = getNewNode(key);
      }else{
        doInsert(&(*r)->left, key);
        return;
      }
    }
    if(key > (*r)->key){
      if((*r)->right == nullptr){
        (*r)->right = getNewNode(key);
      }else{
        doInsert(&(*r)->right, key);
        return;
      }
    }
}

void inOrder(Node* r){
    if(r != nullptr){
      inOrder(r->left);
      cout<<r->key<<" ";
      inOrder(r->right);
    }
}

void preOrder(Node* r){
    if(r != nullptr){
      cout<<r->key<<" ";
      preOrder(r->left);
      preOrder(r->right);
    }
}

void postOrder(Node* r){
    if(r != nullptr){
      postOrder(r->left);
      postOrder(r->right);
      cout<<r->key<<" ";
    }
}

int doCountWithRepeated(Node* r){
    if(r == nullptr){
      return 0;
    }
    return 1+doCountWithRepeated(r->left)+doCountWithRepeated(r->right);
}

void doCountWithNotRepeated(Node* r, vector<int>& keys){
    if(r == nullptr)
      return;
    if(find(keys.begin(), keys.end(), r->key) == keys.end()){
      keys.push_back(r->key);
    }
    doCountWithNotRepeated(r->left, keys);
    doCountWithNotRepeated(r->right, keys);
}

int height(Node* r){
    if(r == nullptr) return 0;
    return 1 + max(height(r->left), height(r->right));
}

bool isBalanced(Node* r){
    int lh;
    int rh;
    if(r == nullptr) return 1;
    lh = height(root->left);
    rh = height(root->right);
    if(abs(lh - rh) <= 1 && isBalanced(r->left) && isBalanced(r->right))
        return 1;
    return 0;
}

void printTreeStructure(const std::string& prefix, Node* r, bool isLeft){
    if(r != nullptr){
      std::cout << prefix;
      std::cout << (isLeft ? "├──" : "└──" );
      std::cout << r->key << std::endl;
      printTreeStructure(prefix + (isLeft ? "│   " : "    "), r->left, true);
      printTreeStructure(prefix + (isLeft ? "│   " : "    "), r->right, false);
    }
}

void deleteTree(Node** r){
    if(*r != nullptr){
      deleteTree(&(*r)->left);
      deleteTree(&(*r)->right);
      delete *r;
    }
}

private:
Node* root;
};

int main()
{
BinarySearchTree bst;
bst.insert(10);
bst.insert(20);
bst.insert(30);
bst.insert(80);
bst.insert(50);
bst.insert(70);
bst.insert(10);
bst.insert(40);
bst.display();
cout<<"Count: "<<bst.countWithRepeated()<<endl;
cout<<"Count not repeated: "<<bst.countWithNotRepeated()<<endl;
cout<<"is balanced: "<<bst.isBalanced()<<endl;
bst.printTreeStructure();
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Hello guys can you t help me with writing a c++ program using classes to for...
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
  • 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,...

  • 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);...

  • Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to com...

    Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...

  • 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

  • write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the word...

    write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the words in order (based on ASCII/UNICODE order) on the screen (or to output text file). Note that you may need to make some changes to BST.java. Sample test: ----jGRASP exec: java -ea removeDuplicates Original Text: a B 2 n w C q K l 0...

  • C++ EXERCISE (DATA STRUCTURES). I just need a code for some functions that are missing. Please...

    C++ EXERCISE (DATA STRUCTURES). I just need a code for some functions that are missing. Please help me figure out. Thanks. C++ BST implementation (using a struct) Enter the code below, and then compile and run the program. After the program runs successfully, add the following functions: postorder() This function is similar to the inorder() and preorder() functions, but demonstrates postorder tree traversal. displayParentsWithTwo() This function is similar to the displayParents WithOne() function, but displays nodes having only two children....

  • Need help in the below question. Answer in java Start with the tree.java program (Listing 8.1)...

    Need help in the below question. Answer in java Start with the tree.java program (Listing 8.1) and modify it to create a binary tree from a string of letters (like A, B, and so on) entered by the user. Each letter will be displayed in its own node. Construct the tree so that all the nodes that contain letters are leaves. Parent nodes can contain some non-letter symbol like +. Make sure that every parent node has exactly two children....

  • guys can you please help me to to write a pseudo code for this program and...

    guys can you please help me to to write a pseudo code for this program and write the code of the program in visual studio in.cpp. compile the program and run and take screenshot of the output and upload it. please help is really appreciated. UTF-8"CPP Instruction SU2019 LA X 119SU-COSC-1436- C Get Homework Help With Che X Facebook -1.amazonaws.com/blackboard.learn.xythos.prod/584b1d8497c84/98796290? response-content-dis 100% School of Engineering and Technology COSC1436-LAB1 Note: in the instruction of the lab change "yourLastName" to your last...

  • Write a C++ program to validate computer user-ids and passwords. A list of valid ids and...

    Write a C++ program to validate computer user-ids and passwords. A list of valid ids and passwords (unsorted) is read from a file and stored in a Binary Search Tree (BST) of UserInfo objects. When user-ids and passwords are entered during execution, this BST is searched to determine whether they are legal. Input (file): UserInfo records for valid users Input (keyboard): Ids and passwords of users logging in Output (screen): Messages indicating whether user-ids and passwords are valid, as well...

  • For this computer assignment, you are to write a C++ program to implement a class for...

    For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. The definition of the class for a binary tree (as a template) is given as follows: template < class T > class binTree { public: binTree ( ); // default constructor unsigned height ( ) const; // returns height of tree virtual void insert ( const T& ); //...

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