diff --git a/README.md b/README.md index b734533..9931ba3 100644 --- a/README.md +++ b/README.md @@ -2184,12 +2184,15 @@ more details. - [*Preorder and Postoder Traversals*](https://www.geeksforgeeks.org/618/) -- [*Level order - traversal*](https://www.geeksforgeeks.org/level-order-tree-traversal/) +- [*Level order traversal*](https://www.geeksforgeeks.org/level-order-tree-traversal/) + - [Source Code](res/Tree/levelWiseTreeTraversal.cpp) + - [*Height of Binary Tree*](https://www.geeksforgeeks.org/write-a-c-program-to-find-the-maximum-depth-or-height-of-a-tree/) +- [*Least Common Ancestor in BST*](res/BST/leastCommonAncestor.cpp) +   **Binary Heaps** diff --git a/res/Binary Search Tree/leastCommonAncestor.cpp b/res/Binary Search Tree/leastCommonAncestor.cpp new file mode 100644 index 0000000..c87022f --- /dev/null +++ b/res/Binary Search Tree/leastCommonAncestor.cpp @@ -0,0 +1,64 @@ +/* + A program to find the least common ancestor of two nodes in BST + Time Complexity : O(logn) where n is size of tree + + Algorithm : As BST follows a specific order where each of the value in right subtree is more than parent node and left sub tree is smaller than parent node + So the concept here used is whenever there is an divergence of value the given node is Least Common Ancestor. + Before that all are common ancestor. +*/ + +#include + +using namespace std; + +typedef struct bstnode{ + bstnode *lchild; + int data; + bstnode *rchild; +}*bstptr; + +void insert(bstptr &T,int n) +{ + if(T==NULL) + { + T=new(bstnode); + T->data=n; + T->lchild=NULL; + T->rchild=NULL; + } + else + { + if(T->data>n) + insert(T->lchild,n); + else + insert(T->rchild,n); + } +} + +int searchLCA(bstptr B,int m,int M) +{ + if(ndata && b>B->data) + return B->data; + if(B->data>n) + searchLCA(B->lchild,n,b); + else if(B->data < n) + searchLCA(B->rchild,n,b); +} + +int main() +{ + bstptr T; + T=new(bstnode); + T=NULL; + int n; + cin>>n; + while(n!=-1) + { + insert(T,n); + cin>>n; + } + int a,b,m,M; + cin>>a>>b; + m = min(a,b), M = max(a,b); + cout< + +using namespace std; + +typedef struct Btnode{ + Btnode *left_child; + char data; + Btnode *right_child; +}*BT_ptr; + +void insert(BT_ptr &T,char n) +{ + if(T==NULL) + { + T = new Btnode; + T->data = n; + T->left_child = NULL; + T->right_child = NULL; + } + char ch; + cout<<"Enter left child for "<>ch; + if(ch!='.') + insert(T->left_child,ch); + cout<<"Enter right child for "<>ch; + if(ch!='.') + insert(T->right_child,ch); + else return; +} + +queueq; +void levelPrint(BT_ptr T) +{ + BT_ptr temp = new Btnode; + temp->data = '#'; + temp->left_child = NULL; + temp->right_child = NULL; + q.push(T); + q.push(temp); + while(q.size()!=1) + { + BT_ptr t = q.front(); + q.pop(); + if(t->data=='#') + { + cout<<"\n"; + q.push(temp); + } + else + { + cout<data<<" "; + if(t->left_child!=NULL) + q.push(t->left_child); + if(t->right_child!=NULL) + q.push(t->right_child); + } + } +} + +int main() +{ + BT_ptr T = NULL; + char ch; + cin>>ch; + insert(T,ch); + levelPrint(T); +} + +