diff --git a/binary_search_tree/build_bst.c++ b/binary_search_tree/build_bst.c++ new file mode 100644 index 0000000..988d362 --- /dev/null +++ b/binary_search_tree/build_bst.c++ @@ -0,0 +1,59 @@ +#include +using namespace std; + +class node +{ + public: + int data; + node *left,*right; + //Constructor: + node(int data) + { + this->data=data; + left=right=NULL; + } +}; +//Build Binary Search Tree: +node *buildBinarySearchTree(node *root , int data) +{ + if(root==NULL) + { + node *temp=new node(data); + return temp; + } + if(data < root->data) + { + root->left=buildBinarySearchTree(root->left , data); + } + else if(data > root->data) + { + root->right=buildBinarySearchTree(root->right , data); + } + return root; +} +//Inorder Traversal: +void inOrderTraversal(node * root) +{ + if(root==NULL) + { + return; + } + inOrderTraversal(root->left); + cout<data<<" "; + inOrderTraversal(root->right); +} +//Main Function: +int main(int argc, char const *argv[]) +{ + node *root = buildBinarySearchTree(root , 8); + root = buildBinarySearchTree(root , 6); + root = buildBinarySearchTree(root , 10); + root = buildBinarySearchTree(root , 5); + root = buildBinarySearchTree(root , 7); + root = buildBinarySearchTree(root , 9); + root = buildBinarySearchTree(root , 11); + + inOrderTraversal(root); + cout< +using namespace std; + +class node +{ + public: + int data; + node *left,*right; + //Constructor: + node(int data) + { + this->data=data; + left=right=NULL; + } +}; +//Find the inorder successor: +int inorderSuccessor(node *root) +{ + node *temp=root; + while(temp->left!=NULL) + { + temp=temp->left; + } + return temp->data; +} +//Searching in BST: +node *deletion(node *root, int data) +{ + if(root==NULL) + { + cout<<"Element has not been found "<data==data) + { + + if((root->left != NULL) and (root->right != NULL)) + { + int successor=inorderSuccessor(root->right); + root->data= successor; + root->right = deletion(root->right , successor); + } + else if(root->left !=NULL) + { + root=root->left; + } + else if(root->right !=NULL) + { + root=root->right; + } + else{ + root=NULL; + } + return root; + } + else if(root->data > data) + { + root->left = deletion(root->left , data); + } + else if(root->data < data) + { + root->right = deletion(root->right , data); + } + return root; +} +//Build Binary Search Tree: +node *buildBinarySearchTree(node *root , int data) +{ + if(root==NULL) + { + node *temp=new node(data); + return temp; + } + if(data < root->data) + { + root->left=buildBinarySearchTree(root->left , data); + } + else if(data > root->data) + { + root->right=buildBinarySearchTree(root->right , data); + } + return root; +} +//Inorder Traversal: +void inOrderTraversal(node * root) +{ + if(root==NULL) + { + return; + } + inOrderTraversal(root->left); + cout<data<<" "; + inOrderTraversal(root->right); +} +//Main Function: +int main(int argc, char const *argv[]) +{ + node *root = buildBinarySearchTree(root , 8); + root = buildBinarySearchTree(root , 3); + root = buildBinarySearchTree(root , 10); + root = buildBinarySearchTree(root , 1); + root = buildBinarySearchTree(root , 6); + root = buildBinarySearchTree(root , 14); + root = buildBinarySearchTree(root , 4); + root = buildBinarySearchTree(root , 7); + root = buildBinarySearchTree(root , 13); + + inOrderTraversal(root); + cout< +#include +using namespace std; +// vector vctr; +class Node +{ + public: + int data; + Node *left; + Node *right; + //Constructor: + Node(int data) + { + this->data=data; + left=right=NULL; + } +}; +//Print: +void printing(vector vctr) +{ + for(auto x : vctr) + { + cout<"; + } + cout< &vctr) +{ + if(root==NULL) + { + return ; + } + if(root->left ==NULL and root->right==NULL) + { + vctr.push_back(root->data); + printing(vctr); + vctr.pop_back(); + return; + } + vctr.push_back(root->data); + printAllPath(root->left , vctr); + printAllPath(root->right , vctr); + vctr.pop_back(); + return; +} +//Build Binary Search Tree: +Node *buildBinarySearchTree(Node *root , int data) +{ + if(root==NULL) + { + Node *temp=new Node(data); + return temp; + } + if(data < root->data) + { + root->left=buildBinarySearchTree(root->left , data); + } + else if(data > root->data) + { + root->right=buildBinarySearchTree(root->right , data); + } + return root; +} +//Inorder Traversal: +void inOrderTraversal(Node * root) +{ + if(root==NULL) + { + return; + } + inOrderTraversal(root->left); + cout<data<<" "; + inOrderTraversal(root->right); +} +//Preorder Traversal: +void preOrderTraversal(Node * root) +{ + if(root==NULL) + { + return; + } + cout<data<<" "; + preOrderTraversal(root->left); + preOrderTraversal(root->right); +} +//Main Function: +int main() +{ + + Node * root = buildBinarySearchTree(root , 8); + root = buildBinarySearchTree(root , 6); + root = buildBinarySearchTree(root , 10); + root = buildBinarySearchTree(root , 5); + root = buildBinarySearchTree(root , 7); + root = buildBinarySearchTree(root , 9); + root = buildBinarySearchTree(root , 11); + + + inOrderTraversal(root); + cout< path; + printAllPath(root,path); + return 0; +} diff --git a/binary_search_tree/print_in_range.c++ b/binary_search_tree/print_in_range.c++ new file mode 100644 index 0000000..04821f1 --- /dev/null +++ b/binary_search_tree/print_in_range.c++ @@ -0,0 +1,99 @@ +#include +using namespace std; + +class node +{ + public: + int data; + node *left,*right; + //Constructor: + node(int data) + { + this->data=data; + left=right=NULL; + } +}; +//Print in Range -->elf: +void printInRange(node *root , int a, int b) +{ + if(root==NULL) + { + return; + } + printInRange(root->left , a , b); + if(a<=root->data and root->data<=b){ + cout<data<<" "; + } + printInRange(root->right , a, b); +} +// Second -Way +void printRange(node *root , int a, int b) +{ + if(root==NULL) + { + return; + } + else if(a<=root->data and root->data<=b) + { + printRange(root->left ,a ,b); + cout<data<<" "; + printRange(root->right ,a ,b); + } + else if(root->data > b) + { + printRange(root->left ,a ,b); + } + else if(root->data < a) + { + printRange(root->right ,a ,b); + } +} +//Build Binary Search Tree: +node *buildBinarySearchTree(node *root , int data) +{ + if(root==NULL) + { + node *temp=new node(data); + return temp; + } + if(data < root->data) + { + root->left=buildBinarySearchTree(root->left , data); + } + else if(data > root->data) + { + root->right=buildBinarySearchTree(root->right , data); + } + return root; +} +//Inorder Traversal: +void inOrderTraversal(node * root) +{ + if(root==NULL) + { + return; + } + inOrderTraversal(root->left); + cout<data<<" "; + inOrderTraversal(root->right); +} +//Main Function: +int main(int argc, char const *argv[]) +{ + node *root = buildBinarySearchTree(root , 8); + root = buildBinarySearchTree(root , 3); + root = buildBinarySearchTree(root , 10); + root = buildBinarySearchTree(root , 1); + root = buildBinarySearchTree(root , 6); + root = buildBinarySearchTree(root , 14); + root = buildBinarySearchTree(root , 4); + root = buildBinarySearchTree(root , 7); + root = buildBinarySearchTree(root , 13); + + inOrderTraversal(root); + cout< +using namespace std; + +class node +{ + public: + int data; + node *left,*right; + //Constructor: + node(int data) + { + this->data=data; + left=right=NULL; + } +}; +//Searching in BST: +void search(node *root, int data) +{ + if(root==NULL) + { + cout<<"Element has not been found "<data==data) + { + cout<<"Element has been found "<data > data) + { + return search(root->left , data); + } + else if(root->data < data) + { + return search(root->right , data); + } +} +//Build Binary Search Tree: +node *buildBinarySearchTree(node *root , int data) +{ + if(root==NULL) + { + node *temp=new node(data); + return temp; + } + if(data < root->data) + { + root->left=buildBinarySearchTree(root->left , data); + } + else if(data > root->data) + { + root->right=buildBinarySearchTree(root->right , data); + } + return root; +} +//Inorder Traversal: +void inOrderTraversal(node * root) +{ + if(root==NULL) + { + return; + } + inOrderTraversal(root->left); + cout<data<<" "; + inOrderTraversal(root->right); +} +//Main Function: +int main(int argc, char const *argv[]) +{ + node *root = buildBinarySearchTree(root , 8); + root = buildBinarySearchTree(root , 6); + root = buildBinarySearchTree(root , 10); + root = buildBinarySearchTree(root , 5); + root = buildBinarySearchTree(root , 7); + root = buildBinarySearchTree(root , 9); + root = buildBinarySearchTree(root , 11); + + inOrderTraversal(root); + cout<