二叉树层序遍历代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
二叉树遍历算法
binary_tree.h
#ifndef BINARY_TREE_H
#define BINARY_TREE_H
typedef struct BinaryNode BinaryNode;
struct BinaryNode {
int element;
BinaryNode *left;
BinaryNode *right;
};
typedef struct BinaryTree BinaryTree;
struct BinaryTree {
int size;
BinaryNode *root;
};
BinaryTree *CreateTree(void);
void ReleaseTree(BinaryTree *tree);
BinaryNode *FindNode(BinaryTree *tree, int element);
BinaryTree *AddNode(BinaryTree *tree, int element);
BinaryTree *DelNode(BinaryTree *tree, int element);
void PreTraversal(BinaryTree *tree);
void MidTraversal(BinaryTree *tree);
void PostTraversal(BinaryTree *tree);
void LevelTraversal(BinaryTree *tree);
#endif
binary_tree.c
#include "binary_tree.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
BinaryTree *CreateTree(void)
{
BinaryTree *tree = (BinaryTree *)calloc(1, sizeof(BinaryTree));
return tree;
}
static BinaryNode *createNode(int element)
{
BinaryNode *node = (BinaryNode *)calloc(1, sizeof(BinaryNode));
return NULL;
}
node->element = element;
return node;
}
static BinaryNode *findNode(BinaryNode *root, int element)
{
if (root == NULL) {
return NULL;
}
if (root->element == element) {
return root;
}
if (root->element > element) {
return findNode(root->left, element);
} else {
return findNode(root->right, element);
}
}
BinaryNode *FindNode(BinaryTree *tree, int element)
{
if (tree == NULL) {
return NULL;
}
return findNode(tree->root, element);
}
static int findParentNode(BinaryNode *root, int element, BinaryNode **parent) {
if (root == NULL) {
return0;
}
if (root->element == element) {
return1;
}
*parent = root;
if (root->element > element) {
return findParentNode(root->left, element, parent);
} else {
return findParentNode(root->right, element, parent);
}
}
static BinaryNode *addNode(BinaryNode *root, int element, int *size)
{
BinaryNode *parent = NULL;
int ret = findParentNode(root, element, &parent);
if (ret == 1) {
return root;
}
BinaryNode *node = createNode(element);
// printf("Failed to create node!\n");
return root;
}
node->element = element;
*size += 1;
if (parent == NULL) {
return node;
}
if (parent->element > element) {
parent->left = node;
} else {
parent->right = node;
}
return root;
}
BinaryTree *AddNode(BinaryTree *tree, int element)
{
if (tree == NULL) {
return NULL;
}
tree->root = addNode(tree->root, element, &tree->size);
return tree;
}
static BinaryNode *findLeftChildMaxNode(BinaryNode *node, BinaryNode **parent) {
BinaryNode *root = node->left;
while (root->right != NULL) {
*parent = root;
root = root->right;
}
return root;
}
static BinaryNode *deleteNode(BinaryNode *node)
{
if (node == NULL) {
return NULL;
}
BinaryNode *ret = NULL;
int flag = 1;
if (node->left == NULL && node->right == NULL) {
ret = NULL;
} else if (node->left == NULL) {
ret = node->right;
} else if (node->right == NULL) {
ret = node->left;
} else {
flag = 0;
BinaryNode *parent = NULL;
BinaryNode *next = findLeftChildMaxNode(node, &parent);
node->element = next->element;
node->left = deleteNode(next);
} else {
parent->right = deleteNode(next);
}
ret = node;
}
if (flag) {
free(node);
}
return ret;
}
static BinaryNode *delNode(BinaryNode *root, int element, int *size) {
BinaryNode *parent = NULL;
int ret = findParentNode(root, element, &parent);
if (ret == 0) {
return root;
}
*size -= 1;
if (parent == NULL) {
return deleteNode(root);
}
if (parent->element > element) {
parent->left = deleteNode(parent->left);
} else {
parent->right = deleteNode(parent->right);
}
return root;
}
BinaryTree *DelNode(BinaryTree *tree, int element)
{
if (tree == NULL) {
return NULL;
}
tree->root = delNode(tree->root, element, &tree->size);
return tree;
}
static void preTraversal(BinaryNode *root)
{
if (root == NULL) {
return;
}
printf("%d,", root->element);
preTraversal(root->left);
preTraversal(root->right);
}
void PreTraversal(BinaryTree *tree)
{
if (tree == NULL || tree->root == NULL) {
printf("PreTraversal tree: [");
preTraversal(tree->root);
printf("]\n");
}
static void midTraversal(BinaryNode *root)
{
if (root == NULL) {
return;
}
midTraversal(root->left);
printf("%d,", root->element);
midTraversal(root->right);
}
void MidTraversal(BinaryTree *tree)
{
if (tree == NULL || tree->root == NULL) {
printf("binary tree is empty!\n");
return;
}
printf("MidTraversal tree: [");
midTraversal(tree->root);
printf("]\n");
}
static void postTraversal(BinaryNode *root)
{
if (root == NULL) {
return;
}
postTraversal(root->left);
postTraversal(root->right);
printf("%d,", root->element);
}
void PostTraversal(BinaryTree *tree)
{
if (tree == NULL || tree->root == NULL) {
printf("binary tree is empty!\n");
return;
}
printf("PostTraversal tree: [");
postTraversal(tree->root);
printf("]\n");
}
static void levelTraversal(BinaryNode *root, int size)
{
BinaryNode **nodes = (BinaryNode **)calloc(size, sizeof(BinaryNode *));
if (nodes == NULL) {
nodes[0] = root;
int pre = 0;
int pos = 1;
while (pos > pre) {
int num = pos - pre;
printf("[");
for (int i = 0; i < num; ++i) {
printf("%d,", nodes[i + pre]->element);
if (nodes[i + pre]->left != NULL) {
nodes[pos++] = nodes[i + pre]->left; }
if (nodes[i + pre]->right != NULL) {
nodes[pos++] = nodes[i + pre]->right; }
}
printf("],");
pre += num;
}
free(nodes);
}
void LevelTraversal(BinaryTree *tree)
{
if (tree == NULL || tree->root == NULL) {
printf("binary tree is empty!\n");
return;
}
printf("LevelTraversal tree: [");
levelTraversal(tree->root, tree->size);
printf("]\n");
}
void releaseNode(BinaryNode *root)
{
if (root == NULL) {
return;
}
releaseNode(root->left);
releaseNode(root->right);
free(root);
}
void ReleaseTree(BinaryTree *tree)
{
if (tree == NULL) {
return;
}
releaseNode(tree->root);
free(tree);
}
说明
测试程序
#include <stdio.h>
#include "binary_tree.h"
int main()
{
BinaryTree *root = CreateTree();
if (root == NULL) {
printf("create binary tree failed!\n");
return-1;
}
for (int i = 0; i < 10; ++i) {
root = AddNode(root, i);
}
LevelTraversal(root);
ReleaseTree(root);
return0;
}
首先创建二叉树,层序遍历算法调用LevelTraversal函数实现。