[2016.1.15] 集训第二天 树 指针

来源:互联网 发布:jquery 遍历json数据 编辑:程序博客网 时间:2024/06/08 06:45

指针是一个好痛苦的事情

然而存树也是

可能我什么时候回望时也会觉得今天的自己很弱吧!

指针完全教程

http://www.runoob.com/cplusplus/cpp-pointers.html
然而如何存树(前序中序后序遍历)
#include<stdio.h>  struct node  {         int data;      struct node* lson,* rson;        node(){lson=rson=NULL;data=0;      }  };  struct node *root,*p,address[1000+5];  int t=0;    void build(struct node *&bt)  {        int x;      scanf("%d",&x);      if(x!=-1)      {          bt=&address[++t];          bt->data=x;//下面两种方式等价;           //(*bt).data=x;          //address[t].data=x;          build(bt->lson);          build(bt->rson);      }      else bt=NULL;        }  void preorder(struct node* bt)  {      if(bt)        {             printf("%d ",bt->data);             preorder(bt->lson);                    preorder(bt->rson);             }  } void inorder(struct node* bt)  {      if(bt)        {         inorder(bt->lson);         printf("%d ",bt->data);             inorder(bt->rson);             }  } void postorder(struct node* bt)  {      if(bt)        {         postorder(bt->lson);         postorder(bt->rson); printf("%d ",bt->data);             }  }  int main()  {      int n,x;      build(root);      preorder(root);      return 0;       }  

                                             
1 0
原创粉丝点击