# 数据结构--树形结构之二叉树--C语言

1.树的概念

2.二叉树的概念

二、二叉树的存储结构

1.二叉树的顺序存储结构

2.二叉树的链式存储

1.前序遍历：

2.中序遍历

3.后序遍历

## 二、二叉树的存储结构

其顺序存储如下图：

### 2.二叉树的链式存储

链式节点构成：

Lchild:指向左孩子的指针；

Rchild:指向右孩子的指针；

data:节点的数据域；

typedef struct BinTreeNode
{
TreeValType data;//数据域
struct BinTreeNode *lchild, *rchlid;//左右孩子
struct BinTreeNode *parent;
}BTNode,*BiTree;


void CreateBinTree(BinTree *t)
{
*t = (BinTree*)malloc(sizeof(BinTree));
(*t)->data = 1;
(*t)->lchild = (BinTree*)malloc(sizeof(BinTree));
(*t)->lchild->data = 2;
(*t)->rchild = (BinTree*)malloc(sizeof(BinTree));
(*t)->rchild->data = 3;
(*t)->rchild->lchild = NULL;
(*t)->rchild->rchild = NULL;
(*t)->lchild->lchild = (BinTree*)malloc(sizeof(BinTree));
(*t)->lchild->rchild = (BinTree*)malloc(sizeof(BinTree));
(*t)->lchild->lchild->data = 4;
(*t)->lchild->rchild->data = 5;
(*t)->lchild->lchild->lchild = NULL;
(*t)->lchild->lchild->rchild = NULL;
(*t)->lchild->rchild->rchild = NULL;
(*t)->lchild->rchild->lchild = NULL;
}
#include<stdio.h>
#include<stdlib.h>
#define TreeValType int

typedef struct BinTreeNode
{
TreeValType data;//数据域
struct BinTreeNode *lchild, *rchild;//左右孩子
struct BinTreeNode *parent;
}BTNode,*BinTree;

void CreateBinTree(BinTree *t)
{
*t = (BinTree*)malloc(sizeof(BinTree));
(*t)->data = 1;
(*t)->lchild = (BinTree*)malloc(sizeof(BinTree));
(*t)->lchild->data = 2;
(*t)->rchild = (BinTree*)malloc(sizeof(BinTree));
(*t)->rchild->data = 3;
(*t)->rchild->lchild = NULL;
(*t)->rchild->rchild = NULL;
(*t)->lchild->lchild = (BinTree*)malloc(sizeof(BinTree));
(*t)->lchild->rchild = (BinTree*)malloc(sizeof(BinTree));
(*t)->lchild->lchild->data = 4;
(*t)->lchild->rchild->data = 5;
(*t)->lchild->lchild->lchild = NULL;
(*t)->lchild->lchild->rchild = NULL;
(*t)->lchild->rchild->rchild = NULL;
(*t)->lchild->rchild->lchild = NULL;
}

int main()
{
BinTree Tree;
CreateBinTree(&Tree);
printf("%d\n", Tree->lchild->rchild->data);
getchar();
return 0;
}

## 三、二叉树的遍历

### 1.前序遍历：

先序遍历采用的是递归的思想：

#include<stdio.h>
#include<stdlib.h>
#define TreeValType int

typedef struct BinTreeNode
{
TreeValType data;//数据域
struct BinTreeNode *lchild, *rchild;//左右孩子
struct BinTreeNode *parent;
}BTNode,*BinTree;

void CreateBinTree(BinTree *t)//创建二叉树
{
*t = (BinTree*)malloc(sizeof(BinTree));
(*t)->data = 1;
(*t)->lchild = (BinTree*)malloc(sizeof(BinTree));
(*t)->lchild->data = 2;
(*t)->rchild = (BinTree*)malloc(sizeof(BinTree));
(*t)->rchild->data = 3;
(*t)->rchild->lchild = (BinTree*)malloc(sizeof(BinTree));
(*t)->rchild->rchild = (BinTree*)malloc(sizeof(BinTree));
(*t)->lchild->lchild = (BinTree*)malloc(sizeof(BinTree));
(*t)->lchild->rchild = (BinTree*)malloc(sizeof(BinTree));
(*t)->lchild->lchild->data = 4;
(*t)->lchild->rchild->data = 5;
(*t)->rchild->rchild->data = 7;
(*t)->rchild->lchild->data = 6;
(*t)->lchild->lchild->lchild = NULL;
(*t)->lchild->lchild->rchild = NULL;
(*t)->lchild->rchild->rchild = NULL;
(*t)->lchild->rchild->lchild = NULL;
(*t)->rchild->lchild->lchild = NULL;
(*t)->rchild->lchild->rchild = NULL;
(*t)->rchild->rchild->rchild = NULL;
(*t)->rchild->rchild->lchild = NULL;
}

void displayVal(BTNode * Val)
{

printf("%d", Val->data);
}

//先序遍历二叉树
void PreOrderTraverse(BinTree t)
{
if (t != NULL)
{
displayVal(t);
PreOrderTraverse(t->lchild);//访问左孩子
PreOrderTraverse(t->rchild);//访问右孩子
}
return;
}

int main()
{
BinTree Tree;
CreateBinTree(&Tree);
printf("先序遍历：\n");
PreOrderTraverse(Tree);
getchar();
return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define TreeValType int
int top = -1;//初始栈顶元素所在位置

//构造节点结构体
typedef struct BinTreeNode
{
TreeValType data;
struct BinTreeNode * lchild, *rchild;
}BTNode,*BTree;

//初始化树的函数
void CreateBinTree(BTree *t)
{
*t = (BTree*)malloc(sizeof(BTree));
(*t)->data = 1;
(*t)->lchild = (BTree*)malloc(sizeof(BTree));
(*t)->rchild = (BTree*)malloc(sizeof(BTree));
(*t)->lchild->data = 2;
(*t)->rchild->data = 3;
(*t)->lchild->lchild = (BTree*)malloc(sizeof(BTree));
(*t)->lchild->rchild = (BTree*)malloc(sizeof(BTree));
(*t)->lchild->lchild->data = 4;
(*t)->lchild->rchild->data = 5;
(*t)->rchild->lchild = (BTree*)malloc(sizeof(BTree));
(*t)->rchild->rchild = (BTree*)malloc(sizeof(BTree));
(*t)->rchild->lchild->data = 6;
(*t)->rchild->rchild->data = 7;
(*t)->lchild->lchild->lchild = NULL;
(*t)->lchild->lchild->rchild = NULL;
(*t)->lchild->rchild->lchild = NULL;
(*t)->lchild->rchild->rchild = NULL;
(*t)->rchild->lchild->lchild = NULL;
(*t)->rchild->lchild->rchild = NULL;
(*t)->rchild->rchild->lchild = NULL;
(*t)->rchild->rchild->rchild = NULL;
}
//前序遍历使用的进栈函数
void push(BTree** a, BTNode* Val)
{
a[++top] = Val;
}

//出栈函数
void pop()
{
if (top ==-1)
{
return;
}
top--;
}

void displayVal(BTNode* Val)
{
printf("%d",Val->data);
}

//获取栈顶元素
BTNode* gettop(BTNode**a)
{
return a[top];
}
//先序遍历非递归算法
void preOrderTraverse(BTree tree)
{
BTNode* a[20];//定义顺序栈
BTNode* p;//定义临时指针
push(a, tree);
while (top != -1)
{
p = gettop(a);//取栈顶元素
pop();//弹栈
while (p)
{
displayVal(p);
//如果结点有右孩子，右孩子进栈
if (p->rchild)
{
push(a, p->rchild);
}
p = p->lchild;//一直指向根节点的最后一个左孩子
}
}
}
int main()
{
BTree tree;
CreateBinTree(&tree);
printf("先序遍历：\n");
preOrderTraverse(tree);
getchar();
return 0;
}

### 2.中序遍历

递归实现：

#include<stdio.h>
#include<stdlib.h>
#define TreeValType int

typedef struct BinTreeNode
{
TreeValType data;
struct BinTreeNode * lchild, *rchild;
}BTNode,*BinTree;

void CreateBinTree(BinTree *t)
{
*t = (BinTree*)malloc(sizeof(BinTree));
(*t)->data = 1;
(*t)->rchild = (BinTree*)malloc(sizeof(BinTree));
(*t)->lchild = (BinTree*)malloc(sizeof(BinTree));
(*t)->lchild->data = 2;
(*t)->rchild->data = 3;
(*t)->rchild->lchild= (BinTree*)malloc(sizeof(BinTree));
(*t)->rchild->rchild = (BinTree*)malloc(sizeof(BinTree));
(*t)->lchild->lchild = (BinTree*)malloc(sizeof(BinTree));
(*t)->lchild->rchild = (BinTree*)malloc(sizeof(BinTree));
(*t)->lchild->lchild->data = 4;
(*t)->lchild->rchild->data = 5;
(*t)->rchild->lchild ->data = 6;
(*t)->rchild->rchild->data = 7;
(*t)->lchild->lchild->lchild = NULL;
(*t)->lchild->lchild->rchild = NULL;
(*t)->lchild->rchild->lchild = NULL;
(*t)->lchild->rchild->rchild = NULL;
(*t)->rchild->lchild->lchild = NULL;
(*t)->rchild->lchild->rchild = NULL;
(*t)->rchild->rchild->lchild = NULL;
(*t)->rchild->rchild->rchild = NULL;
}

//输出节点本身的值
void displayVal(BTNode * Val)
{
printf("%d", Val->data);
}

//中序遍历
void MidOrderTraverse(BinTree t)
{
if (t != 0)
{
MidOrderTraverse(t->lchild);//遍历左孩子
displayVal(t);
MidOrderTraverse(t->rchild);
}
//节点为空，返回上一层
return;
}

int main()
{
BinTree tree;
CreateBinTree(&tree);
printf("中序遍历：\n");
MidOrderTraverse(tree);
getchar();
}

### 3.后序遍历

#include <stdio.h>
#include <string.h>
#include<stdlib.h>
#define TreevaleType int
//构造结点的结构体
typedef struct BiTNode{
TreevaleType data;//数据域
struct BiTNode *lchild, *rchild;//左右孩子指针
}BiTNode, *BiTree;
//初始化树的函数
void CreateBiTree(BiTree *T){
*T = (BiTNode*)malloc(sizeof(BiTNode));
(*T)->data = 1;
(*T)->lchild = (BiTNode*)malloc(sizeof(BiTNode));
(*T)->rchild = (BiTNode*)malloc(sizeof(BiTNode));

(*T)->lchild->data = 2;
(*T)->lchild->lchild = (BiTNode*)malloc(sizeof(BiTNode));
(*T)->lchild->rchild = (BiTNode*)malloc(sizeof(BiTNode));
(*T)->lchild->rchild->data = 5;
(*T)->lchild->rchild->lchild = NULL;
(*T)->lchild->rchild->rchild = NULL;
(*T)->rchild->data = 3;
(*T)->rchild->lchild = (BiTNode*)malloc(sizeof(BiTNode));
(*T)->rchild->lchild->data = 6;
(*T)->rchild->lchild->lchild = NULL;
(*T)->rchild->lchild->rchild = NULL;
(*T)->rchild->rchild = (BiTNode*)malloc(sizeof(BiTNode));
(*T)->rchild->rchild->data = 7;
(*T)->rchild->rchild->lchild = NULL;
(*T)->rchild->rchild->rchild = NULL;
(*T)->lchild->lchild->data = 4;
(*T)->lchild->lchild->lchild = NULL;
(*T)->lchild->lchild->rchild = NULL;
}

//模拟操作结点元素的函数，输出结点本身的数值
void displayVale(BiTNode* vale){
printf("%d ", vale->data);
}
//后序遍历
void LastOrderTraverse(BiTree T){
if (T) {
LastOrderTraverse(T->lchild);//遍历左孩子
LastOrderTraverse(T->rchild);//遍历右孩子
displayVale(T);//调用操作结点数据的函数方法
}
//如果结点为空，返回上一层
return;
}
int main() {
BiTree Tree;
CreateBiTree(&Tree);
printf("后序遍历: \n");
LastOrderTraverse(Tree);
getchar();
}

• 0
点赞
• 6
收藏
觉得还不错? 一键收藏
• 0
评论
06-12
07-05 658
12-12 877
11-19 526
11-29 1064
11-22 233
03-29 4534
03-25 234

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。