Data Structure Learning 7 -- binary tree traversal

Source: Internet
Author: User

The previous article discussed the construction of a binary tree. After the binary tree is built, the next issue is the traversal of the binary tree, that is, reading all the node data of the binary tree.

Three traversal methods: pre-order traversal, middle-order traversal, and post-order traversal.

Forward traversal: first access the root node, then traverse the left subtree in the forward order, and finally traverse the right subtree in the forward order

Middle-order traversal: first, the middle-order traversal of the Left subtree, then access the root node, and finally the Middle-order traversal of the right subtree

Next traversal: traverse the left subtree sequentially, then traverse the right subtree sequentially, and finally access the root node.

The traversal steps above show that the differences between the three are the different access sequence of the root node.

If the process of building a binary tree by recursion is clear, the traversal of the binary tree is easier to understand. Here we use the easiest-to-understand Recursive Method to traverse a binary tree. The Code is as follows:

1 # include <stdio. h> 2 # include <stdlib. h> 3 # include <malloc. h> 4 5 // define the binary tree 6 typedef struct node {7 int data; // Data Element 8 struct node * left; // point to the left subtree 9 struct node * right; // point to the right subtree 10} btree; 11 12 // construct a binary tree: recursive mode 13 int btreecreate (btree ** TP) 14 {15 // constructor, or constructor order: construct 16 int X; 17 scanf ("% d", & X); 18 if (x <= 0) 19 {20 * TP = NULL; // The pointer is null. A pointer in the tree node is null 21 return 0; 22} 23 * TP = (btree *) malloc (sizeof (btree )); // point the pointer in the tree node to the address space 24 if (TP = NULL) 25 return 0; 26 (* TP)-> DATA = X; 27 btreecreate (& (* TP)-> left); 28 btreecreate (& (* TP)-> right); 29 return 1; 30} 31 32 // traversal: pre-order traversal. Recursive mode: First root node, then left subtree and right subtree 33 void preorder (btree * tree) 34 {35 if (tree = NULL) 36 {37 return; 38} 39 printf ("% d", tree-> data); 40 preorder (tree-> left ); 41 preorder (tree-> right); 42} 43 44 // traversal: central order traversal, recursion, first left subtree, then root node, last right subtree 45 void midorder (btree * tree) 46 {47 If (tree = NULL) 48 {49 return; 50} 51 midorder (tree-> left); 52 printf ("% d", tree-> data); 53 midorder (tree-> right ); 54} 55 56 // traversal: Post-sequential traversal. Recursive mode: First left subtree, then right subtree, last root node 57 58 void postorder (btree * tree) 59 {60 if (tree = NULL) 61 {62 Return; 63} 64 postorder (tree-> left); 65 postorder (tree-> right ); 66 printf ("% d", tree-> data); 67} 68 69 int main () 70 {71 // build 72 btree * tree using a binary tree; 73 printf ("create Binary Tree: \ n"); 74 btreecreate (& tree); 75 // print 76 printf ("pre order: \ n") in the forward order "); 77 preorder (tree); 78 printf ("\ n"); 79 // The Middle Order traverses 80 printf ("mid order: \ n"); 81 midorder (tree ); 82 printf ("\ n"); 83 // traverse 84 printf ("post order: \ n"); 85 postorder (tree ); 86 printf ("\ n"); 87 88 return 0; 89}

Test: The following Binary Tree

Input:, 0

The test results are as follows:

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.