----"The essence of C language is the flexibility of pointers. The goal of learning pointers is to use pointers as little as possible. "
A problem was encountered while knocking binary search tree (binary lookup trees). At the time of deletion, if the root node is deleted, the address of the root node should be changed, and the implementation will be very painful. It turns out that a good way to do this is to use level two pointers, which makes it easy to change the address of the root node. I just need to change the stored address in the pointer to the root node. In order to understand the level two pointer, do a little experiment:
typedefstructtree_node{intVal; Tree_node*left, *right, *parent; Tree_node (intx) {val=x; Left=NULL; Right=NULL; Parent=NULL; }}node,*Pnode;intMain () {Node x (1); cout<<&x<<Endl; Node*p = &x; cout<<&p<<" "<<p<<" "<<p->val<<endl;//&p:p own address P: point to (x) P: Point at Address (x) Val valueNode **pp = &p; cout<<&pp<<" "<<pp<<" "<< (*PP) <<" "<< (*PP)->val<<endl;//&pp:pp own address pp: point to (P) address *pp: Remove the value at the address where pp is stored (p) (&x)//you can see that Pnode is actually a node*, so here Pnode is a (node**) is a two-level pointer to P (one-level pointer)Pnode *pnode = &p; cout<<&pnode<<" "<<pnode<<" "<< (*pnode) <<" "<< (*pnode)->val<<Endl; return 0; }
Finally, some basic operations of searching binary tree are completed. Attached Gitlab warehouse Link: [Email protected]:luntai/algorithm.git
C's Level two pointer