0 0 Ashish shah Asked: November 20, 20212021-11-20T11:30:24+05:30 2021-11-20T11:30:24+05:30In: Data Structure Write a program to implement Binary Search Tree. 0 0 Write a program to implement Binary Search Tree. Share Facebook 1 Answer Voted Oldest Recent Professor 2021-11-20T12:02:33+05:30Added an answer on November 20, 2021 at 12:02 pm #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, 20); b.Insert(root, 40); b.Insert(root, 70); b.Insert(root, 60); b.Insert(root, 80); b.Inorder(root); return 0; } Leave an answerCancel replyYou must login to add an answer. Username or email* Password* Remember Me! Forgot Password?
1 Answer