TElemType data;
struct TriTNode *lchild, *rchild;
// 左右孩子指针
struct TriTNode *parent; //双亲指针
} TriTNode, *TriTree;#include "stack.h"
#include "stack.cpp"
#include "queue.cpp"
Status CreateBiTree(BiTree &T) {
char ch;
scanf("%c",&ch);
if (ch=='# ') T = NULL;
else {
if (!(T = (BiTNode *)malloc(sizeof(BiTNode))))
printf("overflow!\n");
T->data = ch; // 生成根结点
CreateBiTree(T->lchild); // 构造左子树
CreateBiTree(T->rchild); // 构造右子树
}
return OK;
} //创建二叉树 CreateBiTree
void Preorder (BiTree T)
{ // 先序遍历二叉树
if (T) {
visit(T->data); // 访问结点
Preorder(T->lchild); // 遍历左子树
Preorder(T->rchild);// 遍历右子树
}
}
Status inorder(BiTree T,SqStack &S,SElemType p))
{//中序遍历非递归算法,s为存储二叉树结点指针栈
InitStack(S);Push(S,T);
while (!StackEmpty(S)){
while (GetTop(S,p)&& p) Push(S,p->lchild);
Pop(S,p);
if (!StackEmpty(S)){
Pop(S,p);
visit(p->data);
Push(S,p->rchild);
}//if
}//while
return OK;
}//inorder
Status PostOrderTraverse(BiTree T,Status ( * Visit)(TElemType e)){
}
int Depth (BiTree T ){ // 返回二叉树的深度
if ( !T ) depthval = 0;
else {
depthLeft = Depth( T->lchild );
depthRight= Depth( T->rchild );
depthval = 1 + (depthLeft > depthRight
depthLeft : depthRight);
}
return depthval;
}
Status LevelOrder(BiTree T){
//按层次遍历二叉树T, Q为队列
If (T!=NULL){ // 若树非空
EnQueue(Q,T); //根结点入队列
While (!QueueEmpty(Q)){
DeQueue(Q,b); //队首元素出队列
visit(b->data); //访问结点
if (b->lchild!=NULL) EnQueue(Q,b->lchild);//左子树非空,则入队列
if (b->rchild!=NULL) EnQueue(Q,b->rchild); //右子树非空,则入队列
}//while
}//if
}LevelOrder
main(){
BiTree T;
printf("创建一个二叉树:\n");
CreateBiTree(T);
return 0;
}
*************************************************************************************************************************
图
***************************************************************************************************************************
#include
#include
#include
#define MAX_VERTEX_NUM 20
#define MAX 100
#define ERROR 0
#define TRUE 1
#define OK 1
#define OVERFLOW -2
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define MAXQSIZE 20
typedef int Status;
typedef char TElemType;
typedef struct
{
int *base;
int front;
int rear;
}SqQueue;
SqQueue Q;
int e=0;
Status InitQueue (SqQueue &Q ) {
Q.base = (int *)malloc(MAXQSIZE *sizeof (int));
if(!Q.base) exit (OVERFLOW);
Q.front = Q.rear = 0;
return OK;
}
Status QueueLength (SqQueue &Q )
{
return (Q.rear - Q.front + MAXQSIZE) % MAXQSIZE;
}
Status EnQueue (SqQueue &Q , int e){
if ((Q.rear+1) % MAXQSIZE == Q.front) return ERROR;
Q.base[Q.rear] = e;
Q.rear = (Q.rear+1) %MAXQSIZE;
return OK;
}
Status DeQueue (SqQueue &Q, int &e) {
if (Q.front == Q.rear) re