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
#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;
}
Hello guys can you t help me with writing a c++ program using classes to for...
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,...
Code in C++ Instructions Write a program which... Prompt the user to enter the number of values that they would like to insert into a binary search tree. Prompt the user to insert, one-at-a-time, a value into the binary search tree. Print the preorder, postorder, and inorder traversal on the tree. Binary Search Tree The methods preorder, postorder, inorder, preorderR, postorderR, and inorderR will all contains the necessary screen output statements to verify that the traversal is happening correctly (see...
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 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 by using a BST (binary search tree) in scenario i would just call the functions of the BTC functions like adding deleting ,,, etc
C++ ONLY Threaded Binary Search Tree Since a binary search tree with N nodes has N + 1 NULL pointers, half the space allocated in a binary search tree for pointer information is wasted. Suppose that if a node has a NULL left child, we make its left child pointer link to its inorder predecessor, and if a node has a NULL right child, we make its right child pointer link to its inorder successor. This is known as a...
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 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....
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...
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....