二叉树使用递归的方式创建: 首先输入数据,如果输入的数据是结束的数据就会返回上一层递归,进入到上一个节点并创建右孩子;如果输入数据符合创建数据的要求,就将改数据赋值给当前节点,并为当前节点的左孩子和右孩子开辟空间,创建左孩子节点;
int createTree(struct binaryTree *root)
{
if(root == NULL){
return 0;
}
int data;
scanf("%d",&data);
if(data == 0){
return 0;
}else{
root->data = data;
}
root->left = (struct binaryTree *)malloc(sizeof(struct binaryTree));
root->right = (struct binaryTree *)malloc(sizeof(struct binaryTree));
printf("please input left data\n");
if(createTree(root->left) == 0){
free(root->left);
root->left = NULL;
}
printf("please input right data\n");
if(createTree(root->right) == 0){
free(root->right);
root->right = NULL;
}
return 1;
}
二叉树递归遍历 通过改变打印节点数据的位置来实现先序,中序,后序递归遍历
void headPrint(struct binaryTree *root)
{
if(root != NULL){
printf("%d ",root->data);
headPrint(root->left);
headPrint(root->right);
}
}
void middlePrint(struct binaryTree *root)
{
if(root != NULL){
middlePrint(root->left);
printf("%d ",root->data);
middlePrint(root->right);
}
}
void endPrint(struct binaryTree * root)
{
if(root != NULL){
endPrint(root->left);
endPrint(root->right);
printf("%d ",root->data);
}
}
二叉树非递归遍历: 为了实现非递归遍历,用到栈;
void forPrint(struct binaryTree *root)
{
struct binaryTree *p = root;
struct binaryTree *stack[10];
int top = -1;
while(p != NULL || top != -1){
if(p != NULL){
stack[++top] = p;
printf("%d ",p->data);
p = p->left;
}else{
p = stack[top--];
p = p->right;
}
}
}
void miPrint(struct binaryTree *root)
{
struct binaryTree *p = root;
struct binaryTree *stack[10];
int top = -1;
while(p != NULL || top != -1){
if(p != NULL){
stack[++top] = p;
p = p->left;
}else{
p = stack[top--];
printf("%d ",p->data);
p = p->right;
}
}
}
层序遍历 使用数组的方式来存放节点,使用两个下标来进行遍历;
void cengPrint(struct binaryTree *root)
{
int index1 = 0;
int index2 = 0;
struct binaryTree *arr[10];
arr[index1++] = root;
while(index1>index2){
if(arr[index2] != NULL){
printf("%d ",arr[index2]->data);
arr[index1++] = arr[index2]->left;
arr[index1++] = arr[index2]->right;
}
index2++;
}
}
整体代码
#include <stdio.h>
#include <stdlib.h>
struct binaryTree{
int data;
struct binaryTree *left;
struct binaryTree *right;
};
int createTree(struct binaryTree *root)
{
if(root == NULL){
return 0;
}
int data;
scanf("%d",&data);
if(data == 0){
return 0;
}else{
root->data = data;
}
root->left = (struct binaryTree *)malloc(sizeof(struct binaryTree));
root->right = (struct binaryTree *)malloc(sizeof(struct binaryTree));
printf("please input left data\n");
if(createTree(root->left) == 0){
free(root->left);
root->left = NULL;
}
printf("please input right data\n");
if(createTree(root->right) == 0){
free(root->right);
root->right = NULL;
}
return 1;
}
void headPrint(struct binaryTree *root)
{
if(root != NULL){
printf("%d ",root->data);
headPrint(root->left);
headPrint(root->right);
}
}
void middlePrint(struct binaryTree *root)
{
if(root != NULL){
middlePrint(root->left);
printf("%d ",root->data);
middlePrint(root->right);
}
}
void endPrint(struct binaryTree * root)
{
if(root != NULL){
endPrint(root->left);
endPrint(root->right);
printf("%d ",root->data);
}
}
void forPrint(struct binaryTree *root)
{
struct binaryTree *p = root;
struct binaryTree *stack[10];
int top = -1;
while(p != NULL || top != -1){
if(p != NULL){
stack[++top] = p;
printf("%d ",p->data);
p = p->left;
}else{
p = stack[top--];
p = p->right;
}
}
}
void miPrint(struct binaryTree *root)
{
struct binaryTree *p = root;
struct binaryTree *stack[10];
int top = -1;
while(p != NULL || top != -1){
if(p != NULL){
stack[++top] = p;
p = p->left;
}else{
p = stack[top--];
printf("%d ",p->data);
p = p->right;
}
}
}
void cengPrint(struct binaryTree *root)
{
int index1 = 0;
int index2 = 0;
struct binaryTree *arr[10];
arr[index1++] = root;
while(index1>index2){
if(arr[index2] != NULL){
printf("%d ",arr[index2]->data);
arr[index1++] = arr[index2]->left;
arr[index1++] = arr[index2]->right;
}
index2++;
}
}
int main()
{
struct binaryTree *root = (struct binaryTree *)malloc(sizeof(struct binaryTree));
printf("plesae input root data\n");
createTree(root);
printf("head Print:\n");
headPrint(root);
printf("\n");
printf("forPrint\n");
forPrint(root);
printf("\n");
printf("middle Print:\n");
middlePrint(root);
printf("\n");
printf("mmiprint\n");
miPrint(root);
printf("\n");
printf("end Print:\n");
endPrint(root);
printf("\n");
printf("cengPrint\n");
cengPrint(root);
printf("\n");
return 0;
}
|