Reprint Address http://blog.csdn.net/SJF0115/article/details/8645991
Tree structure is a kind of important nonlinear data structure, in which tree and two-fork trees are most commonly used.
A binary tree is an ordered tree with a maximum of two subtrees per node. Usually the root of the subtree is referred to as the left subtree and right subtree. Binary trees are often used as binary search trees and two-fork or binary-ordered trees. Each node of a binary tree has a maximum of two subtrees trees (no nodes with a degree greater than 2), and the subtree of the binary tree has left and right points, and the order cannot be reversed. The first layer of the binary tree has a maximum of 2 i-1 nodes, and a two-tree with a depth of K has a maximum of 2^ (k)-1 nodes; for any binary tree T, if its terminal node number (that is, the leaf node number) is N0, the 2 node is n2, then N0 = n2 + 1.
The chain storage structure of binary tree is a kind of important data structure, and its form is defined as follows:
[CPP]View Plaincopy
- Binary tree nodes
- typedef struct bitnode{
- //Data
- char data;
- //Left child hands
- struct Bitnode *lchild,*rchild;
- }bitnode,*bitree;
Binary Tree creation:
By reading a string, the algorithm for building a two-fork tree is as follows:
[CPP]View Plaincopy
- Create a two-fork tree by ordinal sequence
- int Createbitree (Bitree &t) {
- char data;
- ///Enter the value (one character) of the node in the binary tree in order of precedence, ' # ' indicates an empty tree
- scanf ("%c", &data);
- if (data = = ' # ') {
- T = NULL;
- }
- else{
- T = (bitree) malloc (sizeof (Bitnode));
- //Generate root node
- T->data = data;
- //Construct left sub-tree
- Createbitree (T->lchild);
- //Construct right sub-tree
- Createbitree (T->rchild);
- }
- return 0;
- }
Traversal of a binary tree:
Traversal is one of the most basic operations of the tree, so-called traversal of the binary tree, that is, according to a certain rules and order all the nodes of the binary tree, so that each node is visited once, and only be visited once. Because the binary tree is a nonlinear structure, the traversal of the tree is essentially the transformation of each node of the two-fork tree into a linear sequence to be represented.
Recursive algorithm:
[CPP]View Plaincopy
- Output
- void Visit (Bitree T) {
- if (t->data! = ' # ') {
- printf ("%c", t->data);
- }
- }
- First Order traversal
- void preorder (Bitree T) {
- if (T! = NULL) {
- //Access root node
- Visit (T);
- //access to left dial hand nodes
- Preorder (T->lchild);
- //Access right sub-node
- Preorder (T->rchild);
- }
- }
- Middle Sequence traversal
- void Inorder (Bitree T) {
- if (T! = NULL) {
- //access to left dial hand nodes
- Inorder (T->lchild);
- //Access root node
- Visit (T);
- //Access right sub-node
- Inorder (T->rchild);
- }
- }
- Post-post traversal
- void Postorder (Bitree T) {
- if (T! = NULL) {
- //access to left dial hand nodes
- Postorder (T->lchild);
- //Access right sub-node
- Postorder (T->rchild);
- //Access root node
- Visit (T);
- }
- }
Non-recursive algorithm:
<1> first-order traversal:
"Thinking": After access to T->data, the t into the stack, traversing the left subtree, after traversing the Zuozi return, the top element of the stack should be T, out of the stack, and then sequentially traverse the right subtree of T.
[CPP]View Plaincopy
- /* Sequential traversal (non-recursive)
- Idea: After access to T->data, the t into the stack, traversing the left subtree, after traversing the Zuozi return, the top element of the stack should be T, out of the stack, and then first traversing the right subtree of T.
- */
- void PreOrder2 (Bitree T) {
- Stack<bitree> Stack;
- //p is a traversal pointer
- Bitree p = T;
- //stack is not empty or p is not empty when the loop
- While (P | |!stack.empty ()) {
- if (P! = NULL) {
- //deposit in stack
- Stack.push (P);
- //Access root node
- printf ("%c", p->data);
- //traverse the left subtree
- p = p->lchild;
- }
- else{
- //Stack back
- p = stack.top ();
- Stack.pop ();
- //access to right sub-tree
- p = p->rchild;
- }
- }//while
- }
<2> in-sequence traversal
"Idea": T is to traverse the root of the tree pointer, the middle sequence traversal requires that after traversing the left dial hand tree, access to the root, and then traverse the right subtree.
First, the t into the stack, traversing the left subtree, after traversing the Zuozi return, the top element of the stack should be T, out of the stack, access to T->data, and then the right subtree of t traversal.
[CPP]View Plaincopy
- void InOrder2 (Bitree T) {
- Stack<bitree> Stack;
- //p is a traversal pointer
- Bitree p = T;
- //stack is not empty or p is not empty when the loop
- While (P | |!stack.empty ()) {
- if (P! = NULL) {
- //deposit in stack
- Stack.push (P);
- //traverse the left subtree
- p = p->lchild;
- }
- else{
- //fallback, Access root node
- p = stack.top ();
- printf ("%c", p->data);
- Stack.pop ();
- //access to right sub-tree
- p = p->rchild;
- }
- }//while
- }
<3> Post-traversal
"Thinking": T is to traverse the root of the tree pointer, post-order traversal requirements after traversing the left and right subtree, and then access to the root. It is necessary to determine whether the left and right subtrees of the root node have been traversed.
[CPP]View Plaincopy
- Post-post traversal (non-recursive)
- typedef struct bitnodepost{
- Bitree Bitree;
- char tag;
- }bitnodepost,*bitreepost;
- void PostOrder2 (Bitree T) {
- Stack<bitreepost> Stack;
- //p is a traversal pointer
- Bitree p = T;
- Bitreepost BT;
- //stack is not empty or p is not empty when the loop
- While (P! = NULL | |!stack.empty ()) {
- //traverse the left subtree
- While (P! = NULL) {
- BT = (bitreepost) malloc (sizeof (bitnodepost));
- Bt->bitree = p;
- //Visited left dial hand tree
- Bt->tag = ' L ';
- Stack.push (BT);
- p = p->lchild;
- }
- ///left/right sub-tree access complete access to root node
- While (!stack.empty () && (Stack.top ())->tag = = ' R ') {
- BT = Stack.top ();
- //Stack back
- Stack.pop ();
- bt->bitree;
- printf ("%c", bt->bitree->data);
- }
- //Traverse Right sub-tree
- if (!stack.empty ()) {
- BT = Stack.top ();
- //access to right sub-tree
- Bt->tag = ' R ';
- p = bt->bitree;
- p = p->rchild;
- }
- }//while
- }
<4> Hierarchy Traversal
"Thinking": Each node is accessed hierarchically from top to bottom, from left to right, and the queue is used during the hierarchical traversal process.
[CPP]View Plaincopy
- Hierarchical traversal
- void Levelorder (Bitree T) {
- Bitree p = T;
- //Queue
- Queue<bitree> queue;
- //The root node is enqueued
- Queue.push (P);
- //Queue not empty loop
- While (!queue.empty ()) {
- //Enemy elements out of the team
- p = Queue.front ();
- //Access node of P point
- printf ("%c", p->data);
- //Exit queue
- Queue.pop ();
- //Left dial hand tree is not empty, the left sub-tree is enqueued
- if (p->lchild! = NULL) {
- Queue.push (P->lchild);
- }
- //Right subtree is not empty, the right subtree is enqueued
- if (p->rchild! = NULL) {
- Queue.push (P->rchild);
- }
- }
- }
Test Case:
Input:
abc# #DE #g# #F # # #
Output:
Various traversal of binary tree