1. 试给出中序遍历二叉树的算法(要求写出非递归的算法)

来源:百度知道 编辑:UC知道 时间:2024/06/09 03:31:56

void iter_inorder(tree_pointer ptr)
{
for(;;){
for(;ptr;ptr=ptr->left_child)
push(&top,ptr);
ptr=pop(&top);
if(!ptr) break;
printf("%c",ptr->ch);
ptr=ptr->right_child;
}
}

#include "iostream.h"
#include "malloc.h"
#define MAX 100

typedef struct node
{char data;
struct node * lchild,* rchild;
}BTree;

BTree *create()
{int i,j;char x;
BTree *v[MAX],*p,*head;
cout<<"根据顺序存储结构构造二叉树(数字与字符均为0时结束循环)"<<endl<<endl;
while((i!=0)&&(x!=0))
{cout<<"请输入节点的编号(数字):";cin>>i;
cout<<"请输入节点的权值(字符):";cin>>x;
cout<<endl;
p=(BTree *)malloc(sizeof(BTree));
p->data=x;p->lchild=NULL;p->rchild=NULL;
v[i]=p;
if(i==1) head=p;
else
{j=i/2;
if(i%2==0) v[j]->lchild=p;
else v[j]->rchild=p;}
}<