[shorthand] about pointers, references, and recursion and recursion--c++

Source: Internet
Author: User

When writing a search based on a binary sort tree, it is divided into three processes

1. Inserting a two-fork sort tree

2. Establishment of a two-fork sorting tree

3. Search based on binary sort tree

One of the third can be implemented recursively, you can also use the while loop to recursion, so I want to also solve the first step of recursion, see the line, the result gave me blow , the solution recursion failed!

Finally I analyzed the reason:

First look at the original recursive implementation of the way

typedef struct  _treenode{struct _treenode *leftnode;struct _treenode *rightnode; Typedata data;} treenode,*treeroot;//returns the insertion position of the node treenode* insert_tree (Treeroot &root,typedata key) {if (!root) {TreeNode *node=new Treenode;node->data=key;node->leftnode=nullptr;node->rightnode=nullptr;root=node;return Root;} else if (Root->data==key) {return root;} else if (Root->data<key) {return (Insert_tree (Root->rightnode,key));} else if (Root->data>key) {return (Insert_tree (Root->leftnode,key));}}

  

Then, I realized the recursive implementation of my own.

This method is incorrect for treenode* insert_tree2 (treeroot &root,typedata key) {TreeNode *p=root;while (p) {if (P->data==key) { return p;break;} else if (P->data>key) {P=p->leftnode;} Else{p=p->rightnode;}} if (!p) {p=new treenode;p->data=key;p->leftnode=nullptr;p->rightnode=nullptr;return p;}}

  

Seemingly, no problem, but actually made a very bad mistake

As we can see, both ways are passed in the root node in the form of a reference, why use a reference?

Because we will definitely make a new node when we insert it, and then we have to get it into the original tree system, the first recursive way, the last time the knot is built.

TreeNode *node=new treenode;node->data=key;node->leftnode=nullptr;node->rightnode=nullptr; Root=node; return root;

Can be seen, the location of the black, this time the root is referred to the way in, this fact directly changed the original tree system node


In the implementation of recursion, although it is the parameter passed in by reference, we use a pointer local variable to replace him, and at last
P=new Treenode;p->data=key;p->leftnode=nullptr;p->rightnode=nullptr;return p;
How does the p here get it?
P= the left of the previous p,
What are the consequences of such a way?
It changes the contents of the two pointers together, but cannot change the other pointer itself, does it feel incomprehensible?

It doesn't matter that P is assigned according to the left of the previous parent node, assuming that the previous parent node is called F
Well, that's
p=f->left;
At this time p and F->left are nullptr,
Until this time, there's no problem.
Then we kept to change the contents of P,
This time, yes, we changed the contents of the two points by P, but!
F->left has always been a nullptr.
You change more, still have nothing to do with me!



Do not understand, and then see a program
    Node *p=new node;    p->left=nullptr;    p->right=nullptr; Node *l=p->left; Node *q=p->left;q=new node;q->data= ' q '; q->left=q;q->right=q;std::cout<<l->data;std::cout <<q->data;

  

Finally, summarize:

1. Be careful! modifying pointers and modifying pointers together point to something that is different!

2. In the general use of chained storage, when you want to change the relationship between nodes, or add nodes, it is best to use a reference to the pointer (level two pointers, but not recommended).

3. When the second case is used to recursion, the recursion must be cautious, it is best not to recursion!

[shorthand] about pointers, references, and recursion and recursion--c++

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.