编程实现:交换二叉树左右孩子

pengqiuming 2004-11-11 06:44:44
请补充下面的exchange的函数,以完成将二叉树左。右孩子交换的操作,要求使用非递归的方法(exchange函数的参数t为二叉树的根结点),例如:结点A的左孩子是B,右孩子是C,则交换后结点A的左孩子是C,右孩子是B
typedef struct node *tree;
struct node
{
int data;
tree lchild,rchild;
}
exchange( tree t)
{
//请补充
。。。。。。。。
}
...全文
1066 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
seahoyel 2004-11-13
  • 打赏
  • 举报
回复
应该不会吧,有判断条件的!
不过我想了一想,参数好像可以不用那样传!
在改一下!

#include <assert.h>
#include <stdio.h>
#define NUM 100

typedef struct node {
char data;
struct node* LeftChild;
struct node* RightChild;
}*tree,treeNode;

void exchange(tree p)
/* 先序遍历 */
{

tree temp;
/* 用于交换左右子树 */

tree s[NUM];
int top = -1;
/* 定义一个栈,栈顶指针赋为-1,表示空栈 */

assert(p == NULL) ;

do {
if(NUM <= top)return;

if(p != NULL) {
top++;
s[top] = p;
/* 树结点的地址进栈 */

temp = p->LeftChild;
p->LeftChild = p->RightChild;
p->RightChild = temp;
/* 交换左右子树 */

p = p->LeftChild;
/* 遍历左子树,直到左节点为空*/
}
else {
if(top != -1) {
p = s[top];
top--;
/* 退栈操作 */
p = p->RightChild;
}
}
}
while(p != NULL || top != -1);
}
seahoyel 2004-11-13
  • 打赏
  • 举报
回复
递归那就简单多了
void exchange(tree r) /*后序遍历*/
{
if(r != NULL) {
exchange(r->LeftChild);
exchange(r->RightChild);

temp = t->LeftChild;
t->LeftChild = t->RightChild;
t->RightChild = temp;
}
}

huangyang88 2004-11-13
  • 打赏
  • 举报
回复
void exchangTree(tree* tr)
{
if(tr && tr->pleft)
{
tree* ptm = tr->pleft;

exchangTree(tr->pleft);
exchangTree(tr->pright);

tr->pleft = tr->pright;
tr->pright = ptm;

if(tr->pleft)
{
ptm = tr->pleft->pleft;
tr->pleft->pleft = tr->pright->pleft;
tr->pright->pleft = ptm;

ptm = tr->pleft->pright;
tr->pleft->pright = tr->pright->pright;
tr->pright->pright = ptm;
}
}
}
kangting 2004-11-13
  • 打赏
  • 举报
回复
谁可以写个递归调用,谢了
lovelyOLAY 2004-11-13
  • 打赏
  • 举报
回复
是啊
是不用递归
我下去给你编
编好了发给你
我最近有点忙
qingyuan18 2004-11-13
  • 打赏
  • 举报
回复
楼上几位好象都没有用递归呢?
nKannan 2004-11-12
  • 打赏
  • 举报
回复
好像会死循环吧?反复左右交换。
#include <assert.h>
#include <stdio.h>
#define NUM 100

typedef struct node {
char data;
struct node* LeftChild;
struct node* RightChild;
}*tree,treeNode;

void exchange(tree *t)
/* 先序遍历 */
{

tree * temp; // 用于交换左右子树

/* 定义一个栈,栈顶指针赋为-1,表示空栈 */
tree * s[NUM];
int top = -1;

/* 树结点的地址进栈 */
top++;
s[top] = t;

tree * p;
do {
if(NUM <= top)return;

p = s[top];
top--;

/* 交换左右子树 */
temp = p->LeftChild;
p->LeftChild = p->RightChild;
p->RightChild = temp;

if( p->LeftChild!=NULL ) {
s[top]=p->LeftChild;
top++;
}
if( p->RigthChild!=NULL ) {
s[top]=p->RigthChild;
top++;
}

}
while(top != -1);
}
seahoyel 2004-11-11
  • 打赏
  • 举报
回复
改一下

#include <assert.h>
#include <stdio.h>
#define NUM 100

typedef struct node {
char data;
struct node* LeftChild;
struct node* RightChild;
}*tree,treeNode;

void exchange(tree *t)
/* 先序遍历 */
{

tree temp;
/* 用于交换左右子树 */
tree s[NUM];
int top = -1;
/* 定义一个栈,栈顶指针赋为-1,表示空栈 */
tree p = *t;
assert(p == NULL) ;

do {
if(NUM <= top)return;

if(p != NULL) {
top++;
s[top] = p;
/* 树结点的地址进栈 */

temp = p->LeftChild;
p->LeftChild = p->RightChild;
p->RightChild = temp;
/* 交换左右子树 */

p = p->LeftChild;
/* 遍历左子树,直到左节点为空*/
}
else {
if(top != -1) {
p = s[top];
top--;
/* 退栈操作 */
p = p->RightChild;
}
}
}
while(p != NULL || top != -1);
}
seahoyel 2004-11-11
  • 打赏
  • 举报
回复
刚学数据结构,不知道写得怎么样,对不对?请大家评议


#include <assert.h>
#include <stdio.h>
#define NUM 100

typedef struct node {
char data;
struct node* LeftChild;
struct node* RightChild;
}*tree,treeNode;

void exchange(tree *t)
/* 先序遍历 */
{

tree temp;
/* 用于交换左右子树 */
tree s[NUM];
int top = -1;
/* 定义一个栈,栈顶指针赋为-1,表示空指针 */
tree p = *t;
assert(p == NULL) ;

do {
if(NUM <= top)return;

if(p != NULL) {
top++;
s[top] = p;
/* 树结点的地址进栈 */

temp = p->LeftChild;
p->LeftChild = p->RightChild;
p->RightChild = temp;
/* 交换左右子树 */

p = p->LeftChild;
/* 遍历左子树,直到左节点为空*/
}
else {
if(top != -1) {
p = s[top];
top--;
/* 退栈操作tuizu */
p = p->RightChild;
}
}
}
while(p != NULL || top != -1);
}
nKannan 2004-11-11
  • 打赏
  • 举报
回复
真无聊,对于树最方便的操作方法就是递归,却不让用。
另外一个方法就是用队列循环。

exchange(tree t)
{
建立一个链表做为队列
让t进队列
while(队列不为空)
{
取出队列第一个节点
如果左子树不为空,左子树进队列
如果右子树不为空,右子树进队列
交换当前节点左右子树(叶子)
}
}

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧