Create Tree from bottom to top

#include<stdio.h>
#include<stdlib.h>
typedef struct bin_tree_t
{
  struct bin_tree_t *parent;
  struct bin_tree_t *left;
  struct bin_tree_t *right;
  struct bin_tree_t *first;
  struct bin_tree_t *next;

  unsigned int val;

}intTree;

intTree *createIntTree(intTree *left,intTree *right,unsigned int token)
{
    intTree *pt;
    pt=(intTree *)malloc(sizeof(intTree));

    if(__builtin_expect(pt==NULL,0))
    {
        return NULL;
    }
    pt->val=token;
    pt->next=NULL;
    pt->first=NULL;
    pt->parent=NULL;
    pt->left=left;
    pt->right=right;

    if(left!=NULL)
        left->parent=pt;
    if(right!=NULL)
        right->parent=pt;
    return pt;

}


int main(void)
{
    intTree *t1;
    intTree *t2;
    intTree *p;
    intTree *root;
    int loop;

    t1=createIntTree(NULL,NULL,1);

    t2=t1;
    for(loop=2;loop<10;loop++)
    {
        p=createIntTree(t1,NULL,loop);
        t1=p;
    }

    //root=p;
    //visit tree from Top->bottom
    while(p)
    {
        printf("p->val=%d\n",p->val);
        p=p->left;

    }

    printf("###########################################\n");
    //Visit tree from bottom to top
    p=t2;
    while(p)
    {
        printf("p->val=%d\n",p->val);
        p=p->parent;

    }


}

Works very much like a doubly linked list

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325477491&siteId=291194637