diff --git a/Trees/BST implementation.cpp b/Trees/BST implementation.cpp new file mode 100644 index 000000000..6902fbd9d --- /dev/null +++ b/Trees/BST implementation.cpp @@ -0,0 +1,57 @@ +#include +#include +using namespace std; + +struct BSTNode +{ + int data; + BSTNode *left; + BSTNode *right; + +}; + +struct BSTNode *GetNewNode(int data) +{ + struct BSTNode *NewNode = new BSTNode(); + NewNode->data=data; + NewNode->left = NULL ; + NewNode->right = NULL; + + return NewNode; +} + + +struct BSTNode *Insert(BSTNode* root, int data) +{ + if(root==NULL) + root= GetNewNode(data); + + else if(data <= root->data) + root->left = Insert(root->left , data); + + else + root->right = Insert(root->right , data); + + return root; +} + + + + +int main() +{ + struct BSTNode *rootPtr = NULL; + + rootPtr = Insert(rootPtr , 5); + rootPtr = Insert(rootPtr , 7); + rootPtr = Insert(rootPtr , 4); + rootPtr = Insert(rootPtr , 15); + rootPtr = Insert(rootPtr , 17); + rootPtr = Insert(rootPtr , 14); + rootPtr = Insert(rootPtr , 11); + + +return 0; +} + + diff --git a/Trees/Checking is tree BST.cpp b/Trees/Checking is tree BST.cpp new file mode 100644 index 000000000..3750f149e --- /dev/null +++ b/Trees/Checking is tree BST.cpp @@ -0,0 +1,104 @@ +#include +#include +#include +using namespace std; + +struct BSTNode +{ + int data; + BSTNode *left; + BSTNode *right; + +}; + +struct BSTNode *GetNewNode(int data) +{ + struct BSTNode *NewNode = new BSTNode(); + NewNode->data=data; + NewNode->left = NULL ; + NewNode->right = NULL; + + return NewNode; +} + +struct BSTNode *Insert(BSTNode* root, int data) +{ + if(root==NULL) + root= GetNewNode(data); + + else if(data <= root->data) + root->left = Insert(root->left , data); + + else + root->right = Insert(root->right , data); + + return root; +} + +bool IsSubtreeLesser(BSTNode *root , int d) +{ + if(root==NULL) + return 1; + + else if(root->data<=d && IsSubtreeLesser(root->left , d) &&IsSubtreeLesser(root->right , d)) + return 1; + + else + return 0; + +} + +bool IsSubtreeGreater(BSTNode *root , int d) +{ + if(root==NULL) + return 1; + + else if(root->data >d && IsSubtreeGreater(root->left , d) &&IsSubtreeGreater(root->right , d)) + return 1; + + else + return 0; + +} + + +bool IsBST(BSTNode *root) +{ + if(root==NULL) + return 1; + + else if(IsSubtreeLesser(root->left , root->data) && IsSubtreeGreater(root->right,root->data) && IsBST(root->left) && IsBST(root->right)) + return 1; + + else + return 0; + + + +} + + +int main() +{ + struct BSTNode *rootPtr = NULL; + + rootPtr = Insert(rootPtr , 5); + rootPtr = Insert(rootPtr , 7); + rootPtr = Insert(rootPtr , 4); + rootPtr = Insert(rootPtr , 15); + rootPtr = Insert(rootPtr , 17); + rootPtr = Insert(rootPtr , 14); + rootPtr = Insert(rootPtr , 11); + + if(IsBST(rootPtr)) + cout<<"Tree is BST."; + else + cout<<"Tree is not BST."; + + + + +return 0; +} + + diff --git a/Trees/Deleting nodes from BST.cpp b/Trees/Deleting nodes from BST.cpp new file mode 100644 index 000000000..8ebecb713 --- /dev/null +++ b/Trees/Deleting nodes from BST.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +using namespace std; + +struct BSTNode +{ + int data; + BSTNode *left; + BSTNode *right; + +}; + +struct BSTNode *GetNewNode(int data) +{ + struct BSTNode *NewNode = new BSTNode(); + NewNode->data=data; + NewNode->left = NULL ; + NewNode->right = NULL; + + return NewNode; +} + +struct BSTNode *Insert(BSTNode* root, int data) +{ + if(root==NULL) + root= GetNewNode(data); + + else if(data <= root->data) + root->left = Insert(root->left , data); + + else + root->right = Insert(root->right , data); + + return root; +} + +BSTNode *FindMin(BSTNode *root) +{ + if(root==NULL) + return NULL; + + if(root->left==NULL) + return root; + + while(root->left!=NULL) + { + root=root->left; + } + + return root; + +} + + + +struct BSTNode* Delete(BSTNode *root , int data) +{ + if(root==NULL) + return root; + + else if(root->data < data) + root->right=Delete(root->right,data); + + else if(root->data > data) + root->left=Delete(root->left , data); + + else // if data matches + { + // case 1 : no child + + if(root->left==NULL && root->right==NULL ) + { + delete root; + root = NULL; + return root; + } + + // case 2: 1 child + else if(root->left == NULL) + { + BSTNode *temp =root; + root = root->right; + delete temp; + return root; + + } + else if (root->right == NULL) + { + BSTNode *temp =root; + root = root->left; + delete temp; + return root; + + } + + //case 3 : two children + else + { + BSTNode *temp = FindMin(root->right); + root->data= temp->data; + root->right= Delete(root->right , temp->data); + + } + + + } + + return root; +} + + +int main() +{ + struct BSTNode *rootPtr = NULL; + + rootPtr = Insert(rootPtr , 5); + rootPtr = Insert(rootPtr , 7); + rootPtr = Insert(rootPtr , 4); + rootPtr = Insert(rootPtr , 15); + rootPtr = Insert(rootPtr , 17); + rootPtr = Insert(rootPtr , 14); + rootPtr = Insert(rootPtr , 11); + + + rootPtr=Delete(rootPtr,15); + + + +return 0; +} + + diff --git a/Trees/Finding minimum and maximum in BST.cpp b/Trees/Finding minimum and maximum in BST.cpp new file mode 100644 index 000000000..58d396d39 --- /dev/null +++ b/Trees/Finding minimum and maximum in BST.cpp @@ -0,0 +1,90 @@ +#include +#include +using namespace std; + +struct BSTNode +{ + int data; + BSTNode *left; + BSTNode *right; + +}; + +struct BSTNode *GetNewNode(int data) +{ + struct BSTNode *NewNode = new BSTNode(); + NewNode->data=data; + NewNode->left = NULL ; + NewNode->right = NULL; + + return NewNode; +} + + +struct BSTNode *Insert(BSTNode* root, int data) +{ + if(root==NULL) + root= GetNewNode(data); + + else if(data <= root->data) + root->left = Insert(root->left , data); + + else + root->right = Insert(root->right , data); + + return root; +} + + + +int Min(BSTNode *root) +{ + if(root==NULL) + return -1; + + else if(root->left == NULL) + return root->data; + + else + return Min(root->left); + +} + +int Max(BSTNode *root) +{ + if(root==NULL) + return -1; + + else if(root->right == NULL) + return root->data; + + else + return Max(root->right); + +} + + +int main() +{ + struct BSTNode *rootPtr = NULL; + + rootPtr = Insert(rootPtr , 5); + rootPtr = Insert(rootPtr , 7); + rootPtr = Insert(rootPtr , 4); + rootPtr = Insert(rootPtr , 15); + rootPtr = Insert(rootPtr , 17); + rootPtr = Insert(rootPtr , 14); + rootPtr = Insert(rootPtr , 11); + + cout<<"Minimum value = "<left); + PreOrder(root->right); + +} + +// Inorder traversal +void InOrder(BSTNode *root) +{ + if(root==NULL) + return ; + + // LDR + PreOrder(root->left); + + cout<data<<" "; + + PreOrder(root->right); + +} + +// PostOrder Traversal +void PostOrder(BSTNode *root) +{ + if(root==NULL) + return ; + + // LRD + + PreOrder(root->left); + PreOrder(root->right); + + cout<data <<" "; + +} + + + +int main() +{ + struct BSTNode *rootPtr = NULL; + + rootPtr = Insert(rootPtr , 5); + rootPtr = Insert(rootPtr , 7); + rootPtr = Insert(rootPtr , 4); + rootPtr = Insert(rootPtr , 15); + rootPtr = Insert(rootPtr , 17); + rootPtr = Insert(rootPtr , 14); + rootPtr = Insert(rootPtr , 11); + + cout<<"Preorder traversal : "; + PreOrder(rootPtr); + + cout<<"\n\nInorder traversal : "; + InOrder(rootPtr); + + cout<<"\n\nPostorder traversal : "; + PostOrder(rootPtr); + + +return 0; +} + + diff --git a/Trees/Level Order Traversal.cpp b/Trees/Level Order Traversal.cpp new file mode 100644 index 000000000..4ee3e0322 --- /dev/null +++ b/Trees/Level Order Traversal.cpp @@ -0,0 +1,82 @@ +#include +#include +#include +using namespace std; + +struct BSTNode +{ + int data; + BSTNode *left; + BSTNode *right; + +}; + +struct BSTNode *GetNewNode(int data) +{ + struct BSTNode *NewNode = new BSTNode(); + NewNode->data=data; + NewNode->left = NULL ; + NewNode->right = NULL; + + return NewNode; +} + +struct BSTNode *Insert(BSTNode* root, int data) +{ + if(root==NULL) + root= GetNewNode(data); + + else if(data <= root->data) + root->left = Insert(root->left , data); + + else + root->right = Insert(root->right , data); + + return root; +} + + +// Level Order +void LevelOrder(BSTNode *root) +{ + queue Q ; + Q.push(root); + + while(!Q.empty()) + { + BSTNode *ptr = Q.front(); + + cout<data<<" "; + + if(ptr->left!=NULL) + Q.push(ptr->left); + + if(ptr->right!=NULL) + Q.push(ptr->right); + + Q.pop(); + } +} + + +int main() +{ + struct BSTNode *rootPtr = NULL; + + rootPtr = Insert(rootPtr , 5); + rootPtr = Insert(rootPtr , 7); + rootPtr = Insert(rootPtr , 4); + rootPtr = Insert(rootPtr , 15); + rootPtr = Insert(rootPtr , 17); + rootPtr = Insert(rootPtr , 14); + rootPtr = Insert(rootPtr , 11); + + cout<<"Level Order Traversal : "; + LevelOrder(rootPtr); + + + +return 0; +} + + diff --git a/Trees/Searching element in BST.cpp b/Trees/Searching element in BST.cpp new file mode 100644 index 000000000..2cdf8d781 --- /dev/null +++ b/Trees/Searching element in BST.cpp @@ -0,0 +1,85 @@ +#include +#include +using namespace std; + +struct BSTNode +{ + int data; + BSTNode *left; + BSTNode *right; + +}; + +struct BSTNode *GetNewNode(int data) +{ + struct BSTNode *NewNode = new BSTNode(); + NewNode->data=data; + NewNode->left = NULL ; + NewNode->right = NULL; + + return NewNode; +} + + +struct BSTNode *Insert(BSTNode* root, int data) +{ + if(root==NULL) + root= GetNewNode(data); + + else if(data <= root->data) + root->left = Insert(root->left , data); + + else + root->right = Insert(root->right , data); + + return root; +} + + +// Search Function +bool Search(BSTNode *root, int d) +{ + if(root==NULL) + return 0; + + else if(root->data == d) + return 1; + + else if(d < root->data ) + return Search(root->left , d); + + else + return Search(root->right,d); + + +} + + + + +int main() +{ + struct BSTNode *rootPtr = NULL; + + rootPtr = Insert(rootPtr , 5); + rootPtr = Insert(rootPtr , 7); + rootPtr = Insert(rootPtr , 4); + rootPtr = Insert(rootPtr , 15); + + int element; + cout<<"Enter number to search : "; + cin>>element; + + int flag =Search(rootPtr,element); + + if(flag==1) + cout<<"Found!"; + else + cout<<"Not Found!"; + + + +return 0; +} + +