Looking for a job now. Review the binary tree of the data structure! So implement it, but encountered some problems when creating a binary tree!
Tree. h
# Ifndef _ tree_h # DEFINE _ tree_h # include <stdio. h >#include <iostream >#include <stack> using namespace STD; typedef char elemtype; typedef struct node {elemtype data; struct node * left; struct node * right;} node; node * & getleft (node * & node); node * & getright (node * & node); void create (node * & node); void preordertraverse (node * & node ); void inordertraverse (node * & node); void postordertraverse (node * & node); void deletetree (node * & node); # endif
Tree. cpp
# Include "tree. H "Node * & getleft (node * & node) {return node-> left;} node * & getright (node * & node) {return node-> right ;} void create (node * & node) {elemtype ELEM; CIN> ELEM; If (ELEM = '#') {node = NULL; return ;} else {node = new node (); node-> DATA = ELEM; node-> left = NULL; node-> right = NULL; Create (getleft (node )); create (getright (node); // create (node-> left); // create (node-> right) ;}} void preordertraverse (node * & node ){ Cout <"first-order traversal tree:" <Endl; node * P = NULL; stack <node *> S; If (node = NULL) {cout <"tree is empty! "<Endl; return;} s. Push (node); While (! S. empty () {P = S. top (); S. pop (); cout <"node is:" <p-> data <Endl; If (p-> right! = NULL) {S. Push (p-> right);} If (p-> left! = NULL) {S. push (p-> left) ;}}void inordertraverse (node * & node) {cout <"middle-order traversal tree:" <Endl; node * P = NULL; stack <node *> S; S. push (node); While (! S. Empty () {While (P = S. Top ())! = NULL) {If (p-> left! = NULL) {S. push (p-> left);} else {break;} p = S. top (); S. pop (); cout <"node is:" <p-> data <Endl; If (! S. empty () {P = S. top (); S. pop (); cout <"node is:" <p-> data <Endl; S. push (p-> right) ;}}void postordertraverse (node * & node) {cout <"descending traversal tree:" <Endl; node * P = NULL; stack <node *> S; S. push (node); While (! S. Empty () {While (P = S. Top ())! = NULL) {If (p-> right! = NULL) {S. Push (p-> right);} If (p-> left! = NULL) {S. Push (p-> left) ;}else {break ;}// outputs the leftmost node! P = S. top (); S. pop (); cout <"node is:" <p-> data <Endl ;}} void deletetree (node * & node) {If (node! = NULL) {If (node-> left) {deletetree (node-> left);} If (node-> right) {deletetree (node-> right );} delete node; node = NULL ;}}
Main. cpp
# Include "tree. H" Void main () {node * root = NULL; Create (Root); inordertraverse (Root); preordertraverse (Root); deletetree (Root );}
When I call void create (node * & node); To transmit parameters, the parameters are pointer references.
If the function calls
Create (node-> left); Create (node-> right );
No problem!
But if we use the return value of the function to pass it, there will be a problem!
If our function is defined as this way
Node * getleft (node * & node) {return node-> left;} node * getright (node * & node) {return node-> right ;}
When calling
Create (getleft (node); Create (getright (node ));
The compiler will prompt an error!
Error c2664: 'create': cannot convert parameter 1 from 'struct node * 'to 'struct node *&'
The create function requires a pointer reference. If node-> left is correct, because node-> left is a pointer variable, and the pointer reference means the pointer itself, so there is no problem, and there is no replication.
However, if the returned value of the function is a pointer, it may be a copy of node-> left, not a reference. If the compiler passes, the operation is still a copy of the pointer. In this case, we need a pointer reference, so we can modify the return value of the function and return a pointer reference.
Node * & getleft (node * & node) {return node-> left;} node * & getright (node * & node) {return node-> right ;}
Therefore, when referencing a parameter, it is easier to pass it to a variable. If you want to pass a function return value, pay attention to it!