(Programming training) Let's look back again. The data structure is the pre-order, middle-order, and post-order traversal (recursion) of the binary tree, and then let's look back at the binary tree.

Source: Internet
Author: User

(Programming training) Let's look back again. The data structure is the pre-order, middle-order, and post-order traversal (recursion) of the binary tree, and then let's look back at the binary tree.

I recently reviewed the data structure and took a look at the code I wrote in my freshman year. After reading the code, I had a deeper understanding than I had at the beginning.


I hope this will provide some reference for beginners.


It can be run in VC ++ 6.0, and many comments have been written at the beginning.


[Problem description]

Create a binary tree binary linked list based on the sequential storage structure, and traverse the binary tree in ascending, middle, and back order.

[Basic requirements]

· Function: Create a binary tree binary linked list based on the sequential storage structure, and perform sequential, central, and post-sequential traversal.

· Input: sequential storage of input binary trees.

· Output: the first, middle, and last traversal sequences of Binary Trees.

[Test Data]

The input binary tree is stored sequentially, and the sequence number and value of each node are given,

1A

2B

3C

4D

6E

7F

9G

12 H

14I

15J

00

The expected output is the binary tree created accordingly.

Sequence: ABDGCEHFIJ,

Central sequence: DGBAHECIFJ,

Post-sequence: GDBHEIJFCA.

[Module division]

Four modules are designed in total

(1) create a binary linked list CreateBT ();

(2) First traverse the binary tree PreOrder ():

(3) traverse the binary tree InOrder () in ascending order ():

(4) Post-order traversal of Binary Tree PostOrder ():


/* Binary tree traversal */# include <stdio. h> # include <malloc. h> typedef char ElemType; // define the data structure typedef struct BNode {ElemType data; struct BNode * lchild; struct BNode * rchild;} BTNode, * BinTree; // create BinTree CreatBT () {BTNode * q, * ptr [20]; int I, j; ElemType x; BinTree root = NULL; scanf ("% d % c", & I, & x); getchar (); while (x! = '') {Q = (BTNode *) malloc (sizeof (BTNode); q-> data = x; q-> lchild = NULL; q-> rchild = NULL; ptr [I] = q; if (1 = I) root = q; else {j = I/2; if (! (I % 2) // left subtree ptr [j]-> lchild = q; else // right subtree ptr [j]-> rchild = q ;} scanf ("% d % c", & I, & x); getchar ();} return root;} // first traverse void PreOrder (BinTree root) {if (root! = NULL) {printf ("% c", root-> data); PreOrder (root-> lchild); PreOrder (root-> rchild );}} // traverse void InOrder (BinTree root) {if (root! = NULL) {InOrder (root-> lchild); printf ("% c", root-> data); InOrder (root-> rchild );}} // traverse void PostOrder (BinTree root) {if (root! = NULL) {PostOrder (root-> lchild); PostOrder (root-> rchild); printf ("% c", root-> data );}} // main function int main (void) {BinTree root; root = CreatBT (); printf ("sequential traversal \ n"); PreOrder (root ); printf ("\ n"); printf ("sequential traversal \ n"); InOrder (root); printf ("\ n "); printf ("sequential traversal \ n"); PostOrder (root); printf ("\ n"); return 0 ;}


Running result




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.