视频学习地址
代码中树的结构如下图所示
无栈非递归中序遍历二叉树
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
char data;
struct node *lchild,* rchild;
int ltag,rtag;
}tree;
tree get(char c);
tree * pre = NULL;
int main()
{
tree a = get('A');
tree b = get('B');
tree c = get('C');
tree d = get('D');
tree e = get('E');
tree f = get('F');
tree g = get('G');
a.lchild = &b;
b.lchild = &d;
b.rchild = &e;
d.rchild = &g;
a.rchild = &c;
c.lchild = &f;
thread(&a);
pre->rtag = 1;
printf("\n");
newThread(a);
}
tree get(char c){
tree t;
t.data = c;
t.ltag = 0;
t.rtag = 0;
t.lchild = NULL;
t.rchild = NULL;
return t;
}
void thread(tree * root){
if(root == NULL)return;
if(root->ltag == 0)
thread(root->lchild);
visit(root);
if(root->rtag == 0)
thread(root->rchild);
}
void visit(tree * root){
printf("%c\t",root->data);
if(root->lchild == NULL){
root->lchild = pre;
root->ltag = 1;
}
if(pre != NULL && pre->rchild == NULL){
pre->rchild = root;
pre->rtag = 1;
}
pre = root;
}
tree * fristNode(tree * root){
while(root->ltag != 1)root = root->lchild;
return root;
}
tree * nextNode(tree * root){
if(root->rtag == 0 )return fristNode(root->rchild);
return root->rchild;
}
void newThread(tree root){
for(tree * p = fristNode(&root);p != NULL;p = nextNode(p)){
printf("%c\t",p->data);
}
}
中序遍历的逆遍历
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>
typedef struct node{
char data;
struct node *lchild,* rchild;
int ltag,rtag;
}tree;
tree get(char c);
tree * pre = NULL;
int main()
{
tree a = get('A');
tree b = get('B');
tree c = get('C');
tree d = get('D');
tree e = get('E');
tree f = get('F');
tree g = get('G');
a.lchild = &b;
b.lchild = &d;
b.rchild = &e;
d.rchild = &g;
a.rchild = &c;
c.lchild = &f;
thread(&a);
pre->rtag = 1;
printf("\n");
newThread(a);
}
tree get(char c){
tree t;
t.data = c;
t.ltag = 0;
t.rtag = 0;
t.lchild = NULL;
t.rchild = NULL;
return t;
}
void thread(tree * root){
if(root == NULL)return;
if(root->ltag == 0)
thread(root->lchild);
visit(root);
if(root->rtag == 0)
thread(root->rchild);
}
void visit(tree * root){
printf("%c\t",root->data);
if(root->lchild == NULL){
root->lchild = pre;
root->ltag = 1;
}
if(pre != NULL && pre->rchild == NULL){
pre->rchild = root;
pre->rtag = 1;
}
pre = root;
}
tree* lastNode(tree * root){
while(root->rtag != 1)root = root->rchild;
return root;
}
tree * preNode(tree * root){
if(root->ltag == 0 )return lastNode(root->lchild);
return root->lchild;
}
void newThread(tree root){
for(tree * p = lastNode(&root);p != NULL;p = preNode(p)){
printf("%c\t",p->data);
}
}
先序遍历
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>
typedef struct node{
char data;
struct node *lchild,* rchild;
int ltag,rtag;
}tree;
tree get(char c);
tree * pre = NULL;
int main()
{
tree a = get('A');
tree b = get('B');
tree c = get('C');
tree d = get('D');
tree e = get('E');
tree f = get('F');
tree g = get('G');
a.lchild = &b;
b.lchild = &d;
b.rchild = &e;
d.rchild = &g;
a.rchild = &c;
c.lchild = &f;
thread(&a);
pre->rtag = 1;
printf("\n");
newThread(a);
}
tree get(char c){
tree t;
t.data = c;
t.ltag = 0;
t.rtag = 0;
t.lchild = NULL;
t.rchild = NULL;
return t;
}
void thread(tree * root){
if(root == NULL)return;
visit(root);
if(root->ltag == 0)
thread(root->lchild);
if(root->rtag == 0)
thread(root->rchild);
}
void visit(tree * root){
printf("%c\t",root->data);
if(root->lchild == NULL){
root->lchild = pre;
root->ltag = 1;
}
if(pre != NULL && pre->rchild == NULL){
pre->rchild = root;
pre->rtag = 1;
}
pre = root;
}
tree * nextNode(tree * root){
if(root->ltag == 0 )return root->lchild;
return root->rchild;
}
void newThread(tree root){
for(tree * p = &root;p != NULL;p = nextNode(p)){
printf("%c\t",p->data);
}
}