社区
C语言
帖子详情
编程实现:交换二叉树左右孩子
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
打赏
收藏
编程实现:交换二叉树左右孩子
请补充下面的exchange的函数,以完成将二叉树左。右孩子交换的操作,要求使用非递归的方法(exchange函数的参数t为二叉树的根结点),例如:结点A的左孩子是B,右孩子是C,则交换后结点A的左孩子是C,右孩子是B typedef struct node *tree; struct node { int data; tree lchild,rchild; } exchange( tree t) { //请补充 。。。。。。。。 }
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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(队列不为空)
{
取出队列第一个节点
如果左子树不为空,左子树进队列
如果右子树不为空,右子树进队列
交换当前节点左右子树(叶子)
}
}
数据结构-
左右
孩子
交换
(C语言)
数据结构(C语言),关于
二叉树
左右
孩子
的
交换
,即左变右,右变左。
二叉树
的建立与基本操作
编写程序
实现
二叉树
的如下操作: 1) 建立二叉链表 2)
二叉树
的先序、中序、后序遍历 3) 求解
二叉树
的叶子结点个数 4) 将
二叉树
中所有结点的左、右子树相互
交换
输入: 扩展
二叉树
先序序列:ab#d##ce###。其中#...
数据结构-
二叉树
(Java
实现
)
编程
实现
如下功能: 1、假设
二叉树
的结点值是字符,根据输入的一颗
二叉树
的标明了空子树的完整先根遍历序列,建立一棵以二叉链表表示的
二叉树
。 2、对
二叉树
进行先根、中根和后根遍历操作,并输出遍历序列,同时观察...
二叉树
的基本运算
4. 将
二叉树
每个结点的
左右
子树
交换
位置。(选做) [基本要求] 从键盘接受输入(先序),以二叉链表作为存储结构,建立
二叉树
(以先序来建立), [测试数据] 如输入:ABCффDEфGффFффф(其中ф表示空格字符...
二叉树
编码
编程
实现
二叉树
的建立,先序、中序、后序、层序遍历(非递归方法),
二叉树
的高度、
交换
左右
子树,统计叶子节点的数目,判断是否为完全
二叉树
,用括号的形式输出树等功能。 [基本要求] 程序输出菜单界面,菜单包含8...
C语言
69,371
社区成员
243,082
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章