Answer to Question #187908 in Algorithms for Sawaira

Question #187908
Implement Binary Search Tree class with insertion, deletion, preorder, postorder, and inorder traversal functions javascript code.
1
Expert's answer
2021-05-02T03:05:04-0400
#include <iostream>
using namespace std;

class BST
{
    int data;
    BST *left, *right;

public:

    BST();
    BST(int);
    BST* Insert(BST*, int);
    void Inorder(BST*);
};


BST ::BST()
    : data(0)
    , left(NULL)
    , right(NULL)
{
}

BST ::BST(int value)
{
    data = value;
    left = right = NULL;
}

BST* BST ::Insert(BST* root, int value)
{
    if (!root)
    {
        return new BST(value);
    }
    if (value > root->data)
    {
        root->right = Insert(root->right, value);
    }
    else
    {
        root->left = Insert(root->left, value);
    }


    return root;
}



void BST ::Inorder(BST* root)
{
    if (!root) {
        return;
    }
    Inorder(root->left);
    cout << root->data << endl;
    Inorder(root->right);
}

int main()
{
    BST b, *root = NULL;
    root = b.Insert(root, 50);
    b.Insert(root, 30);
    b.Insert(root, 10);
    b.Insert(root, 50);
    b.Insert(root, 40);
    b.Insert(root, 80);
    b.Insert(root, 10);

    b.Inorder(root);
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS