Data structure (C implementation) ------- traversing a binary tree
This article is my learning notes, welcome to reprint, but please note the Source: http://blog.csdn.net/jesson20121020
A binary tree is another tree structure. It features that each node has at most two Subtrees (that is, a node with a degree greater than 2 in a binary tree, the subtree of a binary tree can be left or right, and its order cannot be reversed.
According to the recursive definition of a binary tree, a binary tree consists of three basic units: the root node, the left subtree, And the right subtree. Therefore, if you can traverse these three parts in sequence, that is, the entire binary tree is traversed. If we use L, D, and R to traverse the left subtree, the access root node, and the right subtree, there may be six binary tree traversal schemes, DLR, LDR, LRD, DRL, RDL, and RLD. Only the first three cases are allowed: First-left-right traversal, middle-order traversal, and last-order traversal. In addition, there is also a method for Traversing from top to bottom, traversing each node of a binary tree from left to right is called hierarchical traversal. Therefore, there are four methods to traverse Binary Trees.
Binary tree traversal operation definition:
1. The operation definition for Traversing Binary Trees in sequence:
If the binary tree is empty, the operation is null. Otherwise
1) access the root node
2) traverse the left subtree in sequence
3) traverse the right subtree in sequence
2. Operation definition for traversing a binary tree in a central order:
If the binary tree is empty, the operation is null. Otherwise
1) traverse the left subtree in the middle order
2) access the root node
3) traverse the right subtree in ascending order
3. Operation definitions for Traversing Binary Trees in descending order:
If the binary tree is empty, the operation is null. Otherwise
1) traverse the left subtree in descending order
2) traverse the right subtree in descending order
3) access the root node
4. Operation definition for layered traversal of Binary Trees:
If the binary tree is empty, the operation is null. Otherwise
1) from top to bottom
2) traverse the same layer from left to right
Binary Tree Storage Structure: 1. Sequential storage structure:
#define MAX_TREE_SIZE 100typedef char TElemType; typedef TElemType SqBiTree[MAX_TREE_SIZE];SqBiTree bt;
2. Chain storage structure:
// The # define MAXSIZE 100 of the Binary Tree // The maximum number of knots in the binary tree. typedef char TElemType; typedef struct BiTNode {TElemType data; struct BiTNode * lchild, * rchild;} BiTNode, * BiTree;
The specific implementation of binary tree traversal:
# Include
# Include
// The # define MAXSIZE 100 of the Binary Tree // The maximum number of knots in the binary tree. typedef char TElemType; typedef struct BiTNode {TElemType data; struct BiTNode * lchild, * rchild;} BiTNode, * BiTree; // define the function pointer typedef void (* Visit) (BiTree); // initialize the void Init_BiTree (BiTree * T) {* T = NULL ;} // determine whether the binary tree is NULL. return 1int IsEmpty_BiTree (BiTree * T) {return * T = NULL;} // create a binary tree void Create_BiTree (BiTree * T) {char ch; ch = getchar (); // when the input is "#", the subtree is considered as NULL if (ch = '#') * T = NULL; // Create a tree node else {* T = (BiTree) malloc (sizeof (BiTNode); (* T)-> data = ch; // generate Tree node // generate the left subtree Create_BiTree (& (* T)-> lchild); // generate the right subtree Create_BiTree (& (* T) -> rchild) ;}}// value of the output node void Print_BiTreeNode (BiTree T) {printf ("% c \ t", T-> data );} // first traverse the binary tree void PreOrder_BiTree (BiTree T, Visit visit) {if (! IsEmpty_BiTree (& T) {visit (T); PreOrder_BiTree (T-> lchild, visit); PreOrder_BiTree (T-> rchild, visit );}} // traverse Binary Tree void InOrder_BiTree (BiTree T, Visit visit) {if (! IsEmpty_BiTree (& T) {InOrder_BiTree (T-> lchild, visit); visit (T); InOrder_BiTree (T-> rchild, visit );}} // post-order traversal of the Binary Tree void PostOrder_BiTree (BiTree T, Visit visit) {if (! IsEmpty_BiTree (& T) {PostOrder_BiTree (T-> lchild, visit); PostOrder_BiTree (T-> rchild, visit); visit (T );}} // traverse the binary tree void LevelOrder_BiTree (BiTree T, Visit visit) {// use a queue to store node information. The queue here uses an array in the ordered queue to implement int front = 0; int rear = 0; BiTree BiQueue [MAXSIZE]; BiTree tempNode; if (! IsEmpty_BiTree (& T) {// Add the root node to the queue. BiQueue [rear ++] = T; while (front! = Rear) {// retrieve the Header element of the team and move the pointer of the team's header backward to a tempNode = BiQueue [front ++]; // determine whether the left and right subtree is empty, if it is null, it is added to the queue if (! IsEmpty_BiTree (& (tempNode-> lchild) BiQueue [rear ++] = tempNode-> lchild; if (! IsEmpty_BiTree (& (tempNode-> rchild) BiQueue [rear ++] = tempNode-> rchild; // elements of the output queue header node // Vist_BiTreeNode (tempNode-> data ); visit (tempNode) ;}}int main () {BiTree T; // initialize a binary tree as an empty Binary Tree Init_BiTree (& T ); // create a binary tree Create_BiTree (& T); // first traverse printf ("\ n first traverse Result:"); PreOrder_BiTree (T, Print_BiTreeNode ); // In-order traversal of Binary Tree printf ("\ n In-order traversal results:"); InOrder_BiTree (T, Print_BiTreeNode); // In-order traversal of Binary Tree printf ("\ n In-order traversal results: "); PostOrder_BiTree (T, Print_BiTreeNode); // level traversal Binary Tree printf (" \ n Level traversal result: "); LevelOrder_BiTree (T, Print_BiTreeNode); return 0 ;}
Result of binary tree traversal:
Specify a binary tree. For example, enter 1247 ### 5 #8 ## 36 ###