1 //Recursive algorithm2Template <classT>3 voidPostorder (void(*visit) (bintreenode<t>* T), bintreenode<t>*root)4 {5 if(Root! =NULL) {6Postorder (Visit, root->leftchild);7Postorder (Visit, root->rightchild);8 visit (root);9 }Ten}
1 /*2 non-recursive algorithm 1.3 4 non-recursive algorithm, using the node's right pointer to make a discriminant flag whether the node is the first access, starting from the root node to press into all the leftmost nodes into the stack, because the process of being pressed into the stack determines that the current node's left dial hand nodes have been visited, so only to determine the right child node. If the right child node is empty, it can be thought that it has been accessed, and if it is not empty, the pointer to the right child node is modified to be null as a flag that has been visited. 5 6 */7Template <classT>8 voidPostorder (void(*visit) (bintreenode<t>* T), stack<bintreenode<t>*>s)9 {Tenbintreenode<t>* p =Root; One S.push (NULL); A BOOLFlag =true; - Do { - while(P! =NULL) { the S.push (p); -p = p->Leftchild; - } - while(flag) { + if(!S.isempty ()) { - S.pop (p); + if(P->rightchild = =NULL) Visit (p); A Else{//The right child node is not empty and has not been accessed atFlag =false; -S.push (P);//right child node presses back to Stack -bintreenode<t>* tmp = p->Rightchild; -P->rightchild =NULL; -p =tmp; - } in } - } to} while(!s.isempty ()); +}
1 /*2 non-recursive algorithm 1.3 4 non-recursive algorithm, using the node's right pointer to make a discriminant flag whether the node is the first access, starting from the root node to press into all the leftmost nodes into the stack, because the process of being pressed into the stack determines that the current node's left dial hand nodes have been visited, so only to determine the right child node. If the right child node is empty, it can be thought that it has been accessed, and if it is not empty, the pointer to the right child node is modified to be null as a flag that has been visited. 5 6 */7Template <classT>8 voidPostorder (void(*visit) (bintreenode<t>* T), stack<bintreenode<t>*>s)9 {Tenbintreenode<t>* p =Root; One S.push (NULL); A Do { - while(P! =NULL) { - S.push (p); thep = p->Leftchild; - } - BOOLFlag =true; - while(flag) { + if(!S.isempty ()) { - S.pop (p); + if(P->rightchild = =NULL) Visit (p); A Else{//The right child node is not empty and has not been accessed atFlag =false; -S.push (P);//right child node presses back to Stack -bintreenode<t>* tmp = p->Rightchild; -P->rightchild =NULL; -p =tmp; - } in } - } to} while(!s.isempty ()); +}
1 /*2 non-recursive algorithm 33 construct a new struct and add a variable visit to indicate whether the node has been accessed4 */5 6Template <classT>7 structNewNode {8bintreenode<t>*ptr;9 intvisit;TenNewNode (bintreenode<t>* P): PTR (P), visit (0) {} One } A -Template <classT> - voidPostorder (void(visit*) (bintreenode<t>* T), stack<newnode<t>*>s) the { -bintreenode<t>* p =Root; -newnode* NP =NULL; - Do { + while(P! =NULL) { -NP =NewNode (p); + S.push (NP); Ap = p->Leftchild; at } - BOOLFlag =true; - while(flag) { - S.pop (NP); - if(Np->ptr->rightchild = = NULL | | np->visit = =1) { -Visit (np->ptr); in}Else { - S.push (NP); toFlag =false; +Np->visit =1; -p = np->ptr->Rightchild; the } * } $} while(!s.isempty ());Panax Notoginseng}
Implementation of successive traversal algorithm of binary tree