Many topics related to the binary tree, so the first to write about some of the basic creation and traversal of a binary tree, native code debugging after convenience.
For convenience, as used herein char data type value used to initialize a data array.
Because these things relatively simple, here is not to do too much detail.
create
1, the definition of some of the content:
// 二叉树节点结构体
typedef struct tree_node
{
struct tree_node *pL;
struct tree_node *pR;
char data;
}TREE_NODE_S
// 输入数据的无效值,若读到无效值,则说明该节点为空
#define INVALID -1
// 全局变量,记录当前输入的数组位置
char count = 0
// 在遍历树的时候,需要对data做的操作
typedef void (*pfprocData)(char *p);
2, create the original binary tree using a recursive manner.
The basic idea with the preorder essentially the same, except that one is made to the data output, one is made to the data input.
TREE_NODE_S* createNode(char *str)
{
TREE_NODE_S *pTemp = NULL;
char data = *(str+count);
count ++;
if (data != INVALID)
{
pTemp = (TREE_NODE_S *)calloc(1, sizeof(TREE_NODE_S));
if (NULL == pTemp)
{
return pTemp;
}
pTemp->data = data;
pTemp->pL = createNode(str);
pTemp->pR = createNode(str);
}
return pTemp;
}
3, there is further provided a non-return value, the method of creating the transmission tree two pointers:
createNode2(TREE_NODE_S **p, char *str)
{
TREE_NODE_S *pTemp = NULL;
char data = *(str+count);
count ++;
if (data != INVALID)
{
pTemp = (TREE_NODE_S *)calloc(1, sizeof(TREE_NODE_S));
if (NULL == pTemp)
{
*p = NULL;
return;
}
// 这里直接对指针进行赋值
*p = pTemp;
pTemp->data = data;
createNode2(&(pTemp->pL), str);
createNode2(&(pTemp->pR), str);
}
else
{
*p = NULL;
}
return;
}
Traversal
Three common preamble, in sequence, after traversal:
// 这里pfprocData,是用来处理结构体里面的数据部分的函数
void frontOrder(TREE_NODE_S *p, pfprocData pfunc)
{
if (NULL == p)
{
return;
}
pfunc(&(p->data));
frontOrder(p->pL, pfunc);
frontOrder(p->pR, pfunc);
return;
}
void middleOrder(TREE_NODE_S *p, pfprocData pfunc)
{
if (NULL == p)
{
return;
}
middleOrder(p->pL, pfunc);
pfunc(&(p->data));
middleOrder(p->pR, pfunc);
return;
}
void lastOrder(TREE_NODE_S *p, pfprocData pfunc)
{
if (NULL == p)
{
return;
}
lastOrder(p->pL, pfunc);
lastOrder(p->pR, pfunc);
pfunc(&(p->data));
return;
}
test
// 先创建出如下两种树,然后做遍历输出
// 1
// / \
// 2 4
// \
// 3
char ps1[] = {1, 2, INVALID, 3, INVALID, INVALID, 4, INVALID, INVALID};
// 1
// / \
// 2 6
// / \ \
// 3 5 7
// \
// 4
char ps2[] = {1, 2, 3, INVALID, 4, INVALID, INVALID, 5, INVALID, INVALID, 6, INVALID, 7, INVALID, INVALID};
// 这里只对节点数据进行打印
void procData(char *p)
{
printf("%u ", *p);
}
int main(void)
{
TREE_NODE_S *pstTreeHead1 = NULL;
TREE_NODE_S *pstTreeHead2 = NULL;
pstTreeHead1 = createTree2(ps1);
pstTreeHead2 = createTree2(ps2)
// 如果使用第二个创建方法,则:
// createTree(&pstTreeHead1, ps1);
// createTree(&pstTreeHead2, ps2);
printf("%-14s", "frontOrder:");
frontOrder(pstTreeHead1, procData);
printf("\n");
printf("%-14s", "frontOrder:");
frontOrder(pstTreeHead2, procData);
printf("\n");
printf("%-14s", "middleOrder:");
middleOrder(pstTreeHead2, procData);
printf("\n");
printf("%-14s", "lastOrder:");
lastOrder(pstTreeHead2, procData);
printf("\n");
}