Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
- #include <string.h>
- typedef struct _node
- {
- int key;
- struct _node *left, *right;
- } node;
- void insert(node** at, node* elem)
- {
- assert(elem);
- if(!*at)
- {
- *at = elem;
- }
- else if(elem->key < (*at)->key)
- {
- insert(&(*at)->left, elem);
- }
- else
- {
- insert(&(*at)->right, elem);
- }
- }
- void print_tree(node* at)
- {
- static int i = 0;
- int k;
- if(!at)
- {
- return;
- }
- else
- {
- i++;
- }
- print_tree(at->right);
- for(k = 0; k < i; k++)
- {
- printf(" ");
- }
- printf("%d\n", at->key);
- print_tree(at->left);
- i--;
- }
- int main()
- {
- int i, n;
- node *root = NULL, *np = NULL;
- scanf("%d", &n);
- for(i = 0; i < n; i++)
- {
- np = (node*)malloc(sizeof(node));
- memset(np, 0, sizeof(node));
- scanf("%d", &np->key);
- insert(&root, np);
- }
- printf("\n");
- print_tree(root);
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment