Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- typedef struct tree
- {
- int data;
- struct tree *left;
- struct tree *right;
- } tree;
- void add1(tree** list, int a, int level_loc, int *level_max)
- {
- tree *newNode;
- if ((*list) == NULL)
- {
- newNode = malloc(sizeof(tree));
- newNode->data = a;
- newNode->right = NULL;
- newNode->left = NULL;
- (*list) = newNode;
- }
- else if (a < (*list)->data)
- {
- add1(&((*list)->left), a, level_loc + 1 , level_max);
- }
- else if (a > (*list)->data)
- {
- add1(&((*list)->right), a, level_loc + 1 , level_max);
- }
- if ((level_loc)>(*level_max))
- {
- (*level_max) = level_loc;
- }
- return;
- }
- void output(tree **list, int current_height, int needed_height)
- {
- if ((*list) != NULL)
- {
- if (current_height == needed_height)
- {
- printf("%d ", (*list)->data);
- }
- output(&((*list)->left), current_height + 1, needed_height);
- output(&((*list)->right), current_height + 1, needed_height);
- }
- return;
- }
- int main(int argc, char **argv)
- {
- int level_counter, tree_counter = 0;
- int i, j, a, height = 0;
- tree *tree_0 = NULL;
- for (i = 1; i < argc; i++)
- {
- a = atoi(argv[i]);
- add1(&tree_0, a, 0, &height);
- }
- for (j = height; j >= 0; j--)
- {
- output(&tree_0, j, height);
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement