首页 > 学院 > 开发设计 > 正文

排序二叉树的形成

2019-11-08 02:40:12
字体:
来源:转载
供稿:网友
#include<stdio.h>#include <malloc.h>#define Max 10typedef struct Tree{int data;struct Tree *lchild;struct Tree *rchild;}Tree;}*/void PRo(Tree **tree,int *a,int n);void pro(Tree **tree,int *a,int n){Tree *q=NULL;Tree *p=NULL;int k=0;if(n<Max){if(n==0){(*tree)=(Tree *)(malloc(sizeof(Tree)));(*tree)->lchild=NULL;(*tree)->rchild=NULL;(*tree)->data=a[n];pro(tree,a,n+1);}else{q=(Tree *)malloc(sizeof(Tree));q->lchild=NULL;q->rchild=NULL;q->data=a[n];p=(*tree);while(p->lchild&&p->data>q->data||p->rchild&&p->data<q->data){if(p->data>q->data){p=p->lchild;}else{p=p->rchild;}} if(p->data>q->data){p->lchild=q;}else{p->rchild=q;}pro(tree,a,n+1);}}}int main(){int a[Max]={5,6,45,0,47,7,9,1,65,9};Tree *tree=NULL;pro(&tree,a,0);printf("%d",tree->rchild->rchild->data);}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表