书中2.3.4节中没有给出代码,前序,中序和后序的遍历如下。
#include <stdio.h>
#include <stdlib.h>
struct BinaryTreeNode
{
int value;
struct BinaryTreeNode *left;
struct BinaryTreeNode *right;
};
typedef struct BinaryTreeNode Tree;
Tree* construct(int val){
Tree* node = (Tree*) malloc(sizeof(Tree));
node->value = val;
node->left = NULL;
node->right = NULL;
}
void preOrder(Tree *root){
if (root == NULL){
return;
}
printf("%d ", root->value);
preOrder(root->left);
preOrder(root->right);
}
void inOrder(Tree *root){
if (root == NULL){
return;
}
inOrder(root->left);
printf("%d ", root->value);
inOrder(root->right);
}
void postOrder(Tree *root){
if (root == NULL){
return;
}
postOrder(root->left);
postOrder(root->right);
printf("%d ", root->value);
}
int main(void){
Tree* root = construct(10);
root->left = construct(6);
root->right = construct(14);
root->left->left = construct(4);
root->left->right = construct(8);
root->right->left = construct(12);
root->right->right = construct(16);
printf("preOrder\n");
preOrder(root);
printf("\ninOrder\n");
inOrder(root);
printf("\npostOrder\n");
postOrder(root);
}
三种的递归遍历如上代码