我不太懂这个,忘干净了—_—,只有代码,运行是对的
main.cpp
#include <iostream>
#include "BinTree.h"
using namespace std;
int main()
{
BinTree mytree;
InitBinTree(&mytree,'#');
BinNode *p;
printf("按照先序序列构建二叉树,例如:ABC##DE##F##G#H##\n");
CreateBinTree(&mytree);
printf("先序遍历:\n");
PreOrederBinTree(&mytree);
printf("\n");
printf("中序遍历:\n");
InOrederBinTree(&mytree);
printf("\n");
printf("后序遍历:\n");
PostOrederBinTree(&mytree);
printf("\n");
printf("层次遍历:\n");
LevelOrderBinTree(&mytree);
printf("\n");
char in;
printf("请输入需查询的结点\n");
p=FindNode(&mytree,in);
if(p!=NULL)
printf("%c \n",p->data);
else
printf("无。\n");
printf("树的结点个数为:%d\n",Size(&mytree));
printf("树的深度为:%d\n",Height(&mytree));
BinNode *parent=FindParent(&mytree,p);
printf("%c的父节点是%c \n",p->data,parent->data);
printf("节点%c的左孩子是%c \n",parent->data,FindLchild(parent)->data);
printf("节点%c的右孩子是%c \n",parent->data,FindRchild(parent)->data);
BinTree youtree;
InitBinTree(&youtree,'#');
CopyTree(&youtree,&mytree);
PreOrederBinTree(&youtree);
return 0;
}
BinTree.cpp
#include "BinTree.h"
#include "Queue.h"
#include "Stack.h"
//二叉树的创建和遍历//
void InitBinTree(BinTree *bt,TElemType ref)
{
bt->root=NULL;
bt->refvalue=ref;
}
void CreateBinTree(BinTree *bt)
{
CreateBinTree(bt,bt->root);
}
void CreateBinTree(BinTree *bt,BinNode *&t)
{
TElemType Item;
Item=getchar();
if(Item==bt->refvalue)
{
t=NULL;
}
else
{
t=(BinNode *)malloc(sizeof(BinNode));
assert(t!=NULL);
t->data=Item;
CreateBinTree(bt,t->lchild);
CreateBinTree(bt,t->rchild);
}
}
void PreOrederBinTree(BinTree *bt)
{
PreOrederBinTree(bt->root);
}
void PreOrederBinTree(BinNode *t)
{
if(t!=NULL)
{
printf("%c ",t->data);
PreOrederBinTree(t->lchild);
PreOrederBinTree(t->rchild);
}
}
void InOrederBinTree(BinTree *bt)
{
InOrederBinTree(bt->root);
}
void InOrederBinTree(BinNode *t)
{
if(t!=NULL)
{
InOrederBinTree(t->lchild);
printf("%c ",t->data);
InOrederBinTree(t->rchild);
}
}
void PostOrederBinTree(BinTree *bt)
{
PostOrederBinTree(bt->root);
}
void PostOrederBinTree(BinNode *t)
{
if(t!=NULL)
{
PostOrederBinTree(t->lchild);
PostOrederBinTree(t->rchild);
printf("%c ",t->data);
}
}
void LevelOrderBinTree(BinTree *bt)
{
LevelOrderBinTree(bt->root);
}
void LevelOrderBinTree(BinNode *t)
{
if(t!=NULL)
{
BinNode *p;
LinkQueue myqueue;
InitQueue(&myqueue);
EnQueue(&myqueue,t);
while(!IsEmpty(&myqueue))
{
GetHead(&myqueue,&p);
printf("%c ",p->data);
DeQueue(&myqueue);
if(p->lchild!=NULL)
EnQueue(&myqueue,p->lchild);
if(p->rchild!=NULL)
EnQueue(&myqueue,p->rchild);
}
}
}
二叉树的基本操作练习//
//在二叉树中查找值为x的结点
BinNode * FindNode(BinTree *bt,TElemType key)
{
return FindNode(bt->root,key);
}
BinNode * FindNode(BinNode *t,TElemType key)
{
//1:t为空
if(t==NULL)
return NULL;
//2:根结点为key
if(t->data==key)
return t;
//3:递归查找,先找左子树,再找右子树
else
{
BinNode *p;
scanf("%c",&key);
p=FindNode(t->lchild,key);
if(p==NULL)
{
p=FindNode(t->rchild,key);
}
return p;
}
}
//求二叉树结点个数
int Size(BinTree *bt)
{
return Size(bt->root);
}
int Size(BinNode *t)
{
int n=0; //定义变量计算结点树数
if(t==NULL) //如果树为空,返回0
return n;
else //否则,树的结点数为左子树结点数+右子树结点树+根节点
{
return Size(t->lchild)+Size(t->rchild)+1;
}
}
//求树的高度
int Height(BinTree *bt)
{
return Height(bt->root);
}
int Height(BinNode *t)
{
int h_r,h_l;//定义变量,计算树的左、右子树高度
if(t==NULL) //如果树为空
return 0;
else //树不为空,树的高度为左右子树最大值+1
{
h_l=Height(t->lchild);
h_r=Height(t->rchild);
return (h_l>h_r?h_l:h_r)+1;
}
}
//查找二叉树中某个结点p的父节点
BinNode * FindParent(BinTree *bt,BinNode *p)
{
return FindParent(bt->root,p);
}
BinNode * FindParent(BinNode *t,BinNode *p)
{
//如果为空树,或p为空,则返回空
if(t==NULL || p==NULL)
return NULL;
if(t->lchild==p ||t->rchild==p) //如果t的左孩子或右孩子为p则,返回t
return t;
//否则,递归查找左、右子树
else
{
BinNode *q;
q=FindParent(t->lchild,p);
if(q==NULL)
{
q=FindParent(t->rchild,p);
}
return q;
}
}
//查找结点p的左孩子结点
BinNode * FindLchild(BinNode *p)
{
if(p==NULL) //如果p为空,返回空
return NULL;
else //否则,返回p的左孩子
{
return p->lchild;
}
}
//查找结点p的右孩子结点
BinNode * FindRchild(BinNode *p)
{
if(p==NULL) //如果p为空,返回空
return NULL;
else //否则,返回p的右孩子
{
return p->rchild;
}
}
//判断树空
bool IsEmptyT(BinTree *bt)
{
return bt->root==NULL; //返回根结点为空
}
//树的拷贝:用bt2拷贝bt1树
void CopyTree(BinTree *bt1,BinTree *bt2)
{
CopyTree(bt1->root,bt2->root);
}
void CopyTree(BinNode *&t1,BinNode *t2)
{
if(t2==NULL) //如果t2为空,则t1为空
t1=NULL;
else //否则,递归拷贝
{
t1=(BinNode *)malloc(sizeof(BinNode)); //为t1构建新结点
assert(t1!=NULL); //断言结点开辟成功
t1->data=t2->data; //赋结点值
CopyTree(t1->lchild,t2->lchild); //递归拷贝左子树
CopyTree(t1->rchild,t2->rchild); //递归拷贝右子树
}
}
// 清除树
void ClearTree(BinTree *bt)
{
ClearTree(bt->root);
}
void ClearTree(BinNode *&t)
{
//如果不为空,则先释放左树,再释放右树,最后根
if(t!=NULL)
{
ClearTree(t->lchild);
ClearTree(t->rchild);
free(t);
t=NULL;
}
}
BinTree.h
#ifndef BINTREE_H_INCLUDED
#define BINTREE_H_INCLUDED
#include <stdio.h>
#include <malloc.h>
#include <assert.h>
typedef char TElemType;
typedef struct BinNode
{
TElemType data;
struct BinNode *lchild;
struct BinNode *rchild;
}BinNode;
typedef struct BinTree
{
BinNode *root;
TElemType refvalue;
}BinTree;
//二叉树的创建和遍历//
void InitBinTree(BinTree *bt,TElemType ref);
void CreateBinTree(BinTree *bt);
void CreateBinTree(BinTree *bt,BinNode *&t);
void PreOrederBinTree(BinTree *bt);
void PreOrederBinTree(BinNode *t);
void InOrederBinTree(BinTree *bt);
void InOrederBinTree(BinNode *t);
void PostOrederBinTree(BinTree *bt);
void PostOrederBinTree(BinNode *t);
void LevelOrderBinTree(BinTree *bt);
void LevelOrderBinTree(BinNode *t);
二叉树的基本操作//
//在二叉树中查找值为x的结点
//求二叉树结点个数
//求树的高度
//查找二叉树中某个结点p的父节点
//查找结点p的左孩子结点
//查找结点p的右孩子结点
//判断树空
//树的拷贝:用bt2拷贝bt1树
// 清除树
二叉树的基本操作练习//
//在二叉树中查找值为x的结点
BinNode * FindNode(BinTree *bt,TElemType key);
BinNode * FindNode(BinNode *t,TElemType key);
//求二叉树结点个数
int Size(BinTree *bt);
int Size(BinNode *t);
//求树的高度
int Height(BinTree *bt);
int Height(BinNode *t);
//查找二叉树中某个结点p的父节点
BinNode * FindParent(BinTree *bt,BinNode *p);
BinNode * FindParent(BinNode *t,BinNode *p);
//查找结点p的左孩子结点
BinNode * FindLchild(BinNode *p);
//查找结点p的右孩子结点
BinNode * FindRchild(BinNode *p);
//判断树空
bool IsEmptyT(BinTree *bt);
//树的拷贝:用bt2拷贝bt1树
void CopyTree(BinTree *bt1,BinTree *bt2);
void CopyTree(BinNode *&t1,BinNode *t2);
// 清除树
void ClearTree(BinTree *bt);
void ClearTree(BinNode *&t);
#endif // BINTREE_H_INCLUDED
Queue.h
#ifndef QUEUE_H_INCLUDED
#define QUEUE_H_INCLUDED
#include <stdio.h>
#include <malloc.h>
#include <assert.h>
struct BinNode;
#define ElemType BinNode *
typedef struct QNode //用单链表模拟单链式队列
{
ElemType data;
struct QNode *next;
}QNode;
typedef struct LinkQueue
{
QNode *front; //队头指针
QNode *tail;//队尾指针
}LinkQueue;
void InitQueue(LinkQueue *Q);
void EnQueue(LinkQueue *Q,ElemType x);
void ShowQueue(LinkQueue *Q);
void DeQueue(LinkQueue *Q);
void GetHead(LinkQueue *Q,ElemType *v);
int Length(LinkQueue *Q);
void Clear(LinkQueue *Q);
void Destroy(LinkQueue *Q);
bool IsEmpty(LinkQueue *Q);
void InitQueue(LinkQueue *Q)
{
QNode *s=(QNode *)malloc(sizeof(QNode));//申请单列表的头节点
Q->front=Q->tail=s;
Q->tail->next=NULL;
}
void EnQueue(LinkQueue *Q,ElemType x)
{
QNode *s=(QNode *)malloc(sizeof(QNode));
assert(s!=NULL);
s->data=x;
s->next=NULL;
Q->tail->next=s;
Q->tail=s;
}
void ShowQueue(LinkQueue *Q)
{
QNode *p=Q->front->next;
while(p!=NULL)
{
printf("%c>",p->data);
p=p->next;
}
printf("<:TAL\n");
}
void DeQueue(LinkQueue *Q) //出队操作,相当一头不删除
{
if(Q->front == Q->tail)
return;
QNode *p=Q->front->next;
if(p->next==NULL) //或者:if(p==Q->tail)
Q->tail=Q->front;
Q->front->next=p->next;
free(p);
}
void GetHead(LinkQueue *Q,ElemType *v) //获取对头元素
{
if(Q->front==Q->tail)
return;
QNode *p=Q->front->next;
*v=p->data; //一般链表中最好不要超过两个指向。
}
int Length(LinkQueue *Q)
{
int size=0;
QNode *p=Q->front->next;
while(p!=NULL)
{
size++;
p=p->next;
}
return size;
}
void Clear(LinkQueue *Q)
{
if(Q->front==Q->tail)
return;
QNode *p=Q->front->next;
while(p!=NULL)
{
Q->front->next=p->next;
free(p);
p=Q->front->next;//注意,将p重新指向第一个节点
}
Q->tail=Q->front;
}
void Destroy(LinkQueue *Q)
{
Clear(Q);
free(Q->front);
Q->front=Q->tail=NULL;
}
bool IsEmpty(LinkQueue *Q)
{
return Q->front==Q->tail;
}
#endif // QUEUE_H_INCLUDED
Stack.h
#ifndef STACK_H_INCLUDED
#define STACK_H_INCLUDED
#define Etype BinNode *
#define STACK_INIT_SIZE 8
#define STACK_INC_SIZE 3
struct BinNode;
typedef struct SeqStack
{
Etype *base; //指向整个栈的空间
int capacity;//栈空间的最大容量
int top; //栈顶位置(下标)及当前元素个数
}SeqStack;
bool Inc(SeqStack *s);//当栈满是增加空间
void InitStack(SeqStack *s);
bool IsFull(SeqStack *st);//判断栈是否已满
bool IsEmpty(SeqStack *st);//判断栈是否为空
void Push(SeqStack *s,Etype x);
void Show(SeqStack *s);
void Pop(SeqStack *s);
bool GetTop(SeqStack *s,Etype *v); //获得栈顶元素,注意*v返回元素的地址
int Length(SeqStack *s);
void Clear(SeqStack *s);
void Destroy(SeqStack *s);
bool Inc(SeqStack *s) //增加空间
{
//虽然名义上是增加空间,实际上市重新开辟更大的空间
Etype *newbase=(Etype *)realloc(s->base,sizeof(Etype)*(s->capacity+STACK_INC_SIZE)); //利用realloc函数
if(newbase == NULL)
{
printf("内存不足,无法申请空间!\n");
return false;
}
s->base=newbase;//申请成功,重置栈的基地址
s->capacity+=STACK_INC_SIZE;
return true;
}
void InitStack(SeqStack *s)
{
s->base=(Etype *)malloc(sizeof(Etype)*STACK_INIT_SIZE);
assert(s->base !=NULL);
s->capacity=STACK_INIT_SIZE;
s->top=0;
}
bool IsFull(SeqStack *st)//判断栈是否满
{
return st->top>=st->capacity;
}
bool IsEmpty(SeqStack *st)//判断栈是否为空
{
return st->top==0;
}
void Push(SeqStack *s,Etype x)
{
if(IsFull(s)&&!Inc(s))
{
printf("栈已满,%c无法入栈。\n",x);
return;
}
s->base[s->top]=x; //注意top的用法,它是结构体中的一个元素,不能直接用top表示
s->top++;
//上面两条语句可以简化为:s->base[s->top++]=x;
}
void Show(SeqStack *s)
{
while(!IsEmpty(s))
{
printf("%d\n",s->base[--s->top]);//注意:top--相当于出栈,逻辑不正确。
}
for(int i=s->top-1;i>=0;i--)
{
printf("%c\n",s->base[i]);
}
}
void Pop(SeqStack *s)
{
if(IsEmpty(s))
{
printf("栈已空,不能出栈!\n");
}
s->top--;
}
bool GetTop(SeqStack *s,Etype *v)
{
if(IsEmpty(s))
{
printf("栈已空,不能取元素!\n");
return false;
}
*v=s->base[s->top-1];
return true;
}
int Length(SeqStack *s) //求栈元素的个数
{
return s->top;
}
void Clear(SeqStack *s)
{
s->top=0;
}
void Destroy(SeqStack *s)
{
free(s->base);
s->base=NULL;
s->top=s->capacity=0;
}
#endif // STACK_H_INCLUDED
就是这样,我不太明白为什么图需要队列和栈的头文件呢?