| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 447 人关注过本帖
标题:二叉树程序错误?
只看楼主 加入收藏
henji
Rank: 1
等 级:新手上路
帖 子:227
专家分:0
注 册:2009-4-19
结帖率:38.67%
收藏
 问题点数:0 回复次数:2 
二叉树程序错误?
#include "stdafx.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#define NULL 0
typedef struct bitnode
{   
    char data;   
    struct bitnode *lchild,*rchild;
}bitnode,*bitree;
bitree create(bitree t)//为什么一直输入字符?而不不能退出,程序该如何修改?
{   
    char ch;   
    scanf("%c",&ch);   
    if(ch=='#')
    {
        t=NULL;
    }
    else
    {        
            t=(bitree)malloc(sizeof(bitnode));        
            t->data=ch;   
         
            t->lchild=create(t->lchild);        
            t->rchild=create(t->rchild);  
        }   
   
    return t;
}

void preorder(bitree t)
{   
    if(t)
    {        
        printf("%c",t->data);         
        preorder(t->lchild);               
        preorder(t->rchild);      
      
    }
}

int main(int argc, char* argv[])
{
    bitree t=NULL;   
    t=create(t);   
    preorder(t);
    return 0;
}
搜索更多相关主题的帖子: 二叉树 程序错误 
2010-04-26 21:32
Devil_W
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:9
帖 子:1160
专家分:1797
注 册:2009-9-14
收藏
得分:0 
程序代码:
-BASH-4.0.35$ cat BinTree.h 
/*

 * Author: Devil Wang

 * Data: 01-21-2010

 * 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
2010-04-26 21:47
linjx0123
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:279
专家分:1362
注 册:2006-4-7
收藏
得分:0 
bitree create(bitree t)//为什么一直输入字符?而不不能退出,程序该如何修改?

因为你创建树是用递归创建,递归一直没有结束,最后的那个return t也永远没有可能执行,所以就不能退出了。

试着
if(ch=='#')
    {
        t=NULL;
        return t;
    }
这个语句看看
2010-04-26 22:09
快速回复:二叉树程序错误?
数据加载中...
 
   



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

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