| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 531 人关注过本帖
标题:还是二叉树。。搞不定呀
只看楼主 加入收藏
Nekomimi
Rank: 2
等 级:论坛游民
帖 子:80
专家分:15
注 册:2006-11-21
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:2 
还是二叉树。。搞不定呀
#include <stdio.h>
#define MAX 100
typedef struct BiTNode{
    int data;
    struct BitNode *lchild,*rchild;
}BiTNode,*BiTree;
int creatT(BiTree *T,char c){
    if(c==' '){
        *T=NULL;
    }else{
        if(!((*T)=(BiTNode *)malloc(sizeof(BiTNode)))){
            printf("ERROR");
            return 0;
        }
        (*T)->data=c;
        (*T)->lchild=NULL;
        (*T)->rchild=NULL;
    }
    return 1;
}
void creatTT(BiTree *M,BiTree *N,BiTree *T){
    (*T)->lchild=*M;
    (*T)->rchild=*N;
}
void printfT(BiTree *T){
    if(*T==NULL){
        printf(" ");
    }else{
        printf("%c",(*T)->data);
        printfT(&((*T)->lchild));
        printfT(&((*T)->rchild));
    }
}
int nu(int c){
    if(c=='('||c==')'||c=='+'||c=='-'||c=='*'||c=='/'||c=='#'){
        return 1;
    }else{
        return 0;
    }
}
int than(int m,int c){
    if(m=='+'||m=='-'){
        if(c=='+'||c=='-'||c=='#'||c==')'){
            return 1;
        }else if(m=='*'||m=='/'||c=='('){
            return -1;
        }
    }else if(m=='*'||m=='/'){
        if(c=='+'||c=='-'||c=='*'||c=='/'||c==')'||c=='#'){
            return 1;
        }else if(c=='('){
            return -1;
        }
    }else if(m=='('){
        if(c=='+'||c=='-'||c=='*'||c=='/'||c=='('){
            return -1;
        }else if(c==')'){
            return 0;
        }
    }else if(m==')'){
        if(c=='+'||c=='-'||c=='*'||c=='/'||c==')'||c=='#'){
            return 1;
        }
    }else{
        if(c=='+'||c=='-'||c=='*'||c=='/'||c=='('||c=='#'){
            return -1;
        }else if(c=='#'){
            return 0;
        }
    }
}

int main(int argc, char *argv[])
{
    BiTree *T,*M,*b[MAX],*x,*y;
    int a[MAX],c,i=0,j=0,k,t,r;
    a[0]='#';
    c=getchar();
    while(a[1]!='#'&&c!=EOF){
        if(nu(c)==0){      \\如果是数字
            c-='0';
            creatT(&M,c);
            b[j++]=M;
            c=getchar();
        }else{                    
            k=than(a[i],c);       \\比较运算符优先级
            switch(k){
                case -1:
                    a[++i]=c;
                    c=getchar();
                    break;
                case 0:
                    i--;
                    c=getchar();
                    break;
                case 1:
                    x=b[--j];
                    y=b[--j];
                    t=a[i--];
                    creatT(&T,t);
                    creatTT(y,x,&T);
                    b[j++]=T;
                    break;
                default:break;
            }
        }
    }
    printfT(&T);
    return 0;
}
建立一个计算式的二叉树,输入#结束,比如:2*3+5#。。然后打印出来:+*2  3  5  ,注意有空格。。
现在只能打印出+,而且弹出错误。。到底是哪儿错了呀。。


[ 本帖最后由 Nekomimi 于 2010-1-26 21:45 编辑 ]
搜索更多相关主题的帖子: 二叉树 
2010-01-26 21:42
fqtb16
Rank: 7Rank: 7Rank: 7
来 自:上海
等 级:黑侠
帖 子:96
专家分:504
注 册:2009-12-28
收藏
得分:0 
也是不熟悉

爱拼才会赢
2010-01-27 18:01
Devil_W
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:9
帖 子:1160
专家分:1797
注 册:2009-9-14
收藏
得分:10 
程序代码:
/*                                
 * Author: Devil Wang             
 * Data: 03-21-2009               
 * Notes: Template For Binary Tree
 */                               
#ifndef __BINTREE_H_              
#define __BINTREE_H_              
#include<iostream>                
#include<cstdlib>                 
using std::cin;                   
using std::cout;                  
struct Exception{};               
template<class KeyType,class InfoType>
     struct TNode                   
     {                              
          KeyType key;              
          InfoType info;            
          TNode<KeyType,InfoType> *left;
          TNode<KeyType,InfoType> *right;
          TNode<KeyType,InfoType> *parent;
          TNode<KeyType,InfoType>():left(NULL),right(NULL),parent(NULL){}
          TNode<KeyType,InfoType>(KeyType val, InfoType infoval=0):key(val),left(NULL),right(NULL),parent(NULL)                                                 
               {                                                               
                    info=infoval;                                              
               }                                                               
          friend ostream & operator<<(ostream &out,TNode<KeyType,InfoType> &Node)
               {                                                               
                    out<<Node.key;                                             
                    return out;                                                
               }                                                               
     } ;                                                                       
template<class KeyType, class InfoType>                                        
     class BinTree                                                             
{                                                                              
private:                                                                       
     TNode<KeyType,InfoType> *root;                                            
     size_t Size;                                                              
protected:                                                                     
     TNode<KeyType,InfoType> *minimum( TNode<KeyType,InfoType> *point)const    
     {                                                                         
          TNode<KeyType,InfoType> *p=point;                                    
          while( NULL != p->left )                                             
          {                                                                    
               p=p->left;                                                      
          }                                                                    
          try{                                                                 
               if( NULL == p )                                                 
                    throw Exception();                                         
               return p;                                                       
          }catch( Exception e )                                                
           {                                                                   
                exit;                                                          
           }                                                                   
     }                                                                         
     TNode<KeyType,InfoType> * maximum(TNode<KeyType,InfoType> *point)const    
     {                                                                         
          TNode<KeyType,InfoType> *p=point;                                    
          while( NULL != p->right )                                            
          {                                                                    
               p=p->right;                                                     
          }                                                                    
          try{                                                                 
               if( NULL == p )                                                 
                    throw Exception();                                         
               return p;                                                       
          }catch( Exception e )                                                
           {                                                                   
                exit;                                                          
           }                                                                   

     }
     int leafcont(TNode<KeyType,InfoType> *pointer )
     {                                            
          static int cont=0;                      
          if (NULL!=pointer)                      
          {                                       
               leafcont(pointer->left);           
               if(NULL== pointer->left && NULL==pointer->right)
               {                                             
                    cont++;                                  
               }                                             
               leafcont(pointer->right);                     
               return cont;                                  
          }                                                  
     }                                                       
     void travel(TNode<KeyType,InfoType> *T)const            
     {                                                       
          if( NULL!=T )                                      
          {                                                  
               travel(T->left);                              
               cout<<*T<<" ";                                
               travel(T->right);                             
          }                                                  
          return ;                                           
     }                                                       
public:                                                      
     BinTree<KeyType,InfoType>():root(NULL),Size(0){}        
     BinTree<KeyType,InfoType>(int n):root(NULL),Size(0)     
     {                                                       
          KeyType m;                                         
          for(int i=0; i<n;i++)                              
          {                                                  
               cin>>m;                                       
               BTinsert(m);                                  
          }                                                  
     }                                                       
     ~BinTree()                                              
     {                                                       
         TNode<KeyType,InfoType> *p;                         
         while( NULL != root->left || NULL != root->right)   
         {                                                   
              p=minimum(root);                               
              BTDelete(p);                                   
         }                                                   
         if( NULL !=root )                                   
         {                                                   
              delete root;                                   
         }                                                   
     }                                                       
     void BTInsert(KeyType val)                              
     {                                                       
          if( NULL == root)                                  
          {                                                  
               root=new TNode<KeyType,InfoType>(val);        
               root->parent=root;                            
               Size++;                                       
               return ;                                      
          }                                                  
          else                                               
          {                                                  
               TNode<KeyType,InfoType> *pr=root,*cur;        
               cur=(root->key>val)?root->left:root->right;   
               while( NULL != cur )                          
               {                                             
                    pr=cur;                                  
                    cur=cur->key>val?cur->left:cur->right;   
               }                                             
               try{                                          
                    if( NULL != cur )                        
                         throw Exception();                  
                    cur=new TNode<KeyType,InfoType> (val);   
                    cur->parent=pr;                          
                    if( pr->key > val)                       
                         pr->left=cur;                       
                    else                                     
                         pr->right=cur;                      
               }catch( Exception())                          
                {                                            
                     cout<<"Catch An Exception"<<endl;       
                     cout<<"BTInsert"<<endl;                 
                     exit;                                   
                }                                            
               Size++;                                       
          }                                                  
     }                                                       
     TNode<KeyType,InfoType> * BTSearch(KeyType val)const    
     {                                                       
          TNode<KeyType,InfoType> * p=root;                  
          while( NULL != p && p->key!=val )                  
          {                                                  
               p=(p->key>val)?p->left:p->right;              
          }                                                  
          try                                                
          {                                                  
               if( NULL == p )                               
                    throw Exception();                       
               return p;                                     
          }catch ( Exception e)                              
           {                                                 
                cout<<"Catch An Exception"<<endl;            
                cout<<"BTSearch"<<endl;                      
                return NULL;                                 
           }                                                 
     }                                                       
     TNode<KeyType,InfoType> *BTMaximum()const{return maximum(root);}
     TNode<KeyType,InfoType> *BTMinimum()const{return minimum(root);}
     size_t size()                                                 
     {                                                             
          return Size;                                             
     }                                                             
     bool empty()                                                  
     {                                                             
          return Size==0;                                          
     }                                                             
     TNode<KeyType,InfoType> *BTSuccessor( TNode<KeyType,InfoType> *pointer )const                                                                              
     {                                                                         
          try                                                                  
          {                                                                    
               TNode<KeyType,InfoType> *point=pointer;                         
               if( NULL == point || point== maximum(root))                     
               {                                                               
                    throw Exception();                                         
               }                                                               
               if ( NULL != point->right )                                     
                    return point->right;                                       
               TNode<KeyType,InfoType> *pr=point->parent;                      
               if( NULL == pr )                                                
                    throw Exception();                                         
               while( NULL != pr && point==pr->right )                         
               {                                                               
                    point=pr;                                                  
                    pr=pr->parent;                                             
               }                                                               
               return pr;                                                      
          }catch( Exception e)                                                 
           {                                                                   
                cout<<"Catch An Exception" <<endl;                             
                cout<<"BTSuccessor"<<endl;                                     
                return NULL;                                                   
           }                                                                   
     }                                                                         
     TNode<KeyType,InfoType> * BTPredecessor(TNode<KeyType,InfoType> *pointer )const                                                                            
     {                                                                         
          try                                                                  
          {                                                                    
               TNode<KeyType,InfoType> *point=pointer,*pr;                     
               if( NULL == point || point == BinTree::minimum(root))           
                    throw Exception();                                         
               if( NULL != point->left )                                       
                    return point->left;                                        
               pr=point->parent;                                               
               while( pr->left == point && NULL != pr )                        
               {                                                               
                    point=pr;                                                  
                    pr=pr->parent;                                             
               }                                                               
               return pr;                                                      
          }catch ( Exception e )                                               
           {                                                                   
                cout<<"Catch An Exception" <<endl;                             
                cout<<"BTPredecessor"<<endl;                                   
                return NULL;                                                   
           }                                                                   
     }                                                                         
     void BTDelete( TNode<KeyType,InfoType> * z)                               
     {                                                                         
          TNode<KeyType,InfoType> *x,*y;                                       
          if ( NULL == z->left && NULL == z->right && z==z->parent )           
          {                                                                    
               delete z;                                                       
               z=NULL;                                                         
               root=NULL;                                                      
               Size--;                                                         
               return ;                                                        
          }                                                                    
          if( NULL==z->left || NULL == z->right )                              
               y=z;                                                            
          else                                                                 
               y=BinTree::BTSuccessor(z);                                      
          if( NULL != y->left)                                                 
               x=y->left;                                                      
          else                                                                 
               x=y->right;                                                     
          if ( NULL !=x )                                                      
               x->parent=y->parent;                                            
          if( y->parent==y)                                                    
          {                                                                    
               root=x;                                                         
               x->parent=x;                                                    
          }
          else if (y== y->parent->left)
               y->parent->left=x;
          else
               y->parent->right=x;
          if( z!=y )
          {
               z->key=y->key;
               z->info=y->info;
          }
          delete y;
          Size--;
     }
     void BTTravel()
     {
          travel(root);
          cout<<endl;
     }
     int BTLeafCont(){return leafcont(root);}
};
#endif



N久前写的,2叉排序树的库。

应付你那个应该没问题。
2010-01-28 17:48
快速回复:还是二叉树。。搞不定呀
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020572 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved