npostorder

/*
 * LRD
 */
typedef struct
{
    int tag;
    BinTree t;
}Elem;

void nPostOrder(BinTree t)
{
    Stack s;
    BinTreeNode * p = t;
    Elem stnode;
    if(t == NULL) return;
    s = CreateEmptyStack;
    do{
        // first push
        while(p!=NULL)
        {
            stnode.tag = 1;
            stnode.t = p;
            push(s, stnode);
            p = leftChild(p);
        }
        // pop
        while(!isEmptyStack(s))
        {
            stnode = top(s);
            pop(s);
            p = stnode.t;
        }
        // second push
        if(stnode.tag == 1)
        {
            stnode.tag = 2;
            push(s, stnode);
            p = rightChild(p);
            break;
        }
        else visit(root(p));
    }while(!isEmptyStack);
}
改进下面的这段代码使它能正常运行:// Tree traversal in C #include <stdio.h> #include <stdlib.h> struct node { int item; struct node* left; struct node* right; }; // Inorder traversal void inorderTraversal(struct node* root) { if(root) { inorderTraversal(root->left); printf("%d",root->item); inorderTraversal(root->right); } } // Preorder traversal void preorderTraversal(struct node* root) { printf("%d",root->item); preorderTraversal(root->left); preorderTraversal(root->right); } // Postorder traversal void postorderTraversal(struct node* root) { postorderTraversal(root->left); postorderTraversal(root->right); printf("%d",root->item); } // Create a new Node struct node* create(int value) { struct node* newNode = malloc(sizeof(struct node)); newNode->item = value; newNode->left = NULL; newNode->right = NULL; return newNode; } // Insert on the left of the node struct node* insertLeft(struct node* root, int value) { root->left=create(value); return root->left; } // Insert on the right of the node struct node* insertRight(struct node* root, int value) { root->right=create(value); return root->right; } int main() { struct node* root = create(1); insertLeft(root, 4); insertRight(root, 6); insertLeft(root->left, 42); insertRight(root->left, 3); insertLeft(root->right, 2); insertRight(root->right, 33); printf("Traversal of the inserted binary tree \n"); printf("Inorder traversal \n"); inorderTraversal(root); printf("\nPreorder traversal \n"); preorderTraversal(root); printf("\nPostorder traversal \n"); postorderTraversal(root); }
06-06
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

何书文老师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值