forked from keshavsingh4522/hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c8ff43d
commit 39cb747
Showing
1 changed file
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
//TREE TRAVERSAL | ||
#include<iostream> | ||
#include<algorithm> | ||
using namespace std; | ||
struct node{ | ||
int data; | ||
node* left; | ||
node* right; | ||
}; | ||
|
||
node* newNode(int key) | ||
{ | ||
node* tmp = new node(); | ||
tmp->data = key; | ||
tmp->left = tmp->right = nullptr; | ||
|
||
return tmp; | ||
} | ||
|
||
//INSERTION | ||
node* insert(node* root, int l_data){ | ||
if(root == nullptr){ | ||
return newNode(l_data); | ||
} | ||
if(l_data <= root->data){ | ||
root->left = insert(root->left, l_data); | ||
}else{ | ||
root->right = insert(root->right, l_data); | ||
} | ||
return root; | ||
} | ||
|
||
//INORDER TRAVERSAL | ||
void inorder(node* root){ | ||
if(root == nullptr){ | ||
return; | ||
} | ||
inorder(root->left); | ||
cout<<root->data<<" "; | ||
inorder(root->right); | ||
} | ||
|
||
//PREORDER TRAVERSAL | ||
void preOrder(node* root){ | ||
if(root == nullptr){ | ||
return; | ||
} | ||
cout<<root->data<<" "; | ||
preOrder(root->left); | ||
preOrder(root->right); | ||
} | ||
|
||
//POSTORDER TRAVERSAL | ||
void postOrder(node* root){ | ||
if(root == nullptr){ | ||
return; | ||
} | ||
postOrder(root->left); | ||
postOrder(root->right); | ||
cout<<root->data<<" "; | ||
} | ||
|
||
int main(){ | ||
node *root = newNode(1); | ||
|
||
root->left = newNode(2); | ||
root->right = newNode(3); | ||
root->left->left = newNode(4); | ||
root->left->right = newNode(5); | ||
cout<<"\n Pre order transversal: "; | ||
preOrder(root); | ||
cout<<"\n Inorder transversal: "; | ||
inorder(root); | ||
cout<<"\n Post order transversal: "; | ||
postOrder(root); | ||
} |