算法与数据结构考研试题精析-第6章算法设计题30

在二叉链表表示的二叉树中,增设一个指针域,初值为空,给出算法在不使用堆栈又不破坏二叉树的情况下,前序遍历二叉树。

#include <stdio.h>
#include <stdlib.h>
#define maxsize 20

typedef struct Node
{
    char data;
    struct Node *lchild,*rchild,*father;
}Node,*BiTree;

int CreateBiTree(BiTree *T);
void PreTraverse(BiTree T);

int main()
{
    BiTree *T;
    T=(BiTree *)malloc(sizeof(BiTree));

    CreateBiTree(T);
    BiTree Q[maxsize];
    PreTraverse(*T);
    return 0;
}
int CreateBiTree(BiTree *T)
{
    fflush(stdin);
    printf("please input the node data:");
    char ch;
    scanf("%c",&ch);

    *T=(BiTree)malloc(sizeof(Node));
	(*T)->data=ch;
	(*T)->father=NULL;

	char c;
	fflush(stdin);
	printf("Dose %c has leftchild(y--yes,n--no):",ch);
	scanf("%c",&c);
	if(c=='y')
		CreateBiTree(&(*T)->lchild);
	else
		(*T)->lchild=NULL;
	fflush(stdin);
    printf("Dose %c has rightchild(y--yes,n--no):",ch);
	scanf("%c",&c);
	if(c=='y')
		CreateBiTree(&(*T)->rchild);
	else
		(*T)->rchild=NULL;
    return 0;
}
void PreTraverse(BiTree T)
{
    BiTree t,f;
    t=f=(BiTree)malloc(sizeof(Node));

    t=f=T;  //f为父结点 ,t为当前结点

    //如果不是根结点,或者是根结点,但有右子树,且右子树的父结点为空,则继续,否则停止。
    //否则的情况有两种,一种是从左子树回溯到了根结点,但根结点无右子树,此时应停止
    //第二种为从右子树回溯到了根结点,此时右子树的父结点不为空,
    while(t->father!=t || (t->father==t && t->rchild&&!t->rchild->father))
    {
        if(!t->father) //父结点为空,则该结点尚未被遍历
        {
            printf("%c ",t->data);
            t->father=f;
            if(t->lchild)
            {
                f=t;
                t=t->lchild;
            }
            else if(t->rchild)
            {
                f=t;
                t=t->rchild;
            }
            else
                t=t->father;
        }
        else if(!t->rchild || t->rchild->father) //父结点不为空,则当前结点已遍历,则考虑是否有右子树,如果有,是否已经遍历
            t=t->father;
        else
            t=t->rchild;  //此时为右子树尚未遍历情况
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值