Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <malloc.h>
- #include <string.h>
- struct binary_tree{
- int root;
- struct binary_tree *left, *right;
- };
- struct binary_tree* new_binary_tree(int x){
- struct binary_tree *p;
- p =(struct binary_tree*) malloc(sizeof(struct binary_tree));
- p->root= x;
- p->left = NULL;
- p->right = NULL;
- return p;
- }
- void inorderPref(struct binary_tree *root)
- {
- if(root!=NULL)
- {
- printf("%d ", root->root);
- inorderPref(root->left);
- inorderPref(root->right);
- }
- }
- void inorderPost(struct binary_tree *root)
- {
- if(root!=NULL)
- {
- inorderPost(root->left);
- inorderPost(root->right);
- printf("%d ", root->root);
- }
- }
- struct binary_tree* insert(struct binary_tree *root, int x){
- if(root==NULL)
- return new_binary_tree(x);
- else
- if(x>root->root)
- root->right = insert(root->right, x);
- else
- if (x < root->root)
- root->left = insert(root->left,x);
- return root;
- }
- int main(){
- struct binary_tree *root;
- FILE *file;
- file = fopen("input.txt", "r");
- int n;
- fscanf(file, "%d", &n);
- root = new_binary_tree(n);
- while (fscanf(file, "%d", &n)==1){
- insert(root, n);
- }
- inorderPref(root);
- printf("\n");
- inorderPost(root);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement