将搜索二叉树转换为链表

Given a Binary tree and we have to convert it to a Doubly Linked List (DLL).

给定二叉树,我们必须将其转换为双链表(DLL)。

Algorithm:

算法:

To solve the problem we can follow this algorithm:

为了解决这个问题,我们可以遵循以下算法:

  1. We start to convert the tree node to DLL from the rightmost tree node to the leftmost tree node.

    我们开始从最右边的树节点到最左边的树节点将树节点转换为DLL。

  2. Every time we connect the right pointer of a node to the head of the DLL.

    每次我们将节点的右指针连接到DLL的头时。

  3. Connect the left pointer of the DLL to that node.

    将DLL的左指针连接到该节点。

  4. Make that node to the head of the linked list.

    使该节点位于链接列表的开头。

  5. Repeat the process from right to left most node of the tree.

    从树的最右端到最左端重复该过程。

C++ implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
struct node {int data;
node* left;
node* right;
};
//Create a new node
struct node* create_node(int x)
{struct node* temp = new node;
temp->data = x;
temp->left = NULL;
temp->right = NULL;
return temp;
}
//convert a BST to a DLL
void BinarytoDll(node* root, node** head)
{if (root == NULL)
return;
BinarytoDll(root->right, head);
root->right = *head;
if (*head != NULL) {(*head)->left = root;
}
*head = root;
BinarytoDll(root->left, head);
}
//Print the list
void print(node* head)
{struct node* temp = head;
while (temp) {cout << temp->data << " ";
temp = temp->right;
}
}
//print the tree in inorder traversal
void print_tree(node* root)
{if (root == NULL) {return;
}
print_tree(root->left);
cout << root->data << " ";
print_tree(root->right);
}
int main()
{struct node* root = create_node(5);
root->left = create_node(6);
root->right = create_node(7);
root->left->left = create_node(8);
root->left->right = create_node(1);
root->right->right = create_node(0);
cout << "Print Tree" << endl;
print_tree(root);
struct node* head = NULL;
BinarytoDll(root, &head);
cout << "\nDoubly Linked List" << endl;
print(head);
return 0;
}

Output

输出量

Print Tree
8 6 1 5 7 0
Doubly Linked List
8 6 1 5 7 0

翻译自: https://www.includehelp.com/cpp-programs/convert-a-given-binary-tree-to-doubly-linked-list-dll.aspx

将搜索二叉树转换为链表

将搜索二叉树转换为链表_将给定的二叉树转换为双链表(DLL)相关推荐

  1. c语言单链表_突破C语言难点之单链表?一绘图即可

    数据结构之单链表 单链表是一种链式存取的数据结构,用一组地址任意的存储单元 存放线性表中的数据元素 .链表中的数据是以结点来表示的,每个结点的构成:元素( 数据元素 的映象) + 指针 (指示后继元素 ...

  2. c语言链表与字符结合,C语言实现双链表的(终端)添加和查询

    文章如果有写的不对的地方,欢迎指正^^ 双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱.所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结 ...

  3. java中的列表栈链表_Java数据结构(栈,队列,双链表)

    (1)栈package ChapterOne; public class Stack { //栈数组 long stackArr[]; //栈的大小 int maxSize; //栈的顶部 int t ...

  4. c语言 链表_小陈的C语言笔记---链表(详细讲解基本操作和概念)

    关于链表的TIPS: 链表中各结点在内存中可以不是连续存放的,各数据接点的存储顺序与数据元素之间的逻辑关系可以不一致,而数据元素之间的逻辑关系是由指针域来确定的. 在链表结点 的数据结构中,结构体内的 ...

  5. python创建一个有序链表_算法2-2:生成递增有序链表+两个链表合并

    #include #define Length sizeof(struct node) typedef struct node {int data; struct node *next; }Lnode ...

  6. c++ascii码转换为数字_在C++中将字符串转换为数字

    有许多情况需要将数字转换为字符串或将字符串转换为数字.本文中提到了一些实现此任务的方法. 将字符串转换为数字 方法1:使用stringstream类或sscanf() 方法2:使用stoi()或ato ...

  7. java简单单向链表_用java简单的实现单链表的基本操作

    packagecom.tyxh.link;//节点类 public classNode {protected Node next; //指针域 protected int data;//数据域 pub ...

  8. js将英文转换为数字_如何将MP4视频转换为GIF

    GIF(图形交换格式)基本上是动画图像,由于能够传达大量信息而被广泛使用.因此,当您无法使用MP4或其他视频的时候,GIF就可以解决此问题.此外,在处理大量动画时,需要将MP4和其他格式的视频转换为G ...

  9. python如何把矩阵转换为图片_如何将numpy数组转换为(并显示)图片

    函数式编程 用python显示一张图片方法如下:import matplotlib.pyplot as plt # plt 用于显示图片import matplotlib.image as mpimg ...

最新文章

  1. 想学科大讯飞成为下一个业界黑马?这些项目了解一下
  2. lua学习笔记之位及字节
  3. 最大玻尔兹曼分布的mASK信号在AWGN信道下的容量计算
  4. Python基础1 历史 变量
  5. 万户OA应变大考验之新员工学习篇
  6. pythonの连接MySQL数据库
  7. 服务器的安装文件,服务器存储安装文件
  8. 蓝桥杯 ADV-102 算法提高 单词个数统计
  9. IntelliJ IDEA中使用sonar插件,忽略规则和重启规则
  10. 关于Maven里的setting.xml下载
  11. 如何将base64码保存为图片
  12. 场面火爆!5G+智慧灯杆融合发展论坛在北京顺利召开
  13. linux系统外接硬盘_电脑主硬盘linux系统,外接硬盘win7系统.如何启动外接硬盘的win7系统?...
  14. 京东商品图片 自动下载 抓取 c# 爬虫
  15. 三、Hive数据仓库应用之Hive数据操作语言(超详细步骤指导操作,WIN10,VMware Workstation 15.5 PRO,CentOS-6.7)
  16. hackbar小插件
  17. linux内存双通道,两根内存就是双通道?太年轻
  18. 学习LSM(Linux security module)之二:编写并运行一个简单的demo
  19. css3中nth-child()的用法
  20. 战神引挚手游数据库解析mysql/mir

热门文章

  1. ios时间相差多少天_上海自驾拉萨,走川进青出,应如何规划线路?需要多少天时间?...
  2. iphone如何信任软件_【手机软件】千禾影院:全新观影神器,支持安卓+iOS,最新、最全、高清、免费!...
  3. 计算机入门 姚班,清华“姚班”:学霸中的尖子生,大佬毕业后都去哪了?
  4. flash 用 html 播放,使用flash插件在HTML上播放音频
  5. oracle命令行安装
  6. rebase参数以及注意事项
  7. 13 张图带你学懂 Kubernetes Service(转载)
  8. 腾讯云ubuntu18安装图形化界面
  9. Shell字符串操作集合
  10. inux CentOS 7 修改内核启动默认顺序