# 树遍历（预购，订购和后订购）

## 预购遍历

### 算法

```Algorithm:
Preorder(root):
Step:1 Print the data of the Node.
Step:2 Move to the left side of the node(traverse left-subtree).
Step:3 Move to the right side of the node(traverse right-subtree).```

## 有序遍历

### 算法

```Algorithm:
Inorder(root):
Step:1 Move to the left side of the node(traverse left-subtree).
Step:2 Print the data of the Node.
Step:3 Move to the right side of the node(traverse right-subtree).```

## 后遍历

### 算法

```Algorithm:
Postorder(root):
Step:1 Move to the left side of the node(traverse left-subtree).
Step:2 Move to the right side of the node(traverse right-subtree).
Step:3 Print the data of the Node.```

## 实施

```/*C++ Implementation of print the Preorder, Inorder, Postorder traversal of given binary tree*/
#include<bits/stdc++.h>
using namespace std;
/*Structure of Node of BT which contain pointer to left child and right child and a data for node.*/
struct Node{
int data;
struct Node* left;// for left child;
struct Node* right;// for right child;
Node(int value)// create a node using new Node;
{
data=value;
left=NULL;
right=NULL;
}
};
/*Function which print preorder of the given tree*/
void Preorder_tree(Node* root)
{
if(root==NULL)
{
return;
}
cout<<root->data<<" ";
Preorder_tree(root->left);
Preorder_tree(root->right);
}
/*Function which print inorder of the given tree*/
void Inorder_tree(Node* root)
{
if(root==NULL)
{
return;
}
Preorder_tree(root->left);
cout<<root->data<<" ";
Preorder_tree(root->right);
}
/*Function which print postorder of the given tree*/
void Postorder_tree(Node* root)
{
if(root==NULL)
{
return;
}
Preorder_tree(root->left);
Preorder_tree(root->right);
cout<<root->data<<" ";
}
int main()
{
/*construct tree*/
Node* root= new Node(0);//root node;
root->left= new Node(1);
root->right= new Node(2);
root->left->left= new Node(3);
root->left->right= new Node(4);
root->right->left= new Node(5);
root->right->right= new Node(6);
cout<<"Preorder traversal of BT: ";
Preorder_tree(root);cout<<"\n";
cout<<"Inorder traversal of BT: ";
Inorder_tree(root);cout<<"\n";
cout<<"Postorder traversal of BT: ";
Postorder_tree(root);cout<<"\n";
return 0;
}```
```Preorder traversal of BT: 0 1 3 4 2 5 6
Inorder traversal of BT: 1 3 4 0 2 5 6
Postorder traversal of BT: 1 3 4 2 5 6 0```