假设二叉树采用二叉链表存储结构,设计算法实现:(非递归)实现下列功能

三月神 2010-12-08 04:30:28
假设二叉树采用二叉链表存储结构,设计算法实现:(非递归)实现下列功能
1、对于用户输入的任何结点值计算出该二叉树中的层次。
2、设计1个算法输出从没个叶子结点到根结点的路径。



建立二叉树链表代码已给出,请帮忙做出2个功能
#include<stdio.h>
#include<malloc.h>
#define M 10
typedef int DataType;/*元素的数据类型*/
typedef struct node
{
DataType data;
struct node *lchild,*rchild;
}BitTNode,*BiTree;
int front=0,rear=0;
BitTNode *que[10];
BitTNode *creat()
{
BitTNode *t;
DataType x;
scanf("%d",&x);
if(x==0) t=NULL;
else
{
t=(BitTNode *)malloc(sizeof(BitTNode));
t->data=x;
t->lchild=creat();
t->rchild=creat();
}
return(t);
}/*creat*/
void enqueue(BitTNode *t)//入队
{
if (front!=(rear+1)%M)
{
rear=(rear+1)%M;
que[rear]=t;
}
}/*enqueue*/
BitTNode * delqueue()//出队
{
if(front==rear)return NULL;
{
front=(front+1)%M;
return (que[front]);
}
}
void levorder(BiTree t)//层次遍历
{
BitTNode *p;
if(t!=NULL)
{
enqueue(t);
while (front!=rear)
{
p=delqueue();
printf("%4d",p->data);
if(p->lchild!=NULL) enqueue(p->lchild);
}
}
}
void main()
{
BitTNode *root;
root=creat();
printf("\n层次顺序遍历二叉树");
levorder(root);
}
...全文
2203 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
三月神 2010-12-20
  • 打赏
  • 举报
回复
还有第1个功能没有实现,但照样给分
三月神 2010-12-20
  • 打赏
  • 举报
回复
thanks
my1111ym 2010-12-13
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <malloc.h>
#include <iostream> //

#define M 10
typedef int DataType;

typedef struct node
{
DataType data;
bool visit; //
struct node *lchild,*rchild;
}BitTNode,*BiTree;

int front=0,rear=0;
BitTNode *que[M];

BitTNode* creat()
{
BitTNode *t;
DataType x;
scanf_s("%d",&x); //
if(x == 0) {
t = NULL;
} else {
t=(BitTNode *)malloc(sizeof(BitTNode));
t->data=x;
t->visit = false; //
t->lchild=creat();
t->rchild=creat();
}
return t;
}

void enqueue(BitTNode *t)
{
if (front!=(rear+1)%M)
{
rear=(rear+1)%M;
que[rear]=t;
}
}

BitTNode * delqueue()
{
if(front==rear)return NULL;
{
front=(front+1)%M;
return (que[front]);
}
}

void levorder(BiTree t) { //层次遍历你原来少了一部分,遍历不全
BitTNode *p;
if(t != NULL) {
enqueue(t);
while (front != rear) {
p = delqueue();
printf("%4d", p->data);
if (p->lchild != NULL)
enqueue(p->lchild);
if (p->rchild != NULL) //
enqueue(p->rchild); //
}
} else { //
printf("树为空!"); //
return;//
} //
}

int children(BitTNode *n) { //0表示是叶子节点,1表示只有左孩子,-1表示只有右孩子,2表示有两个孩子
if (n->lchild == NULL && n->rchild == NULL) {
return 0;
} else if (n->lchild != NULL && n->rchild == NULL) {
return 1;
} else if (n->lchild == NULL && n->rchild != NULL) {
return -1;
} else {
return 2;
}
}

/*
*主要思路:根据非递归中序遍历的思想来修改的。
*主要的不同时在要弹出数组时,不是访问过久弹出,而是要分情况。
*如果是叶子节点,直接弹出,如果是其他节点,判断是否所有的孩子
*都已经访问过,如果是,则弹出。
*再有,que数组我不是按照你的队列形式来用的,就是一普通的数组,随机
*读取数据。
*/

void showAllPath(BiTree t) {
BitTNode *temp = NULL;
BitTNode *erase = NULL;
bool flag;
front = rear = 0;

//把que数组清空
for (int m = 0; m < M; m++) {
que[m] = NULL;
}

if (t != NULL) {
//先让根节点入数组
que[rear] = t;
rear++;
t->visit = true;
//以下主要是根据中序遍历来改变的,不懂得地方可以去看书
while (front != rear) {
temp = que[rear - 1];
while (temp != NULL) {
que[rear] = temp->lchild;
rear++;
if (temp->lchild != NULL) {
temp->lchild->visit = true;
}
temp = temp->lchild;
}
rear--; // 空指针弹出

if (front != rear) {
temp = que[rear - 1];
//如果是叶子节点则打印出数组中所有的数据
if (children(temp) == 0) {
int i = front;
for (i; i < rear; i++) {//?
printf("%4d", que[i]->data);
}
printf("\n");
} //if(children(temp == 0)
//下面就是要判断什么节点要弹出数组
flag = true;
while (flag && front != rear) {
erase = que[rear - 1];
if (children(erase) == 0) {//如果是叶子节点,直接弹出,并且不退出循环
rear--;
que[rear] = NULL;
} else if (children(erase) == 1) {//只有左孩子
if (erase->lchild->visit) {
rear--;
que[rear] = NULL;
} else {
flag = false;
}
} else if (children(erase) == -1) {//只有右孩子
if (erase->rchild->visit) {
rear--;
que[rear] = NULL;
} else {
flag = false;
}
} else { //两个孩子都有
if (erase->lchild->visit && erase->rchild->visit) {
rear--;
que[rear] = NULL;
} else {
flag = false;
}
}
} //while (flag && front != rear)
//把temp的有孩子入数组,重新开始遍历右孩子
que[rear] = temp->rchild;
rear++;
if (temp->rchild != NULL) {
temp->rchild->visit = true;
}
}
} // while(front != rear)
} else { //if (t != NULL)
printf("树为空!");
return;
}
}

int main() {
BitTNode *root;
root=creat();
printf("\n层次顺序遍历二叉树");
levorder(root);
printf("\n");

showAllPath(root);
system("pause");
return 0;
}

有什么问题,欢迎提问,都是相互学习嘛。另外,别忘了给分哦,呵呵~~

33,007

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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