Stack.h
#pragma once
#include "预定义.h"
#include <iostream>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
template<class SElemType>
struct SqStack
{
SElemType* base, * top;
int stacksize;
};
template<class SElemType>
Status InitStack(SqStack<SElemType> &S)
{
S.base = (SElemType*) malloc(STACK_INIT_SIZE * sizeof(SElemType));
if (!S.base)
exit(OVERFLOW);
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return OK;
}
template<class SElemType>
Status DestroyStack(SqStack<SElemType> &S)
{
if (!S.base)
return ERROR;
free(S.base);
S.base = S.top = NULL;
S.stacksize = 0;
return OK;
}
template<class SElemType>
Status StackEmpty(SqStack<SElemType> S)
{
if (S.top == S.base)
return TRUE;
return FALSE;
}
template<class SElemType>
Status GetTop(SqStack<SElemType> S, SElemType &e)
{
if (S.top == S.base) return ERROR;
e = *(S.top - 1);
return OK;
}
template<class SElemType>
Status Push(SqStack<SElemType> &S, SElemType e)
{
if (S.top - S.base >= S.stacksize)
{
S.base = (SElemType*) realloc(S.base,
(S.stacksize + STACKINCREMENT) * sizeof(SElemType));
if (!S.base)
exit(OVERFLOW);
S.top = S.base + S.stacksize;
S.stacksize += STACKINCREMENT;
}
*S.top++ = e;
return OK;
}
template<class SElemType>
Status Pop(SqStack<SElemType> &S, SElemType &e)
{
if (S.top == S.base)
return ERROR;
e = *--S.top;
return OK;
}
Queue.h
#pragma once
#include "预定义.h"
#include <stdio.h>
#include <stdlib.h>
#define MAXQSIZE 100
template<class T>
struct Queue {
T base[MAXQSIZE];
int front;
int rear;
};
template<class T>
int InitQueue(Queue<T>& Q)
{
Q.front = Q.rear = 0;
return OK;
}
template<class T>
int QueueLength(Queue<T> Q)
{
return Q.rear - Q.front;
}
template<class T>
int EnQueue(Queue<T>& Q, T e)
{
if (QueueLength(Q) > MAXQSIZE)
return ERROR;
Q.base[Q.rear] = e;
Q.rear++;
return OK;
}
template<class T>
int DeQueue(Queue<T>& Q, T& e)
{
if (Q.front == Q.rear)
return ERROR;
e = Q.base[Q.front];
Q.front++;
return OK;
}
template<class T>
int EmptyQueue(Queue<T> Q)
{
if (Q.front == Q.rear)
return 1;
return 0;
}
BiTree.h
#pragma once
#include "Queue.h"
#include <cstdlib>
#include <iostream>
#define MAX(x, y) ((x) > (y) ? (x) : (y))
template<class T>
struct BitNode {
T data;
BitNode<T>*Lchild, *Rchild;
};
template<class T>
using BiTree = BitNode<T>*;
template<class T>
BiTree<T> Initiate()
{
BitNode<T>* bt;
bt = (BitNode<T>*)malloc(sizeof(BitNode<T>));
if (!bt)
return 0;
bt->Lchild = bt->Rchild = NULL;
return bt;
}
template<class T>
BiTree<T> Creat(T x, BiTree<T> Lbt, BiTree<T> Rbt)
{
BitNode<T>* p;
if ((p = (BitNode<T>*)malloc(sizeof(BitNode<T>))) == NULL)
return NULL;
p->data = x;
p->Lchild = Lbt;
p->Rchild = Rbt;
return p;
}
template<class T>
BiTree<T> InsertL(BiTree<T> bt, T x, BiTree<T> parent)
{
BitNode<T>* p;
if (parent == NULL)
return NULL;
if ((p = (BitNode<T>*)malloc(sizeof(BitNode<T>))) == NULL)
return NULL;
p->data = x;
p->Lchild = p->Rchild = NULL;
p->parent = NULL;
if (parent->Lchild == NULL)
parent->Lchild = p;
else
{
p->Lchild = parent->Lchild;
parent->Lchild = p;
}
return bt;
}
template<class T>
BiTree<T> DeleteL(BiTree<T> bt, BiTree<T> parent)
{
Queue<BitNode<T>*> q;
if (!bt)
return;
EnQueue(q, bt);
while (!EmptyQueue(q))
{
BitNode<T>* aNode;
DeQueue(q, aNode);
if (!aNode->Lchild)
EnQueue(q, aNode->Lchild);
if (!aNode->Rchild)
EnQueue(q, aNode->Rchild);
free(aNode);
}
return bt;
}
template<class T>
int PreOrderTraverse(BiTree<T> bt)
{
if (bt)
{
std::cout << bt->data << ' ';
if (bt->Lchild)
PreOrderTraverse(bt->Lchild);
if (bt->Rchild)
PreOrderTraverse(bt->Rchild);
}
return 1;
}
template<class T>
int InOrderTraverse(BiTree<T> bt)
{
if (bt)
{
if (bt->lChild)
InOrderTraverse(bt->lchild);
std::cout << bt->data << ' ';
if (bt->rChild)
InOrderTraverse(bt->rchild);
}
return 1;
}
template<class T>
int PostOrderTraverse(BiTree<T> bt)
{
if (bt)
{
if (bt->lChild)
PostOrderTraverse(bt->lchild);
if (bt->rChild)
PostOrderTraverse(bt->rchild);
std::cout << bt->data << ' ';
}
return 1;
}
template<class T>
void LevelOrder(BiTree<T> bt)
{
Queue<BitNode<T>*> q;
if (!bt)
return;
EnQueue(q, bt);
while (!EmptyQueue(q))
{
BitNode<T>* aNode;
DeQueue(q, aNode);
std::cout << aNode->data;
if (!aNode->Lchild)
EnQueue(q, aNode->Lchild);
if (!aNode->Rchild)
EnQueue(q, aNode->Rchild);
}
}
template<class T>
BiTree<T> Search(BiTree<T> bt, T x)
{
BiTree<T> p = NULL;
if (bt)
{
if (bt->data == x)
return bt;
if (bt->lchild)
p = Search(bt->lchild, x);
if (p)
return p;
if (bt->rchild)
p = Search(bt->rchild, x);
if (p)
return p;
}
return NULL;
}
template<class T>
int BiTreeLeavesCount(BiTree<T> bt)
{
if (!bt)
return 0;
if (!bt->left && !bt->right)
return 1;
int nLeaf = BiTreeLeavesCount(bt->left);
nLeaf += BiTreeLeavesCount(bt->right);
return nLeaf;
}
template<class T>
int BiTreeNodeCount(BiTree<T> bt)
{
if (!bt)
return 0;
int n = 1;
n += BiTreeNodeCount(bt->left) + BiTreeNodeCount(bt->right);
return n;
}
template<class T>
int BiTreeLevelCount(BiTree<T> bt)
{
if (!bt)
return 0;
int LLevel = BiTreeLevelCount(bt->left);
int RLevel = BiTreeLeavesCount(bt->right);
return 1 + MAX(LLevel, RLevel);
}
Array.h
#pragma once
#include "预定义.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
template<class T>
struct Array {
T* base;
int dim;
int* bounds;
int* constants;
};
#define MAX_ARRAY_DIM 8
template<class T>
Status InitArray(Array<T>& A, int dim, ...)
{
if (dim < 1 || dim > MAX_ARRAY_DIM)
return ERROR;
A.dim = dim;
A.bounds = (int*)malloc(dim * sizeof(int));
if (!A.bounds)
exit(OVERFLOW);
int elemtotal = 1;
va_list ap;
va_start(ap, dim);
for (int i = 0; i < dim; i++)
{
A.bounds[i] = va_arg(ap, int);
if (A.bounds[i] < 0)
return ERROR;
elemtotal *= A.bounds[i];
}
va_end(ap);
A.base = (T*)malloc(elemtotal * sizeof(T));
if (!A.base)
exit(OVERFLOW);
A.constants[dim - 1] = 1;
for (int i = dim - 2; i >= 0; --i)
A.constants[i] = A.bounds[i + 1] * A.constants[i + 1];
return OK;
}
template<class T>
Status DestroyArray(Array<T>& A)
{
if (!A.base)
return ERROR;
free(A.base);
A.base = NULL;
if (!A.bounds)
return ERROR;
free(A.bounds);
A.bounds = NULL;
if (!A.constants)
return ERROR;
free(A.constants);
A.constants = NULL;
return OK;
}
template<class T>
Status Locate(Array<T> A, va_list ap, int& off)
{
off = 0;
for (int i = 0; i < A.dim; i++)
{
int ind = va_arg(ap, int);
if (ind < 0 || ind >= A.bounds[i])
return OVERFLOW;
off += A.constants[i] * ind;
}
return OK;
}
template<class T>
Status Value(Array<T> A, T& e, ...)
{
va_list ap;
va_start(ap, e);
int off;
Status result = Locate(A, ap, off);
if (result <= 0)
return result;
e = *(A.base + off);
return OK;
}
template<class T>
Status Assign(Array<T>& A, T e, ...)
{
va_list ap;
va_start(ap, e);
int off;
Status result = Locate(A, ap, off);
if (result <= 0)
return result;
*(A.base + off) = e;
return OK;
}