数据结构的二叉链表中序遍历
来源:互联网 发布:五子棋c语言判断胜负 编辑:程序博客网 时间:2024/05/20 11:22
数据结构的二叉链表中序遍历
// Tree.cpp : Defines the entry point for the console application.//---二叉链表存储表示---#include "stdafx.h"#include <iostream>#include <stdlib.h>#include <malloc.h>using namespace std;#define STACK_INI_SIZE 100#define STACKINCREMENT 10typedef char TElemType;typedef int Status;typedef char SElemType;//数据类型typedef struct BiTreeNode{SElemType data;struct BiTreeNode *lchild, *rchild;}BiTreeNode,*BiTree;typedef struct{SElemType *base;SElemType *top;int stacksize; //当前已分配空间}SqStack;bool InitStack(SqStack &s){s.base = (SElemType *)malloc(sizeof(SElemType));if (!s.base)exit(OVERFLOW);s.top = s.base;s.stacksize = STACK_INI_SIZE;return true;}bool getTop(SqStack &s, SElemType e){if (s.top == s.base)//若栈不空,则用e 返回S的栈顶元素return false;e = *(s.top - 1);return true;}bool Push(SqStack &S, SElemType e){if (S.top - S.base >= S.stacksize){S.base = (SElemType *)realloc(S.base, (S.stacksize + STACKINCREMENT)*sizeof(SElemType));if (!S.base)return false;S.top = S.base + S.stacksize;S.stacksize += STACKINCREMENT;}*S.top = e;S.top++;return true;}bool Pop(SqStack &S, SElemType &e){if (S.top == S.base)return true;elsereturn false;}bool createTree(BiTree &T){char ch;cin >> ch;if (ch == '0')T = NULL;else{if (!(T = (BiTreeNode *)malloc(sizeof(BiTreeNode))))exit(OVERFLOW);T->data = ch;createTree(T->lchild);createTree(T->rchild);}cout<<"to be Inorder"<<endl;return true;}bool InorderTraverse(BiTree &T){//中序遍历二叉树T的非递归算法SqStack s;InitStack(s);BiTree p;p = T;while (p || s.top != s.base){if (p){Push(s, p->data);p = p->lchild;}else{Pop(s, p->data);cout<<p->data<<endl;p = p->rchild;}}return true;}void main(){cout << "enter the data";BiTree t;createTree(t);cout << "here:" << endl;InorderTraverse(t);}
0 0
- 数据结构的二叉链表中序遍历
- 数据结构 - 二叉树的遍历
- 数据结构-----二叉树的遍历
- 数据结构----二叉树的遍历
- 数据结构 二叉树的遍历
- 【数据结构】 二叉树的遍历
- 【数据结构】二叉树的遍历
- 数据结构 - 二叉树的遍历
- 数据结构二叉树的遍历
- 二叉树的遍历(数据结构)
- 数据结构--二叉树的遍历
- [数据结构]二叉树的遍历
- 【数据结构】二叉树的遍历
- 【数据结构】二叉树的遍历
- 数据结构----二叉树的遍历
- 数据结构-二叉树的遍历
- 数据结构-二叉树的遍历
- C#与数据结构--二叉树的遍历
- CodeForces 612B HDD is Outdated Technology
- PHP URL 重定向 的三种方法
- CodeForces 612C Replace To Make Regular Bracket Sequence
- ubuntu sublime 支持拼音输入的方法
- 聊聊Android的缓存(一)
- 数据结构的二叉链表中序遍历
- hdu1061Rightmost Digit
- (转载)Python学习–数据分析路线
- 从程序员到CTO的Java技术路线图
- 【递推】【NOIP模拟】彩灯的问题 Lights
- CodeForces 612D The Union of k-Segments
- 一个关于类数组中如何使用new
- android studio 添加第三方jar
- 软件工程3