关于二叉排序树树建立并返回根节点

来源:互联网 发布:java 运行jar 依赖包 编辑:程序博客网 时间:2024/06/08 04:58

今天做了一道题,发现需要返回根节点,不想多想,就上网搜了一下,发现其中提供的办法都是需要使用父节点,其实并不需要使用父节点。

只需要使用递归函数返回值就可以

struct  T{    int x;    T *lchild,*rchild;};T* init(T* &t){//树t的初始状态为t=NULL;e==0说明没有叶子节点    int e;    scanf("%d",&e);    if(e==0)t=NULL;    else {        t=new T;        t->x=e;        init(t->lchild);        init(t->rchild);    }    return t;}    
建立二叉排序树

struct T{    int x;    T *lchild,*rchild;};T* insert(T* &t,int data){    if(!t){        t=new T;        t->x=data;        t->lchild=t->rchild=NULL;    }    else if(t->x>data)insert(t->lchild,data);    else if(t->x<data)insert(t->rchild,data);    return t;}T* init(T* &t){//<span style="font-family: 'Courier New', Courier, monospace; font-size: 14px; white-space: pre-wrap;">1 4 3 2 9 7 18 22 0   0代表输入结束</span>    int e;    while(scanf("%d",&e)&&e)        t=insert(t,e);    return t;}

这里介绍一下指针,指针的引用。

函数参数的传递的是传值,指针因为传的是地址,所以形参与实参共同指向一个变量,修改形参的值,变量的值也就得到了修改

#include <cstdio>struct T{    int x;    T *lchild,*rchild;};void init(T* t){    T *p=new T;    p->x=2;    p->rchild=p->lchild=NULL;    t->lchild=p;    p=new T;    p->x=3;    p->rchild=p->lchild=NULL;    t->rchild=p;}void in(T* head){    if(head){        in(head->lchild);        printf("%d ",head->x);        in(head->rchild);    }}int main(){    T p;    T *head=&p;    head->x=1;    head->rchild=head->lchild=NULL;    init(head);    in(head);}//输出2 1 3
输出说明了修改了head指向的变量也就是变量p的值。
加引用其实就是相当于传指针的效果,看下面

#include <cstdio>void fun(int &x,int y,int* z){    x=4;    y=5;    *z=6;    return;}int main(){    int a=1,b=2,c=3;    printf("%d %d %d\n",a,b,c);    fun(a,b,&c);    printf("%d %d %d\n",a,b,c);    return 0;}/*1 2 34 2 6Process returned 0 (0x0)   execution time : 0.169 sPress any key to continue.*/
修改了第一个和第三个的值,那么就可以理解为引用就是指针的便捷方式。

#include <cstdio>struct T{    int x;    T *lchild,*rchild;};void init(T* &t){    t=NULL;}void in(T* head){    if(head){        in(head->lchild);        printf("%d ",head->x);        in(head->rchild);    }}int main(){    T p;    T *head=&p;    head->x=1;    head->rchild=head->lchild=NULL;    printf("%x\n",head);    init(head);    printf("%x",head);    //in(head);}//没引用输出28ff10   28ff10//有引用输出28ff14   0
总结如下:形参为T &t,修改的是实参的值;形参为T *t,修改的是实参地址所指向的变量的值;形参为T* &t,修改的是实参的值,也就是指针的值。

所以在数据结构课程中递归建立树中(如第一段代码)如果没有加引用那么在递归函数的第一层中会修改树根的值x,在调用建立子树的时候因为根节点的孩子存储的是^,那么在函数调用的时候只是传入了^,  修改了^所指向的内容,也就当然没用了。

 不用引用动态建树

#include <cstdio>struct T{    int x;    T* lchild;    T* rchild;};void init(T* t,int e){    while(t->rchild||t->lchild){            if(t->x>e&&!t->lchild)break;            if(t->x<e&&!t->rchild)break;            if(t->x>e){t=t->lchild;}            if(t->x<e){t=t->rchild;}    }    if(t->x>e){        t->lchild=new T;        t->lchild->x=e;        t->lchild->lchild=t->lchild->rchild=NULL;    }    else {        t->rchild=new T;        t->rchild->x=e;        t->rchild->lchild=t->rchild->rchild=NULL;    }}void in(T* t){    if(t){        in(t->lchild);        printf("%d ",t->x);        in(t->rchild);    }}int main(){    int e;    T* t=new T;    t->lchild=t->rchild=NULL;    scanf("%d",&e);    t->x=e;    while(scanf("%d",&e)&&e)        init(t,e);    in(t);    return 0;}
输入以0结尾;










1 0
原创粉丝点击