zoukankan      html  css  js  c++  java
  • COJ1005(Binary Search Tree analog)

    题目链接

    二分查找树的模拟。不能用数组模拟,那样内存会爆掉。

    View Code
     1 #include <stdio.h>
     2 #define N 1000
     3 struct node
     4 {
     5   int x;
     6   struct node *left,*right;
     7 }node[N];
     8 char f;
     9 void Insert(struct node *r,struct node *p)
    10 {
    11   if(!r)  return;
    12   if(r->x>p->x)
    13   {
    14     if(r->left) Insert(r->left,p);
    15     else  r->left=p;
    16   }
    17   else
    18   {
    19     if(r->right)  Insert(r->right,p);
    20     else  r->right=p;
    21   }
    22 }
    23 void pretral(struct node *p)
    24 {
    25   if(!p)  return;
    26   if(f) printf("%d",p->x),f=0;
    27   else  printf(" %d",p->x);
    28   pretral(p->left);
    29   pretral(p->right);
    30 }
    31 void intral(struct node *p)
    32 {
    33   if(!p)  return;
    34   intral(p->left);
    35   if(f) printf("%d",p->x),f=0;
    36   else  printf(" %d",p->x);
    37   intral(p->right);
    38 }
    39 void postod(struct node *p)
    40 {
    41   if(!p)  return;
    42   postod(p->left);
    43   postod(p->right);
    44   if(f) printf("%d",p->x),f=0;
    45   else  printf(" %d",p->x);
    46 }
    47 int main()
    48 {
    49   int t,i,n,x;
    50   struct node root;
    51   struct node *p;
    52   scanf("%d",&t);
    53   while(t--)
    54   {
    55     scanf("%d",&n);
    56     scanf("%d",&root.x);
    57     root.left=root.right=0;
    58     for(i=0;i<n-1;i++)
    59     {
    60       scanf("%d",&x);
    61       p=&node[i];
    62       p->x=x;
    63       p->left=p->right=0;
    64       Insert(&root,p);
    65     }
    66     f=1;
    67     pretral(&root);
    68     printf("\n");
    69     f=1;
    70     intral(&root);
    71     printf("\n");
    72     f=1;
    73     postod(&root);
    74     printf("\n");
    75     printf("\n");
    76   }
    77   return 0;
    78 } 
  • 相关阅读:
    员工转正考核
    前端高级技术考察
    前端基础技术考察
    高级前端评定表
    初级前端评定表
    前端工程师等级评定
    前端软实力
    Decode Ways
    深度学习在目标跟踪中的应用
    Interleaving String
  • 原文地址:https://www.cnblogs.com/algorithms/p/2470345.html
Copyright © 2011-2022 走看看