中跟遍历__非递归操作
来源:互联网 发布:网络政治参与 编辑:程序博客网 时间:2024/05/01 16:55
// 先序(附带叶子标记)建树#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<stack>#include<string>using namespace std;struct node{ char data; node* lchild; node* rchild;};void buildtree(char* str,node* root){ stack<node*> sta; int pos = 0; int len = strlen(str); node* tmp; root->data = str[pos++]; root->lchild = NULL; root->rchild = NULL; sta.push(root); while(pos < len) { while(true) { node* nl = new node; nl->data = str[pos]; nl->lchild = NULL; nl->rchild = NULL; if(str[pos]=='*') { free(nl); break; } sta.top()->lchild = nl; sta.push(nl); ++pos; } tmp = sta.top(); sta.pop(); ++pos; while(str[pos]=='*') { if(!sta.empty()) { tmp = sta.top(); sta.pop(); } ++pos; } if(pos >= len) break; node* nr = new node; nr->data = str[pos]; nr->lchild = NULL; nr->rchild = NULL; sta.push(nr); tmp->rchild = nr; ++pos; } return;}void visit_rootsecond(node* root){ stack<node*> sta; bool _end = false; node* p; sta.push(root); while(!sta.empty()) { p = sta.top(); while(p) { p = p->lchild; sta.push(p); } sta.pop(); p = sta.top(); printf("%c",p->data); p = p->rchild; sta.pop(); if(p) { sta.push(p); continue; } while(p==NULL) { if(sta.empty()) { _end = true; break; } p = sta.top(); sta.pop(); p = p->rchild; } if(!_end) { sta.push(p); } }}int main(){ char* str = new char[100]; gets(str); //printf("%s",str); node* root = new node; buildtree(str,root); return 0;}
阅读全文
0 0
- 中跟遍历__非递归操作
- 先根遍历__非递归操作
- 二叉树的非递归遍历操作
- 二叉树的遍历 非递归操作
- ~二叉树的非递归遍历操作~
- 二叉树的遍历--非递归操作
- 遍历二叉树的各种操作(非递归遍历)
- 遍历二叉树的各种操作(非递归遍历)
- 遍历二叉树的各种操作(非递归遍历)
- 遍历二叉树的各种操作(非递归遍历)
- 遍历二叉树的各种操作(非递归遍历)
- 遍历二叉树的各种操作(非递归遍历)
- 遍历二叉树的各种操作(非递归遍历)
- 遍历二叉树的各种操作(非递归遍历)
- 遍历二叉树的各种操作(非递归遍历)
- 遍历二叉树的各种操作(非递归遍历)
- 遍历二叉树的各种操作(非递归遍历)
- 遍历二叉树的各种操作(非递归遍历)
- CSS基础(三)
- POI导出EXCEL经典实现
- EM算法之高斯混合模型(一)
- Linux SD/MMC/SDIO驱动分析(新)
- javascript 对象的深浅拷贝
- 中跟遍历__非递归操作
- cannot execute binary file 安装jdk问题
- servlet11-复杂自定义标签
- Linux系统源文件sources.list
- 3-6 环状序列(如何实现环状字符串的比较)
- MyEclipse6.5安装SVN插件方法
- 阅读《RobHess的SIFT源码分析:综述》笔记
- Linux内核之mmc子系统-sdio
- Hibernate理解