Binary Search Tree Implementation
Binary Search Tree Implementation
Binary Search Tree Implementation
Search Tree
-
Implemenati
on in C++
// Simple program to create a BST of integers and search an
element in it
#include<iostream>
using namespace std;
//Definition of Node for Binary search tree
struct BstNode {
int data;
BstNode* left;
BstNode* right;
};
#include <iostream>
class BSTNode
{
private:
int data;
BSTNode* left;
BSTNode* right;
public:
BSTNode(int data)
{
this -> data = data;
this -> left = NULL;
this -> right = NULL;
}
~BSTNode();
void print_node ()
{
std::cout << "Value at the node: " << this -> data << std::endl;
if (this -> left != NULL)
std::cout << "Value at left node: " << this -> left -> data
<< std::endl;
if (this -> right != NULL)
std::cout << "Value at right node: " << this -> right -> data
<< std::endl;
}
int main()
{
BSTNode* root = new BSTNode(10);
root -> print_node();
root -> insert(9);
root -> insert(7);
root -> insert(6);
root -> print_node();
root -> insert(14);
root -> print_node();
std::cout << "Finding 10: " << root -> find(10) << std::endl;
std::cout << "Finding 8: " << root -> find(8) << std::endl;
root -> insert(19);
root -> insert(70);
root -> insert(3);
root -> print_node();
root -> insert(4);
root -> insert(8);
root -> print_node();
std::cout << "Finding 70: " << root -> find(70) << std::endl;
std::cout << "Finding 8: " << root -> find(8) << std::endl;
std::cout << "Finding 17: " << root -> find(17) << std::endl;
}