[Programming question] enter a binary search tree to convert the tree to its image

Source: Internet
Author: User

15th questions (tree ):
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
};

 

Ideas:

After a few questions, I finally thought about it. You can do the post-order traversal.

1/* 2 15th question (tree): 3 Question: enter a binary search tree to convert the tree to its image. 4 is in the converted Binary Search Tree, the left subtree has more nodes than the right subtree. 5. Use recursive and cyclic methods to convert tree images. 6 For example, input: 7 8 8/9 6 10 10 // 11 5 7 9 11 12 output: 13 8 14/15 10 6 16 // 17 11 9 7 5 18 defines the node of the Binary Search Tree as: 19 struct bstreenode // a node in the binary search tree (BST) 20 {21 int m_nvalue; // value of node 22 bstreenode * m_pleft; // left child of node 23 bstreenode * m_pright; // right child of node 24 }; 25 26 start time = 27 end time = 28 */29 30 31 // post-thought traversal 32 # include <iostream> 33 # include <vector> 34 using namespace STD; 35 36 typedef struct bstreenode // a node in the binary search tree (BST) 37 {38 int m_nvalue; // value of node 39 bstreenode * m_pleft; // left child of node 40 bstreenode * m_pright; // right child of node 41} bstreenode; 42 43 int addbstreenode (bstreenode * & T, int data) // 44 {45 if (t = NULL) in the tree with T as the root of data) // The root node handles 46 {47 t = (bstreenode *) mall OC (sizeof (bstreenode); 48 t-> m_nvalue = data; 49 T-> m_pleft = NULL; 50 t-> m_pright = NULL; 51} 52 else 53 {54 bstreenode * x = T; 55 bstreenode * PX = NULL; 56 while (X! = NULL) 57 {58 If (Data >=x-> m_nvalue) 59 {60 PX = x; 61 X = x-> m_pright; 62} 63 else 64 {65 PX = x; 66 x = x-> m_pleft; 67} 68} 69 70 If (data> = PX-> m_nvalue) 71 {72 PX-> m_pright = (bstreenode *) malloc (sizeof (bstreenode); 73 PX-> m_pright-> m_nvalue = data; 74 PX-> m_pright-> m_pleft = NULL; 75 PX-> m_pright = NULL; 76} 77 else 78 {79 PX-> m_pleft = (bstreenode *) malloc (sizeof (bstreen Ode); 80 PX-> m_pleft-> m_nvalue = data; 81 PX-> m_pleft = NULL; 82 PX-> m_pleft-> m_pright = NULL; 83} 84} 85 return 1; 86} 87 88 int recursivemirror (bstreenode * t) 89 {90 if (t = NULL) 91 {92 return 0; 93} 94 else 95 {96 recursivemirror (t-> m_pleft); 97 recursivemirror (t-> m_pright); 98 bstreenode * x = T-> m_pleft; 99 T-> m_pleft = T-> m_pright; 100 t-> m_pright = x; 101} 102 103 int Nonrecursivemirror (bstreenode * t) 105 {106 vector <bstreenode *> stack; 107 int tag [50] = {0}; 108 int tagnum = 0; 109 bstreenode * x = T; 110 stack. push_back (t); 111 while (! Stack. Empty () 112 {113 while (x = stack. Back ())! = NULL) 114 {115 x = x-> m_pleft; 116 stack. push_back (x); 117 tag [tagnum ++] = 0; 118} 119 while (TAG [tagnum-1] = 1) // check whether data is processed before the 120 {121 stack is displayed. pop_back (); 122 tagnum --; 123 bstreenode * cur = stack. back (); 124 bstreenode * TMP = cur-> m_pleft; 125 cur-> m_pleft = cur-> m_pright; 126 cur-> m_pright = TMP; 127 128} 129 stack. pop_back (); 130 tagnum --; 131 If (! Stack. empty () // note the condition 132 {133 stack. push_back (stack. back ()-> m_pright); 134 tag [tagnum ++] = 1; 135} 136 137} 138 return 1; 139} 140 int main () 141 {142 bstreenode * t = NULL; 143 addbstreenode (T, 8); 144 addbstreenode (T, 6); 145 addbstreenode (T, 10); 146 addbstreenode (t, 5); 147 addbstreenode (T, 7); 148 addbstreenode (T, 9); 149 addbstreenode (T, 11); 150 151 nonrecursivemirror (t); 152 recursivemirror (t ); 153 return 1; 154}
View code

 

Search for answers on the Internet, and you can find that the first order traversal is also possible. In addition, sequential traversal is easier to write than sequential traversal.

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.