二叉树的结点
typedef struct BiTNode
{
int key;
struct BiTNode* left;//左子树
struct BiTNode* right;//右子树
} BiTNode, * BiTree;
两个辅助函数
1、创建结点
BiTNode* CreateBiTNode()
{
BiTNode* node = malloc(sizeof(BiTNode));
if (node != NULL)
{
node->key = 0;
node->left = NULL;
node->right = NULL;
}
return node;
}
2、添加数据
在结点node上添加数据,如果node为空,则创建结点后再添加数据,返回添加数据后的结点。
BiTNode* InsertBiTNode(BiTNode* node, int key)
{
if (node == NULL)
node = CreateBiTNode();
node->key = key;
return node;
}
遍历算法
1、递归遍历
(1)先序遍历
void Preorder(BiTree root)
{
if (root == NULL)
{
printf("NULL\n");
return;
}
printf("%d ", root->key);// 根
if (root->left != NULL) Preorder(root->left);
if (root->right != NULL) Preorder(root->right);
}
(2)中序遍历
void Inorder(BiTree root)
{
if (root == NULL)
{
printf("NULL\n");
return;
}
if (root->left != NULL) Inorder(root->left);
printf("%d ", root->key);// 根
if (root->right != NULL) Inorder(root->right);
}
(3)后序遍历
void Postorder(BiTree root)
{
if (root == NULL)
{
printf("NULL\n");
return;
}
if (root->left != NULL) Postorder(root->left);
if (root->right != NULL) Postorder(root->right);
printf("%d ", root->key);// 根
}
2、迭代遍历
(1)先序遍历
void PreorderIteration(BiTree root)
{
if (root == NULL)
{
printf("NULL\n");
return;
}
BiTNode* stack[10] = { NULL };
int stackTop = -1;
stack[++stackTop] = root;
while (stackTop != -1)
{
BiTNode* node = stack[stackTop];
stack[stackTop--] = NULL;
printf("%d ", node->key);
if (node->right != NULL)
stack[++stackTop] = node->right;
if (node->left != NULL)
stack[++stackTop] = node->left;
}
}
(2)中序遍历
void InorderIteration(BiTree root)
{
if (root == NULL)
{
printf("NULL\n");
return;
}
BiTNode* stack[10] = { NULL };
int stackTop = -1;
BiTNode* move = root;
while (stackTop != -1 || move != NULL)
{
while (move != NULL)
{
stack[++stackTop] = move;
move = move->left;
}
if (stackTop != -1)
{
move = stack[stackTop];
stack[stackTop--] = NULL;
printf("%d ", move->key);
move = move->right;
}
}
}
(3)后序遍历
void PostorderIteration(BiTree root)
{
if (root == NULL)
{
printf("NULL\n");
return;
}
BiTNode* stack[10] = { NULL };
int stackTop = -1;
BiTNode* current = root;
BiTNode* visited = root;
while (stackTop != -1 || current != NULL)
{
while (current != NULL)
{
stack[++stackTop] = current;
current = current->left;
}
current = stack[stackTop];
if (current->right != NULL && current->right != visited)
{
current = current->right;
}
else
{
printf("%d ", current->key);
visited = current;
current = NULL;
stack[stackTop--] = NULL;
}
}
}
测试函数
int main()
{
// 构建二叉树
BiTree root = NULL;
root = InsertBiTNode(root, 10);
root->left = InsertBiTNode(root->left, 20);
root->right = InsertBiTNode(root->right, 30);
BiTNode* left = root->left;
left->left = InsertBiTNode(left->left, 5);
left->right = InsertBiTNode(left->right, 8);
BiTNode* right = root->right;
right->left = InsertBiTNode(right->left, 35);
right->right = InsertBiTNode(right->right, 38);
printf("先序遍历:\n");
Preorder(root);
printf("\n");
PreorderIteration(root);
printf("\n");
printf("中序遍历:\n");
Inorder(root);
printf("\n");
InorderIteration(root);
printf("\n");
printf("后序遍历:\n");
Postorder(root);
printf("\n");
PostorderIteration(root);
printf("\n");
if (root != NULL)
free(root);
return 0;
}
测试结果