服装公司网站源码,沈阳沈阳建设工程信息网站,空间信息网站,上海发布最新情况本题代码如下
void swap(tree* t)
{if (*t)// 如果当前节点非空 {treenode* temp (*t)-lchild;// 临时存储左子节点 (*t)-lchild (*t)-rchild;// 将右子节点赋值给左子节点(*t)-rchild temp;// 将临时存储的左子节点赋值给右子节点 swap((*t)-l…本题代码如下
void swap(tree* t)
{if (*t)// 如果当前节点非空 {treenode* temp (*t)-lchild;// 临时存储左子节点 (*t)-lchild (*t)-rchild;// 将右子节点赋值给左子节点(*t)-rchild temp;// 将临时存储的左子节点赋值给右子节点 swap((*t)-lchild);// 递归交换左子树的左右子节点swap((*t)-rchild);// 递归交换右子树的左右子节点 } }
}
完整测试代码
#includestdio.h
typedef struct treenode
{char data;struct treenode* lchild, * rchild;
}treenode,*tree;
void buildtree(tree* t)
{char ch;ch getchar();if (ch #)*t NULL;else{*t (treenode*)malloc(sizeof(treenode));(*t)-data ch;(*t)-lchild NULL;(*t)-rchild NULL;buildtree((*t)-lchild);buildtree((*t)-rchild);}
}
void swap(tree* t)
{if (*t){treenode* temp (*t)-lchild;(*t)-lchild (*t)-rchild;(*t)-rchild temp;swap((*t)-lchild);swap((*t)-rchild);}
}
void print(tree *t)
{if (*t){printf(%c , (*t)-data);print((*t)-lchild);print((*t)-rchild);}
}
int main()
{tree t;buildtree(t);printf(原有的前序序列为);print(t);swap(t);printf(\n交换后的前序序列为);print(t);return 0;
}
用ABD##E##CF##G##测试 /* A B C D E F G */
修改之后为
/* A C B
G F E D */