当前位置 : 主页 > 手机开发 > harmonyos >

线索二叉树

来源:互联网 收集:自由互联 发布时间:2023-08-25
#includestdlib.h #includestdio.h struct ele { int value; int L; int R; ele*left; ele*right; }*pre; void build(ele*p) { int n; scanf("%d",n); if(n==0) p=NULL; else { p=(ele*)malloc(sizeof(ele)); p-value=n; build(p-left); build(p-right); } }
#include<stdlib.h> 

 #include<stdio.h> 

 struct ele 

 { 

int value; 
  

int L; 

int R; 

ele*left; 

ele*right; 

 }*pre; 

 void build(ele*&p) 

 { 

int n; 

scanf("%d",&n); 

if(n==0) 

p=NULL; 

else 

{ 

p=(ele*)malloc(sizeof(ele)); 

p->value=n; 

build(p->left); 

build(p->right); 

} 

 } 

 void inter(ele*&p) 

 { 

if(p) 

{ 

inter(p->left); 

if(p->left==NULL) 

{ 

p->L=1; 

p->left=pre; 

} 

else 

p->L=0; 

if(pre->right==NULL) 

{ 

pre->R=1; 

pre->right=p; 

} 

else 

pre->R=0; 

pre=p; 

inter(p->right); 

} 

 } 

 void order(ele*&p,ele*&head) 

 { 

head=(ele*)malloc(sizeof(ele)); 

head->L=0; 

head->R=1; 

head->right=p; 

if(!p) 

head->right=p; 

else 

{ 

head->left=p; 

pre=p; 

inter(p); 

pre->R=1; 

pre->right=head; 

head->right=pre; 

} 

 } 

 int main() 

 { 

void search(ele*&head); 

ele*p; 

ele *head; 

build(p); 

order(p,head); 

search(head); 

return 0; 

 } 

 void search(ele*&head) 

 { 

ele *p=head->left; 

while(p!=head) 

{ 

while(p->L==0) 

p=p->left; 

printf("%d 
  ",p->value); 

while(p->R==1&&p->right!=head) 

{ 

p=p->right; 

printf("%d 
  ",p->value); 

} 

p=p->right; 

} 

 }
上一篇:
下一篇:没有了
网友评论