"binary search tree in cpp using class" Code Answer's

You're definitely familiar with the best coding language C++ that developers use to develop their projects and they get all their queries like "binary search tree in cpp using class" answered properly. Developers are finding an appropriate answer about binary search tree in cpp using class related to the C++ coding language. By visiting this online portal developers get answers concerning C++ codes question like binary search tree in cpp using class. Enter your desired code related query in the search bar and get every piece of information about C++ code related question on binary search tree in cpp using class. 

binary search tree in cpp using class

By TedTed on May 25, 2021
/* Program to implement Binary Search Tree in c++ using classes  */
#include<iostream>
#include<stdlib.h>
#include<cstdlib>
using namespace std;

struct Node {
    int data;
    Node* left;
    Node* right;
};

class BinaryTree {
    private:
        struct Node* root;
    public:
        BinaryTree() {
            root = NULL;
        }
        Node* createNode(int);
        Node* insertNode(Node*, int);
        Node* deleteNode(Node*, int);
        void inOrder(Node*);
        void preOrder(Node*);
        void postOrder(Node*);
        Node* findMinimum(Node*);

        Node* getRoot() {
            return root;
        }

        void setRoot(Node* ptr) {
            root = ptr; 
        }
};

Node* BinaryTree :: createNode(int n) {
    Node* newNode = new struct Node();
    newNode->data = n;
    newNode->left = NULL;
    newNode->right = NULL;
    return newNode; 
}


Node* BinaryTree :: findMinimum(Node* rootPtr) {
    while(rootPtr->left != NULL) {
        rootPtr = rootPtr->left;
    }
    return rootPtr;
}


Node* BinaryTree :: insertNode(Node* rootPtr, int n) {
    if(rootPtr == NULL) {
        return createNode(n);
    }
    if(n < rootPtr->data) {
        rootPtr->left = insertNode(rootPtr->left, n);
    }
    if(n > rootPtr->data) {
        rootPtr->right = insertNode(rootPtr->right, n);
    }
    return rootPtr;
}


Node* BinaryTree :: deleteNode(Node* rootPtr, int n) {
    if(rootPtr == NULL) {
        cout<<"Node to be deleted is not present.!"<<endl;
        return rootPtr;
    }
    else if(n < rootPtr->data) {
        rootPtr->left = deleteNode(rootPtr->left, n);
    } else if(n > rootPtr->data) {
        rootPtr->right = deleteNode(rootPtr->right, n);
    } else {
        if(rootPtr->left == NULL && rootPtr->right == NULL) {
            delete rootPtr;
            rootPtr = NULL;
        }
        else if(root->left == NULL) {
            struct Node* temp = rootPtr;
            rootPtr = rootPtr->right;
            delete temp;
        }
        else if(rootPtr->right == NULL) {
            struct Node* temp = rootPtr;
            rootPtr = rootPtr->left;
            delete temp;
        } else {
            Node* temp = findMinimum(rootPtr->right);
            rootPtr->data = temp->data;
            rootPtr->left = deleteNode(rootPtr->right, temp->data);
        }
    }

    return rootPtr;
}


void BinaryTree :: inOrder(Node* root) {
    if(root == NULL) {
        return;
    }
    inOrder(root->left);
    cout<<root->data<<"\t";
    inOrder(root->right);
}

void BinaryTree :: preOrder(Node* root) {
    if(root == NULL) return;
    cout<<root->data<<"\t";
    preOrder(root->left);
    preOrder(root->right);
}

void BinaryTree :: postOrder(Node* root) {
    if(root == NULL) return;
    postOrder(root->left);
    postOrder(root->right);
    cout<<root->data<<"\t";
}

int main() {
    BinaryTree l1;
    int ch, ele, res;
    Node* ptr;
    do {
            cout<<"1 - Insert Node\n";
            cout<<"2 - IN-ORDER Traversal\n";
            cout<<"3 - PRE-ORDER Traversal\n";
            cout<<"4 - POST-ORDER Traversal\n";
            cout<<"Enter choice\n";
            cin>>ch;
            switch(ch) {
                case 1: 
                    cout<<"Entre element to insert to the List\n";
                    cin>>ele;

                    ptr = l1.insertNode(l1.getRoot(), ele);
  
                    l1.setRoot(ptr);
                    break;
                case 2:
                    cout<<"---IN-ORDER TRAVERSAL---"<<endl;
                    l1.inOrder(l1.getRoot());
                    cout<<endl;
                    break;
                case 3:
                    cout<<"---PRE-ORDER TRAVERSAL---"<<endl;
                    l1.preOrder(l1.getRoot());
                    cout<<endl;
                    break;
                case 4:
                    cout<<"---POST-ORDER TRAVERSAL---"<<endl;
                    l1.postOrder(l1.getRoot());
                    cout<<endl;
                    break;
                case 5:
                    cout<<"Enter node to be deleted."<<endl;
                    cin>>ele;
                    ptr = l1.deleteNode(l1.getRoot(), ele);
                    l1.setRoot(ptr);
                default: cout<<"Invalid choice"<<endl;
            }
    } while(ch >=1 && ch <= 5);
    return 0;
}

Add Comment

1

All those coders who are working on the C++ based application and are stuck on binary search tree in cpp using class can get a collection of related answers to their query. Programmers need to enter their query on binary search tree in cpp using class related to C++ code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about binary search tree in cpp using class for the programmers working on C++ code while coding their module. Coders are also allowed to rectify already present answers of binary search tree in cpp using class while working on the C++ language code. Developers can add up suggestions if they deem fit any other answer relating to "binary search tree in cpp using class". Visit this developer's friendly online web community, CodeProZone, and get your queries like binary search tree in cpp using class resolved professionally and stay updated to the latest C++ updates. 

C++ answers related to "binary search tree in cpp using class"

View All C++ queries

C++ queries related to "binary search tree in cpp using class"

binary search tree in cpp using class binary tree search deletion in a binary search tree binary search tree sorted order how to do binary search in c++ using STL binary index tree c++ binary indexed tree binary tree deletion top view of binary tree c++ heap sort heapify and max heap in binary tree searching display insert in a binary serach tree Write a program in C++ to find post-order predecessor of a node in a Binary Tree vertical traversal of binary tree Print Nodes in Top View of Binary Tree binary algebra cpp binary search program c++ binary search stl binary search function in c++ binary search in c++ c++ binary search binary search algorithm c++ binary search lower bound C Binary Search binary search in java Binary Search implementation binary search in c binary search in stl cpp class constructor cpp how to create an object of template class cpp make class abstract cpp nested class cpp class access array member by different name class cpp Write a C++ program using class and objects. You have to define multiple-member functions outside class and all those functions will be the same name diameter of tree using dfs reverse string efficient in cpp without using function find in set of pairs using first value cpp how to run cpp using gcc vscode Temporary file using MSFT API in cpp infix to prefix using cpp linked list program how to shorten code using using c++ in class with typename binary addition using bitwise operators Print Decimal to binary using stack passing the value to base class constructor from derived class c++ calling base class function from derived class object class friend to another class syntax is not a nonstatic data member or base class of class gfg bottom view of tree gfg right view of tree bst to insert tree gfg left view of tree tree in c++ stl gfg top view of tree dfenwick tree code c++ avl tree implementation c++ find the graph is minimal spanig tree or not centroid of a tree how to print cpp cpp print vector sum of 2 numbers in cpp cpp thread sleep declaration vs. definition cpp change int to string cpp print in cpp Simple cpp code sine function in cpp split vector in half cpp cpp get data type sum of 2 numbers in cpp function rng cpp cpp float to int check file exist cpp basic cpp programs how to compile and run cpp code in terminal fastio cpp cpp throw string stock a file in a vector cpp reverse sort cpp how to get string from user in cpp sqrt cpp cpp #include "" Dangling Pointer in cpp create a dictionary cpp print space in array cpp dereferencing pointer in cpp count method in cpp how can make string value in cpp how to compare lower case character to uppercase cpp cpp get float from integer division reverse an array in cpp include spaces while reading strings in cpp cpp random number in range cpp split string by space cpp get last element of vector sha256 cpp string in cpp cpp goiver all the map values unary overload operator cpp initialzing a 2d vector in cpp check if map key has alue cpp how to get input in cpp error: ‘memset’ was not declared in this scope in cpp passing an 2d array in cpp ? in cpp hashmap in cpp include cpp polymorphism-program.cpp cpp float to string min and max heap in cpp cpp rand cpp starting code cpp return array gets in cpp getline cpp cpp random int conditional operator in cpp foreach cpp new keyword in cpp write in file cpp check prime no for large value in cpp length of array in cpp how to find 2d vector length cpp pow in cpp header file bitmask or cpp cpp regex match iostream library in cpp cpp std list example memcpy library cpp cpp sample code stack function in cpp this keyword in cpp recursion in cpp with reference cpp linked list concatenation cpp int and stirng cpp macro strcpy in cpp list in cpp cpp stack strlen in cpp check if a string is palindrome cpp char* to int in cpp next palindrome number in cpp how to append an element to an array in cpp cin in cpp format string cpp npos in cpp how to convert integer to string in cpp pointers in cpp balanced brackets hackerrank solution in cpp sort function in cpp define in cpp abs in cpp how to encode utf-8 cpp multiple threads cpp cpp loop through object swap in cpp print pattern and space in cpp dynamic array cpp file open cpp ouvrir un fichier en cpp cpp array init value priority queue cpp cpp map iterate over keys 2d vector in cpp constructor cpp create lambda with recursion check if map key has value cpp cpp online compiler accumulate in cpp create matrix cpp or in cpp map in cpp passing a 2d array cpp cpp lambda function how to delete an element in vector pair in cpp #define in cpp cpp get screen resolution set in cpp cpp unordered_map has key cpp program to find average of n numbers decimal to hex cpp continue in cpp log in cpp declaring iterator in cpp cpp language explained for loop in cpp cpp function takes in vector friend function cpp reference build a prefix array cpp cpp malloc remove the last element of a vector in cpp cpp map insert pointers to pointers in cpp vector of pairs declaration in cpp how to make a function in cpp cpp lambda cpp function that returns two arguments end vs cend in cpp switch cpp

Browse Other Code Languages

CodeProZone