Question 15th:
Question: enter a binary search tree to convert the tree to its image,
That is, in the converted Binary Search Tree, the left subtree has more nodes than the right subtree.
Use recursive and cyclic methods to convert tree images.
For example, enter:
8
/\
6 10
/\/\
5 7 9 11
Output:
8
/\
10 6
/\/\
11 9 7 5
The nodes defining the binary search tree are:
Struct BSTreeNode // a node in the binary search tree (BST)
{
Int m_nValue; // value of node
BSTreeNode * m_pLeft; // left child of node
BSTreeNode * m_pRight; // right child of node
};
[Cpp] # include <iostream>
# Include <iomanip>
# Include <stack>
Using namespace std;
// Defines the node of the Binary Search Tree:
Struct BSTreeNode // a node in the binary search tree (BST)
{
Int m_nValue; // value of node
BSTreeNode * m_pLeft; // left child of node
BSTreeNode * m_pRight; // right child of node
};
Void cfun (BSTreeNode * pt)
{
If (! Pt ){
Return;
}
BSTreeNode * ptem = pt-> m_pLeft;
Pt-> m_pLeft = pt-> m_pRight;
Pt-> m_pRight = ptem;
If (pt-> m_pLeft ){
Cfun (pt-> m_pLeft );
}
If (pt-> m_pRight ){
Cfun (pt-> m_pRight );
}
}
// Consider implementing this algorithm cyclically
Void MirrorIteratively (BSTreeNode * pTreeHead)
{
If (! PTreeHead)
Return;
Std: stack <BSTreeNode *> stackTreeNode;
StackTreeNode. push (pTreeHead );
While (stackTreeNode. size ())
{
BSTreeNode * pNode = stackTreeNode. top ();
StackTreeNode. pop ();
// Swap the right and left child sub-tree
BSTreeNode * pTemp = pNode-> m_pLeft;
PNode-> m_pLeft = pNode-> m_pRight;
PNode-> m_pRight = pTemp;
// Push left child sub-tree into stack if not null
If (pNode-> m_pLeft)
StackTreeNode. push (pNode-> m_pLeft );
// Push right child sub-tree into stack if not null
If (pNode-> m_pRight)
StackTreeNode. push (pNode-> m_pRight );
}
}
// This topic shows the method of recursion into a loop
Int main ()
{
System ("pause ");
Return 0;
}
# Include <iostream>
# Include <iomanip>
# Include <stack>
Using namespace std;
// Defines the node of the Binary Search Tree:
Struct BSTreeNode // a node in the binary search tree (BST)
{
Int m_nValue; // value of node
BSTreeNode * m_pLeft; // left child of node
BSTreeNode * m_pRight; // right child of node
};
Void cfun (BSTreeNode * pt)
{
If (! Pt ){
Return;
}
BSTreeNode * ptem = pt-> m_pLeft;
Pt-> m_pLeft = pt-> m_pRight;
Pt-> m_pRight = ptem;
If (pt-> m_pLeft ){
Cfun (pt-> m_pLeft );
}
If (pt-> m_pRight ){
Cfun (pt-> m_pRight );
}
}
// Consider implementing this algorithm cyclically
Void MirrorIteratively (BSTreeNode * pTreeHead)
{
If (! PTreeHead)
Return;
Std: stack <BSTreeNode *> stackTreeNode;
StackTreeNode. push (pTreeHead );
While (stackTreeNode. size ())
{
BSTreeNode * pNode = stackTreeNode. top ();
StackTreeNode. pop ();
// Swap the right and left child sub-tree
BSTreeNode * pTemp = pNode-> m_pLeft;
PNode-> m_pLeft = pNode-> m_pRight;
PNode-> m_pRight = pTemp;
// Push left child sub-tree into stack if not null
If (pNode-> m_pLeft)
StackTreeNode. push (pNode-> m_pLeft );
// Push right child sub-tree into stack if not null
If (pNode-> m_pRight)
StackTreeNode. push (pNode-> m_pRight );
}
}
// This topic shows the method of recursion into a loop
Int main ()
{
System ("pause ");
Return 0;
}