二叉树基本操作
学习用递归函数:
1、求二叉树 叶子数量
2、求树的高度
3、拷贝二叉树
4、递归遍历
5、释放二叉树
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <Windows.h>
struct BinaryNode
{
char ch;
struct BinaryNode* lChild;
struct BinaryNode* rChild;
};
void calculateLeafNum(struct BinaryNode* root, int *p)
{
if (root == NULL)
{
return;
}
if (root->lChild == NULL && root->rChild == NULL)
{
(*p)++;
}
calculateLeafNum(root->lChild, p);
calculateLeafNum(root->rChild, p);
}
int getTreeHeight(struct BinaryNode* root)
{
if (root == NULL)
{
return 0;
}
int lHeight = getTreeHeight(root->lChild);
int rHeight = getTreeHeight(root->rChild);
int height = lHeight > rHeight ? lHeight + 1 : rHeight + 1;
return height;
}
struct BinaryNode* copyTree(struct BinaryNode* root)
{
if (root == NULL)
{
return NULL;
}
struct BinaryNode* lChlild = copyTree(root->lChild);
struct BinaryNode* rClild = copyTree(root->rChild);
struct BinaryNode* newNode = malloc(sizeof(struct BinaryNode));
if (newNode == NULL)
{
return NULL;
}
newNode->ch = root->ch;
newNode->lChild = lChlild;
newNode->rChild = rClild;
return newNode;
}
void recursion(struct BinaryNode* root)
{
if (root == NULL)
{
return;
}
recursion(root->lChild);
recursion(root->rChild);
printf("%c ", root->ch);
}
void freeTree(struct BinaryNode* root)
{
if (root == NULL)
{
return;
}
freeTree(root->lChild);
freeTree(root->rChild);
printf("%c被释放了\n", root->ch);
free(root);
}
void test01()
{
struct BinaryNode nodeA = { 'A', NULL, NULL };
struct BinaryNode nodeB = { 'B', NULL, NULL };
struct BinaryNode nodeC = { 'C', NULL, NULL };
struct BinaryNode nodeD = { 'D', NULL, NULL };
struct BinaryNode nodeE = { 'E', NULL, NULL };
struct BinaryNode nodeF = { 'F', NULL, NULL };
struct BinaryNode nodeG = { 'G', NULL, NULL };
struct BinaryNode nodeH = { 'H', NULL, NULL };
nodeA.lChild = &nodeB;
nodeA.rChild = &nodeF;
nodeB.rChild = &nodeC;
nodeC.lChild = &nodeD;
nodeC.rChild = &nodeE;
nodeF.rChild = &nodeG;
nodeG.lChild = &nodeH;
int num = 0;
calculateLeafNum(&nodeA, &num);
int height = getTreeHeight(&nodeA);
struct BinaryNode* newTree = copyTree(&nodeA);
printf("后序递归遍历:");
recursion(newTree);
printf("\n");
freeTree(newTree);
}
int main(void)
{
test01();
system("pause");
return EXIT_SUCCESS;
}
二叉树非递归遍历
1、将根节点 入栈
2、只要栈中元素个数大于 0 执行循环
获取栈顶元素
出栈
如果标志位真 直接输出 并且执行下一次循环
如果为假 将标志改为真
将右子树 左子树 根 入栈
执行下一次循环
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <Windows.h>
#include "seqStack.h"
struct BinaryNode
{
char ch;
struct BinaryNode* lChild;
struct BinaryNode* rChild;
int flag;
};
void nonRecursion(struct BinaryNode* root)
{
if (root == NULL)
{
return;
}
SeqStack myStack = init_SeqStack();
push_SeqStack(myStack, root);
while (size_SeqStack(myStack) > 0)
{
struct BinaryNode* pTop = top_SeqStack(myStack);
pop_SeqStack(myStack);
if (pTop->flag == 1)
{
printf("%c ", pTop->ch);
continue;
}
pTop->flag = 1;
if (pTop->lChild != NULL)
{
push_SeqStack(myStack, pTop->lChild);
}
if (pTop->rChild != NULL)
{
push_SeqStack(myStack, pTop->rChild);
}
push_SeqStack(myStack, pTop);
}
destroy_SeqStack(myStack);
}
void test01()
{
struct BinaryNode nodeA = { 'A', NULL, NULL,0 };
struct BinaryNode nodeB = { 'B', NULL, NULL,0 };
struct BinaryNode nodeC = { 'C', NULL, NULL,0 };
struct BinaryNode nodeD = { 'D', NULL, NULL,0 };
struct BinaryNode nodeE = { 'E', NULL, NULL,0 };
struct BinaryNode nodeF = { 'F', NULL, NULL,0 };
struct BinaryNode nodeG = { 'G', NULL, NULL,0 };
struct BinaryNode nodeH = { 'H', NULL, NULL,0 };
nodeA.lChild = &nodeB;
nodeA.rChild = &nodeF;
nodeB.rChild = &nodeC;
nodeC.lChild = &nodeD;
nodeC.rChild = &nodeE;
nodeF.rChild = &nodeG;
nodeG.lChild = &nodeH;
nonRecursion(&nodeA);
}
int main(void)
{
test01();
system("pause");
return EXIT_SUCCESS;
}