| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 408 人关注过本帖
标题:求助 帮修改一下程序 C++ 根据二叉树的中序和后序序列重新生成二叉树
只看楼主 加入收藏
舞伶舞
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2013-12-26
收藏
 问题点数:0 回复次数:1 
求助 帮修改一下程序 C++ 根据二叉树的中序和后序序列重新生成二叉树
已知二叉树T中结点的中序和后序遍历序列,建立二叉树,并输出先序序列。
 这是我写的程序 最后有错误 求助!主要是源代码最后两段的问题 求解答 !
.h如下:
 #include<iostream>
 using namespace std;
 #include<stdlib.h>

 template<class T>
 struct BinTreeNode{//链式二叉树结点结构
    T data;
     BinTreeNode<T> *leftChild,*rightChild;
     BinTreeNode():leftChild(NULL),rightChild(NULL){ }
     BinTreeNode(T x,BinTreeNode<T> *l=NULL,BinTreeNode<T> * r=NULL)
             :data(x),leftChild(l),rightChild(r){ }
 };

 template <class T>
 class BinaryTree{//链式二叉树类
public:
     BinaryTree():root(NULL){ }//构造函数
    BinaryTree(T value):RefValue(value),root(NULL){ }//构造函数(NULL结点标志value)
    ~BinaryTree(){ if(root) destroy(root); }//析构函数

    friend istream& operator>> <T>(istream &in,BinaryTree<T> &Tree);

     void  preOrder(void (*visit)(BinTreeNode<T> *&p)) //前序遍历,visit是访问函数
    { preOrder(root,visit); }
     void   inOrder(void (*visit)(BinTreeNode<T> *&p)) //中序遍历
    { inOrder(root,visit); }
     void postOrder(void (*visit)(BinTreeNode<T> *&p)) //后序遍历
    { postOrder(root,visit); }   
     BinaryTree(BinaryTree<T>& s){ root=Copy(s.root); }//复制构造函数,调用Copy
     bool IsEmpty(){ return root==NULL; }//判树空否
    BinTreeNode<T>* Parent(BinTreeNode<T>* current){//返回父结点
        if(root==NULL || root==current) return NULL;//调用同名保护成员函数
        else return Parent(root,current);
     }
     BinTreeNode<T>* LeftChild(BinTreeNode<T>* current)//返回左子女
    { return (current!=NULL)?current->leftChild:NULL; }
     BinTreeNode<T>* RightChild(BinTreeNode<T>* current)//返回右子女
    { return (current!=NULL)?current->rightChild:NULL; }
     int Height(){ return Height(root); }//返回树高度,调用同名保护成员函数
    int Size(){ return Size(root); }//返回树的结点数,调用同名保护成员函数
    BinTreeNode<T>* getRoot()const{ return root; }    //取根
    void createBinaryTree();

 protected:
     BinTreeNode<T> *root;//二叉树的根指针
    T RefValue;//数据输入停止标志,标记NULL结点
    void destroy(BinTreeNode<T> *&subTree);//p196删除使之为空树
    void CreateBinTree(istream &in,BinTreeNode<T> *&subTree);//P202前序建立二叉树
    void preOrder(BinTreeNode<T> *&subTree,void (*visit)(BinTreeNode<T> *&p));//p199前序遍历,visit是访问函数
    void inOrder(BinTreeNode<T> *&subTree,void (*visit)(BinTreeNode<T> *&p)); //p199中序遍历,visit是访问函数
    void postOrder(BinTreeNode<T> *&subTree,void (*visit)(BinTreeNode<T> *&p)); //p200后序遍历,visit是访问函数
    BinTreeNode<T>* Copy(BinTreeNode<T>*);//p201复制--?
    BinTreeNode<T>* Parent(BinTreeNode<T>*, BinTreeNode<T>*);
                                         //p196返回父结点,重载函数--?
    int Height(BinTreeNode<T>*)const;//p200返回树高度,重载函数--?
    int Size(BinTreeNode<T>*)const;//p200返回树的结点数,重载函数--?
    friend ostream& operator<< <T>(ostream& out,BinaryTree<T>& Tree);
     void Traverse(BinTreeNode<T>*,ostream&);//p196前序遍历输出--?
    friend bool operator==<T>(const BinaryTree<T>& s,const BinaryTree<T>& t);//判二叉树相等
    BinTreeNode<T>* createBinaryTree(T* inlist,T* postlist,int i,int j,int k,int l);
 };

 template<class T>
 istream& operator>> (istream &in,BinaryTree<T> &Tree)
 { Tree.CreateBinTree(in,Tree.root);    return in; }//重载操作,输入

template<class T>//后序遍历删除
void BinaryTree<T>::destroy(BinTreeNode<T> *&subTree){
     if(subTree==NULL) return;
     destroy(subTree->leftChild);
     destroy(subTree->rightChild);
     delete subTree; subTree=NULL;
 }

 //CreateBinTree(递归前序遍历建立二叉树,P202)的实现;
template<class T>
 void BinaryTree<T>::CreateBinTree(istream &in,BinTreeNode<T> *&subTree)
 {
     T item;
     if(!in.eof())
     {
         in>>item;
         if(item!=RefValue)
         {
             subTree=new BinTreeNode<T>(item);
             if(subTree==NULL)
             {cerr<<"存储分配错误!"<<endl;exit(1);}
             CreateBinTree(in,subTree->leftChild);
             CreateBinTree(in,subTree->rightChild);   
         }
         else subTree=NULL;
     }
 };
 //preOrder(递归前序遍历,p199)的实现;
template<class T>
 void BinaryTree<T>::preOrder(BinTreeNode<T> *&subTree,void(*visit)(BinTreeNode<T> *&p))
 {
     if(subTree!=NULL)
     {
         visit(subTree);
         preOrder(subTree->leftChild,visit);
         preOrder(subTree->rightChild,visit);
     }
 };
 //(inOrder(递归中序遍历,p199)的实现;
template< class T>
 void BinaryTree<T>::inOrder(BinTreeNode<T> *&subTree,void(*visit)(BinTreeNode<T> *&p))
 {
     if( subTree!=NULL)
     {
         inOrder(subTree->leftChild,visit);
         visit(subTree);
         inOrder(subTree->rightChild,visit);
     }
 };
 //postOrder(递归后序遍历,p200)的实现。
template<class T>
 void BinaryTree<T>::postOrder(BinTreeNode<T> *&subTree,void(*visit)(BinTreeNode<T> *&p))
 {
     if(subTree!=NULL)
     {
         postOrder(subTree->leftChild,visit);
         postOrder(subTree->rightChild,visit);
         visit(subTree);
     }
 };
 //Copy(复制,p201)的实现;
template<class T>
 BinTreeNode<T> *BinaryTree<T>::Copy(BinTreeNode<T> *orignode)
 {
     if(orignode==NULL) return NULL;
     BinTreeNode<T> *temp=new BinTreeNode<T>;
     temp->data=orignode->data;
     temp->leftChild=Copy(orignode->leftChild);
     temp->rightChild=Copy(orignode->rightChild);
     return temp;
 };
 //Parent(返回父结点,p196)的实现;
template<class T>
 BinTreeNode<T> *BinaryTree<T>::Parent(BinTreeNode<T>*subTree,BinTreeNode<T>*current)
 {
     if(subTree==NULL) return NULL;
     if(subTree->leftChild==current||subTree->rightChild==current)
         return subTree;
     BinTreeNode<T> *p;
     if((p=Parent(subTree->leftChild,current))!=NULL) return p;
     else return Parent(subTree->rightChild,current);
 };
 //Height(返回树高度,p200)的实现;
template<class T>
 int BinaryTree<T>::Height(BinTreeNode<T>*subTree)const
 {
     if(subTree==NULL) return 0;
     else
     {
         int i=Height(subTree->leftChild);
         int j=Height(subTree->rightChild);
         return (i<j)?j+1:i+1;
     }
 };
 //Size(返回树的结点数,p200)的实现;
template<class T>
 int BinaryTree<T>::Size(BinTreeNode<T>*subTree)const
 {
     if(subTree==NULL) return 0;
     else return 1+Size(subTree->leftChild)+Size(subTree->rightChild);
 };

 //输出树,重载
template<class T>
 ostream& operator<<(ostream& out,BinaryTree<T>& Tree){
     out<<"二叉树的前序遍历\n";
     Tree.Traverse(Tree.root,out);
     out<<endl;
     return out;
 }

 //Traverse(前序遍历输出,p196)的实现;
template<class T>
 void BinaryTree<T>::Traverse(BinTreeNode<T>*subTree,ostream&out)
 {
     if(subTree!=NULL)
     {
         out<<subTree->data<<' ';
         Traverse(subTree->leftChild,out);
         Traverse(subTree->rightChild,out);
     }     
 };
 //判二叉树相等
template<class T>
 bool operator==(const BinaryTree<T>&s,const BinaryTree<T>&t)
 {
     return(equal(s.root,t.root))?true:false;
 };
 //判结点相等
template<class T>
 bool equal(BinTreeNode<T>*a,BinTreeNode<T>*b)
 {
     if(a==NULL&&b==NULL) return true;
     if(a!=NULL&&b!=NULL&&a->data==b->data
         &&equal(a->leftChild,b->leftChild)
         &&equal(a->rightChild,b->rightChild))
         return true;
     else return false;
 };

 template<class T>
 //主调程序:利用中序序列和后序序列构造二叉树
void BinaryTree<T>::createBinaryTree()
 {
     int n;
     cout<<"输入二叉树的结点个数n=";
     cin>>n;
     T*inlist=new T[n+1];
     cout<<"输入二叉树的中序序列:";
     cin>>inlist;
     T*postlist=new T[n+1];
     cout<<"输入二叉树的后序序列:";
     cin>>postlist;
     root=createBinaryTree(inlist,postlist,int i ,int j , int k,int l );
 };

 template <class T>
 BinTreeNode<T>* createBinaryTree(T* inlist,T* postlist,int i,int j,int k,int l)
 {
     int n;
     BintreeNode*p;
     p=(BinTreeNode*)malloc(size of(BinTreeNode));
     p->data=*(postlist+1);//从后序遍历序列中读取结点信息
    n=1;
     for(;*(inlist+n)!=*(postlist+1);n++;)//在中序遍历序列中确定结点的位置
        if(n==i)p->leftChild=NULL;
         else
         p->leftChild=*createBinaryTree(inlist,postlist,i,n-1,k,k+n-i-1);//递归调用左子树
    if(n==j)
         p->rightChild=NULL;
     else
         p->rightChild=*createBinaryTree(inlist,postlist,n+1,j,k+n-i,l-1);//递归调用右子树
    return p;
 }
 .cpp如下:
#include<iostream>
 using namespace std;
 #include"aaaa.h"

 template<class T>//输出一个二叉树结点的数据
void visit(BinTreeNode<T> *&p){cout<<p->data;}

 void main()
 {
     BinaryTree<char> BT;
     BT.createBinaryTree();
     cout<<"前序遍历输出二叉树:\n";
     BT.preOrder(visit);
     cout<<endl;
 }
搜索更多相关主题的帖子: include 源代码 二叉树 
2013-12-27 16:52
舞伶舞
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2013-12-26
收藏
得分:0 
求解答!!
2013-12-27 16:54
快速回复:求助 帮修改一下程序 C++ 根据二叉树的中序和后序序列重新生成二叉树
数据加载中...
 
   



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

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