1.两种结构转换
#define MAXSIZE 100
//双亲表示法
typedef struct PTNode
{
char data;
int parent;
}PTNode;
typedef struct PTNode *pNode;
typedef struct PTree
{
PTNode *nodes;//也可以:pNode nodes[maxsize]
int r, n;
}PTree, *pTree;
int IniPTree(pTree T)
{
T->n = 0;
return 0;
}
int CreatePTree(pTree T)
{
if (T == NULL)
return -1;
T->r = -1;
T->nodes = (pNode)(malloc(sizeof(struct PTNode)*MAXSIZE));
if (T->nodes == NULL)
return -1;
IniPTree(T);
int tmpint;
printf_s("input data count\n");
scanf_s("%d", &tmpint);
getchar();
T->n = tmpint;
for (int i = 0; i < T->n; i++)
{
int tmpindex;
char tmpdata;
printf_s("\n");
printf_s("input data%d:\n", i);
scanf_s(" %c", &tmpdata);
getchar();
fflush(stdin);
printf_s("input parent index%d:\n", i);
scanf_s("%d", &tmpindex);
getchar();
fflush(stdin);
T->nodes[i].data = tmpdata;
T->nodes[i].parent = tmpindex;
printf_s("T->node[%d]:%c,parent:%d,add:%d\n", i, T->nodes[i].data, T->nodes[i].parent, &(T->nodes[i]));
}
printf_s("Finished\n");
return 0;
}
typedef struct List_Node* listNode;
struct List_Node
{
pNode node;
listNode next;
};
listNode FindPTreeChilds(pTree T, int parentindex)
{
listNode listtmp=NULL;
for (int i = 0; i < T->n; i++)
{
if (T->nodes[i].parent==parentindex)
{
if (listtmp == NULL)
{
listtmp= (listNode)(malloc(sizeof(struct List_Node)));
listtmp->node = (pNode)(malloc(sizeof(struct List_Node)));
listtmp->node->data = T->nodes[i].data;
listtmp->node->parent = T->nodes[i].parent;
listtmp->next = NULL;
}
else
{
listNode tmp = (listNode)(malloc(sizeof(struct PTNode)));
pNode tmpNode = (pNode)(malloc(sizeof(struct PTNode)));
tmpNode->data = T->nodes[i].data;
tmpNode->parent = T->nodes[i].parent;
tmp->node = tmpNode;
tmp->next = listtmp->next;
listtmp->next = tmp;
}
}
}
return listtmp;
}
int GetListlength(listNode list)
{
if (list != NULL)
{
return 1 + GetListlength(list->next);
}
return 0;
}
//孩子表示法(链表)
typedef struct CTNode* ct_pNode;
struct CTNode
{
int index;
ct_pNode next;
};
typedef struct CTBox* ct_pBox;
typedef struct CTBox
{
char data;
ct_pNode fistchild;
}CTBox;
typedef struct CTree* ctree;
struct CTree
{
CTBox *boxs;
int r;
int n;
};
//查找辅助函数
char FindCharOfPTree(int index, pTree T)
{
if (T->nodes[index].data != NULL)
{
return T->nodes[index].data;
}
else
{
return NULL;
}
}
int FindIndexOfPTree(char c, pTree T)
{
for (int i = 0; i < T->n; i++)
{
if (T->nodes[i].data == c)
{
return i;
}
}
return -1;
}
int FindIndexOfCTree(char c, ctree T)
{
for (int i = 0; i < T->n; i++)
{
if (T->boxs[i].data == c)
{
return i;
}
}
return -1;
}
void CreateCTreeByCopyPTree( ctree cT, pTree pT)
{
int n = pT->n;
cT->n = n; cT->r = -1;
cT->boxs = NULL;
if (n > 0)
{
cT->boxs = (ct_pBox)(malloc(sizeof(struct CTBox)*n));
}
for (int i = 0; i < n; i++)
{
cT->boxs[i].data = FindCharOfPTree(i,pT);//按存储在Pt.Nodes的存储顺序对ct的data编号
}
for (int j = 0; j < n; j++)
{
char value = cT->boxs[j].data;
int indexinPT = FindIndexOfPTree(value,pT);
listNode list = FindPTreeChilds(pT,indexinPT);
listNode li = list;
ct_pNode linkresult=NULL;
while (li != NULL)
{
ct_pNode link = (ct_pNode)(malloc(sizeof(struct CTNode)));
link->index = FindIndexOfCTree(li->node->data,cT);
if (linkresult == NULL)
{
linkresult = link;
linkresult->next = NULL;
}
else
{
link->next=linkresult->next;
linkresult->next = link;
}
li = li->next;
}
cT->boxs[j].fistchild=linkresult;
}
}
void PrintChildTree(char c,ctree T)
{
for (int i = 0; i < T->n; i++)
{
if (T->boxs[i].data == c)
{
int index = FindIndexOfCTree(c, T);
printf_s("Finded data %c ,No. %d\n", T->boxs[i].data, index);
printf_s("Child datas:\n");
ct_pNode link = T->boxs[i].fistchild;
while (link!=NULL)
{
int ind = link->index;
char ch = T->boxs[ind].data;
printf_s("%c,No.%d\t", ch, ind);
link = link->next;
}
}
}
}
2.查找和层数、层序遍历
void Find(int x, pTree root,pTree* result)
{
if (root == NULL)
return;
Find(x, root->Left,result);
if (root->Element == x)
{
*result = root;
}
Find(x, root->Right,result);
}
void FindParents(int x, pTree root, pTree* parent)//若x在根节点,则对pTree* parent无操作
{
if (root == NULL)
return;
FindParents(x, root->Left, parent);
if ((root->Left) != NULL)
{
if ((root->Left)->Element == x)
{
*parent = root;
}
}
if ((root->Right) != NULL)
{
if ((root->Right)->Element == x)
{
*parent = root;
}
}
FindParents(x, root->Right, parent);
}
int FindDepth(int x, pTree root)
{
int i = 1;
if (x == root->Element)
return i;
pTree* tmp = (pTree*)(malloc(sizeof(pTree)));
Find(x, root, tmp);
while ((*tmp)->Element != root->Element)
{
FindParents((*tmp)->Element, root, tmp);
i++;
}
return i;
}
int NodesCount(pTree root,int* count)
{
if (root == NULL)
return;
NodesCount(root->Left,count);
(*count)++;
NodesCount(root->Right,count);
}
typedef struct LinkNode;
typedef struct LinkNode* plist;
struct LinkNode
{
int Element;
int Depth;
plist Next;
};
void Insert(int x, plist list)
{
plist tmp = (plist)(malloc(sizeof(struct LinkNode)));
tmp->Element = x;
tmp->Next = list->Next;
list->Next = tmp;
}
void InOrderRead(pTree root,plist list)
{
if (root == NULL)
return;
InOrderRead(root->Left, list);
Insert(root->Element, list);
InOrderRead(root->Right, list);
}
void ReadByDepth(pTree root, plist list,int count)
{
plist listp = list->Next;
int maxDepth = 0;
while (listp!=NULL)
{
listp->Depth = FindDepth(listp->Element,root);
if (listp->Depth > maxDepth)
maxDepth = listp->Depth;
listp = listp->Next;
}
listp = list;
for (int i = 1; i <= maxDepth; i++)
{
listp = list;
printf_s("Depth %d has elements:\t",i);
while (listp != NULL)
{
if (listp->Depth == i)
printf_s("%d\t", listp->Element);
listp = listp->Next;
}
printf_s("\n");
}
}