C语言前序遍历二叉树
时间: 2024-01-23 14:13:06 浏览: 86
以下是C语言实现二叉树前序遍历的递归和非递归方法:
1.递归方法:
```c
void PreOrderTraverse(BiTree T) {
if (T) {
printf("%d ", T->data); //访问当前结点
PreOrderTraverse(T->lchild); //遍历当前结点的左子树
PreOrderTraverse(T->rchild); //遍历当前结点的右子树
}
}
```
2.非递归方法:
```c
void PreOrderTraverse(BiTree T) {
if (!T) {
return;
}
BiTree stack[MAXSIZE];
int top = -1;
stack[++top] = T;
while (top != -1) {
BiTree p = stack[top--];
printf("%d ", p->data); //访问当前结点
if (p->rchild) {
stack[++top] = p->rchild; //先将右子树入栈
}
if (p->lchild) {
stack[++top] = p->lchild; //再将左子树入栈
}
}
}
```
相关问题
使用c语言编写前序遍历二叉树
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *create_node(int data)
{
struct node *new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
}
void preorder(struct node *root)
{
if(root != NULL)
{
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
}
int main()
{
struct node *root = create_node(1);
root->left = create_node(2);
root->right = create_node(3);
root->left->left = create_node(4);
root->left->right = create_node(5);
printf("Preorder traversal of binary tree is: ");
preorder(root);
return 0;
}
创建二叉树,前序遍历二叉树,中序遍历二叉树,后序遍历二叉树的c语言源文件与头文件
在C语言中,创建二叉树、前序、中序和后序遍历通常涉及到结构体定义节点、递归函数以及主函数的编写。这里提供一个简化的例子,包括二叉树的基本结构和遍历函数。
首先,我们创建一个名为`binary_tree.h`的头文件:
```cpp
// binary_tree.h
#ifndef BINARY_TREE_H
#define BINARY_TREE_H
#include <stdio.h>
// 定义二叉树节点结构
typedef struct TreeNode {
int val; // 节点值
struct TreeNode *left; // 左子节点指针
struct TreeNode *right; // 右子节点指针
} TreeNode;
// 创建新节点的函数
TreeNode* createNode(int value);
// 前序遍历函数
void preOrderTraversal(TreeNode* root);
// 中序遍历函数
void inOrderTraversal(TreeNode* root);
// 后序遍历函数
void postOrderTraversal(TreeNode* root);
#endif // BINARY_TREE_H
```
接下来,我们创建`binary_tree.c`的源文件:
```cpp
// binary_tree.c
#include "binary_tree.h"
// 创建新节点的函数
TreeNode* createNode(int value) {
TreeNode* newNode = (TreeNode*)malloc(sizeof(TreeNode));
if (newNode != NULL) {
newNode->val = value;
newNode->left = newNode->right = NULL;
}
return newNode;
}
// 前序遍历函数
void preOrderTraversal(TreeNode* root) {
if (root != NULL) {
printf("%d ", root->val);
preOrderTraversal(root->left);
preOrderTraversal(root->right);
}
}
// 中序遍历函数
void inOrderTraversal(TreeNode* root) {
if (root != NULL) {
inOrderTraversal(root->left);
printf("%d ", root->val);
inOrderTraversal(root->right);
}
}
// 后序遍历函数
void postOrderTraversal(TreeNode* root) {
if (root != NULL) {
postOrderTraversal(root->left);
postOrderTraversal(root->right);
printf("%d ", root->val);
}
}
```
在主程序中,你可以像这样使用这些函数:
```cpp
int main() {
// 初始化树并调用遍历函数
TreeNode* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
// ...继续添加更多节点...
printf("前序遍历: ");
preOrderTraversal(root);
printf("\n");
printf("中序遍历: ");
inOrderTraversal(root);
printf("\n");
printf("后序遍历: ");
postOrderTraversal(root);
printf("\n");
return 0;
}
```
这只是一个基础的示例,实际应用中可能需要处理更复杂的情况,比如用户输入创建树、删除节点等操作。现在你已经得到了前序、中序和后序遍历的基础框架。
阅读全文
相关推荐













