From 363efb4b764a859075d564f246f86b694595c650 Mon Sep 17 00:00:00 2001 From: Mohd Sufiyan Ansari Date: Mon, 19 Oct 2020 11:52:01 +0530 Subject: [PATCH 1/6] Level wise Tree Traversal --- README.md | 4 +- res/Tree/Tree_Level_Printing.cpp | 88 ++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 res/Tree/Tree_Level_Printing.cpp diff --git a/README.md b/README.md index b734533..b4d12bd 100644 --- a/README.md +++ b/README.md @@ -2185,7 +2185,9 @@ more details. Traversals*](https://www.geeksforgeeks.org/618/) - [*Level order - traversal*](https://www.geeksforgeeks.org/level-order-tree-traversal/) + - traversal*](https://www.geeksforgeeks.org/level-order-tree-traversal/) + - [Source Code](res/Tree/Tree_Level_Printing.cpp) + - [*Height of Binary Tree*](https://www.geeksforgeeks.org/write-a-c-program-to-find-the-maximum-depth-or-height-of-a-tree/) diff --git a/res/Tree/Tree_Level_Printing.cpp b/res/Tree/Tree_Level_Printing.cpp new file mode 100644 index 0000000..c71f5d9 --- /dev/null +++ b/res/Tree/Tree_Level_Printing.cpp @@ -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 + +using namespace std; + +typedef struct btnode{ + btnode *lc; + char data; + btnode *rc; +}*btptr; + +void insert(btptr &T,char n) +{ + if(T==NULL) + { + T = new btnode; + T->data = n; + T->lc = NULL; + T->rc = NULL; + } + char ch; + cout<<"Enter left child for "<>ch; + if(ch!='.') + insert(T->lc,ch); + cout<<"Enter right child for "<>ch; + if(ch!='.') + insert(T->rc,ch); + else return; +} + +queueq; +void level(btptr T) +{ + btptr temp = new btnode; + temp->data = '#'; + temp->lc = NULL; + temp->rc = NULL; + q.push(T); + q.push(temp); + while(q.size()!=1) + { + btptr t = q.front(); + q.pop(); + if(t->data=='#') + { + cout<<"\n"; + q.push(temp); + } + else + { + cout<data<<" "; + if(t->lc!=NULL) + q.push(t->lc); + if(t->rc!=NULL) + q.push(t->rc); + } + } +} + +int main() +{ + btptr T = NULL; + char ch; + cin>>ch; + insert(T,ch); + level(T); +} + + From 728e85f1f21caf5d0ab4bf3e7da5d7b2363fb872 Mon Sep 17 00:00:00 2001 From: Mohd Sufiyan Ansari Date: Mon, 19 Oct 2020 12:01:50 +0530 Subject: [PATCH 2/6] Added a Source Code for finding the least common ancestor in BST --- README.md | 5 +- .../Least_Common_Ancestor.cpp | 64 +++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 res/Binary Search Tree/Least_Common_Ancestor.cpp diff --git a/README.md b/README.md index b4d12bd..5aaeea7 100644 --- a/README.md +++ b/README.md @@ -2184,14 +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/Tree_Level_Printing.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/Least_Common_Ancestor.cpp) +   **Binary Heaps** diff --git a/res/Binary Search Tree/Least_Common_Ancestor.cpp b/res/Binary Search Tree/Least_Common_Ancestor.cpp new file mode 100644 index 0000000..c87022f --- /dev/null +++ b/res/Binary Search Tree/Least_Common_Ancestor.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< Date: Mon, 19 Oct 2020 13:56:23 +0530 Subject: [PATCH 3/6] Renamed it to Proper Title --- res/Tree/{Tree_Level_Printing.cpp => Level Order Printing.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename res/Tree/{Tree_Level_Printing.cpp => Level Order Printing.cpp} (100%) diff --git a/res/Tree/Tree_Level_Printing.cpp b/res/Tree/Level Order Printing.cpp similarity index 100% rename from res/Tree/Tree_Level_Printing.cpp rename to res/Tree/Level Order Printing.cpp From 66d880dd2a0bd33c445978e963c0264603380c2a Mon Sep 17 00:00:00 2001 From: Mohd Sufiyan Ansari Date: Mon, 19 Oct 2020 14:01:19 +0530 Subject: [PATCH 4/6] Added Proper class abd variable names --- res/Tree/Level Order Printing.cpp | 44 +++++++++++++++---------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/res/Tree/Level Order Printing.cpp b/res/Tree/Level Order Printing.cpp index c71f5d9..62b64e4 100644 --- a/res/Tree/Level Order Printing.cpp +++ b/res/Tree/Level Order Printing.cpp @@ -20,45 +20,45 @@ using namespace std; -typedef struct btnode{ - btnode *lc; +typedef struct Btnode{ + Btnode *left_child; char data; - btnode *rc; -}*btptr; + Btnode *right_child; +}*BT_ptr; -void insert(btptr &T,char n) +void insert(BT_ptr &T,char n) { if(T==NULL) { - T = new btnode; + T = new Btnode; T->data = n; - T->lc = NULL; - T->rc = NULL; + T->left_child = NULL; + T->right_child = NULL; } char ch; cout<<"Enter left child for "<>ch; if(ch!='.') - insert(T->lc,ch); + insert(T->left_child,ch); cout<<"Enter right child for "<>ch; if(ch!='.') - insert(T->rc,ch); + insert(T->right_child,ch); else return; } -queueq; -void level(btptr T) +queueq; +void levelPrint(BT_ptr T) { - btptr temp = new btnode; + BT_ptr temp = new Btnode; temp->data = '#'; - temp->lc = NULL; - temp->rc = NULL; + temp->left_child = NULL; + temp->right_child = NULL; q.push(T); q.push(temp); while(q.size()!=1) { - btptr t = q.front(); + BT_ptr t = q.front(); q.pop(); if(t->data=='#') { @@ -68,21 +68,21 @@ void level(btptr T) else { cout<data<<" "; - if(t->lc!=NULL) - q.push(t->lc); - if(t->rc!=NULL) - q.push(t->rc); + if(t->left_child!=NULL) + q.push(t->left_child); + if(t->right_child!=NULL) + q.push(t->right_child); } } } int main() { - btptr T = NULL; + BT_ptr T = NULL; char ch; cin>>ch; insert(T,ch); - level(T); + levelPrint(T); } From d329ebae2b0942b2838c4db410155389f14abd8e Mon Sep 17 00:00:00 2001 From: Mohd Sufiyan Ansari Date: Wed, 21 Oct 2020 00:20:14 +0530 Subject: [PATCH 5/6] Renamed Files --- .../{Least_Common_Ancestor.cpp => leastCommonAncestor.cpp} | 0 res/Tree/{Level Order Printing.cpp => levelWiseTreeTraversal.cpp} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename res/Binary Search Tree/{Least_Common_Ancestor.cpp => leastCommonAncestor.cpp} (100%) rename res/Tree/{Level Order Printing.cpp => levelWiseTreeTraversal.cpp} (100%) diff --git a/res/Binary Search Tree/Least_Common_Ancestor.cpp b/res/Binary Search Tree/leastCommonAncestor.cpp similarity index 100% rename from res/Binary Search Tree/Least_Common_Ancestor.cpp rename to res/Binary Search Tree/leastCommonAncestor.cpp diff --git a/res/Tree/Level Order Printing.cpp b/res/Tree/levelWiseTreeTraversal.cpp similarity index 100% rename from res/Tree/Level Order Printing.cpp rename to res/Tree/levelWiseTreeTraversal.cpp From d24b917970d11007c2785e598cc224e3639d3768 Mon Sep 17 00:00:00 2001 From: Mohd Sufiyan Ansari Date: Wed, 21 Oct 2020 00:21:29 +0530 Subject: [PATCH 6/6] Renamed Files --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5aaeea7..9931ba3 100644 --- a/README.md +++ b/README.md @@ -2185,13 +2185,13 @@ more details. Traversals*](https://www.geeksforgeeks.org/618/) - [*Level order traversal*](https://www.geeksforgeeks.org/level-order-tree-traversal/) - - [Source Code](res/Tree/Tree_Level_Printing.cpp) + - [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/Least_Common_Ancestor.cpp) +- [*Least Common Ancestor in BST*](res/BST/leastCommonAncestor.cpp)   **Binary Heaps**