| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 949 人关注过本帖
标题:中根后根遍历并构造二叉树、有错求大神帮忙看一下
只看楼主 加入收藏
八神
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-9-4
收藏
 问题点数:0 回复次数:0 
中根后根遍历并构造二叉树、有错求大神帮忙看一下
程序输出不了结果、求大神帮忙看一下

BinaryNode.h

#include "BinaryNode.h"
template<class T>
class BinaryTree
{
    public:
        BinaryNode<T>*root;
        BinaryTree();
        BinaryTree(T inlist[],T postlist[],int n);
        void inOrder();
        void postOrder();

    private:   
        void inOrder(BinaryNode<T>*p);
        void postOrder(BinaryNode<T>*p);
        BinaryNode<T>*create(T inlist[],T postlist[],int inStart,int postStart,int n);
};

template <class T>
BinaryTree<T>::BinaryTree()                    //构造空二叉树
{
    root=NULL;
}
template <class T>                                               //中根后跟构造二叉树、n指定序列长度
BinaryTree<T>::BinaryTree(T inlist[],T postlist[],int n)
{
    root=create(inlist, postlist,0,0,n);
}
template<class T>                                                   //中根后根创建子树,根节点是inlist[i],返回根节点指针
BinaryNode<T>*BinaryTree<T>::create(T inlist[],T postlist[],int inStart,int postStart,int n)
{
    BinaryNode<T>*p=NULL;
    if(n>0)
    {
        T elem=inlist[inStart]; //根节点值
        
        int i=0;
        while (i<n && elem!=postlist[postStart+i])
            i++;
        p->left=create(inlist,postlist,inStart,postStart,i);
        p=new BinaryNode<T>(elem);
        p->right=create(inlist,postlist,inStart+i+1,postStart+i+1,n-1-i);
    }
    return p;
}
template<class T>
void BinaryTree<T>::inOrder()
{
    cout<<"中根次序遍历二叉树:";
    inOrder(root);
    cout<<endl;
}
template<class T>
void BinaryTree<T>::inOrder(BinaryNode<T>*p)
{
    if(p!=NULL);
    {
   
        inOrder(p->left);
        cout<<p->data<<" ";
        inOrder(p->right);
    }
}
template<class T>
void BinaryTree<T>::postOrder()
{
    cout<<"后根次序遍历二叉树:";
    postOrder(root);
    cout<<endl;
}
template<class T>
void BinaryTree<T>::postOrder(BinaryNode<T>*p)
{
    if(p!=NULL);
    {
        
        postOrder(p->left);
        postOrder(p->right);
        cout<<p->data<<" ";
    }
}


主函数
#include <iostream.h>
#include "BinaryTree.h"
int main()
{
    char inlist[]="DGBAECHF";
    char postlist[]="GDBEHFCA";
    BinaryTree<char>bitree(inlist,postlist,8);
    bitree.inOrder();
    bitree.postOrder();
    return 0;
}
搜索更多相关主题的帖子: 二叉树 public private include create 
2013-06-06 20:11
快速回复:中根后根遍历并构造二叉树、有错求大神帮忙看一下
数据加载中...
 
   



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

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