Non-recursive C language for Binary Tree first, middle and back traversal, recursive Binary Tree

Source: Internet
Author: User

Non-recursive C language for Binary Tree first, middle and back traversal, recursive Binary Tree

1 # include <stdio. h> 2 # include <stdlib. h> 3 # define INIT_STACK_SIZE 100 4 # define STACKINCREMENT 10 5 6 // ****** Binary Tree binary linked list storage structure ***** // 7 typedef struct BiNode 8 {9 char data; 10 struct BiNode * lchild, * rchild; 11 int visitcount; // number of visits (used in post-order traversal) 12} BiNode, * BiTree; 13 14 typedef struct 15 {16 BiNode ** base; 17 BiNode ** top; 18 int stacksize; 19} SqStack; 20 21 // ****** initialize stack ***** // 22 void InitStack (Sq Stack & S) 23 {24 if (! (S. base = (BiNode **) malloc (sizeof (BiTree) * INIT_STACK_SIZE) 25 {26 return; 27} 28 S. top = S. base; 29 S. stacksize = INIT_STACK_SIZE; 30 31 return; 32} 33 34 // ***** element into Stack ***** // 35 void Push (SqStack & S, biNode * e) 36 {37 if (S. top-S. base> = S. stacksize) 38 {39 if (! (S. base = (BiNode **) realloc (S. base, sizeof (BiTree) * (STACKINCREMENT + S. stacksize) 40 {41 return; 42} 43 S. stacksize + = STACKINCREMENT; 44} 45 * S. top ++ = e; 46 47 return; 48} 49 50 // ***** element output stack ***** // 51 void Pop (SqStack & S, biNode ** e) 52 {53 if (S. base = S. top) 54 {55 return; 56} 57 * e = * -- S. top; 58 59 return; 60} 61 62 // ***** top element of the stack *** // 63 int GetTop (SqStack S, BiNode *** e) 64 {6 5 if (S. base = S. top) 66 {67 return 0; 68} 69 * e = * (S. top-1); 70 71 return 1; 72} 73 74 // ***** determine whether the stack is empty ***** // 75 int StackEmpty (SqStack S) 76 {77 if (S. top = S. base) 78 return 1; 79 else 80 return 0; 81} 82 83 // ***** enter the node values (one character) in the binary tree in the FIFO order ), the space character indicates that the empty tree constructs the binary tree T ***** represented by the binary chain table. // 84 void CreateBiTree (BiTree & T) 85 {86 char ch; 87 scanf ("% c", & ch); 88 if (ch = '') 89 {90 T = NULL; 91} 92 else 93 {94 if (! (T = (BiNode *) malloc (sizeof (BiNode) 95 {96 return; 97} 98 T-> data = ch; // generate the root node 99 CreateBiTree (T-> lchild); // construct the left subtree 100 CreateBiTree (T-> rchild); // construct the right subtree 101} 102 103 return; 104} 105 106 // ***** first traverse the binary tree method 1 ***** // 107 void PreorderTraverse_1 (BiTree T) 108 {109 BiTree p; 110 SqStack S; 111 InitStack (S); 112 Push (S, T); // The root pointer is pushed to the stack 113 while (! StackEmpty (S) 114 {115 while (GetTop (S, & p) 116 {117 printf ("% c", p-> data ); 118 Push (S, p-> lchild); 119} 120 Pop (S, & p); // empty pointer stack rollback 121 if (! StackEmpty (S) 122 {123 Pop (S, & p); // 124 Push (S, p-> rchild) of the root node stack ); // right child stack 125} 126} 127 128 return; 129} 130 131 // ****** first traverse binary tree method 2 ***** // 132 void PreorderTraverse_2 (BiTree T) 133 {134 BiTree p = T; 135 SqStack S; 136 InitStack (S); 137 138 while (p |! StackEmpty (S) 139 {140 if (p) 141 {142 printf ("% c", p-> data); 143 Push (S, p ); 144 p = p-> lchild; 145} 146 else 147 {148 Pop (S, & p); 149 p = p-> rchild; 150} 151} 152 153 154 // ****** method 1 ***** // 155 void InOrderTraverse_1 (BiTree T) 156 {157 SqStack S; 158 BiTree p; 159 InitStack (S); 160 Push (S, T); 161 while (! StackEmpty (S) 162 {163 while (GetTop (S, & p) 164 {165 Push (S, p-> lchild ); // 166} 167 Pop (S, & p) at the end of the left direction; // empty pointer stack rollback 168 if (! StackEmpty (S) 169 {170 Pop (S, & p); 171 printf ("% c", p-> data); 172 Push (S, p-> rchild); 173} 174} 175 176 return; 177} 178 179 // ***** method 2 ***** of the central order traversal Binary Tree // 180 void InOrderTraverse_2 (BiTree T) 181 {182 BiTree p = T; 183 SqStack S; 184 InitStack (S); 185 186 while (p |! StackEmpty (S) 187 {188 if (p) 189 {190 Push (S, p); 191 p = p-> lchild; 192} 193 else 194 {195 Pop (S, & p); 196 printf ("% c", p-> data); 197 p = p-> rchild; 198} 199} 200 201 202 return; 203} 204 205 // ***** post-order traversal of a binary tree ***** // void PostOrderTraverse (BiTree T) 206 {207 SqStack S; 208 BiTree p = T; 209 InitStack (S); 210 211 while (p |! StackEmpty (S) 212 {213 if (p) 214 {215 if (p-> visitcount! = 2) 216 {217 p-> visitcount = 1; 218 Push (S, p); 219} 220 p = p-> lchild; 221} 222 else 223 {224 Pop (S, & p); 225 if (p-> visitcount = 2) 226 {227 printf ("% c ", p-> data); 228} 229 else 230 {231 p-> visitcount ++; 232 Push (S, p); 233} 234 p = p-> rchild; 235} 236} 237 238 return; 239} 240 int main (void) 241 {242 BiTree T; 244 printf ("Enter the node value (character) in the binary tree in the FIFO order, and the space character indicates the empty tree: \ n"); 245 CreateBiTree (T ); 246 247 printf ("first order traversal method 1 Result:"); 248 PreorderTraverse_1 (T); 249 printf ("\ n "); 250 251 printf ("first order traversal method 2 Result:"); 252 PreorderTraverse_2 (T); 253 printf ("\ n "); 254 255 printf ("ordinal Traversal method 1 Result:"); 256 InOrderTraverse_1 (T); 257 printf ("\ n "); 258 259 printf ("method 2 in the middle order traversal result:"); 260 InOrderTraverse_2 (T); 261 printf ("\ n "); 262 263 printf ("the result of post-sequential traversal is:"); 264 PostOrderTraverse (T); 265 printf ("\ n"); 266 267 return 0; 268}

The following binary tree is used as an example to describe the value (character) of the node input in the binary tree in the FIFO order, and construct the binary tree according to the algorithm given in this article.

The order of input characters is:-+ a Space * B space-c space d space/e space f space, which can verify the traversal algorithm provided in this 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.