问一个二叉树遍历的问题

jianzhibeihang 2010-07-12 09:28:22
一颗二叉树,没个子节点都有一个指向起父结点的指针,让你在线性空间内遍历这颗树 ......

typedef struct node{
int data;
struct node *left;
struct node *right;
struct node *parent;
}Node;


我做出来个死循环,谢谢大家指教!
...全文
127 点赞 收藏 11
写回复
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
jianzhibeihang 2010-07-13
[Quote=引用 9 楼 chj_2003 的回复:]

父节点指针是用来判断节点路径的
[/Quote]

给个代码参考下吧??
回复
chj_2003 2010-07-13
父节点指针是用来判断节点路径的
回复
jianzhibeihang 2010-07-13
[Quote=引用 7 楼 chj_2003 的回复:]
#include <stdio.h>
#include <stdlib.h>

typedef struct node{
int data;
struct node *left;
struct node *right;
struct node *next;
}Node;



void incu……
[/Quote]
哥们你这里没用到指向父节点的指针啊???这不是这个题目的本意了啊
回复
chj_2003 2010-07-13

#include <stdio.h>
#include <stdlib.h>

typedef struct node{
int data;
struct node *left;
struct node *right;
struct node *next;
}Node;



void incursive(Node *root)
{
Node *p = root;
if(root == NULL)
return;

printf("%d\n",p->data);
incursive(p->left);
incursive(p->right);
}









int main()
{
Node *p1,*p2,*p3,*p4,*p5,*root;
root = (Node *)malloc(sizeof(Node));
p1 = (Node *)malloc(sizeof(Node));
p2 = (Node *)malloc(sizeof(Node));


root->data = 0;
root->left = p1;
root->right = p2;
root->next = NULL;

p1->data = 1;
p1->left = NULL;
p1->right = p3;
p1->next = root;

p2->data = 2;
p2->left = NULL;
p2->right = NULL;
p2->next = root;

p3->data = 3;
p3->left = p4;
p3->right = p5;
p3->next = p1;

p4->data = 4;
p4->left = NULL;
p4->right = NULL;
p4->next = p3;

p5->data = 5;
p5->left = NULL;
p5->right = NULL;
p5->next = p3;

incursive(root);

free(root);
free(p1);
free(p2);
free(p3);
free(p4);
free(p5);

system("pause");
return 0;
}
回复
chj_2003 2010-07-13
if(p == NULL)
printf("Fuck\n");哥们,你这句很有意思。
回复
jianzhibeihang 2010-07-13
[Quote=引用 4 楼 pmars 的回复:]

C/C++ code

Node *FindLeft(Node *p)
{
if(p == NULL)
return NULL;
if(p->left != NULL)
p = p->left;
return p;
}

void incursive(Node *root)
{
Node *p = ro……
[/Quote]

怎么改呢???帮帮忙吧....
回复
pmars 2010-07-13

Node *FindLeft(Node *p)
{
if(p == NULL)
return NULL;
if(p->left != NULL)
p = p->left;
return p;
}

void incursive(Node *root)
{
Node *p = root;
if(root == NULL)
return;
if(root->left == NULL && root->right == NULL)
printf("%d\n",root->data);

if(p->left == NULL)
{
printf("%d\n",p->data);
p = p->right;
p = FindLeft(p);
}
else{
// printf("Hello1\n");
p = FindLeft(p); //第一次执行到这里的时候p被赋值为p1
}
/*
if(p == NULL)
printf("Fuck\n");
*/
while(p != NULL)//之后执行这里
{
// printf("Hello2!\n");
printf("%d\n",p->data);
if(p->right != NULL)//
incursive(p->right);//p1又右子树,所以执行,但是始终没有更改p的值,导致每次while循环都TRUE,死了就
else
{
printf("%d\n",p->data);
p = p->next;
if(p == root)
break;
}
}
}


回复
chj_2003 2010-07-13
[Quote=引用 10 楼 jianzhibeihang 的回复:]
引用 9 楼 chj_2003 的回复:

父节点指针是用来判断节点路径的


给个代码参考下吧??
[/Quote]
if(p->next->left==p)左孩子;if(p->next->right==p)右孩子;推到根节点知道路径了
回复
budweiser 2010-07-12
楼主不妨自己调试下 看是哪里的问题、
回复
budweiser 2010-07-12
楼主 我不能不说你的代码写的非常乱。。。
回复
jianzhibeihang 2010-07-12
这是我的代码....

#include <stdio.h>
#include <stdlib.h>

typedef struct node{
int data;
struct node *left;
struct node *right;
struct node *next;
}Node;

Node *FindLeft(Node *p)
{
if(p == NULL)
return NULL;
if(p->left != NULL)
p = p->left;
return p;
}

void incursive(Node *root)
{
Node *p = root;
if(root == NULL)
return;
if(root->left == NULL && root->right == NULL)
printf("%d\n",root->data);

if(p->left == NULL)
{
printf("%d\n",p->data);
p = p->right;
p = FindLeft(p);
}
else{
// printf("Hello1\n");
p = FindLeft(p);
}
/*
if(p == NULL)
printf("Fuck\n");
*/
while(p != NULL)
{
// printf("Hello2!\n");
printf("%d\n",p->data);
if(p->right != NULL)
incursive(p->right);
else
{
printf("%d\n",p->data);
p = p->next;
if(p == root)
break;
}
}
}

int main()
{
Node *p1,*p2,*p3,*p4,*p5,*root;
root = (Node *)malloc(sizeof(Node));
p1 = (Node *)malloc(sizeof(Node));
p2 = (Node *)malloc(sizeof(Node));
p3 = (Node *)malloc(sizeof(Node));
p4 = (Node *)malloc(sizeof(Node));
p5 = (Node *)malloc(sizeof(Node));


root->data = 0;
root->left = p1;
root->right = p2;
root->next = NULL;

p1->data = 1;
p1->left = NULL;
p1->right = p3;
p1->next = root;

p2->data = 2;
p2->left = NULL;
p2->right = NULL;
p2->next = root;

p3->data = 3;
p3->left = p4;
p3->right = p5;
p3->next = p1;

p4->data = 4;
p4->left = NULL;
p4->right = NULL;
p4->next = p3;

p5->data = 5;
p5->left = NULL;
p5->right = NULL;
p5->next = p3;

incursive(root);

free(root);
free(p1);
free(p2);
free(p3);
free(p4);
free(p5);

system("pause");
return 0;
}

回复
发动态
发帖子
数据结构与算法
创建于2007-08-27

3.2w+

社区成员

数据结构与算法相关内容讨论专区
申请成为版主
社区公告
暂无公告