C Language Enhancement (11) Binary Tree Mirroring changes | Requirements: Do not use recursion

Source: Internet
Author: User

it took so long. recursion, now don't let recursion, can you?


Through this problem, you can learn

    • How to mirror change a binary tree
    • What is the nature of recursion
    • How to skillfully use a secondary stack


Topic:
Enter a two-dollar lookup tree to convert the tree to its mirror,
That is, in the converted two-dollar lookup tree, the nodes of the Zuozi are larger than the nodes of the right subtree.
Requirements:
Do not use recursion


For example, enter:


Output:



The method of changing the image of a two-fork tree is simple, as long as the left and right nodes of all nodes are swapped, using recursion can be very easy to implement.

The following is a recursive method to implement the function of mirror change

/** two fork Tree mirror Change-recursive method implementation */void revertsetree_recursion (Btreenode *root) {if (!root) return; Btreenode *p;p=root->leftnode;root->leftnode=root->rightnode;root->rightnode=p;if (Root->leftNode ) revertsetree_recursion (Root->leftnode); if (Root->rightnode) revertsetree_recursion (Root->rightNode);}

Now the problem is, do not use recursion, how to do?


Ideas:

The essence of recursion is to store the stack of elements as functions,

So the simplest way to accomplish the same task is to use a secondary stack to simulate recursion, supplemented by a looping statement

    • First, put the tree's head node in the stack.
    • In the loop, as long as the stack is not empty, the top node of the stack pops up, exchanging its left and right subtrees
    • If it has a left dial hand tree, press its left subtree into the stack.
    • If it has a right subtree, press its right subtree into the stack.
    • Swapping the left and right subtree of his son's knot in the next loop.

The following is a function that uses a circular statement to implement a mirror change

/** two fork Tree image Change--loop method implementation */void Revertsetree_circle (Btreenode *phead) {if (!phead) return;stack<btreenode*> stacklist ; Stacklist.push (Phead); First, the tree's head node is put into the stack. while (Stacklist.size ())//In the loop, as long as the stack is not empty, pop up the stack top node, exchange its left and right subtree {btreenode* pnode=stacklist.top (); Stacklist.pop (); Btreenode *ptemp;ptemp=pnode->leftnode;pnode->leftnode=pnode->rightnode;pnode->rightnode=ptemp;if ( Pnode->leftnode) Stacklist.push (Pnode->leftnode); If there is a left dial hand tree, press its left subtree into the stack if (pnode->rightnode) Stacklist.push (Pnode->rightnode); If there is a right sub-tree, press its right subtree into the stack}}

Source Code

/** Title: Enter a two-dollar lookup tree to convert the tree to its image, that is, in the converted two-tuple lookup tree, the nodes of the Zuozi are larger than the nodes of the right subtree. Requirements: Do not use recursive thinking of all nodes of the left and right node reversal using recursion can be very simple to implement because the nature of recursion is the compiler generates a function call stack, so the simplest way to do the same task with a loop is to use a secondary stack to simulate recursion first we put the tree's head node into the stack. In the loop, as long as the stack is not empty, the top node of the stack pops up, exchanging its left and right subtree. If it has a left dial hand tree, press its left subtree into the stack, and if it has a right subtree, press its right subtree into the stack. This will swap the left and right subtree of his son's node in the next cycle. */#include <stdlib.h> #include <iostream> #include <sstream> #include < Vector> #include <stack>using namespace std;//create a two-fork tree structure struct btreenode{int value; Btreenode *leftnode; Btreenode *rightnode;};/ * Binary Tree constructor is inserted (left small right Large) node two fork tree root node value */void inserttobtree (Btreenode * &node,int value) {//node is empty, by binary sorting algorithm The Insert node is the root node if (null==node) {Btreenode * btnode=new btreenode (); btnode->value=value;btnode->leftnode=null;btnode- >leftnode=null;node=btnode;} else{//node non-null//value less than root node, recursive left node if ((Node->value) >value) Inserttobtree (node->leftnode,value);//value greater than root node, Recursive right node else if (value> (Node->value)) Inserttobtree (node->rightnode,value);//value equal to existing node value, prompting error elseprintf (" The node already exists and cannot be inserted again! ");}} /* Pre-sequence traversal binary sort tree Btnode two fork root node */void gothroughbtreE (Btreenode * btnode) {//node is empty, return if (Null==btnode) return;cout<<btnode->value<<endl;//left node is not empty, continue to left deep if (Null!=btnode->leftnode) Gothroughbtree (Btnode->leftnode);//Right not empty, right depth if (Null!=btnode->rightnode) { Gothroughbtree (Btnode->rightnode);}} /** two fork Tree mirror Change-recursive method implementation */void revertsetree_recursion (Btreenode *root) {if (!root) return; Btreenode *p;p=root->leftnode;root->leftnode=root->rightnode;root->rightnode=p;if (Root->leftNode ) revertsetree_recursion (Root->leftnode); if (Root->rightnode) revertsetree_recursion (Root->rightNode);} /** two fork Tree image Change--loop method implementation */void Revertsetree_circle (Btreenode *phead) {if (!phead) return;stack<btreenode*> stacklist ; Stacklist.push (Phead); First, the tree's head node is put into the stack. while (Stacklist.size ())//In the loop, as long as the stack is not empty, pop up the stack top node, exchange its left and right subtree {btreenode* pnode=stacklist.top (); Stacklist.pop (); Btreenode *ptemp;ptemp=pnode->leftnode;pnode->leftnode=pnode->rightnode;pnode->rightnode=ptemp;if ( Pnode->leftnode) Stacklist.push (Pnode->leftnode); If there is a left dial hand tree, press its left subtree into the stack if (pNode->rightnode) Stacklist.push (Pnode->rightnode); If there is a right subtree, press its right subtree into the stack}}void main () {Btreenode *root=null;//two fork root node//Create two fork sort Tree inserttobtree (root,8); Inserttobtree (root,6 ); Inserttobtree (root,10); Inserttobtree (root,5); Inserttobtree (root,7); Inserttobtree (root,9); InsertToBTree (Root, 11);//Traverse binary tree cout<< "original binary Tree:" <<endl;gothroughbtree (Root);//revertsetree_recursion (root); Revertsetree_circle (Root);cout<< "mirrored binary Tree:" <<endl;gothroughbtree (root); system ("Pause");}

Run Diagram



About the use of auxiliary stacks in C language Enhancement (ii) design can be used to find the minimum elements of the stack has been introduced

This time again use, I believe students will have a deeper understanding of it experience.


Summarize

The essence of recursion is a stack of functions stored

C Language Enhancement (11) Binary Tree Mirroring changes | Requirements: Do not use recursion

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.