c语言将链表写入二进制文件

Problem statement: Write a C program to convert a binary tree into a single linked list by traversing level-wise.

问题陈述:编写一个C程序,通过逐级遍历将二进制树转换为单个链表

Example:

例:

The above binary tree is converted to 2 → 7 → 5 → 2 → 6 → 9 → 5 → 11 → 4 → NULL

上面的二叉树被转换为2→7→5→2→6→9→5→11→4→NULL

Solution

  1. Building a linked list - Setting the first node as Head and then appending other nodes.

    构建链接列表 -将第一个节点设置为Head,然后附加其他节点。

  2. Traversing & displaying a single link list.

    遍历并显示单个链接列表 。

  3. Building a tree & level order traversal of a tree.

    建立树和树的层级遍历 。

Algorithm:

算法:

  1. Assign the root value as head node value in linked list.

    在链接列表中将根值分配为头节点值。

  2. Do level-order traversal

    进行水平顺序遍历

    For each

    对于每个

    tree node create a single linked list node and append it to the linked list.

    树节点创建一个链接列表节点 ,并将其附加到链接列表。

  3. Display the single linked list.

    显示单个链接列表。

How the tree is converted to the single list...

树如何转换为单个列表...

Let’s do solve the above example.

让我们来解决上面的例子。

Root=2;
Queue status: 2
----------------------------------------------------
1st iteration
Queue not empty
Queue front is 2
Head is null, thus head is 2
Linked list up to now:2->NULL
Pop 2
Push: 2->left(7) & 2->right(5)
Queue status: 7, 5
----------------------------------------------------
2nd iteration
Queue not empty
Queue front is 7
Head is not null, thus append 7
Linked list up to now:2->7->NULL
Pop 7
Push: 7->left(2)& 7->right(6)
Queue status: 5, 2, 6
----------------------------------------------------
3rd iteration
Queue not empty
Queue front is 5
Head is not null, thus append 5
Linked list up to now:2->7->5->NULL
Pop 5
Push: 5->right (9) only (5->left is NULL)
Queue status: 2, 6, 9
----------------------------------------------------
4th iteration
Queue not empty
Queue front is 2
Head is not null, thus append 2
Linked list up to now:2->7->5->2->NULL
Pop 2
Push: Nothing ( both child are NULL)
Queue status: 6, 9
----------------------------------------------------
5th iteration
Queue not empty
Queue front is 6
Head is not null, thus append 6
Linked list up to now:2->7->5->2->6->NULL
Pop 6
Push: 6->left(5) and 6->right(11)
Queue status: 9, 5, 11
----------------------------------------------------
6th iteration
Queue not empty
Queue front is 9
Head is not null, thus append 9
Linked list up to now:2->7->5->2->6->9->NULL
Pop 9
Push: 9->left(4) only (right child NULL)
Queue status: 5, 11, 4
----------------------------------------------------
7th iteration
Queue not empty
Queue front is 5
Head is not null, thus append 5
Linked list up to now:2->7->5->2->6->9->5->NULL
Pop 5
Push: Nothing (both child NULL)
Queue status: 11, 4
----------------------------------------------------
8th iteration
Queue not empty
Queue front is 11
Head is not null, thus append 11
Linked list up to now:2->7->5->2->6->9->5->11->NULL
Pop 11
Push: Nothing (both child NULL)
Queue status: 4
----------------------------------------------------
8th iteration
Queue not empty
Queue front is 4
Head is not null, thus append 4
Linked list up to now: 2->7->5->2->6->9->5->11->4->NULL
Pop 4
Push: Nothing (both child NULL)
Queue status: empty queue
----------------------------------------------------
Iteration stops
So final link list is: 2->7->5->2->6->9->5->11->4->NULL
.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

通过逐级遍历将二叉树转换为单链接列表的C实现 (C implementation to to convert a Binary Tree into a Singly Linked List by Traversing Level by Level)

#include <bits/stdc++.h>
using namespace std;
// tree node is defined
class tree{
public:
int data;
tree *left;
tree *right;
};
class sll{public:
int data;
sll* next;
};
sll* creatnode(int d){ //create node for single linked list
sll* temp=(sll*)malloc(sizeof(sll));
temp->data=d;
temp->next=NULL;
return temp;
}
void display(sll* head){sll* current=head; // current node set to head
printf("displayig the converted list...\n");
while(current!=NULL){ //traverse until current node isn't NULL
if(current->next)
printf("%d->",current->data);
else
printf("%d->NULL\n",current->data);
current=current->next; // go to next node
}
}
sll* flatten(tree* root)
{//Declare queue using STL
sll* head=NULL,*tempL;
queue<tree*> q;
//enqueue the root
q.push(root);
vector<int> store;
tree* temp;
//do the level order traversal & build single linked list
while(!q.empty()){//dequeue
temp=q.front();
q.pop();
if(head==NULL){//for inserting first node
head=creatnode(temp->data);
tempL=head;
}
else{//for inserting rest of the nodes
tempL->next=creatnode(temp->data);
tempL=tempL->next;
}
// do level order traversing
if(temp->left)//for left child
q.push(temp->left);
if(temp->right)//for right child
q.push(temp->right);
}
return head;
}
tree* newnode(int data)  // creating new node for tree
{
tree* node = (tree*)malloc(sizeof(tree));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
int main()
{
//**same tree is builted as shown in example**
cout<<"same tree is built as shown in example\n";
tree *root=newnode(2);
root->left= newnode(7);
root->right= newnode(5);
root->right->right=newnode(9);
root->right->right->left=newnode(4);
root->left->left=newnode(2);
root->left->right=newnode(6);
root->left->right->left=newnode(5);
root->left->right->right=newnode(11);
cout<<"converting the tree into a single link list...\n";
cout<<"by traversing the tree level-wise\n";
sll* head=flatten(root);
//displaying the list built from the tree
display(head);
return 0;
}

Output

输出量

same tree is built as shown in example
converting the tree into a single link list...
by traversing the tree level-wise
displayig the converted list...
2->7->5->2->6->9->5->11->4->NULL

翻译自: https://www.includehelp.com/c-programs/convert-a-binary-tree-into-a-singly-linked-list-by-traversing-level-by-level.aspx

c语言将链表写入二进制文件

c语言将链表写入二进制文件_通过逐级遍历将二进制树转换为单链表的C程序相关推荐

  1. 链表的特点,单链表的定义、存储结构,单链表的基本操作(判断链表是否为空、销毁链表、清空链表、求链表表长、查找、插入、删除,建立单链表)

    目录 一.链表(链式存储结构)的特点 二.单链表的定义和表示 1.带头结点的单链表 2.单链表的存储结构 三.单链表基本操作的实现 1.单链表的初始化(带头结点的单链表) 2.补充单链表的几个常用简单 ...

  2. 6-8 从单链表LA指定位置删除连续n个元素并插入单链表LB的指定位置 (10 分)

    6-8 从单链表LA指定位置删除连续n个元素并插入单链表LB的指定位置 (10 分) 设指针la和lb分别指向两个无头结点单链表中的首元结点,试编写算法,从表la中删除自第i个元素起共len个元素,并 ...

  3. python链表的创建_《大话数据结构》配套源码:链表(Python版)

    该书随书源码的语言为C:我参考书中内容和配套源码,写了一套Python格式的配套源码.这套配套源码并非直接翻译C语言的配套源码,而是结合我的理解略作了修改. SinglyLinkedNode 单链表结 ...

  4. java 查找链表中间元素_如何在Java中一次性查找Java中链表的中间元素

    如何在一次传递中找到LinkedList的中间元素?这是一个 Java 和非Java程序员面试时经常被问到的编程问题.这个问题类似于检查回文或计算阶乘,有时也会要求编写代码.为了回答这个问题,候选人必 ...

  5. python中链表和数组_数据结构笔记(一):数组、链表|python基础教程|python入门|python教程...

    https://www.xin3721.com/eschool/pythonxin3721/ (一)数组 数组(Array)是一种线性表数据结构.它用一组连续的内存空间,来存储一组具有相同类型的数据. ...

  6. java带头结点的单链表_自己实现集合框架 (五): 带头结点单链表的实现

    这是系列文章,每篇文章末尾均附有源代码地址.目的是通过模拟集合框架的简单实现,从而对常用的数据结构和java集合有个大概的了解.当然实现没有java集合的实现那么复杂,功能也没有那么强大,但是可以通过 ...

  7. java带头节点的单链表_自己实现集合框架(五):带头结点单链表的实现

    这是系列文章,每篇文章末尾均附有源代码地址.目的是通过模拟集合框架的简单实现,从而对常用的数据结构和java集合有个大概的了解.当然实现没有java集合的实现那么复杂,功能也没有那么强大,但是可以通过 ...

  8. php数据结构链表代码,数据结构之线性表——链式存储结构之单链表(php代码实现)...

    /** * * 1. 类LNode用作创建单链表时,生成新的节点. * 2. 类SingleLinkList用于创建单链表以及对单链表的一些操作方法(实例化此类就相当于创建了一个空链表) * 3. C ...

  9. (关于单链表的真题)已知一个带有表头结点的单链表...请设计一个尽可能高效的算法,查找链表中倒数第k个位置的结点。

    真题描述 已知一个带有表头结点的单链表,结点结构为 data next 假设该链表只给出了头指针head.在不改变链表的前提下,请设计一个尽可能高效的算法,查找链表中倒数第k个位置上的结点. 若查找成 ...

最新文章

  1. 在列表显示某个内容,但数据表没有这个字段
  2. 多大、谷歌大脑获ICML 2021杰出论文奖,田渊栋、陆昱成获荣誉提名!
  3. Linux下完全卸载ORACLE 10G的方法
  4. wxWidgets随笔(9)-utf8~wxString存储二进制数据(4)
  5. Linux|UNIX下LAMP环境的搭建及常见问题[连载3]
  6. 机器学习:怎样才能做到从入门到不放弃?
  7. 389 find the difference
  8. icd植入是大手术吗_母狗绝育是大手术吗?手术完需要住院吗?绝育后是不是会变胖?...
  9. C++ 智能指针简介
  10. XP-在恢复时返回到欢迎屏幕
  11. 使用hector构图_如何使用均衡的构图拍摄更清晰的照片
  12. 淘宝/天猫、1688、京东按图搜索淘宝商品(拍立淘)API接口
  13. echarts:x轴文字竖排显示
  14. 安卓虚拟键盘_安卓这些年变化多惊人?那些老玩家才懂的回忆
  15. 1.关于tomcat的startup.bat文件闪退,而日志文件没有任何信息
  16. setClickable,setEnabled,setFocusable 的区别
  17. k图着色 局部搜索算法与模拟退火算法的python实现
  18. obj文件、mtl文件结构说明
  19. 期末测验: 课程水平综合测验 (第10周)
  20. 2006~2016,国产「产品经理」相关图书·十年盘点

热门文章

  1. java中对象多态时成员变量,普通成员函数及静态成员函数的调用情况
  2. 华为畅享max有没有人脸识别_华为畅享7s有人脸识别吗 让我来告诉你
  3. usb接口供电不足_1个USB接口变成4个?什么东西那么“牛”?请你花2分钟了解一下...
  4. java多线程编程_《java多线程编程实战指南》读书笔记 -- 基本概念
  5. android webview 长按复制,Android webview 点击或长按有蒙层 – 热爱改变生活
  6. python学习1:注释\变量类型\转换函数\转义字符\运算符
  7. jdbc连接数据scanip_JDBC连接数据库的四种方式:DriverManager,DataSource,DBCP,C3P0
  8. 创建索引名称已由现有对象使用_Excel编程周末速成班第3课:Excel对象模型
  9. nvidia显示设置不可用_Nvidia显示设置不可用,您当前未使用连接到NVIDIA GPU的显示器的解决方法...
  10. xampp mysql 卸载_卸载Xampp并安装apache + mysql + php 过程