Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BT Traversal algorithms #6758

Closed
wants to merge 2 commits into from
Closed
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
53 changes: 53 additions & 0 deletions code/BinaryTree Traversal/LevelorderTraversal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include<iostream>
#include<queue>
#include<stack>
using namespace std;
struct node{
int data;
node*left;
node*right;
};
class BinaryTree{
public:
node*root;
int ch;
BinaryTree(){
root=nullptr;
ch=0;
}
//Binary Tree Creation Accordingly.
void treestructure(int arr[]){
root=new node{arr[0],nullptr,nullptr};
root->left=new node{arr[1],nullptr,nullptr};
root->right=new node{arr[2],nullptr,nullptr};
root->left->left=new node{arr[3],nullptr,nullptr};
root->left->right=new node{arr[4],nullptr,nullptr};
root->right->left=new node{arr[5],nullptr,nullptr};
}
//Traversing through levels starting from level 1 to all the way level h.
void Levelorder(node*curr){
if(curr==nullptr){
return;
}
queue<node*>q;
q.push(curr);
while(!q.empty()){
node*current=q.front();
cout<<current->data<<" ";
q.pop();
if(current->left){
q.push(current->left);
}
if(current->right){
q.push(current->right);
}
}
}
};
int main(){
BinaryTree b;
int arr[6]={1,2,3,4,5,6};
b.treestructure(arr);
b.Levelorder(b.root);
}

64 changes: 64 additions & 0 deletions code/BinaryTree Traversal/inordertraversal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include<iostream>
#include<queue>
#include<stack>
using namespace std;
struct node{
int data;
node*left;
node*right;
};
class BinaryTree{
public:
node*root;
int ch;
BinaryTree(){
root=nullptr;
ch=0;
}

//Binary Tree Creation Accordingly.
void treestructure(int arr[]){
root=new node{arr[0],nullptr,nullptr};
root->left=new node{arr[1],nullptr,nullptr};
root->right=new node{arr[2],nullptr,nullptr};
root->left->left=new node{arr[3],nullptr,nullptr};
root->left->right=new node{arr[4],nullptr,nullptr};
root->right->left=new node{arr[5],nullptr,nullptr};
}
//Inorder Traversing(left root right)
void Inorder(node*curr){
if(curr==nullptr){
return;
}
Inorder(curr->left);
cout<<curr->data<<" ";
Inorder(curr->right);
}

//Inorder but without Recursion using stack.
void IterativeInorder(node*curr){
if(curr==nullptr){
return;
}
stack<node*>s;
while(!s.empty()||curr!=nullptr){
if(curr!=nullptr){
s.push(curr);
curr=curr->left;
}
else{
curr=s.top();
s.pop();
cout<<curr->data<<" ";
curr=curr->right;
}
}
}
};
int main(){
BinaryTree b;
int arr[6]={1,2,3,4,5,6};
b.treestructure(arr);
b.IterativeInorder(b.root);
}

98 changes: 98 additions & 0 deletions code/BinaryTree Traversal/postorderTraversal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include<iostream>
#include<queue>
#include<stack>
using namespace std;
struct node{
int data;
node*left;
node*right;
};
class BinaryTree{
public:
node*root;
int ch;
BinaryTree(){
root=nullptr;
ch=0;
}

//Binary Tree Creation Accordingly.
void treestructure(int arr[]){
root=new node{arr[0],nullptr,nullptr};
root->left=new node{arr[1],nullptr,nullptr};
root->right=new node{arr[2],nullptr,nullptr};
root->left->left=new node{arr[3],nullptr,nullptr};
root->left->right=new node{arr[4],nullptr,nullptr};
root->right->left=new node{arr[5],nullptr,nullptr};
}
//Post order Traversing (left right root)
void Postorder(node*curr){
if(curr==nullptr){
return;
}
Postorder(curr->left);
Postorder(curr->right);
cout<<curr->data<<" ";
}
//using 2 stack
void TwoStackPostorder(node*curr){
if(curr==nullptr){
return;
}
stack<node*>s;
stack<node*>s1;
s.push(curr);
while(!s.empty()){
s1.push(s.top());
s.pop();
node*current=s1.top();
if(current->left){
s.push(current->left);
}
if(current->right){
s.push(current->right);
}
}
while(!s1.empty()){
node*temp=s1.top();
cout<<temp->data<<" ";
s1.pop();
}
}
//1 stack only
void OneStackPostorder(node*curr){
if(curr==nullptr){
return;
}
stack<node*>s;
while(!s.empty()||curr!=nullptr){
if(curr!=nullptr){
s.push(curr);
curr=curr->left;
}
else{
node *temp=s.top()->right;
if(temp==nullptr){
temp=s.top();
s.pop();
cout<<temp->data<<" ";
while(!s.empty()&&temp==s.top()->right){
temp=s.top();
s.pop();
cout<<temp->data<<" ";
}
}
else{
curr=temp;
}
}
}
}
};
int main(){
BinaryTree b;
int arr[6]={1,2,3,4,5,6};
b.treestructure(arr);
b.Postorder(b.root);
}

63 changes: 63 additions & 0 deletions code/BinaryTree Traversal/preorderTraversal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include<iostream>
#include<queue>
#include<stack>
using namespace std;
struct node{
int data;
node*left;
node*right;
};
class BinaryTree{
public:
node*root;
int ch;
BinaryTree(){
root=nullptr;
ch=0;
}

//Binary Tree Creation Accordingly.
void treestructure(int arr[]){
root=new node{arr[0],nullptr,nullptr};
root->left=new node{arr[1],nullptr,nullptr};
root->right=new node{arr[2],nullptr,nullptr};
root->left->left=new node{arr[3],nullptr,nullptr};
root->left->right=new node{arr[4],nullptr,nullptr};
root->right->left=new node{arr[5],nullptr,nullptr};
}
//Preorder Traversing(print untill left goes to null the root right)
void Preorder(node*curr){
if(curr==nullptr){
return;
}
cout<<curr->data<<" ";
Preorder(curr->left);
Preorder(curr->right);
}
//Traversing but not through recursion but through stack
void IterativePreorder(node*curr){
if(curr==nullptr){
return;
}
stack<node*>s;
s.push(curr);
while(!s.empty()){
node*current=s.top();
cout<<current->data<<" ";
s.pop();
if(current->right){
s.push(current->right);
}
if(current->left){
s.push(current->left);
}
}
}
};
int main(){
BinaryTree b;
int arr[6]={1,2,3,4,5,6};
b.treestructure(arr);
b.IterativePreorder(b.root);
}