Basic algorithm Exercise 1, basic algorithm Exercise 1

Source: Internet
Author: User

Basic algorithm Exercise 1, basic algorithm Exercise 1

Recursive and non-recursive traversal of Binary Trees:

1 # include <fstream> 2 # include <iostream> 3 4 using namespace std; 5 6/* 7 Non-recursive implementations of post-sequential traversal are the most difficult of the three traversal methods. In the post-order traversal, ensure that both the left and right children have been accessed and that the left child can access the root node before the right child 8, this brings difficulties to process control. The following two ideas are described. 9 first thought: For any node P, add it to the stack, and search down along the left subtree until the node with no left child is found, at this time, the node appears at the top of the stack, but it cannot be taken out of the stack and accessed at this time, so its right child is still being accessed. So next we will perform the same processing for the right subtree according to the same rules. When the right subtree 11 is accessed, the node appears at the top of the stack, and then it can be accessed out of the stack. This ensures the correct access order. It can be seen that in this process, each node appears at the top of the stack twice, and can be accessed only when it appears at the top of the stack for the second time. Therefore, you need to set another variable to identify whether the node appears at the top of the stack for the first time. 13. Second approach: ensure that the root node can be accessed only after the access from the left and right children. Therefore, for any node P, first add it to the stack. If P does not have the left and right children, 14 can access it directly; or P has the Left or Right children, but both the left and right children have been accessed, you can also directly access this node. If the preceding 15 conditions are not met, P's right child and left child are added to the stack in sequence. This ensures that each time the top element of the stack is retrieved, the left child is accessed in front of the right child, and both the left child and the Right child are accessed in front of the knot 16. 17 */18 19 # define queue_len 100 20 21 struct node 22 {23 char c; 24 struct node * lch, * rch; 25 bool flag; 26 node (): flag (0) {} 27 void get_c () 28 {29 printf ("% c", c); 30} 31}; 32 33 node * set_tree (); // create 34 void pre_order (node * tree); // traverse 35 void in_order (node * tree) in the first order; // traverse 36 void post_order (node * tree) in the middle order ); // post-order traversal 37 void level_order (node * tree); // layer traversal 38 void nr_pre_order (node * tree); // non-recursive first-order traversal 39 void nr_in_order (node * tree); // non-recursive central order traversal 40 void nr_post_order_1 (node * tree); // non-recursive post order traversal 41 void nr_post_order_2 (node * tree ); // non-recursive post-order traversal 42 43 int main () 44 {45 // freopen ("D: \ input. in "," r ", stdin); 46 // freopen (" D: \ output. out "," w ", stdout); 47 node * tree = set_tree (); 48 printf (" sequential traversal: "); 49 pre_order (tree ); 50 puts (""); 51 printf ("middle-order traversal:"); 52 in_order (tree); 53 puts (""); 54 printf ("back-order traversal: "); 55 post_or Der (tree); 56 puts (""); 57 printf ("layered traversal:"); 58 level_order (tree); 59 puts (""); 60 printf ("sequential traversal:"); 61 nr_pre_order (tree); 62 puts (""); 63 printf ("sequential traversal:"); 64 nr_in_order (tree ); 65 puts (""); 66 printf ("post-order traversal:"); 67 nr_post_order_1 (tree); 68 puts (""); 69 printf ("post-order traversal: "); 70 nr_post_order_2 (tree); 71 puts (" "); 72 return 0; 73} 74 node * set_tree () 75 {76 node * p, * s; 77 node * gen = new node; 78 gen-> c =' A'; 79 80 gen-> lch = new node; 81 p = gen-> lch; 82 p-> c = 'B'; 83 p-> rch = NULL; 84 p-> lch = new node; 85 p = p-> lch; 86 p-> c = 'D'; 87 p-> lch = NULL; 88 p-> rch = new node; 89 p = p-> rch; 90 p-> c = 'G'; 91 p-> lch = NULL; 92 p-> rch = NULL; 93 94 gen-> rch = new node; 95 p = gen-> rch; 96 p-> c = 'C '; 97 p-> lch = new node; 98 s = p-> lch; 99 s-> c = 'E'; 100 s-> lch = NULL; 101 s-> rch = NULL; 102 p-> rch = new node; 103 s = p-> rch; 104 s-> c = 'F '; 105 s-> lch = NULL; 106 s-> rch = NULL; 107 108 return gen; 109} 110 void pre_order (node * tree) 111 {112 if (tree! = NULL) 113 {114 tree-> get_c (); 115 pre_order (tree-> lch); 116 pre_order (tree-> rch ); 117} 118} 119 void in_order (node * tree) 120 {121 if (tree! = NULL) 122 {123 in_order (tree-> lch); 124 tree-> get_c (); 125 in_order (tree-> rch ); 126} 127} 128 void post_order (node * tree) 129 {130 if (tree! = NULL) 131 {132 post_order (tree-> lch); 133 post_order (tree-> rch); 134 tree-> get_c (); 135} 136} 137 void level_order (node * tree) 138 {139 node * queue_1 [queue_len]; 140 int front, rear; 141 142 if (tree = NULL) return; 143 front =-1; 144 rear = 0; 145 queue_1 [rear] = tree; 146 while (front! = Rear) 147 {148 front ++; 149 queue_1 [front]-> get_c (); 150 151 if (queue_1 [front]-> lch! = NULL) 152 {153 rear ++; 154 queue_1 [rear] = queue_1 [front]-> lch; 155} 156 if (queue_1 [front]-> rch! = NULL) 157 {158 rear ++; 159 queue_1 [rear] = queue_1 [front]-> rch; 160} 161} 162 void nr_pre_order (node * tree) 164 {165 node * stack_1 [queue_len]; 166 int top =-1; 167 168 if (tree = NULL) return; 169 node * p = tree; 170 while (p! = NULL | top! =-1) 171 {172 while (p! = NULL) 173 {174 p-> get_c (); 175 stack_1 [++ top] = p; 176 p = p-> lch; 177} 178 if (top =-1) return; 179 p = stack_1 [top --]-> rch; 180} 181} 182 void nr_in_order (node * tree) 183 {184 node * stack_1 [queue_len]; 185 int top =-1; 186 187 if (tree = NULL) return; 188 node * p = tree; 189 while (p! = NULL | top! =-1) 190 {191 while (p! = NULL) 192 {193 stack_1 [++ top] = p; 194 p = p-> lch; 195} 196 if (top =-1) return; 197 stack_1 [top]-> get_c (); 198 p = stack_1 [top --]-> rch; 199} 200} 201 void nr_post_order_1 (node * tree) 202 {203 node * stack_1 [queue_len]; 204 int top =-1; 205 206 if (tree = NULL) return; 207 node * p = tree; 208 while (p! = NULL | top! =-1) 209 {210 while (p! = NULL) 211 {212 stack_1 [++ top] = p; 213 p = p-> lch; 214} 215 if (top =-1) return; 216 p = stack_1 [top]; 217 if (p-> flag = 0) {p-> flag = 1; p = p-> rch ;} 218 else {p-> get_c (), top --; p = NULL;} 219} 220} 221 void nr_post_order_2 (node * tree) 222 {223 node * stack_1 [queue_len]; 224 int top = 0; 225 226 if (tree = NULL) return; 227 node * p = tree; 228 while (top! =-1) 229 {230 p = stack_1 [top]; 231 if (p-> lch = NULL | p-> lch-> flag = 0) & (p-> rch = NULL | p-> rch-> flag = 0) 232 {233 p-> get_c (); 234 p-> flag = 0; 235 top --; 236} 237 else238 {239 if (p-> rch! = NULL) 240 {241 stack_1 [++ top] = p-> rch; 242} 243 if (p-> lch! = NULL) 244 {245 stack_1 [++ top] = p-> lch; 246} 247} 248}View Code

 

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.