Programmer interview question selection (11): image of Binary Search Tree

Source: Internet
Author: User
Question: enter a binary search tree and 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
};

Analysis: although we may not be able to understand what the image means at once, the above example gives us an intuitive feeling, that is, the left and right subtree of the switching node. We try to traverse the Binary Search Tree in the example and exchange the left and right subtree of each node. The traversal first accesses header node 8, and we exchange its left and right subtree to get:

8
//
10 6
////
9 11 5 7

We found that the left and right subtree values of nodes 6 and 10 are still smaller than the right subtree values. We try to exchange the left and right subtree values to get:

8
//
10 6
////
11 9 7 5

That is, the required output.

The above analysis proves our intuition: every time a node is accessed while traversing a binary search tree, its left and right subtree is exchanged. It is not difficult to implement this method using recursion. Just make a slight modification to the code that traverses the binary search tree. The reference code is as follows:

//////////////////////////////////////// ///////////////////////////////
// Mirror a BST (swap the left right child of each node) recursively
// The Head of BST in initial call
//////////////////////////////////////// ///////////////////////////////
Void initialize recursively (bstreenode * pnode)
{
If (! Pnode)
Return;

// Swap the right and left child sub-tree
Bstreenode * ptemp = pnode-> m_pleft;
Pnode-> m_pleft = pnode-> m_pright;
Pnode-> m_pright = ptemp;

// Mirror left child sub-tree if not null
If (pnode-> m_pleft)
Repeated recursively (pnode-> m_pleft );

// Mirror right child sub-tree if not null
If (pnode-> m_pright)
Repeated recursively (pnode-> m_pright );
}

Because the essence of recursion is that the compiler generates a function call stack, the simplest way to use a loop to complete the same task is to use an auxiliary stack to simulate recursion. First, we put the header node of the tree into the stack. In a loop, as long as the stack is not empty, the top node of the stack pops up and switches its left and right subtree. If it has a left subtree, press its left subtree into the stack; if it has a right subtree, press its right subtree into the stack. In this way, the left and right subtree of its son node can be exchanged in the next loop. The reference code is as follows:

//////////////////////////////////////// ///////////////////////////////
// Mirror a BST (swap the left right child of each node) iteratively
// Input: ptreehead: The head of BST
//////////////////////////////////////// ///////////////////////////////
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 );
}
}

 

Related Article

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.