#include <iostream>
using namespace std;
//二叉链表
struct BinNode
{
int data;
struct BinNode * lchild, *rchild;
};
typedef struct BinNode BinNode;
typedef struct BinNode * BinTree;
//三叉链表 多了一个指向双亲节点指针,可以很快访问到双亲
struct BinTNode
{
int data;
struct BinTNode * lchild, *rchild;
struct BinTNode * parent;
};
//先序遍历
void Preindex(BinNode * root)
{
if (root == NULL)
{
return;
}
printf("%d ", root->data);
Preindex(root->lchild);
Preindex(root->rchild);
}
//中序遍历
void Middleindex(BinNode * root)
{
if (root == NULL)
{
return;
}
Middleindex(root->lchild);
printf("%d ", root->data);
Middleindex(root->rchild);
}
//后序遍历
void Lastindex(BinNode * root)
{
if (root == NULL)
{
return;
}
Lastindex(root->lchild);
Lastindex(root->rchild);
printf("%d ", root->data);
}
int main01()
{
BinNode t1, t2, t3, t4, t5;
memset(&t1, 0, sizeof(t1));
memset(&t2, 0, sizeof(t2));
memset(&t3, 0, sizeof(t3));
memset(&t4, 0, sizeof(t4));
memset(&t5, 0, sizeof(t5));
t1.data = 1;
t2.data = 2;
t3.data = 3;
t4.data = 4;
t5.data = 5;
t1.lchild = &t2;
t1.rchild = &t3;
t2.lchild = &t4;
t3.rchild = &t5;
//遍历
Preindex(&t1);
cout << endl;
Middleindex(&t1);
cout << endl;
Lastindex(&t1);
cout << endl;
return 0;
}
int main02()
{
BinNode * t1,* t2,* t3,* t4,* t5;
t1 = (BinNode *)malloc(sizeof(BinNode));
t2 = (BinNode *)malloc(sizeof(BinNode));
t3 = (BinNode *)malloc(sizeof(BinNode));
t4 = (BinNode *)malloc(sizeof(BinNode));
t5 = (BinNode *)malloc(sizeof(BinNode));
memset(t1, NULL, sizeof(BinNode)); //必须初始化,否则会报错
memset(t2, NULL, sizeof(BinNode));
memset(t3, NULL, sizeof(BinNode));
memset(t4, NULL, sizeof(BinNode));
memset(t5, NULL, sizeof(BinNode));
t1->data = 1;
t2->data = 2;
t3->data = 3;
t4->data = 4;
t5->data = 5;
t1->lchild = t2;
t1->rchild = t3;
t2->lchild = t4;
t3->rchild = t5;
Preindex(t1);
cout << endl;
Middleindex(t1);
cout << endl;
Lastindex(t1);
cout << endl;
free(t1);
free(t2);
free(t3);
free(t4);
free(t5);
return 0;
}
//双亲链表(数组表示)
typedef struct TwoBinNode //定义节点,结点结构
{
int data;
struct TwoBinNode* parent; //指向parent
int Flag; //0为左孩子,1为右孩子
}TwoBinNode;
typedef struct TwoBinTree //树结构
{
TwoBinNode *node;
int num;
TwoBinNode *root;
}TwoBinTree;
//双亲链表(数组表示) 建树
int main()
{
TwoBinTree * tree = (TwoBinTree *)malloc(sizeof(TwoBinTree));
tree->node = (TwoBinNode *)malloc(sizeof(TwoBinNode) * 100);
tree->num = 0;
tree->root = NULL;
memset(tree->node, NULL, sizeof(TwoBinNode) * 100);
tree->node[0].data = 1;
tree->node[0].parent = NULL;
tree->node[0].Flag = NULL;
tree->root = &tree->node[0];
tree->num++;
tree->node[1].data = 2;
tree->node[0].parent = &tree->node[0];
tree->node[0].Flag = 0;
tree->num++;
tree->node[2].data = 3;
tree->node[0].parent = &tree->node[0];
tree->node[0].Flag = 1;
tree->num++;
tree->node[3].data = 4;
tree->node[0].parent = &tree->node[1];
tree->node[0].Flag = 0;
tree->num++;
tree->node[4].data = 5;
tree->node[4].parent = &tree->node[2];
tree->node[0].Flag = 1;
tree->num++;
for (int i = 0; i < tree->num; i++)
{
cout << tree->node[i].data << " ";
}
cout << endl;
return 0;
}
理论学习参考链接:https://blog.csdn.net/w_linux/article/details/78473491