C语言之一般树
1、一般树

将这种一般的树转化成我们熟悉的单链表形式,这有三层,每一层都可以看成单链表或者多个分散的单链表
数据节点如下:
struct tree {
int elem;
struct tree *FirstChild;
struct tree *NextBro;
};
每个节点和第一个孩子还有下一个兄弟链接
#include <stdio.h>
#include <stdlib.h> struct tree {
int elem;
struct tree *FirstChild;
struct tree *NextBro;
}; struct tree *root_ptr = NULL; /* 知道第一个孩子的位置,将要添加的节点放到链尾 */
int register_child(struct tree **first, struct tree *tree_ptr)
{
struct tree *ptr = *first;
while (ptr->NextBro)
ptr = ptr->NextBro; ptr->NextBro = tree_ptr;
return ;
} /* 3层树
* floor: 要添加的链表位于第几层
* FirstFa: 是第一层第几个节点的孩子
* num:节点的值
*/
int add_tree(int floor, int FirstFa, int num)
{
struct tree *tree_ptr = (struct tree *)calloc(, sizeof(struct tree));
if (!tree_ptr) {
printf("calloc error\n");
return -;
}
if (!root_ptr) {
if (floor == )
root_ptr = tree_ptr;
}
else {
if (floor == ) {
printf("root really exist\n");
goto error;
}
else if (floor == ) {
if (!(root_ptr->FirstChild))
root_ptr->FirstChild = tree_ptr;
else
register_child(&(root_ptr->FirstChild), tree_ptr);
}
else if (floor == ) {
int i;
struct tree *last_fa = root_ptr->FirstChild;
if (!last_fa) {
printf("no first floor\n"); //第1层没有
goto error;
}
for (i = ; i < FirstFa; i++)
last_fa = last_fa->NextBro;
if (!last_fa) {
printf("your father No exist\n"); //对应的父节点没有
goto error;
}
if (!(last_fa->FirstChild))
last_fa->FirstChild = tree_ptr;
else
register_child(&(last_fa->FirstChild), tree_ptr);
}
}
tree_ptr->elem = num;
tree_ptr->FirstChild = NULL;
tree_ptr->NextBro = NULL;
return ;
error:
free(tree_ptr);
return -;
} /* 输出该节点和节点下的所以数据 */
int output_fa_and_child(struct tree *fa)
{
static int cnt = ;
printf("data %d : %d\n", cnt++, fa->elem);
struct tree *vy = fa->FirstChild;
while (vy) {
output_fa_and_child(vy); //递归调用
vy = vy->NextBro;
}
return ;
} /* 输出树中的所有数据 */
int output_tree_data(void)
{
if (!root_ptr) {
printf("no data\n");
return -;
}
output_fa_and_child(root_ptr);
return ;
} int main()
{
int i;
int ret;
/* 向树中添加10个节点 */
int num[] = { ,,,,,,,,,,, };
ret = add_tree(, , num[]);
if (ret < ) {
printf("add_tree error\n");
}
for (i = ; i < ; i++) {
ret = add_tree(, , num[i]);
if (ret < ) {
printf("add_tree error\n");
}
}
for (i = ; i < ; i++) {
ret = add_tree(, , num[ + i]);
if (ret < ) {
printf("add_tree error\n");
}
}
/* 输出所有节点中的数据 */
ret = output_tree_data();
if (ret < )
printf("output_tree_data error\n");
return ;
}
填充树后的图如下:

输出数据顺序是1、2、3、6、7、8、9、0、4、5

C语言之一般树的更多相关文章
- 2021.11.09 P2292 [HNOI2004]L语言(trie树+AC自动机)
2021.11.09 P2292 [HNOI2004]L语言(trie树+AC自动机) https://www.luogu.com.cn/problem/P2292 题意: 标点符号的出现晚于文字的出 ...
- 数据结构-C语言递归实现树的前中后序遍历
#include <stdio.h> #include <stdlib.h> typedef struct tree { int number ; struct tree *l ...
- 数据结构(C语言)关于树、二叉树、图的基本操作。
1) 编写算法函数int equal(tree t1, tree t2),判断两棵给定的树是否等价: int equal(tree t1,tree t2) { int k; if(t1==NULL&a ...
- BZOJ 1212 L语言(DP+字典树)
求能被理解的最长前缀. 很显然的dp.令dp[i]=true,表示前缀i能理解.否则不能理解.那么dp[i+len]=dp[i]=true,当s[len]能匹配str[i,i+len]. 由于模式串长 ...
- bzoj1212: [HNOI2004]L语言(字典树)
1212: [HNOI2004]L语言 题目:传送门 题解: 看完题目之后就觉得可以暴力在字典树上之间询问,一开始还傻了以为用文章来建,肯定用单词啊: 那么我们可以用一个v数组表示当前字符串1~i的区 ...
- 分类-回归树模型(CART)在R语言中的实现
分类-回归树模型(CART)在R语言中的实现 CART模型 ,即Classification And Regression Trees.它和一般回归分析类似,是用来对变量进行解释和预测的工具,也是数据 ...
- LeetCode刷题总结-树篇(中)
本篇接着<LeetCode刷题总结-树篇(上)>,讲解有关树的类型相关考点的习题,本期共收录17道题,1道简单题,10道中等题,6道困难题. 在LeetCode题库中,考察到的不同种类的树 ...
- 【深入ASP.NET原理系列】--ASP.NET页面生命周期
前言 ASP.NET页面运行时候,页面将经历一个生命周期,在生命周期中将执行一系列的处理步骤.包括初始化.实例化控件.还原和维护状态.运行时间处理程序代码以及进行呈现.熟悉页面生命周期非常重要,这样我 ...
- 【Framework】HTTP运行期与页面执行模型
HTTP运行期 HTTP运行期处理客户端应用程序(例如Web浏览器)进入的一个Web请求,通过处理它的应用程序的适当组件路由请求,然后产生响应并发回提出请求的客户端应用程序. 进入的HTTP Web请 ...
随机推荐
- ubuntu14.04server版安装redis
此博客记录首次在ubuntu14.04上安装redis过程. 以下采用两种方式进行安装 方法一:进入redis的官网下载(地址:https://redis.io/download)目前版本为4.0.9 ...
- 【Leetcode】【Medium】Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- Yii框架记录
Yii框架记录 Yii 结构 使用yii开发一段时间,发现自身知其形不知其意,重温了下yii,理解框架,也可以梳理自己的知识库,借鉴成长,阶段性总结如下: 模型 模型是MVC模式中的一部分,是表现业务 ...
- SAP专家培训之Netweaver ABAP内存管理和内存调优最佳实践
培训者:SAP成都研究院开发人员Jerry Wang 1. Understanding Memory Objects in ABAP Note1: DATA itab WITH HEADER LINE ...
- PhoneGap 的文件 api
一. 文件系统的请求 请求文件系统通过 window.requestFileSystem 来完函数声明如下: window.requestFileSystem(type, size, successC ...
- PopupWindow学习笔记
最近写程序第一次用到了PopupWindow,便简单了学习了一下.特此记下自己的收获.PopupWindow是一种悬浮框,比AlertDialog要灵活的多.先简单了实现一个PopWindow的效果, ...
- python 获取某个月的全部日期
import calendar print range(calendar.monthrange(year, month)[1]+1)[1:]
- functions and closures are reference types-函数和闭包是引用类型
Closures Are Reference Types In the example above, incrementBySeven and incrementByTen are constants ...
- [转] 从此不再惧怕URI编码:JavaScript及C# URI编码详解
混乱的URI编码 JavaScript中编码有三种方法:escape.encodeURI.encodeURIComponent C#中编码主要方法:HttpUtility.UrlEncode.Serv ...
- docker-3-常用命令(上)
帮助命令: docker version docker info docker --help 镜像命令: docker images: 列出本地主机上的镜像 各个选项说明: ...