已知前序是ABCDEFG   中序是CBEDAFG   求二叉树  
   
  首先从取前序第1个字母(A)   按此字母把中序分成两段   (CBED)   (AFG)  
  A做根   (CBED)做左子树   (FG)右子树  
  再按长度把前序后面的部分分成(BCDE)   (FG)  
   
  问题就转换成  
  已知前序是BCDE   中序是CBED   求二叉树  
  和  
  已经前序是FG   中序是FG   求二叉树  
   
  看出来了没有,递归求解的.  
  下面的步骤省略分析.  
  前序   中序   :   根   左子树分解   右子树分解  
  BCDE   CBED   :   B       (C)   (C)         (ED)   (DE)  
  C         C         :   C  
  DE    ED       :   D       (E)   (E)  
  E         E         :   E  
   
  FG       FG       :   F                             (G)   (G)  
  G         G         :   G  
  得出的二叉树就是  
            A  
          /         B       F  
      /   \         C       D       G  
          /  
        E  
   
  /*Function:    
    *     rebirth   the   tree   by   it's   preorder   squence   and   inorder   squence.  
    *Prototype:    
    * struct   btnode   *   Create_Tree(int   *   PreorderSquen,   int   *   InorderSquen,   int   SquenLength)  
    *Input   parameter:  
    *     PreorderSquen:   an   integer   pointer,   denote   the   start   address   of   the   preorder   squence  
    *     InorderSquen:   an   integer   pointer,   denote   the   start   address   of   the   inorder   squence  
    *     SquenceLength:   an   integer,   denote   the   length   of   the   squence,   namely   the   number   of   the   tree's   node    
    *Output:  
    *     a   btnode   pointer,   point   the   root   of   the   created   tree  
    *Notice:  
    *     the   pointers,   Preorder   and   Inorder,   shouldn't   be   null  
    *     also,   the   two   squence   should   have   the   same   set   of   nodes  
    *     the   value   of   SquenLength   should   be   bigger   than   zero  
    */  
   
    struct   btnode  
    {  
    int   value;  
    struct   btnode   *   LeftChild;  
    struct   btnode   *   RightChild;  
    };  
   
  struct   btnode   *   Create_Tree(int   *   PreorderSquen,   int   *   InorderSquen,   int   SquenLength)  
  {  
  int   position_inorder; //save   where   the   head   element   of   preorder   squence   is   in   the   inoder   squence  
  struct   btnode   *   head; //point   the   root   of   the   tree  
   
  if   (SquenLength   >   0)  
  {  
  //initialize   the   new   node  
  head   =   (struct   btnode   *)malloc(sizeof(struct   btnode));  
  head->value   =   *PreorderSquen;  
  head->LeftChild   =   NULL;  
  head->RightChild   =   NULL;  
   
  position_inorder   =   Find(*PreorderSquen,   InorderSquen,   SquenLength);  
  /*here   ignore   the   condition   that   the   function   Find   return   -1*/  
  head->LeftChild   =   Create_Tree(PreorderSquen++,   InorderSquen,   position_inorder);  
  head->RightChild   =   Create_Tree((PreorderSquen   +   position_inorder),   (InorderSquen   +   position_inorder),   (SquenceLength   -   position_inorder));  
  return   head;  
  }  
  else    
  return   NULL;  
  }  
   
  //return   the   element   x   position   in   object_squence,   from   1   to   length  
  int   Find(int   x,   int   *   object_squence,   int   length)  
  {  
  int   i;  
   
  i   =   0;  
  while   ((i   <   length)   &&   (*(object_squence   +   i)   !=   x))  
  {  
  i++;  
  }  
  if   (i   <   length)  
  {  
  return   (i+1);  
  }  
  else  
  {  
  return   -1;  
  }  
  }  
 
posted on 2009-10-17 22:48  AlexusLi  阅读(690)  评论(0编辑  收藏  举报