#include <stdio.h>
#include <stdlib.h>

struct node {
    int val;
    struct node *left, *right;
};

struct list {
    struct node *val;
    struct list *next;
};

void init_node(struct node **r, int val) {
    *r = malloc(sizeof **r);
    (*r)->val = val;
    (*r)->left = NULL;
    (*r)->right = NULL;
}

void push_list(struct list **p, struct node *val) {
    struct list *tmp;
    tmp = malloc(sizeof *tmp);
    tmp->val = val;
    tmp->next = *p;
    *p = tmp;
}

void pop_list(struct list **p, struct node **val) {
    struct list *tmp;
    tmp = *p;
    *val = tmp->val;
    *p = (*p)->next;
    free(tmp);
}

int in_list(struct list *p, struct node *val) {
    for ( ; p ; p = p->next)
        if (p->val == val)
            return 1;
    return 0;
}

int empty_list(struct list *p) {
    if (p)
        return 0;
    else
        return 1;
}

void print_tree(struct node *r) {
    if (!r)
        return;
    print_tree(r->left);
    printf("%d ", r->val);
    print_tree(r->right);
}

/*
             27
       /            \
     18             34
   /    \         /     \
  4     21       29     39
 / \   /  \     /  \   /  \
n   n n    n   n    n n    n
*/
void print_tree2(struct node *current) {
    struct list *prev = NULL, *saw = NULL;
    while (current) {
        if (current->left && !in_list(saw, current->left)) {
            push_list(&prev, current);
            current = current->left;
        } else if (current->right && !in_list(saw, current->right)) {
            push_list(&prev, current);
            current = current->right;
        } else {
            printf("%d ", current->val);
            if (!empty_list(prev))
                pop_list(&prev, &current);
            else
                return;
        }
        push_list(&saw, current);
    }
}

/*
             27
       /            \
     18              34
   /    \         /     \
  4     21       29     39
 / \   /  \     /  \   /  \
n   n n    n   n    n n    n
4 21 18 29 39 34 27
18 4 18 21 18 27 34 29 34 39 34 27
4 18 21 18 27 29 34 39 34 27
4 18 21 27 29 34 39
*/
void print_tree3(struct node *current) {
    struct list *prev = NULL, *seen = NULL;
    while (current) {
        push_list(&seen, current);
        if (current->left && !in_list(seen, current->left)) {
            push_list(&prev, current);
            current = current->left;
        } else if (current->right && !in_list(seen, current->right)) {
            push_list(&prev, current);
            current = current->right;
        } else {
            if (!empty_list(prev))
                pop_list(&prev, &current);
            else
                return;
        }
        if ((!current->left || in_list(seen, current->left)) &&
            (!current->right || !in_list(seen, current->right)))
            printf("%d ", current->val);
    }
}

int sum_tree(struct node *r) {
    if (!r)
        return 0;
    return sum_tree(r->left) + r->val + sum_tree(r->right);
}

struct node **search_tree(struct node **r, int val) {
    if (!*r || (*r)->val == val)
        return r;
    else if (val < (*r)->val)
        r = search_tree(&(*r)->left, val);
    else if (val > (*r)->val)
        r = search_tree(&(*r)->right, val);
}

struct node **search_tree2(struct node **r, int val) {
    while (*r) {
        if ((*r)->val == val)
            return r;
        else if (val < (*r)->val)
            r = &(*r)->left;
        else if (val > (*r)->val)
            r = &(*r)->right;
    }
    return r;
}

int insert_node(struct node **r, int val) {
    struct node **pos = search_tree2(r, val);
    if (!*pos) {
        init_node(pos, val);
        return 1;
    } else
        return 0;
}

int in_tree(struct node *r, int val) {
    struct node **pos = search_tree2(&r, val);
    if (*pos)
        return 1;
    else
        return 0;
}

int main() {
    int res;
    struct node *r = NULL;
    insert_node(&r, 27);
    insert_node(&r, 18);
    insert_node(&r, 34);
    insert_node(&r, 4);
    insert_node(&r, 21);
    insert_node(&r, 29);
    insert_node(&r, 39);
    print_tree3(r);
    printf("\n");
    /*
    res = in_tree(r, 4);
    printf("%d\n", res);
    res = sum_tree(r);
    printf("%d\n", res);
    */
    return 1;
}
