查找二叉树的节点的父节点

2022-05-10 00:34:12   文档大全网     [ 字体: ] [ 阅读: ]

#文档大全网# 导语】以下是®文档大全网的小编为您整理的《查找二叉树的节点的父节点》,欢迎阅读!
节点,查找
#include "stdio.h" #include "malloc.h" #include "process.h" #define max 100 typedef struct node{ int data;

struct node *lchild,*rchild; }Bitree;

Bitree *a[max]; int re[max]; Bitree *Build() {

int x;

int front,rear; Bitree *t,*s; t=NULL;

front=1;rear=0;

printf("请输入树 #结束\n"); scanf("%d",&x); while(x!=0) {

s=NULL; if(x!=-1) { s=(Bitree *)malloc(sizeof(Bitree)); s->data=x;s->lchild=s->rchild=NULL; } rear++; a[rear]=s; if(rear==1) t=s; else { if(s!=NULL&&a[front]!=NULL) if(rear%2==0) a[front]->lchild=s; else a[front]->rchild=s; if(rear%2==1) front++; } scanf("%d",&x); }

return t; }

void Output(int i) { int j;

printf("祖先节点为: \n"); for(j=0;j


printf("%4d",re[j]); printf("\n"); getchar(); exit(0); }

void Find(Bitree *t,int u,int n) {

if(t==NULL) return;

if (t->data==u) Output(n);

if (t->lchild==NULL&&t->rchild==NULL) return; else re[n]=t->data; Find(t->lchild,u,n+1); Find(t->rchild,u,n+1); }

void main() {

Bitree *t; int u; t=Build();

printf("请输入待查找数字: "); scanf("%d",&u); Find(t,u,0);

printf(" 查无此数 \n"); }









本文来源:https://www.wddqxz.cn/80c5532375c66137ee06eff9aef8941ea76e4b33.html

相关推荐