Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // program to implement simple binary search tree in python
- #include<iostream>
- using namespace std;
- class node{
- protected:
- int element;
- node* left;
- node* right;
- public:
- //constructor that accepts only element
- node(int element){
- this->element = element;
- this->left = NULL;
- this->right = NULL;
- }
- //constructor that accepts element, left Link, Right Link
- node(int element, node* leftLink, node* rightLink){
- this->element = element;
- this->left = leftLink;
- this->right = rightLink;
- }
- //method to update data of the node
- void updateData(int element){
- this->element = element;
- }
- //method to update the left Link of the node
- void updateLeftLink(node* temp){
- this->left = temp;
- }
- //method to update the right link of the node
- void updateRightLink(node* temp){
- this->right = temp;
- }
- //method that returns the element of the node
- int getElement(){
- return this->element;
- }
- //method that returns left Link
- node* getLeftNode(){
- return this->left;
- }
- //method that returns the right Link
- node* getRightNode(){
- return this->right;
- }
- };
- //binary search tree class
- class BST{
- protected:
- node* root;
- public:
- //constructor for bst
- BST(){
- root = NULL;
- }
- //returns true if the root node is null
- bool isEmpty(){
- return(root == NULL);
- }
- //returns root node
- node* getRoot(){
- return root;
- }
- void insert(int element){
- node* temp = new node(element);
- //if tree is empty put it at root node
- if(root == NULL){
- root = temp;
- }
- else{
- bool inserted = false;
- //creating a node pointer to traverse the tree
- node* p = root;
- //keep looping while the node is not inserted
- while(not inserted){
- //if element of the new node is less than the current node than insert it to the left
- if(p->getElement() > temp->getElement()){
- if(p->getLeftNode() == NULL){
- p->updateLeftLink(temp);
- inserted = true;
- }
- else{
- p = p->getLeftNode();
- }
- }
- //if element of the new node is greater than the current node than insert it to the right
- else if(p->getElement() < temp->getElement()){
- if(p->getRightNode() == NULL){
- p->updateRightLink(temp);
- inserted = true;
- }
- else{
- p = p->getRightNode();
- }
- }
- }
- }
- }
- // method that returns true if the data is present in the bst
- bool search(int data){
- bool found = false;
- // creating a temp node to traverse the binary tree
- node* tempNode = root;
- while(tempNode != NULL){
- // if data is less than the node element then search on the left hand side
- if(data < tempNode->getElement()){
- tempNode = tempNode->getLeftNode();
- }
- // if data is greater than the node element search on the right hand side
- else if(data > tempNode->getElement()){
- tempNode = tempNode->getRightNode();
- }
- // if data is equal to the node element then return true
- else if(data == tempNode->getElement()) {
- found = true;
- cout<<"\n\nThe Data "<<data<<" is found in the BST.";
- return true;
- }
- }
- // if data is not found then return false
- if(not found){
- cout<<"\n\nThe Data "<<data<<" is not found in the BST.";
- return false;
- }
- }
- // recursive program to display tree using Preorder traversal
- void displayPreorder(node* n){
- if(n == NULL){
- return;
- }
- cout<<n->getElement()<<" ";
- displayPreorder(n->getLeftNode());
- displayPreorder(n->getRightNode());
- }
- // recursive program to display tree using Inorder traversal
- void displayInorder(node* n){
- if(n == NULL){
- return;
- }
- displayInorder(n->getLeftNode());
- cout<<n->getElement()<<" ";
- displayInorder(n->getRightNode());
- }
- // recursive program to display tree using Postorder traversal
- void displayPostorder(node* n){
- if(n == NULL){
- return;
- }
- displayPostorder(n->getLeftNode());
- displayPostorder(n->getRightNode());
- cout<<n->getElement()<<" ";
- }
- };
- int main()
- {
- BST b1;
- b1.insert(7);
- b1.insert(29);
- b1.insert(25);
- b1.insert(36);
- b1.insert(71);
- b1.insert(24);
- b1.insert(5);
- b1.insert(9);
- b1.insert(1);
- node* root = b1.getRoot();
- cout<<"The Root element of the BST is : "<<root->getElement()<<endl;
- cout<<"\nThe Preorder traversal of the BST is : ";
- b1.displayPreorder(root);
- cout<<"\n\nThe Postorder traversal of the BST is : ";
- b1.displayPostorder(root);
- cout<<"\n\nThe Inorder traversal of the BST is : ";
- b1.displayInorder(root);
- //searching an element in the Linked List
- b1.search(24);
- b1.search(77);
- }
Add Comment
Please, Sign In to add comment