实现二叉树的相关的操作:
- 先序遍历树(递归)
- 中序遍历树(递归)
- 后序遍历树(递归)
- 层序遍历树
- 创建一棵树
- 树的销毁
- 树的拷贝
- 二叉树中节点的个数
- 二叉树叶子节点的个数
- 二叉树第K层节点的个数
- 树的高度
- 在二叉树中查找节点
- 找当前节点的左子树
- 找当前节点的右子树
- 找当前节点的父节点
- 非递归完成先序遍历
- 非递归完成中序遍历
- 非递归完成后序遍历
- 二叉树的镜像
- 非递归完成二叉树的镜像
-
相关的操作的代码如下:
test.h的实现
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#define MAXSIZE 100
typedef char TreeNodeType;
typedef struct TreeNode* SeqQType;
typedef struct SeqQ{
SeqQType data[MAXSIZE];
size_t size;
size_t head;
size_t tail;
size_t capacity;
}SeqQ;
typedef struct TreeNode* StackType;
typedef struct SeqStack{
StackType* data;
int size;
int capacity;
}SeqStack;
typedef struct TreeNode{
struct TreeNode* lchild;
struct TreeNode* rchild;
TreeNodeType data;
}TreeNode;
void TreeInit(TreeNode** root,TreeNodeType value);
void PreOrder(TreeNode* root);
void InOrder(TreeNode* root);
void PostOrder(TreeNode* root);
void LevelOrder(TreeNode* root);
TreeNode* TreeCreate(TreeNodeType array[], size_t size,size_t *index, TreeNodeType null_node);
TreeNode* TreeDestroy(TreeNode** root);
TreeNode* TreeDestroy1(TreeNode** root);
TreeNode* TreeClone(TreeNode* root);
size_t TreeSize1(TreeNode* root);
size_t TreeSize2(TreeNode* root);
size_t TreeLeafSize(TreeNode* root);
size_t TreeLevelSize(TreeNode* root, size_t K);
size_t TreeHight(TreeNode* root);
TreeNode* TreeFind(TreeNode* root, TreeNodeType to_find);
TreeNode* TreeLchild(TreeNode* root);
TreeNode* TreeRchild(TreeNode* root);
TreeNode* TreeParent(TreeNode* root, TreeNode* node);
void PreOrderByLoop(TreeNode* root);
void InOrderByLoop(TreeNode* root);
void PostOrderByLoop(TreeNode* root);
void TreeMirror(TreeNode* root);
void TreeMirrorByLoop(TreeNode* root);
int IsCompleteTree(TreeNode* root);
test.c的实现
#include "tree.h"
void TreeInit(TreeNode** root,TreeNodeType value)
{
if(root==NULL)
{
return;
}
(*root)=malloc(sizeof(TreeNode));
(*root)->lchild=NULL;
(*root)->rchild=NULL;
(*root)->data=value;
}
void PreOrder(TreeNode* root)
{
if(root==NULL)
{
return;
}
printf("%c ",root->data);
PreOrder(root->lchild);
PreOrder(root->rchild);
}
void InOrder(TreeNode* root)
{
if(root==NULL)
{
return;
}
InOrder(root->lchild);
printf("%c ",root->data);
InOrder(root->rchild);
}
void PostOrder(TreeNode* root)
{
if(root==NULL)
{
return;
}
PostOrder(root->lchild);
PostOrder(root->rchild);
printf("%c ",root->data);
}
void SeqQInit(SeqQ* sq)
{
if (sq == NULL)
{
return;
}
sq->size = 0;
sq->head = 0;
sq->tail = 0;
}
void SeqQResize(SeqQ* sq){
if (sq == NULL){
return;
}
if (sq->size <= MAXSIZE){
return;
}
int size = MAXSIZE * 2 + 1;
SeqQType* new = (SeqQType*)malloc(sizeof(SeqQType));
int i =0;
for (; i<sq->size; i++){
new[i] = sq->data[i];
}
free(sq->data);
}
void SeqQPush(SeqQ* sq, SeqQType value)
{
if (sq == NULL){
return;
}
if (sq->size>=MAXSIZE)
{
SeqQResize(sq);
return;
}
sq->data[sq->tail++] = value;
if (sq->tail >= MAXSIZE){
sq->tail = 0;
}
++sq->size;
}
void SeqQPop(SeqQ* sq){
if (sq == NULL){
return;
}
if (sq->size == 0){
return;
}
++sq->head;
if (sq->head >= MAXSIZE){
sq->head = 0;
}
--sq->size;
}
int SeqQFront(SeqQ* sq, SeqQType* value){
if (sq == NULL || value==NULL){
return 0;
}
if (sq->size == 0){
return 0;
}
*value = sq->data[(sq->head)];
return 1;
}
void LevelOrder(TreeNode* root)
{
if(root==NULL)
{
return;
}
SeqQ sq;
SeqQInit(&sq);
SeqQPush(&sq,root);
while(1)
{
SeqQType front;
int ret=SeqQFront(&sq,&front);
if(ret == 0)
{
break;
}
printf("%c ",front->data);
SeqQPop(&sq);
if(front->lchild!=NULL)
{
SeqQPush(&sq,front->lchild);
}
if(front->rchild!=NULL)
{
SeqQPush(&sq,front->rchild);
}
}
}
TreeNode* TreeNodeCreate(TreeNodeType value)
{
TreeNode* newnode=(TreeNode*)malloc(sizeof(TreeNode));
newnode->data=value;
newnode->lchild=NULL;
newnode->rchild=NULL;
return newnode;
}
TreeNode* TreeCreate(TreeNodeType array[], size_t size,size_t *index, TreeNodeType null_node)
{
if(index==NULL)
{
return NULL;
}
if(*index>=size){
return NULL;
}
if(array[*index]==null_node)
{
return NULL;
}
TreeNode* newnode=TreeNodeCreate(array[*index]);
++(*index);
newnode->lchild=TreeCreate(array,size,index,null_node);
++(*index);
newnode->rchild=TreeCreate(array,size,index,null_node);
return newnode;
}
void DestroyNode(TreeNode** node)
{
if(node==NULL || *node==NULL)
return;
free(*node);
*node=NULL;
}
TreeNode* TreeDestroy(TreeNode** root)
{
if(root==NULL)
{
return NULL;
}
if(*root==NULL)
{
return NULL;
}
TreeDestroy(&(*root)->lchild);
TreeDestroy(&(*root)->rchild);
DestroyNode(root);
*root=NULL;
}
TreeNode* TreeDestroy1(TreeNode** root)
{
if(root==NULL)
{
return NULL;
}
if(*root==NULL)
{
return NULL;
}
TreeNode* lchild=(*root)->lchild;
TreeNode* rchild=(*root)->rchild;
DestroyNode(root);
TreeDestroy(&lchild);
TreeDestroy(&rchild);
}
TreeNode* TreeClone(TreeNode* root)
{
if(root==NULL)
{
return NULL;
}
TreeNode* new_node=TreeNodeCreate(root->data);
new_node->lchild=TreeClone(root->lchild);
new_node->rchild=TreeClone(root->rchild);
return new_node;
}
void _Treesize(TreeNode* root,size_t* size)
{
if(root==NULL)
{
return ;
}
++(*size);
_Treesize(root->lchild,size);
_Treesize(root->rchild,size);
}
size_t TreeSize1(TreeNode* root)
{
if(root==NULL)
{
return 0;
}
size_t size=0;
_Treesize(root,&size);
return size;
}
size_t TreeSize2(TreeNode* root)
{
if(root==NULL)
{
return 0;
}
return (1+TreeSize2(root->lchild)+TreeSize2(root->rchild));
}
size_t TreeLeafSize(TreeNode* root)
{
if(root==NULL)
{
return 0;
}
if(root->lchild==NULL && root->rchild==NULL)
{
return 1;
}
return TreeLeafSize(root->lchild)+TreeLeafSize(root->rchild);
}
size_t TreeLevelSize(TreeNode* root, size_t K)
{
if(root==NULL || K <1)
{
return 0;
}
if(K==1)
{
return 1;
}
return TreeLevelSize(root->lchild,K-1)+TreeLevelSize(root->rchild,K-1);
}
size_t TreeHight(TreeNode* root)
{
if(root==NULL)
{
return 0;
}
if(root->lchild==NULL && root->rchild==NULL)
{
return 1;
}
size_t lhight=TreeHight(root->lchild);
size_t rhight=TreeHight(root->rchild);
return 1+(lhight>rhight?lhight:rhight);
}
TreeNode* TreeFind(TreeNode* root, TreeNodeType to_find)
{
if(root==NULL)
{
return NULL;
}
if(root->data==to_find)
{
return root;
}
else if(root->data<to_find)
{
TreeNode* lvalue=TreeFind(root->lchild,to_find);
}
return TreeFind(root->rchild,to_find);
}
TreeNode* TreeLchild(TreeNode* root)
{
if(root==NULL)
{
return NULL;
}
return root->lchild;
}
TreeNode* TreeRchild(TreeNode* root)
{
if(root==NULL)
{
return NULL;
}
return root->rchild;
}
TreeNode* TreeParent(TreeNode* root, TreeNode* node)
{
if(root==NULL || node==NULL)
{
return NULL;
}
if(root->lchild==node || root->rchild==node)
{
return root;
}
TreeNode* lvalue=TreeParent(root->lchild,node);
TreeNode* rvalue=TreeParent(root->rchild,node);
return lvalue!=NULL?lvalue:rvalue;
}
void SeqStackInit(SeqStack* stack){
if(stack==NULL){
return;
}
stack->size=0;
stack->capacity=1000;
stack->data=(StackType*)malloc(stack->capacity*sizeof(StackType));
}
void SeqStackResize(SeqStack* stack)
{
if(stack==NULL){
return;
}
if((stack->size)<(stack->capacity)){
return;
}
stack->capacity=stack->capacity*2+1;
StackType* new=(StackType*)malloc(stack->capacity*sizeof(StackType));
int i=0;
for(i=0;i<stack->size;i++){
new[i]=stack->data[i];
}
free(stack->data);
stack->data=new;
}
void SeqStackPush(SeqStack* stack,StackType value)
{
if(stack==NULL){
return;
}
if((stack->size)>=(stack->capacity)){
SeqStackResize(stack);
}
stack->data[stack->size++]=value;
}
void SeqStackPop(SeqStack* stack)
{
if(stack==NULL){
return;
}
if(stack->size==0){
return ;
}
--stack->size;
}
int SeqStackTop(SeqStack* stack,StackType* value)
{
if(stack==NULL){
return 0;
}
if(stack->size==0){
return 0;
}
*value=stack->data[stack->size-1];
return 1;
}
void PreOrderByLoop(TreeNode* root)
{
if(root==NULL)
{
return ;
}
SeqStack stack;
SeqStackInit(&stack);
SeqStackPush(&stack,root);
TreeNode* cur=NULL;
while(SeqStackTop(&stack,&cur))
{
SeqStackPop(&stack);
printf("%c ",cur->data);
if(cur->rchild!=NULL)
{
SeqStackPush(&stack,cur->rchild);
}
if(cur->lchild!=NULL)
{
SeqStackPush(&stack,cur->lchild);
}
}
}
void InOrderByLoop(TreeNode* root)
{
if(root==NULL)
{
return ;
}
SeqStack stack;
SeqStackInit(&stack);
TreeNode* cur=root;
while(1)
{
while(cur!=NULL)
{
SeqStackPush(&stack,cur);
cur=cur->lchild;
}
TreeNode* top=NULL;
int ret=SeqStackTop(&stack,&top);
if(ret==0)
{
return;
}
printf("%c ",top->data);
SeqStackPop(&stack);
cur=top->rchild;
}
}
void PostOrderByLoop(TreeNode* root)
{
if(root==NULL)
{
return ;
}
SeqStack stack;
SeqStackInit(&stack);
TreeNode* cur=root;
TreeNode* pre=NULL;
while(1)
{
while(cur!=NULL)
{
SeqStackPush(&stack,cur);
cur=cur->lchild;
}
TreeNode* top=NULL;
int ret=SeqStackTop(&stack,&top);
if(ret==0)
{
return;
}
if(top->rchild==NULL || top->rchild==pre)
{
printf("%c ",top->data);
SeqStackPop(&stack);
pre=top;
}
else
{
cur=top->rchild;
}
}
}
void swp(TreeNode** a,TreeNode** b)
{
TreeNode* tmp=*a;
*a=*b;
*b=tmp;
}
void TreeMirror(TreeNode* root)
{
if(root==NULL)
{
return ;
}
swp(&root->lchild,&root->rchild);
TreeMirror(root->lchild);
TreeMirror(root->rchild);
}
void TreeMirrorByLoop(TreeNode* root)
{
if(root==NULL)
{
return;
}
SeqQ sq;
SeqQInit(&sq);
SeqQPush(&sq,root);
TreeNode* cur=NULL;
while(SeqQFront(&sq,&cur))
{
swp(&cur->lchild,&cur->rchild);
SeqQPop(&sq);
if(cur->lchild!=NULL)
{
SeqQPush(&sq,cur->lchild);
}
if(cur->rchild!=NULL)
{
SeqQPush(&sq,cur->rchild);
}
}
}
int IsCompleteTree(TreeNode* root)
{
if(root == NULL){
return 0;
}
SeqQ q;
SeqQInit(&q);
SeqQPush(&q,root);
int flag = 0;
TreeNode* cur = NULL;
while(SeqQFront(&q,&cur)){
if(flag == 0){
if(cur->lchild != NULL && cur->rchild != NULL){
SeqQPush(&q,cur->lchild);
SeqQPush(&q,cur->rchild);
}
else if(cur->lchild == NULL && cur->rchild != NULL){
return 0;
}else if(cur->lchild != NULL && cur->rchild == NULL){
flag = 1;
SeqQPush(&q,cur->lchild);
}else{
flag = 1;
}
}
if(cur->lchild == NULL && cur->rchild == NULL){
;
}else{
return 0;
}
}
return 1;
}
#define TEST_HEADER printf("\n===========%s=============\n",__FUNCTION__);
void test1()
{
TEST_HEADER;
TreeNode* root;
TreeInit(&root,'a');
TreeNode* b=TreeNodeCreate('b');
TreeNode* c=TreeNodeCreate('c');
TreeNode* d=TreeNodeCreate('d');
TreeNode* e=TreeNodeCreate('e');
TreeNode* f=TreeNodeCreate('f');
TreeNode* g=TreeNodeCreate('g');
root->lchild=b;
root->rchild=c;
b->lchild=d;
b->rchild=e;
e->lchild=g;
c->rchild=f;
printf("先序遍历树\n");
PreOrder(root);
printf("\n");
}
void test2()
{
TEST_HEADER;
TreeNode* root;
TreeInit(&root,'a');
TreeNode* b=TreeNodeCreate('b');
TreeNode* c=TreeNodeCreate('c');
TreeNode* d=TreeNodeCreate('d');
TreeNode* e=TreeNodeCreate('e');
TreeNode* f=TreeNodeCreate('f');
TreeNode* g=TreeNodeCreate('g');
root->lchild=b;
root->rchild=c;
b->lchild=d;
b->rchild=e;
e->lchild=g;
c->rchild=f;
printf("中序遍历树\n");
InOrder(root);
printf("\n");
}
void test3()
{
TEST_HEADER;
TreeNode* root;
TreeInit(&root,'a');
TreeNode* b=TreeNodeCreate('b');
TreeNode* c=TreeNodeCreate('c');
TreeNode* d=TreeNodeCreate('d');
TreeNode* e=TreeNodeCreate('e');
TreeNode* f=TreeNodeCreate('f');
TreeNode* g=TreeNodeCreate('g');
root->lchild=b;
root->rchild=c;
b->lchild=d;
b->rchild=e;
e->lchild=g;
c->rchild=f;
printf("后序遍历树\n");
PostOrder(root);
printf("\n");
}
void test4()
{
TEST_HEADER;
TreeNode* root;
TreeInit(&root,'a');
TreeNode* b=TreeNodeCreate('b');
TreeNode* c=TreeNodeCreate('c');
TreeNode* d=TreeNodeCreate('d');
TreeNode* e=TreeNodeCreate('e');
TreeNode* f=TreeNodeCreate('f');
TreeNode* g=TreeNodeCreate('g');
root->lchild=b;
root->rchild=c;
b->lchild=d;
b->rchild=e;
e->lchild=g;
c->rchild=f;
printf("层序遍历树\n");
LevelOrder(root);
printf("\n");
}
void testtreecreate()
{
TEST_HEADER;
TreeNodeType array[]="abd##eg###c#f##";
size_t size=strlen(array);
size_t index=0;
TreeNodeType null_node='#';
TreeNode* tmp=TreeCreate( array, size,&index,null_node);
PreOrder(tmp);
printf("\n");
InOrder(tmp);
printf("\n");
PostOrder(tmp);
printf("\n");
LevelOrder(tmp);
printf("\n");
}
void testtreedestroy()
{
TEST_HEADER;
TreeNodeType arr[]="abd##eg###c#f##";
size_t size1=strlen(arr);
size_t index=0;
TreeNodeType null_node='#';
TreeNode* p=TreeCreate( arr, size1,&index,null_node);
PreOrder(p);
printf("\n");
printf("销毁之后的结果为: \n");
TreeDestroy(&p);
printf("\n");
PreOrder(p);
printf("\n");
}
void testclone()
{
TEST_HEADER;
TreeNodeType arr[]="abd##eg###c#f##";
size_t size=strlen(arr);
size_t index=0;
TreeNodeType null_node='#';
TreeNode* root=TreeCreate( arr, size,&index,null_node);
TreeNode* new_node= TreeClone(root);
printf("对创建好的树进行先中后遍历:\n");
PreOrder(new_node);
printf("\n");
InOrder(new_node);
printf("\n");
PostOrder(new_node);
printf("\n");
}
void testtreesize()
{
TEST_HEADER;
TreeNodeType arr[]="abd##eg###c#f##";
size_t size=strlen(arr);
size_t index=0;
TreeNodeType null_node='#';
TreeNode* root=TreeCreate( arr, size,&index,null_node);
size_t tmp=TreeSize2(root);
printf("%d\n",tmp);
}
void testleafsize()
{
TEST_HEADER;
TreeNodeType arr[]="abd##eg###c#f##";
size_t size=strlen(arr);
size_t index=0;
TreeNodeType null_node='#';
TreeNode* root=TreeCreate( arr, size,&index,null_node);
size_t tmp=TreeLeafSize(root);
printf("%d\n",tmp);
}
void testlevelsize()
{
TEST_HEADER;
TreeNodeType arr[]="abd##eg###c#f##";
size_t size=strlen(arr);
size_t index=0;
TreeNodeType null_node='#';
TreeNode* root=TreeCreate( arr, size,&index,null_node);
size_t m=4;
size_t tmp=TreeLevelSize(root,4);
printf("%d\n",tmp);
}
void testtreehight()
{
TEST_HEADER;
TreeNodeType arr[]="abd##eg###c#f##";
size_t size=strlen(arr);
size_t index=0;
TreeNodeType null_node='#';
TreeNode* root=TreeCreate( arr, size,&index,null_node);
size_t tmp=TreeHight(root);
printf("%d\n",tmp);
}
void testfind()
{
TEST_HEADER;
TreeNodeType arr[]="abd##eg###c#f##";
size_t size=strlen(arr);
size_t index=0;
TreeNodeType null_node='#';
TreeNode* root=TreeCreate( arr, size,&index,null_node);
printf("查找元素‘c'是不是存在树中: \n");
TreeNode* tmp=TreeFind(root,'c');
printf("%c\n",tmp->data);
}
void testlchild()
{
TEST_HEADER;
TreeNodeType arr[]="abd##eg###c#f##";
size_t size=strlen(arr);
size_t index=0;
TreeNodeType null_node='#';
TreeNode* root=TreeCreate( arr, size,&index,null_node);
TreeNode* value=TreeLchild(root);
printf("%c\n",value->data);
}
void testrchild()
{
TEST_HEADER;
TreeNodeType arr[]="abd##eg###c#f##";
size_t size=strlen(arr);
size_t index=0;
TreeNodeType null_node='#';
TreeNode* root=TreeCreate( arr, size,&index,null_node);
TreeNode* tmp=TreeRchild(root);
printf("%c\n",tmp->data);
}
void testparent()
{
TEST_HEADER;
TreeNodeType arr[]="abd##eg###c#f##";
size_t size=strlen(arr);
size_t index=0;
TreeNodeType null_node='#';
TreeNode* root=TreeCreate( arr, size,&index,null_node);
TreeNode* ret= TreeParent(root, root->lchild->rchild);
printf("%c\n",ret->data);
}
void testpreorderbyloop()
{
TEST_HEADER;
TreeNodeType arr[]="abd##eg###c#f##";
size_t size=strlen(arr);
size_t index=0;
TreeNodeType null_node='#';
TreeNode* root=TreeCreate( arr, size,&index,null_node);
printf("非递归的实现先序遍历: \n");
PreOrderByLoop(root);
printf("\n");
}
void testinorder()
{
TEST_HEADER;
TreeNodeType arr[]="abd##eg###c#f##";
size_t size=strlen(arr);
size_t index=0;
TreeNodeType null_node='#';
TreeNode* root=TreeCreate( arr, size,&index,null_node);
printf("非递归的实现中序遍历: \n");
InOrderByLoop(root);
printf("\n");
}
void testpostorderloop()
{
TEST_HEADER;
TreeNodeType arr[]="abd##eg###c#f##";
size_t size=strlen(arr);
size_t index=0;
TreeNodeType null_node='#';
TreeNode* root=TreeCreate( arr, size,&index,null_node);
printf("非递归的实现后序遍历: \n");
PostOrderByLoop(root);
printf("\n");
}
void testtreemirror1()
{
TEST_HEADER;
TreeNodeType arr[]="abd##eg###c#f##";
size_t size=strlen(arr);
size_t index=0;
TreeNodeType null_node='#';
TreeNode* root=TreeCreate( arr, size,&index,null_node);
printf("非递归的实现镜像: ");
TreeMirror(root);
printf("\n");
printf("先序打印镜像后的树: ");
PreOrder(root);
printf("\n");
printf("中序打印镜像后的树:");
InOrder(root);
printf("\n");
printf("后序打印镜像后的树:");
PostOrder(root);
printf("\n");
printf("层序打印镜像后的树:");
LevelOrder(root);
printf("\n");
}
void testtreemirror2()
{
TEST_HEADER;
TreeNodeType arr[]="abd##eg###c#f##";
size_t size=strlen(arr);
size_t index=0;
TreeNodeType null_node='#';
TreeNode* root=TreeCreate( arr, size,&index,null_node);
printf("非递归的实现镜像: ");
TreeMirrorByLoop(root);
printf("\n");
printf("先序打印镜像后的树: ");
PreOrder(root);
printf("\n");
printf("中序打印镜像后的树:");
InOrder(root);
printf("\n");
printf("后序打印镜像后的树:");
PostOrder(root);
printf("\n");
printf("层序打印镜像后的树:");
LevelOrder(root);
printf("\n");
}
void testiscomplete()
{
TEST_HEADER;
TreeNodeType arr[]="abd##eg###c#f##";
size_t size=strlen(arr);
size_t index=0;
TreeNodeType null_node='#';
TreeNode* root=TreeCreate( arr, size,&index,null_node);
int value=IsCompleteTree(root);
printf("IsCompleteTree expect is 0,actual is %d\n",value);
}
int main()
{
test1();
test2();
test3();
test4();
testtreecreate();
testtreedestroy();
testclone();
testtreesize();
testleafsize();
testlevelsize();
testtreehight();
testfind();
testlchild();
testrchild();
testparent();
testpreorderbyloop();
testinorder();
testpostorderloop();
testtreemirror1();
testtreemirror2();
testiscomplete();
return 0;
}