数据结构.非递归

// henhap.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

//二叉树链式存储的头文件
typedef char datatype;         //结点属性值类型
typedef struct node            //二叉树结点的类型
{
datatype data;
struct node* lchild,*rchild;
}bintnode;
typedef bintnode *bintree;
bintree root;                   //指向二叉树根结点指针

//下面是些栈的操作 为非递归实现做准备

typedef struct stack            //栈结构定义
{
bintree data[100];          //data 元素类型为   指针
int tag[100];               //为栈中元素作的标记,用于后序遍历
int top;                    //栈顶指针
}seqstack;

void push(seqstack* s,bintree t) //进栈
{
s->data[++s->top]=t;
}

bintree pop(seqstack* s)            //出栈
{
if(s->top!=-1)               //非递归遍历中,top初始值为-1
{
   s->top--;
   return (s->data[s->top+1]);
}
else
   return NULL;
}


//按照前序遍历顺序建立一棵给定的二叉树

void createbintree(bintree* t)  
{
char ch;
if((ch=getchar())==' ')
   *t=NULL;
else
{
   *t=(bintnode*)malloc(sizeof(bintnode));   //产生二叉树根结点
   (*t)->data=ch;
   createbintree(&(*t)->lchild);             //递归实现左子树的创建
   createbintree(&(*t)->rchild);             //递归实现右子树的创建
}
}


//二叉树前序遍历 递归 实现

void preorder(bintree t)            //t 是指针变量,而不是 结点 结构体变量
{
if(t)
{
   cout<<t->data<<"   ";
   preorder(t->lchild);
   preorder(t->rchild);
}
}

//二叉树前序遍历 非递归 实现

void preorder1(bintree t)
{
seqstack s;
s.top=-1;                            //top初始值为-1
while((t)||(s.top!=-1))              // 当前 处理的 子树 不为空,或栈不为空,则循环
{
   while(t)
   {
    cout<<t->data<<" ";         //访问当前子树根结点
    s.top++;
    s.data[s.top]=t;             //当前子树根结点进栈(因为还要访问右子树)
    t=t->lchild;                 //访问此根结点 左孩子
   }                                //循环直到遍历完 当前子树 根结点,和其 左 孩子
   if(s.top>-1)
   {
    t=pop(&s);                   //当前子树跟结点出栈
    t=t->rchild;                 //访问其右孩子
   }
}
}

//二叉树中序遍历递归算法

void inorder(bintree t)
{
if(t)
{
   inorder(t->lchild);
   cout<<t->data<<" ";
   inorder(t->rchild);
}
}

//二叉树中序遍历 非递归 算法

void inorder1(bintree t)
{
seqstack s;
s.top=-1;
while((t!=NULL)||(s.top!=-1))
{
   while(t)                  
   {
    push(&s,t);        
    t=t->lchild;       //子树根结点全部进栈
   }
   if(s.top!=-1)
   {
    t=pop(&s);
    cout<<t->data<<" ";   //输出根结点,其实也就是左孩子或右孩子(没有孩子的根结点是它父亲的左或右孩子)
    t=t->rchild;           //进入右孩子访问
   }
}
}


//后序递归遍历二叉树

void postorder(bintree t)
{
if(t)
{
   postorder(t->lchild);
   postorder(t->rchild);
   cout<<t->data<<" ";
}
}

//后序遍历 非递归 实现

void postorder1(bintree t)
{
seqstack s;
s.top=-1;
while((t)||(s.top!=-1))
{
   while(t)
   {
    s.top++;
    s.data[s.top]=t;              //子树根结点进栈
    s.tag[s.top]=0;        //设此根结点标志初始化为0,表示左右孩子都没访问,当访问完左子树 tag 变为1
    t=t->lchild;           //进入左子树访问。(左子树根结点全部进栈)
   }
   while((s.top>-1)&&(s.tag[s.top]==1))
   {
    t=s.data[s.top];     
    cout<<t->data<<" "; //没有孩子的根结点,也就是它父亲的左孩子或右孩子
    s.top--;
   }
   if(s.top>-1)
   {
    t=s.data[s.top];
    s.tag[s.top]=1;          //进入右子树 前,标志tag变为1
    t=t->rchild;             //进入右子树
   }
   else
    t=NULL;
}
}

 

int main()
{
bintree tree;
cout<<"输入根结点:";
createbintree(&tree);


cout<<"/n前序递归遍历二叉树:";
preorder(tree);
cout<<"/n前序非递归遍历二叉树:";
    preorder1(tree);
cout<<"/n-----------------------------/n";

cout<<"/n中序递归遍历二叉树:";
    inorder(tree);
    cout<<"/n中序非递归遍历二叉树:";
    inorder1(tree);
    cout<<"/n----------------------------/n";

cout<<"/n后序递归遍历二叉树:";
    postorder(tree);
cout<<"/n后序非递归遍历二叉树:";
    postorder1(tree);
cout<<endl;
}

 

 

 

 

 

 


// stdafx.h : include file for standard system include files,
//  or project specific include files that are used frequently, but
//      are changed infrequently
//

#if !defined(AFX_STDAFX_H__C0CBEA7B_E2A6_4828_A3E7_58F987B9CE12__INCLUDED_)
#define AFX_STDAFX_H__C0CBEA7B_E2A6_4828_A3E7_58F987B9CE12__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


// TODO: reference additional headers your program requires here

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__C0CBEA7B_E2A6_4828_A3E7_58F987B9CE12__INCLUDED_)

 

 

 

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值