编写非递归算法实现二叉树的前序遍历

  1. 题目要求:编写非递归算法实现二叉树的前序遍历。

  2. 遍历代码

    void PreOrderTraverseNR(BiTree T)
    {
        SqStack stack;
    	InitStack(&stack);
        Push(&stack,T);
        while (!StackEmpty(stack))
        {
            BiTree p;
            Pop(&stack,&p);
            printf("%c",p->data);
            if(p->rchild)
            	Push(&stack,p->rchild);
            if (p->lchild)
            	Push(&stack,p->lchild);
        }
    }
    
  3. 完整程序

    #include<stdio.h>
    #include<stdlib.h>
    #define MAXSIZE 100
    typedef struct BiTNode
    {
       char data;
       struct BiTNode *lchild,*rchild;
    }BiTNode,*BiTree;
    typedef struct
    {
        BiTree data[MAXSIZE];
        int top;
    }SqStack;
    void InitStack(SqStack *S)
    { 
        S->top=-1;
    }
    int Push(SqStack *S,BiTree e)
    {
        if(S->top==MAXSIZE-1)
            return 0;
        S->top++; 
        S->data[S->top]=e;
        return 1;
    }
    void Pop(SqStack *S,BiTree *e)
    { 
        if(S->top==-1)
            return;
        *e=S->data[S->top];
        S->top--;
    }
    int StackEmpty(SqStack S)
    { 
        if (S.top==-1)
            return 1;
        else
            return 0;
    }
    //前序递归建立一棵二叉树 
    void CreateBiTree(BiTree *T)
    {
    	char ch;
    	scanf("%c",&ch);
    	if(ch=='#') 
    		*T=NULL;
    	else
    	{
    		*T=(BiTree)malloc(sizeof(BiTNode));
    		if(!*T)
    			return;
    		(*T)->data=ch;	
    		CreateBiTree(&(*T)->lchild);
    		CreateBiTree(&(*T)->rchild);
    	}
    }
    //二叉树的非递归前序遍历 
    void PreOrderTraverseNR(BiTree T)
    {
        SqStack stack;
    	InitStack(&stack);
        Push(&stack,T);
        while (!StackEmpty(stack))
        {
            BiTree p;
            Pop(&stack,&p);
            printf("%c",p->data);
            if(p->rchild)
            	Push(&stack,p->rchild);
            if (p->lchild)
            	Push(&stack,p->lchild);
        }
    }
    int main()
    {
    	BiTree T;
    	printf("按照前序序列构造一棵二叉树:");
    	CreateBiTree(&T);//ABD##E##CF##G##
    	printf("前序非递归遍历结果:");
    	PreOrderTraverseNR(T); //ABDECFG
    }
    
  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
非常抱歉,作为一名 AI 语言模型,并没有能力直接进行编程和运行程序。但是,我可以为您提供一些基础知识与思路,希望可以帮助您解决问题。 在二叉树中,中序遍历、前序遍历和后序遍历是最基本且重要的三种遍历方式,其中后序遍历指的是先访问左孩子结点,再访问右孩子结点,最后访问根结点。下面分别介绍二叉树中序遍历和后序遍历的递归和非递归算法实现二叉树中序遍历递归算法: ```c void inorderTraversal(TreeNode* root) { if (root != NULL) { inorderTraversal(root->left); // 访问左子树 printf("%d ", root->val); // 访问根节点 inorderTraversal(root->right); // 访问右子树 } } ``` 二叉树中序遍历非递归算法:使用栈来实现中序遍历 ```c void inorderTraversal(TreeNode* root) { stack<TreeNode*> st; TreeNode* p = root; while (p != NULL || !st.empty()) { if (p != NULL) { st.push(p); p = p->left; } else { p = st.top(); st.pop(); printf("%d ", p->val); p = p->right; } } } ``` 二叉树后序遍历递归算法: ```c void postorderTraversal(TreeNode* root) { if (root != NULL) { postorderTraversal(root->left); postorderTraversal(root->right); printf("%d ", root->val); } } ``` 二叉树后序遍历非递归算法:使用一个栈和两个指针来实现后序遍历 ```c void postorderTraversal(TreeNode* root) { stack<TreeNode*> st; TreeNode* p = root; TreeNode* last = NULL; // 上次访问的结点 while (p != NULL || !st.empty()) { if (p != NULL) { st.push(p); p = p->left; } else { TreeNode* top = st.top(); if (top->right != NULL && top->right != last) { // 如果右子树存在,且未被访问 p = top->right; } else { // 否则,访问该节点 st.pop(); printf("%d ", top->val); last = top; } } } } ``` 希望以上的内容可以对您编写程序有所帮助,如果还有其他问题请随时提出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值