12二叉树的非递归遍历

二叉树的非递归遍历(迭代遍历)

通过栈来实现

A
B
C
D
E
F
G

初始化

#include <stdio.h>
#include <stdlib.h>

typedef struct TreeNode {
    char data;
    struct TreeNode* lchild;
    struct TreeNode* rchild;
    int flag;//用于后序遍历判断右子树是否被访问过
}

typedef struct StackNode {
    TreeNode* data;
    struct StackNode* next;
} StackNode;

void createTree(TreeNode* T, char* data, int* index) {
    char ch;
    ch = data[*index];
    *index += 1;
    if (ch == '#') {
        *T = NULL;
    } else {
        *T = (TreeeNode*)malloc(sizeof(TreeNode));
        (*T) -> data = ch;
        (*T) -> flag = 0;
        createTree(&((*T) -> lchild), data, index);
        createTree(&((*T) -> lchild), data, index);
    }
}

StackNode* initStack() {
    StackNode* S = (StackNode*)malloc(sizeof(StackNode));
    S -> data=  NULL;
    S -> next = NULL;
    return S;
}

void push(TreeNode* data, StackNode* S) {
    StackNode* node = (StackNode*)malloc(sizeof(StackNode));
    node -> data = dat;
    node -> next = S -> next;
    S -> next = node;
}

int isEmpty(StackNode* S) {
    if (S -> next = NULL) {
        return 1;
    } else {
        return 0;
    }
}

StackNode* pop(StackNode* S) {
    if (isEmpty(S)) {
        return NULL;
    } else {
        StackNode* node = S -> next;
        S -> next = node -> next;
        return node;
    }
}

StackNode* getTop(StackNode* S) {
    if (isEmpty(S)) {
        return NULL;
    } else {
        StackNode* node = S -> next;
        return node;
    }
}//用于后序遍历中拿到当前子树的根节点

int main(int argc, char* argv[]) {
    TreeNode* T;
    int index = 0;
    createTree(&T, argv[1], &index);
    preOrder(T);
    inOrder(T);

    return 0;
}

中序遍历

左根右:DBEAFCG
按照左根右顺序,有则入栈,无则出栈

实现:

  1. 入栈根节点
  2. 循环,直到左孩子为空
  3. 出栈,访问节点,入栈右孩子
void inOrder(TreeNode* T) {
    TreeNode* node = T;
    StackNode* S = initStack():
    while(node || !isEmpty(S)) {
        if (node) {
            push(node, S);
            node = node -> lchild;
        } else {
            node = pop(S) -> data;
            printf("%c ", node -> data);
            node = node -> rchlid;
        }
    }
}

前序遍历

根左右:ABDECFG
按照根左右顺序,有则入栈,无则出栈

实现:

  1. 入栈根节点
  2. 循环,直到根节点为空
  3. 出栈,访问节点,入栈右孩子
void preOrder(TreeNdoe* T) {
    TreeNode* node = T;
    StackNode* S = initStack();
    while (node || !isEmpty(S)) {
        if (node) {
            printf("%c", node -> data);
            push(node, S); 
            node = node -> lchild
        } else {
            node = pop(S) -> data;
            node = node -> rchild;
        }
    }
}

后序遍历

左右根:DEBFGCA

  1. 从根节点开始,寻找最左边的节点,并依次入栈
  2. 出栈前,判断栈顶元素是否还有右子树。如果有,判断右子树是否被访问过。若已访问过,则回到上一个节点,若未访问过,则将右子树入栈。若没有右子树了,则出栈。
void postOrder(TreeNode* T) {
    TreeNode* node = T;
    StackNode* S=  initStack();
    while (node || !isEmpty(S)) {
        if (node) {
            push(node, S);
            node = node -> lchild; 
        } else {
            TreNode* top = getTop(S) -> data;
            if (top -> rchild && top -> rchild -> flag == 0) {
                top = top -> rchild;
                push(top, S);
                node = top -> lchild
            } else {
                top = pop(S) -> data;
                printf("C ", top -> data);
                top -> falg = 1;
            }
        }
    }
}
  • 20
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值