Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- typedef struct node
- {
- int data;
- struct node *left, *right;
- } x;
- x *root=NULL;
- x *temp=NULL;
- x *newx;
- void insert(x *temp, x *newx)
- {
- if (temp->data > newx->data)
- {
- if (temp->left == NULL)
- {
- temp->left=newx;
- }
- else
- {
- insert(temp->left, newx);
- }
- }
- else
- {
- if (temp->right == NULL)
- {
- temp->right=newx;
- }
- else
- {
- insert(temp->right, newx);
- }
- }
- }
- int maximum(x *temp)
- {
- while (temp->right != NULL)
- {
- temp=temp->right;
- }
- printf("Maximum : ");
- printf("%d\n", temp->data);
- }
- int minimum(x *temp)
- {
- while (temp->left != NULL)
- {
- temp= temp->left;
- }
- printf("Minimum : ");
- printf("%d\n", temp->data);
- }
- int search(x *temp, int x)
- {
- if (temp->data == x)
- {
- printf("Found\n");
- }
- else if (temp->left == NULL && temp->right == NULL)
- {
- printf("Not found\n");
- }
- else if (temp->data > x)
- {
- search(temp->left, x);
- }
- else if (temp->data <=x)
- {
- search(temp->right, x);
- }
- }
- int preorder(x *temp)
- {
- if (temp != NULL)
- {
- printf("%d ", temp->data);
- preorder(temp->left);
- preorder(temp->right);
- }
- }
- int inorder(x *temp)
- {
- if (temp != NULL)
- {
- inorder(temp->left);
- printf("%d ", temp->data);
- inorder(temp->right);
- }
- }
- int postorder(x *temp)
- {
- if (temp != NULL)
- {
- printf("%d ", temp->data);
- postorder(temp->left);
- postorder(temp->right);
- }
- }
- int main ()
- {
- int i, n;
- scanf("%d", &n);
- for (i=0; i<n; i++)
- {
- newx = (x*) malloc(sizeof(x));
- scanf("%d", &newx->data);
- newx->right=NULL;
- newx->left=NULL;
- if (root==NULL)
- {
- root=newx;
- }
- else
- {
- insert(root, newx);
- }
- }
- maximum(root);
- minimum(root);
- int x, p;
- scanf("%d", &x);
- for (i=0; i<x; i++)
- {
- scanf("%d", &p);
- search(root, p);
- }
- printf("\nPreorder\n");
- preorder(root);
- printf("\n\n\Inorder\n");
- inorder(root);
- printf("\n\nPostorder\n");
- postorder(root);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement