-
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
ac90e3a
commit affc5bd
Showing
1 changed file
with
97 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,97 @@ | ||
#include<stdlib.h> | ||
#include<stdio.h> | ||
|
||
struct bin_tree { | ||
int data; | ||
struct bin_tree * right, * left; | ||
}; | ||
typedef struct bin_tree node; | ||
|
||
void insert(node ** tree, int val) | ||
{ | ||
node *temp = NULL; | ||
if(!(*tree)) | ||
{ | ||
temp = (node *)malloc(sizeof(node)); | ||
temp->left = temp->right = NULL; | ||
temp->data = val; | ||
*tree = temp; | ||
return; | ||
} | ||
|
||
if(val < (*tree)->data) | ||
{ | ||
insert(&(*tree)->left, val); | ||
} | ||
else if(val > (*tree)->data) | ||
{ | ||
insert(&(*tree)->right, val); | ||
} | ||
|
||
} | ||
|
||
void print_preorder(node * tree) | ||
{ | ||
if (tree) | ||
{ | ||
printf("%c\n",tree->data); | ||
print_preorder(tree->left); | ||
print_preorder(tree->right); | ||
} | ||
|
||
} | ||
|
||
void print_inorder(node * tree) | ||
{ | ||
if (tree) | ||
{ | ||
print_inorder(tree->left); | ||
printf("%c\n",tree->data); | ||
print_inorder(tree->right); | ||
} | ||
} | ||
|
||
void print_postorder(node * tree) | ||
{ | ||
if (tree) | ||
{ | ||
print_postorder(tree->left); | ||
print_postorder(tree->right); | ||
printf("%c\n",tree->data); | ||
} | ||
} | ||
|
||
|
||
void main() | ||
{ | ||
node *root; | ||
node *tmp; | ||
//int i; | ||
|
||
root = NULL; | ||
//Inserting nodes into tree | ||
int m; | ||
printf("Enter the max value and elements:"); | ||
scanf("%d", &m); | ||
for(int i=0;i<m;i++) | ||
{ | ||
char ap; | ||
//printf("enter the element:"); | ||
scanf("%c", &ap); | ||
int n = ap; | ||
// printf("%d\n",n); | ||
|
||
insert(&root,n); | ||
} | ||
|
||
// Printing nodes of tree | ||
printf("Pre Order Display\n"); | ||
print_preorder(root); | ||
|
||
printf("In Order Display\n"); | ||
print_inorder(root); | ||
|
||
printf("Post Order Display\n"); | ||
print_postorder(root); | ||
|
||
} |