- 栈
- 二叉树
- 非递归遍历
stack.h
#ifndef STACK_H
#define STACK_H
#include"BinaryTree.h"
#define STACKSIZE 100
typedef BtNode * StackType;
typedef struct //定义栈结构
{
StackType * data; //存放结点的地址
int top; //栈顶
int maxsize; //栈最大容量
}Stack;
void Init_Stack(Stack *sp); //初始化
bool full(Stack *sp); //满
bool empty(Stack *sp); //空
bool push(Stack *sp, StackType x); //入栈
StackType top(Stack *sp); //返回栈顶的地址
void pop(Stack *sp); //出栈
#endif
BinaryTree.h
#ifndef BINARYTREE_H
#define BINARYTREE_H
typedef char ElemType; // 定义数据为char
typedef struct BtNode // BinaryTreeNode //定义结点类型
{
BtNode *leftchild;
BtNode *rightchild;
ElemType data;
}BtNode, *BinaryTree;
struct RetNode //下文函数 返回两个孩子 及路长
{
BtNode *child1;
BtNode *child2;
int path;
};
BtNode * Buynode(); //申请结点
void Freenode(BtNode *p); //释放结点
BtNode * FindNearParent(BtNode *ptr,BtNode *child1,BtNode *child2); //寻找两个结点的最近公共双亲
int MaxPath(BtNode *ptr); //计算最远的路长
RetNode RetTwoPMaxPath(BtNode *ptr); //返回最远的两个结点及路长
int RetTwoPMaxPath(BtNode *ptr,BtNode *&child1,BtNode *&child2); //求指定两结点的路长
int SizeBinaryBrch(BtNode *ptr); //双分支结点的个数
int SizeOneBrch(BtNode *ptr); //单分支结点的个数
int SizeOneLBrch(BtNode *ptr); //左单分支结点的个数
int SizeOneRBrch(BtNode *ptr); //右单分支结点的个数
void NicePerOrder(BtNode *ptr); //非递归前序
void NiceInOrder(BtNode *ptr); //非递归中序
void NicePastOrder(BtNode *ptr); //非递归后序
bool Is_Full_BinaryTree(BtNode *ptr); //满
bool Is_Comp_BinaryTree(BtNode *ptr); //完全
bool Is_Balance_BinaryTree(BtNode *ptr); //平衡
#endif
Std.h
#ifndef STD_H
#define STD_H
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
int Max(int a,int b);
#endif
Std.cpp
#include"Std.h"
int Max(int a,int b)
{
return a>b? a:b;
}
Stack.cpp
#include"Std.h"
#include"Stack.h"
#define STACKBASE -1
void Init_Stack(Stack *sp)
{
if(sp == NULL) return ;
sp->top = -1;
sp->maxsize = STACKSIZE;
sp->data = (StackType*)malloc(sizeof(StackType)*sp->maxsize);
memset(sp->data,0,sizeof(StackType)*sp->maxsize); //memset 按字节初始化 0
}
bool full(Stack *sp) //满
{
if(sp == NULL || sp->maxsize == sp->top +1)
return true;
else
return false;
}
bool empty(Stack *sp) //空
{
if(sp == NULL || sp->maxsize == STACKBASE)
return true;
else
return false;
}
bool push(Stack *sp, StackType x) //入栈
{
bool res = false;
if(!full(sp))
{
sp->data[++sp->top] = x;
res = true;
}
return res;
}
StackType top(Stack *sp) //返回栈顶的地址
{
return sp->data[sp->top];
}
void pop(Stack *sp) //出栈
{
if(sp != NULL && sp->top >= 0)
{
sp->top--;
}
}
BinaryTree.cpp
#include"Std.h"
#include"BinaryTree.h"
#include"Stack.h"
BtNode * Buynode()
{
BtNode *s = (BtNode*)malloc(sizeof(BtNode));
if(s == NULL) exit(1);
memset(s,0,sizeof(BtNode));
return s;
}
void Freenode(BtNode *p)
{
free(p);
}
//非递归 前序
void NicePerOrder(BtNode *ptr)
{
if(NULL == ptr) return ; // 避免少写==将NULL赋值给ptr
Stack st;
Init_Stack(&st); //top=-1
push(&st,ptr); //将头入栈
while(!empty(&st))
{
ptr = top(&st); //返回栈顶的地址
pop(&st); //出栈
printf("%c ",ptr->data);
if(ptr->rightchild != NULL) //右孩子入栈
push(&st,ptr->rightchild);
if(ptr->leftchild != NULL) //左孩子入栈
push(&st,ptr->leftchild);
}
}
//非递归中序遍历
void NiceInOrder(BtNode *ptr)
{
if(ptr == NULL) return ;
Stack st; // BtNode * 栈中放指针
Init_Stack(&st);
while(ptr != NULL || !empty(&st))
{
while(ptr != NULL)
{
push(&st,ptr); //入栈
ptr = ptr->leftchild; //有左孩子即入栈
}
ptr = top(&st); //遍历完左孩子 得栈顶元素
pop(&st); //出栈
printf("%c ",ptr->data);
ptr = ptr->rightchild; //赋值 开始遍历右孩子
}
}
//非递归后序遍历
void NicePastOrder(BtNode *ptr)
{
if(ptr == NULL) return ;
Stack st; // BtNode *;
Init_Stack(&st);
BtNode *tag = NULL;
while(ptr != NULL || !empty(&st))
{
while(ptr != NULL)
{
push(&st,ptr);
ptr = ptr->leftchild; //遍历 左
}
ptr = top(&st); //出栈顶
pop(&st);
if(ptr->rightchild == NULL || ptr->rightchild == tag)
{
printf("%c ",ptr->data);
tag = ptr;
ptr = NULL;
}
else
{
push(&st,ptr);
ptr = ptr->rightchild;
}
}
}