//二叉树--链式存储
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
struct BiTNode{
//数据域
int value;
//左孩子
struct BiTNode *lChild;
//右孩子
struct BiTNode *rChild;
};
typedef struct BiTNode BiTNode;
typedef struct BiTNode* BiTree;
struct LinkNode{
BiTNode* data;
struct LinkNode *next;
};
typedef struct LinkNode LinkNode;
struct LinkQueue{
LinkNode *front;
LinkNode *rear;
};
typedef struct LinkQueue LinkQueue;
/*
初始化tree
*/
BiTNode* initTree(BiTree root){
root=NULL;
return root;
}
/*
构造树
*/
BiTNode* constructTree(BiTree root){
BiTNode *p11=(BiTNode *)malloc(sizeof(BiTNode));
(*p11).value=11;
(*p11).lChild=NULL;
(*p11).rChild=NULL;
BiTNode *p12=(BiTNode *)malloc(sizeof(BiTNode));
(*p12).value=12;
(*p12).lChild=NULL;
(*p12).rChild=NULL;
BiTNode *p5=(BiTNode *)malloc(sizeof(BiTNode));
(*p5).value=5;
(*p5).lChild=NULL;
(*p5).rChild=p11;
BiTNode *p6=(BiTNode *)malloc(sizeof(BiTNode));
(*p6).value=6;
(*p6).lChild=p12;
(*p6).rChild=NULL;
BiTNode *p7=(BiTNode *)malloc(sizeof(BiTNode));
(*p7).value=7;
(*p7).lChild=NULL;
(*p7).rChild=NULL;
BiTNode *p2=(BiTNode *)malloc(sizeof(BiTNode));
(*p2).value=2;
(*p2).lChild=NULL;
(*p2).rChild=p5;
BiTNode *p3=(BiTNode *)malloc(sizeof(BiTNode));
(*p3).value=3;
(*p3).lChild=p6;
(*p3).rChild=p7;
BiTNode *p1=(BiTNode *)malloc(sizeof(BiTNode));
(*p1).value=1;
(*p1).lChild=p2;
(*p1).rChild=p3;
root=p1;
return root;
}
/*
先序遍历--根左右
*/
void preOrder(BiTree biTree){
if(biTree!=NULL){
printf("%d ",(*biTree).value);
preOrder((*biTree).lChild);
preOrder((*biTree).rChild);
}
}
/*
中序遍历--左根右
*/
void inOrder(BiTree biTree){
if(biTree!=NULL){
inOrder((*biTree).lChild);
printf("%d ",(*biTree).value);
inOrder((*biTree).rChild);
}
}
/*
后序遍历--左右根
*/
void postOrder(BiTree biTree){
if(biTree!=NULL){
postOrder((*biTree).lChild);
postOrder((*biTree).rChild);
printf("%d ",(*biTree).value);
}
}
/*
树的深度
*/
int treeDepth(BiTree root){
if(root==NULL){
return 0;
}else{
int l=treeDepth((*root).lChild);
int r=treeDepth((*root).rChild);
return l>r?l+1:r+1;
}
}
/*
初始化队列
返回值
0--失败
1--成功
*/
int initQueue(LinkQueue *queue){
LinkNode *newNode=(LinkNode *)malloc(sizeof(LinkNode));
if(newNode==NULL){
return 0;
}
(*newNode).next=NULL;
(*queue).front=newNode;
(*queue).rear=newNode;
}
/*
入队操作
返回值
0--失败
1--成功
*/
int enQueue(LinkQueue *queue,BiTNode* data){
LinkNode *newNode=(LinkNode *)malloc(sizeof(LinkNode));
if(newNode==NULL){
return 0;
}
(*newNode).data=data;
(*newNode).next=NULL;
(*queue).rear->next=newNode;
(*queue).rear=newNode;
return 1;
}
/*
出队操作
*/
BiTNode* deQueue(LinkQueue *queue){
LinkNode *front=(*queue).front;
LinkNode *rear=(*queue).rear;
//被出队的结点--头结点的下一个结点
LinkNode *tmpNode=(*front).next;
BiTNode* result=(*tmpNode).data;
(*front).next=(*tmpNode).next;
if((*tmpNode).next==NULL){
(*queue).rear=front;
}
free(tmpNode);
return result;
}
/*
判断队列是否为空
1--为空
0--不为空
*/
int queueEmpty(LinkQueue queue){
if(queue.front==queue.rear){
return 1;
}else{
return 0;
}
}
/*
层次遍历
*/
void levelOrder(BiTree root){
if(root==NULL){
return ;
}
LinkQueue linkQueue;
initQueue(&linkQueue);
enQueue(&linkQueue,root);
BiTNode* p;
while(queueEmpty(linkQueue)==0){
p=deQueue(&linkQueue);
printf("%d ",(*p).value);
if((*p).lChild!=NULL){
enQueue(&linkQueue,(*p).lChild);
}
if((*p).rChild!=NULL){
enQueue(&linkQueue,(*p).rChild);
}
}
}
BiTNode *tmpResult=NULL;
/*
利用先序遍历,找到某一节点,并赋值给tmpResult
*/
void preOrderTwo(BiTree biTree){
if(biTree!=NULL){
if((*biTree).value==5){
tmpResult=biTree;
}
preOrderTwo((*biTree).lChild);
preOrderTwo((*biTree).rChild);
}
}
/*
中序前驱
q-->当前被访问的结点
pre-->上一个被访问的结点
target-->求target结点的前驱
只要target==q,pre就是target的前驱结点
最后将pre赋值给tmpResult
*/
void inPreBiTNode(BiTree biTree,BiTNode *q,BiTNode *target){
BiTNode *pre=NULL;
if(biTree!=NULL){
inPreBiTNode((*biTree).lChild,q,target);
pre=q;
q=biTree;
if(q==target){
tmpResult=pre;
}
inPreBiTNode((*biTree).rChild,q,target);
}
}
/*
中序后继
q-->当前被访问的结点
pre-->上一个被访问的结点
target-->求target结点的前驱
只要target==pre,q就是target的后继结点
最后将q赋值给tmpResult
*/
void inNextBiTNode(BiTree biTree,BiTNode *q,BiTNode *target){
BiTNode *pre=NULL;
if(biTree!=NULL){
inNextBiTNode((*biTree).lChild,q,target);
pre=q;
q=biTree;
if(pre==target){
tmpResult=q;
}
inNextBiTNode((*biTree).rChild,q,target);
}
}
/*
先序前驱
*/
void prePreBiTNode(BiTree biTree,BiTNode *q,BiTNode *target){
BiTNode *pre=NULL;
if(biTree!=NULL){
pre=q;
q=biTree;
if(q==target){
tmpResult=pre;
}
prePreBiTNode((*biTree).lChild,q,target);
prePreBiTNode((*biTree).rChild,q,target);
}
}
/*
先序后继
*/
void preNextBiTNode(BiTree biTree,BiTNode *q,BiTNode *target){
BiTNode *pre=NULL;
if(biTree!=NULL){
pre=q;
q=biTree;
if(pre==target){
tmpResult=q;
}
preNextBiTNode((*biTree).lChild,q,target);
preNextBiTNode((*biTree).rChild,q,target);
}
}
/*
后序前驱
*/
BiTNode *q3=NULL;
void postPreBiTNode(BiTree biTree,BiTNode *q,BiTNode *target){
BiTNode *pre=NULL;
if(biTree!=NULL){
postPreBiTNode((*biTree).lChild,q,target);
postPreBiTNode((*biTree).rChild,q,target);
pre=q3;
q3=biTree;
if(q3==target){
tmpResult=pre;
}
}
}
/*
后序后继
*/
void postNextBiTNode(BiTree biTree,BiTNode *q,BiTNode *target){
BiTNode *pre=NULL;
if(biTree!=NULL){
postNextBiTNode((*biTree).lChild,q,target);
postNextBiTNode((*biTree).rChild,q,target);
pre=q3;
q3=biTree;
if(pre==target){
tmpResult=q3;
}
}
}
int main(){
BiTNode *q=NULL;
BiTree root;
root=initTree(root);
root=constructTree(root);
printf("先序:\n");
preOrder(root);
printf("\n中序:\n");
inOrder(root);
printf("\n后序:\n");
postOrder(root);
printf("\n深度:\n");
printf("%d",treeDepth(root));
printf("\n");
printf("\n层次:\n");
levelOrder(root);
printf("\n");
preOrderTwo(root);
printf("\n");
inPreBiTNode(root,q,tmpResult);
printf("\n结点5的中序前驱是:%d",(*tmpResult).value);
preOrderTwo(root);
printf("\n");
inNextBiTNode(root,q,tmpResult);
printf("\n结点5的中序后继是:%d",(*tmpResult).value);
printf("\n");
printf("\n");
preOrderTwo(root);
printf("\n");
prePreBiTNode(root,q,tmpResult);
printf("\n结点5的先序前驱是:%d",(*tmpResult).value);
preOrderTwo(root);
printf("\n");
preNextBiTNode(root,q,tmpResult);
printf("\n结点5的先序后继是:%d",(*tmpResult).value);
printf("\n");
printf("\n");
preOrderTwo(root);
printf("\n");
postPreBiTNode(root,q,tmpResult);
printf("\n结点5的后序前驱是:%d",(*tmpResult).value);
preOrderTwo(root);
printf("\n");
postNextBiTNode(root,q,tmpResult);
printf("\n结点5的后序后继是:%d",(*tmpResult).value);
printf("\n");
return 0;
}
二叉树--链式存储
最新推荐文章于 2024-07-19 23:42:30 发布