Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- struct node
- {
- int data;
- struct node* left;
- struct node* right;
- };
- struct node* createNode(int value)
- {
- struct node* newNode = (struct node*) malloc (sizeof(struct node));
- newNode->data = value;
- newNode->left = NULL;
- newNode->right = NULL;
- return newNode;
- }
- struct node* insert(struct node* root, int data)
- {
- if (root == NULL)
- return createNode(data);
- if (data < root->data)
- root->left = insert(root->left, data);
- else if (data > root->data)
- root->right = insert(root->right, data);
- return root;
- }
- void inorder(struct node* root)
- {
- if(root == NULL)
- return;
- inorder(root->left);
- printf("%d ->", root->data);
- inorder(root->right);
- }
- int main()
- {
- struct node *root = NULL;
- int n, k, value;
- scanf("%d %d", &n, &k);
- while(n--) {
- scanf("%d", &value);
- root = insert(root, value);
- }
- inorder(root);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement