资源简介
广义表与森林相互转换,森林与二叉树相互转换,二叉树与遍历序列(先序/中序)相互转换,森林先根遍历和后根遍历

代码片段和文件信息
#include
#include
#include
#include
using namespace std;
//存有孩子结点索引的单链表结点
typedef struct TreelinkNode
{
int index;
struct TreelinkNode *next;
} TreelinkNode*pTreelinkNode;
//孩子链表结点(树结点)
typedef struct TreeNode
{
pTreelinkNode link;
int data;
} TreeNode*pTreeNode;
//森林结点
typedef struct FTreeNode
{
pTreeNode tree;
struct FTreeNode *next;
} FTreeNode*pFTreeNode;
//森林链表的初始化操作
void InitFTree(pFTreeNode &fTree)
{
fTree = (pFTreeNode)malloc(sizeof(FTreeNode));
fTree->next = NULL;
}
//森林链表的插入操作
void InsertFTree(pFTreeNode &fTreepTreeNode node)
{
pFTreeNode t_node = fTree;
pFTreeNode newnode = (pFTreeNode)malloc(sizeof(FTreeNode));
while(t_node->next != NULL)
{
t_node = t_node->next;
}
newnode->tree = node;
t_node->next = newnode;
newnode->next = NULL;
}
//广义表建立树链表(孩子链表)
void GListToTree(pTreeNode &treeint ¤t_indexint parent_index)
{
char ch;
pTreelinkNode newnode;
pTreelinkNode link = (pTreelinkNode)malloc(sizeof(TreelinkNode));
link->index = current_index;
link->next = NULL;
tree[parent_index].link = link;
while(true)
{
scanf(“%c“&ch);
if(ch == ‘(‘)
{
parent_index = current_index;
current_index ++;
scanf(“%d“&tree[current_index].data);
tree[current_index].link = NULL;
GListToTree(treecurrent_indexparent_index);
}
else if(ch == ‘‘)
{
current_index ++;
scanf(“%d“&tree[current_index].data);
tree[current_index].link = NULL;
newnode = (pTreelinkNode)malloc(sizeof(TreelinkNode));
newnode->index = current_index;
newnode->next = NULL;
link->next = newnode;
link = link->next;
}
else
{
return;
}
}
}
//广义表组建立森林链表
void GListToFTree(pFTreeNode &fTree)
{
char ch;
int current_indexparent_index;
pTreeNode tree;
scanf(“%c“&ch);
while(ch != ‘#‘)
{
current_index = -1;
tree=(pTreeNode)malloc(50*sizeof(TreeNode));
current_index ++;
scanf(“%d“&tree[current_index].data);
tree[current_index].link = NULL;
scanf(“%c“&ch);
if(ch != ‘)‘)
{
parent_index = current_index;
current_index ++;
scanf(“%d“&tree[current_index].data);
tree[current_index].link = NULL;
GListToTree(treecurrent_indexparent_index);
scanf(“%c“&ch);
}
InsertFTree(fTreetree);
scanf(“%c“&ch);
}
}
//存储先序遍历序列记录的单链表结点
typedef struct linkNode
{
int data;
struct linkNode *next;
} linkNode*plinkNode;
//单链表的初始化
void InitlinkList(plinkNode &link)
{
link=(plinkNode)ma
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
I.A.... 14183 2012-03-22 22:13 树与二叉树\main.cpp
I..D... 0 2012-04-08 11:11 树与二叉树
----------- --------- ---------- ----- ----
14183 2
- 上一篇:用汇编编写的10个数的冒泡排序
- 下一篇:基于专家知识的决策树分类
相关资源
- 基于MFC的文件目录树的对话框实现代
- 基于最小生成树的全局优化立体匹配
- B+树的源代码
- 基于八叉树的网格简化算法实现
- tiny4412上移植linux4.4支持设备树的最终
- B-树的一些基本操作
- 数据结构课程设计,基于二叉排序树
- 红黑树的实现
- 基于二叉排序数的个人通信录附课程
- 根据二叉树的抽象数据类型的定义,
- btree.zip实现二叉树的可视化处理,很
- 实现二叉树的可视化处理,很好的源
- 《数据结构与算法》第四次课内容安
- AVLTree的实现与分析
- 数据结构综合课设二叉树的建立与遍
- 数据结构实验报告8-树-求二叉树先序
- 树的基本运算:创建树;输出树凹入
- 编译原理语法树的实现
- ——搜索二叉树的插入,查找和删除
- Visio 画《机器学习实战》第三章决策
- 二叉树的实现各种遍历算法
- 哈夫曼树的基本操作
- unity 数据结构树的深度遍历
- AVL树的实现-插入和删除结点,并拥有
- 数据结构课程设计二叉树的非递归遍
- AVL树的动态演示与快速构建
- C数据结构最小生成树的构造
- 决策树的R语言实现
- 数据结构关于二叉树的各种算法
- 数据结构-B树的完整实现
评论
共有 条评论