一、二叉树的递归遍历
二叉树的递归遍历.c
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>//二叉树的结点
typedef struct BINARYNODE {char ch;struct BINARYNODE *lchild;struct BINARYNODE *rchild;
}BinaryNode;void Recursion(BinaryNode* root)
{if (root == NULL) //退出条件return;//先序遍历printf("%c", root->ch); //访问根结点Recursion(root->lchild); //左子树遍历Recursion(root->rchild); //右子树遍历//中序遍历/*Recursion(root->lchild);printf("%c ", root->ch);Recursion(root->rchild);*///后序遍历/*Recursion(root->lchild);Recursion(root->rchild);printf("%c ", root->ch);*/
}void CreateBinaryTree()
{/*A/ \B F\ \C G/ \ /D E H*///创建结点BinaryNode node1 = { 'A',NULL,NULL }; //字符需用'' 字符串用""BinaryNode node2 = { 'B',NULL,NULL };BinaryNode node3 = { 'C',NULL,NULL };BinaryNode node4 = { 'D',NULL,NULL };BinaryNode node5 = { 'E',NULL,NULL };BinaryNode node6 = { 'F',NULL,NULL };BinaryNode node7 = { 'G',NULL,NULL };BinaryNode node8 = { 'H',NULL,NULL };node1.lchild = &node2;node1.rchild = &node6;node2.lchild = &node3;node3.lchild = &node4;node3.rchild = &node5;node6.rchild = &node7;node7.lchild = &node8;//递归遍历Recursion(&node1);
}int main()
{CreateBinaryTree();return 0;
}
运行结果:
二、求二叉树的叶子结点数
求二叉树的叶子结点数.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>//二叉树的结点
typedef struct BINARYNODE {char ch;struct BINARYNODE *lchild;struct BINARYNODE *rchild;
}BinaryNode;
int num = 0;
void CaculateLeafNum(BinaryNode* root)
{if (root == NULL) //退出条件return;if (root->lchild == NULL && root->rchild == NULL){num++;}CaculateLeafNum(root->lchild); //左子树遍历CaculateLeafNum(root->rchild); //右子树遍历
}void CaculateLeafNum(BinaryNode* root,int *pNum)
{if (root == NULL) //退出条件return;if (root->lchild == NULL && root->rchild == NULL){(*pNum)++;}CaculateLeafNum(root->lchild,pNum); //左子树遍历CaculateLeafNum(root->rchild,pNum); //右子树遍历
}void CreateBinaryTree()
{/*A/ \B F\ \C G/ \ /D E H*///创建结点BinaryNode node1 = { 'A',NULL,NULL }; //字符需用'' 字符串用""BinaryNode node2 = { 'B',NULL,NULL };BinaryNode node3 = { 'C',NULL,NULL };BinaryNode node4 = { 'D',NULL,NULL };BinaryNode node5 = { 'E',NULL,NULL };BinaryNode node6 = { 'F',NULL,NULL };BinaryNode node7 = { 'G',NULL,NULL };BinaryNode node8 = { 'H',NULL,NULL };node1.lchild = &node2;node1.rchild = &node6;node2.lchild = &node3;node3.lchild = &node4;node3.rchild = &node5;node6.rchild = &node7;node7.lchild = &node8;//叶子结点个数/*CaculateLeafNum(&node1);printf("叶子结点数:%d",num);*/int leafNum = 0;CaculateLeafNum(&node1, &leafNum);printf("叶子结点数:%d", leafNum);}int main()
{CreateBinaryTree();return 0;
}
运行结果:
三、求树的高度
求树的高度.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>//二叉树的结点
typedef struct BINARYNODE {char ch;struct BINARYNODE *lchild;struct BINARYNODE *rchild;
}BinaryNode;int getTreeHeight(BinaryNode* root)
{if (root == NULL) //退出条件return 0;//求出左子树的高度int lheight = getTreeHeight(root->rchild);//求出右子树的高度int rheight = getTreeHeight(root->lchild);//取左子树和右子树中最大值+1int height = lheight > rheight ? lheight + 1 : rheight + 1;return height;
}void CreateBinaryTree()
{/*A/ \B F\ \C G/ \ /D E H*///创建结点BinaryNode node1 = { 'A',NULL,NULL }; //字符需用'' 字符串用""BinaryNode node2 = { 'B',NULL,NULL };BinaryNode node3 = { 'C',NULL,NULL };BinaryNode node4 = { 'D',NULL,NULL };BinaryNode node5 = { 'E',NULL,NULL };BinaryNode node6 = { 'F',NULL,NULL };BinaryNode node7 = { 'G',NULL,NULL };BinaryNode node8 = { 'H',NULL,NULL };node1.lchild = &node2;node1.rchild = &node6;node2.lchild = &node3;node3.lchild = &node4;node3.rchild = &node5;node6.rchild = &node7;node7.lchild = &node8;//求树的高度int height = getTreeHeight(&node1);printf("树的高度:%d\n", height);}int main()
{CreateBinaryTree(); return 0;
}
运行结果:
四、二叉树的拷贝和释放
二叉树的拷贝和释放.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>//二叉树的结点
typedef struct BINARYNODE {char ch;struct BINARYNODE *lchild;struct BINARYNODE *rchild;
}BinaryNode;//遍历二叉树
void Recursion(BinaryNode* root)
{if (root == NULL) //退出条件return;//先序遍历printf("%c", root->ch); //访问根结点Recursion(root->lchild); //左子树遍历Recursion(root->rchild); //右子树遍历
}//拷贝二叉树
BinaryNode* CopyBinaryTree(BinaryNode* root)
{if (root == NULL)return NULL;//拷贝左子树BinaryNode* lchild=CopyBinaryTree(root->lchild);//拷贝右子树BinaryNode* rchild=CopyBinaryTree(root->rchild);//创建结点BinaryNode* newnode =(BinaryNode*) malloc(sizeof(BinaryNode));newnode->ch = root->ch;newnode->lchild = lchild;newnode->rchild = rchild;return newnode;}//释放二叉树内存
void FreeSpaceBinaryTree(BinaryNode* root)
{if (root == NULL)return;//释放左子树FreeSpaceBinaryTree(root->lchild);//释放右子树FreeSpaceBinaryTree(root->rchild);//释放当前结点FreeSpaceBinaryTree(root);
}
void CreateBinaryTree()
{/*A/ \B F\ \C G/ \ /D E H*///创建结点BinaryNode node1 = { 'A',NULL,NULL }; //字符需用'' 字符串用""BinaryNode node2 = { 'B',NULL,NULL };BinaryNode node3 = { 'C',NULL,NULL };BinaryNode node4 = { 'D',NULL,NULL };BinaryNode node5 = { 'E',NULL,NULL };BinaryNode node6 = { 'F',NULL,NULL };BinaryNode node7 = { 'G',NULL,NULL };BinaryNode node8 = { 'H',NULL,NULL };node1.lchild = &node2;node1.rchild = &node6;node2.lchild = &node3;node3.lchild = &node4;node3.rchild = &node5;node6.rchild = &node7;node7.lchild = &node8;BinaryNode *root = CopyBinaryTree(&node1);Recursion(root);FreeSpaceBinaryTree(root);}int main()
{CreateBinaryTree();return 0;
}
运行结果:
五、二叉树的非递归遍历
LinkList.h
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define MAX_SIZE 1024
#define TRUE 1
#define FALSE 0//链表结点--存储下一个结点指针
typedef struct LINKNODE {struct LINKNODE *next;
}LinkNode;//链表--保存头结点,和链表长度
typedef struct LINKLIST {struct LINKNODE head;int size;
}LinkList;//初始化
LinkList* Init_linkList();//压入元素
void Push_LinkList(LinkList* stack, LinkNode* data);//取出栈顶元素
LinkNode* Top_LinkList(LinkList* stack);//弹出栈顶元素
void Pop_LinkList(LinkList* stack);//判断是否为空
int IsEmpty_LinkList(LinkList* stack);//返回栈元素个数
int Size_LinkList(LinkList* stack);//清空栈元素
void Clear_LinkList(LinkList* stack);//销毁栈元素
void FreeSpace_LinkList(LinkList* stack);
LinkList.c
#include"LinkList.h"
//初始化
LinkList* Init_linkList()
{LinkList* stack = (LinkList*)malloc(sizeof(LinkList));stack->head.next = NULL;stack->size = 0;return stack;
}//压入元素
void Push_LinkList(LinkList* stack, LinkNode* data)
{if (stack == NULL){return;}if (data == NULL){return;}data->next = stack->head.next;//stack->head.next = data->next; //问题2:没有绕对。。stack->head.next = data;stack->size++;
}//返回栈顶元素
LinkNode* Top_LinkList(LinkList* stack)
{if (stack == NULL){return NULL;}if (stack->size == 0) {return NULL;}return stack->head.next;
}//弹出栈顶元素
void Pop_LinkList(LinkList* stack)
{if (stack == NULL){return;}if (stack->size == 0) {return;}//第一个有效结点LinkNode *pNext = stack->head.next;//pNext->next = stack->head.next; 我的错误做法stack->head.next = pNext->next;stack->size--;}//判断是否为空
int IsEmpty_LinkList(LinkList* stack)
{if (stack == NULL){return -1;}if (stack->size == 0)return TRUE;return FALSE;
}//返回栈元素个数
int Size_LinkList(LinkList* stack)
{if (stack == NULL){return -1;}return stack->size;
}//清空栈元素
void Clear_LinkList(LinkList* stack)
{if (stack == NULL){return;}stack->head.next = NULL;stack->size = 0;
}//销毁栈元素
void FreeSpace_LinkList(LinkList* stack)
{if (stack == NULL){return;}free(stack);
}
二叉树的非递归遍历.c
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"LinkList.h"
#define MY_FALSAE 0
#define MY_TRUE 1
//二叉树的结点
typedef struct BINARYNODE {char ch;struct BINARYNODE *lchild;struct BINARYNODE *rchild;
}BinaryNode;//二叉树的非递归遍历
typedef struct BITREESTACKNODE {LinkNode node;BinaryNode* root;int flag;
}BiTreeStackNode;//创建栈中结点
BiTreeStackNode* CreateBiTreeStackNode(BinaryNode* node, int flag)
{BiTreeStackNode* newnode = (BiTreeStackNode*)malloc(sizeof(BiTreeStackNode));newnode->root = node;newnode->flag = flag;return newnode;
}void NonRecursion(BinaryNode* root)
{//创建栈LinkList *stack = Init_linkList();//把根结点扔到栈里Push_LinkList(stack, (LinkNode*)CreateBiTreeStackNode(root, MY_FALSAE));while (Size_LinkList(stack) > 0){//弹出栈顶元素BiTreeStackNode* node=(BiTreeStackNode*)Top_LinkList(stack);Pop_LinkList(stack);//判断弹出的结点是否为空if (node->root == NULL){continue;}if (node->flag == MY_TRUE){printf("%c", node->root->ch);}//先序。放入顺序和遍历顺序正好相反else {//当前结点的右结点入栈Push_LinkList(stack, (LinkNode*)CreateBiTreeStackNode(node->root->rchild, MY_FALSAE));//当前结点的左结点入栈Push_LinkList(stack, (LinkNode*)CreateBiTreeStackNode(node->root->lchild, MY_FALSAE));//当前结点入栈node->flag = MY_TRUE;Push_LinkList(stack, (LinkNode*)node);} }
}//遍历二叉树
void Recursion(BinaryNode* root)
{if (root == NULL) //退出条件return;//先序遍历printf("%c", root->ch); //访问根结点Recursion(root->lchild); //左子树遍历Recursion(root->rchild); //右子树遍历
}//二叉树的非递归遍历
void CreateBinaryTree()
{/*A/ \B F\ \C G/ \ /D E H*///创建结点BinaryNode node1 = { 'A',NULL,NULL }; //字符需用'' 字符串用""BinaryNode node2 = { 'B',NULL,NULL };BinaryNode node3 = { 'C',NULL,NULL };BinaryNode node4 = { 'D',NULL,NULL };BinaryNode node5 = { 'E',NULL,NULL };BinaryNode node6 = { 'F',NULL,NULL };BinaryNode node7 = { 'G',NULL,NULL };BinaryNode node8 = { 'H',NULL,NULL };node1.lchild = &node2;node1.rchild = &node6;node2.lchild = &node3;node3.lchild = &node4;node3.rchild = &node5;node6.rchild = &node7;node7.lchild = &node8;//二叉树的非递归遍历NonRecursion(&node1);
}
int main()
{CreateBinaryTree();return 0;
}
运行结果: