遍历二叉树-递归和非递归算法

来源:互联网 发布:stc8单片机 编辑:程序博客网 时间:2024/05/20 13:15

遍历二叉树的三种方法:

前序:根节点->左子树->右子树

中序:左子树->根节点->右子树

后序:左子树->右子树->根节点

 

非递归算法中序遍历二叉树,设S为一个栈,p为指向根节点的指针,处理过程如下:

1)当p非空时,压栈p指针,并将p指向该节点的左子树。

2)当p为空时,弹出栈顶元素,显示节点元素,并将p指向该节点的右子树。

3)重复步骤1)和2),直到栈空且p空。

C语言代码如下:

// 二叉树节点typedef struct Bitree{char data;struct Bitree *lchild, *rchild;}Bitree;// 新节点Bitree *new(char data){Bitree *a = (Bitree *)malloc(sizeof(Bitree));a->data = data;a->lchild = NULL;a->rchild = NULL;return a;}// 中序遍历二叉树的递归算法void inorder(Bitree *t){if (t){inorder(t->lchild);printf("%c ", t->data);inorder(t->rchild);}}// 前序遍历二叉树的递归算法void preorder(Bitree *t){if (t){printf("%c ", t->data);preorder(t->lchild);preorder(t->rchild);}}// 后序遍历二叉树的递归算法void postorder(Bitree *t){if (t){postorder(t->lchild);postorder(t->rchild);printf("%c ", t->data);}}// 前序遍历二叉树的非递归算法void preorder2(Bitree *t){Bitree *s[32];// s是指针数组,数组中元素为二叉树节点的指针int top = -1;while (t!=NULL || top != -1){// 压栈直到左子树为空while (t != NULL){printf("%c ", t->data);s[++top] = t;t = t->lchild;}if (top != -1){t = s[top--];// 出栈t = t->rchild;// 指向该节点的右孩子,回到while循环压栈}}}// 中序遍历二叉树的非递归算法void inorder2(Bitree *t){Bitree *s[32];// s是指针数组,数组中元素为二叉树节点的指针int top = -1;while (t!=NULL || top != -1){// 压栈直到左子树为空while (t != NULL){s[++top] = t;t = t->lchild;}if (top != -1){t = s[top--];// 出栈printf("%c ", t->data);t = t->rchild;// 指向该节点的右孩子,回到while循环压栈}}}// 后序遍历二叉树的非递归算法void postorder2(Bitree *t){Bitree *s[32];// s是指针数组,数组中元素为二叉树节点的指针int tag[32];// s中相对位置的元素的tag: 0或1int top = -1;while (t!=NULL || top != -1){// 压栈直到左子树为空while (t != NULL){s[++top] = t;tag[top] = 0;t = t->lchild;}// 当栈非空,并且栈顶元素tag为1时,出栈并访问while (top!=-1 && tag[top]==1){printf("%c ", s[top--]->data);}// 当栈非空时,将栈顶tag置1,并指向栈顶元素的右孩子if (top != -1){tag[top] = 1;t = s[top]->rchild;}}}int main(){// 申请空间构造一棵二叉树Bitree *a = new('A');Bitree *b = new('B');Bitree *c = new('C');Bitree *d = new('D');Bitree *e = new('E');Bitree *f = new('F');a->lchild = b;a->rchild = c;b->lchild = d;b->rchild = e;c->rchild = f;// 递归算法printf("preorder: ");preorder(a);printf("/n");printf("inorder: ");inorder(a);printf("/n");printf("postorder: ");postorder(a);printf("/n");printf("/n");// 非递归算法printf("preorder2: ");preorder2(a);printf("/n");printf("inorder2: ");inorder2(a);printf("/n");printf("postorder2: ");postorder2(a);printf("/n");free(a); free(b); free(c); free(d); free(e); free(f);return 0;}
原创粉丝点击