Chinaunix首页 | 论坛 | 博客
  • 博客访问: 53053
  • 博文数量: 15
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 107
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-24 18:59
文章分类
文章存档

2015年(5)

2014年(1)

2013年(9)

我的朋友

分类: C/C++

2013-08-07 22:13:58

今天写一个前序和中序确定二叉树的玩意儿,结果一开始把计算逻辑和二叉树一起写,但是写的时候,二叉树自己要实现到那个程度没有想好,于是一改再改,把自己改进去了,犯二一下午
晚饭时候发现确定序列和二叉树可以分别实现,再结合,所以先敲了一段确定建树序列的代码。而且发现递归这个玩意儿 只有他的确抛出结果的时候,会觉得很有意思。机器压栈好快啊 ,比自己的脑子块多了。O(∩_∩)O哈哈哈~


#include
#include
#include


using namespace std;


int creatBT(string & pl, string & il, char parent = '\0'){
        if(pl.size() == 1){
                cout << pl[0];
                if(parent != '\0'){
                        cout << "\tparent:\t" << parent;
                }
                cout << endl;
                return 0;
        }else if(pl.size() > 1){
                cout << pl[0];
                if(parent != '\0'){
                        cout << "\tparent:\t" << parent;
                }else{
                        cout << "\troot";
                }
                cout << endl;


                string tpll,till,tplr,tilr;
                int leftChildSize = il.find(pl[0]);
                tpll = pl.substr(1,leftChildSize);
                till = il.substr(0,leftChildSize);
                //cout << tpll << "\n" << till << endl;
                creatBT(tpll,till,pl[0]);


                tplr = pl.substr(leftChildSize + 1);
                tilr = il.substr(leftChildSize + 1);
                //cout << tplr << "\n" << tilr << endl;
                creatBT(tplr,tilr,pl[0]);
        }


        return 0;
}


int main(){
        string preList("12473568"), inList("47215386");
        creatBT(preList, inList);
        return 0;
}

阅读(3995) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~