-双亲表示法
const int MaxSize = 10;
typedef int DataType;
typedef struct
{
DataType data;
int parent;
} PNode;
typedef struct
{
PNode Data[MaxSize];
int treeNum;
} PTree;
const int MaxSize = 10;
typedef int DataType;
typedef struct CTNode // 定义孩子结点
{
int child;
struct CTNode *next;
} CTNode;
Typedef struct // 定义表头结点
{
DataType data;
CTNode *firstchild;
} CBNode;
typedef struct // 定义孩子表示法
{
CBNode data[MaxSize];
int treeNum;
} CTTree;
typedef int DataType;
typedef struct TNode
{
DataType data;;
struct Tnode *firstchild, *rightsib;
} Tnode;
const int MaxHigh = 10;
typedef int DataType;
typedef struct
{
DataType data[2^MaxHigh - 1];
int biTreeNum;
} SeqBiTree;
typedef int DataType;
typedef struct BiNode
{
DataType Data;
struct BiNode *lchild, *rchild;
} BiNode, *root;
// 前序遍历
void PreOrder(BiNode *root)
{
if(root == NULL)
return;
else {
printf(root->data);
PreOrder(root->lchild);
PreOrder(root->rchild);
}
}
// 后序遍历
void PostOrder(BiNode *root)
{
if(root == NULL)
return;
else {
PreOrder(root->lchild);
PreOrder(root->rchild);
printf(root->data);
}
}
// 层序遍历,使用队列Q
void LevelOrder(BiNode *root)
{
front=rear = -1;
if(root == NULL)
return;
Q[++rear] = root;
while( front != rear ) {
q = Q[++front];
printf(q->data);
if (q->lchild != NULL )
Q[++rear] = q->lchild;
if (q->rchild != NULL )
Q[++rear] = q->rchild;
}
}
// pre[n]、pin[n]中分别保存二叉树的前序序列和中序序列
// pos(x, pin, i)返回数组pin中从第i个元素开始等于x的位置
BiNode* Create(BiNode *root, int i1, int i2, int k)
{
if(K <= 0)
root = NULL;
else {
root = (BiNode *)malloc(sizeof(BiNode));
root->data = pre[i1];
m = pos(pre[i1], pin, i2);
leftlen = m - i2;
rightlen = k - (leftlen + 1);
root->lchild = Creat(root->lchild, i1+1, i2, leftlen);
root->rchild = Creat(root->rchild, i1+leftlen+1, i2, rightlen);
}
return root;
}
todo