Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand Down
64 changes: 64 additions & 0 deletions res/Binary Search Tree/leastCommonAncestor.cpp
Original file line number Diff line number Diff line change
@@ -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<bits/stdc++.h>

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(n<B->data && 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<<searchLCA(T,m,M);
}
88 changes: 88 additions & 0 deletions res/Tree/levelWiseTreeTraversal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
Given a tree like this
A
/ \
B C
/ \ / \
F G H I
/ \
J K

Level Printing:
A
B C
F G H I
J K
*/


#include<bits/stdc++.h>

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 "<<n<<" (. if none): ";
cin>>ch;
if(ch!='.')
insert(T->left_child,ch);
cout<<"Enter right child for "<<n<<" (. if none) :";
cin>>ch;
if(ch!='.')
insert(T->right_child,ch);
else return;
}

queue<BT_ptr>q;
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<<t->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);
}