About binary sort tree set up and return root node

Source: Internet
Author: User

Today did a problem, found that need to return to the root node, do not want to think more, on the Internet search, found that the method provided by the need to use the parent node, in fact, do not need to use the parent node.

Just use the recursive function to return a value to

struct  t{    int x;    T *lchild,*rchild;}; t* Init (t* &t) {///tree T has an initial state of t=null;e==0 Description No leaf node    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;}    
Set up a two-fork sorting tree

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-wr AP; " >1 4 3 2 9 7 0 0 for   input end </span>    int e;    while (scanf ("%d", &e) &&e)        T=insert (t,e);    return t;}

Here is a reference to pointers, pointers.

The function parameter passes the value, the pointer because passes the address, therefore the shape participates in the argument together to point to a variable, modifies the parameter the value, the variable value also has been modified

#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);} Output 2 1 3
The output shows that the variable that the head points to is the value of the variable p.
Plus the reference is actually equivalent to the effect of the pointer, see below

#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 2 6Process returned 0 (0x0)   execution time:0.169 spress any key to continue.*/
The first and third values are modified so that they can be understood as a convenient way to refer to a pointer.

#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);} No reference output 28FF10   28ff10//has a reference output 28FF14   0
It is summarized as follows: The parameter is T &t, the value of the argument is modified, the parameter is T *t, the value of the variable that the argument address points to is modified, the parameter is t* &t, and the value of the argument is modified, which is the value of the pointer.

So in the data structure class recursively build the tree (such as the first paragraph of code) if there is no reference then in the first layer of the recursive function will modify the root value x, in the call to establish a subtree because the child of the root node is stored ^, then in the function call just passed ^, modified the ^ points to the content, Of course it's useless.

Do not cite dynamic achievements

#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;}
The input ends with 0;










Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

About binary sort tree set up and return root node

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.