MS - 1 - 把二元查找树转变成排序的双向链表
## 1. 把二元查找树转变成排序的双向链表 ##
### 题目: 输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。 ###
要求不能创建任何新的结点,只调整指针的指向。
10
/ \
6 14
/ \ / \
4 8 12 16
转换成双向链表 4=6=8=10=12=14=16。
首先我们定义的二元查找树节点的数据结构如下:
struct BSTreeNode
{
int m_nValue; // value of node
BSTreeNode *m_pLeft; // left child of node
BSTreeNode *m_pRight; // right child of node
};
下面是使用C++泛型写的一种算法:
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std; template<typename T>
struct BSTTreeNode
{
BSTTreeNode(T v, BSTTreeNode* lNode = NULL, BSTTreeNode* rNode=NULL)
:m_Value(v), m_pLeft(lNode), m_pRight(rNode){}
T m_Value;
BSTTreeNode* m_pLeft;
BSTTreeNode* m_pRight;
}; template<typename T>
void TravelBSTree(BSTTreeNode<T> *root)
{
if (NULL == root)
{
return;
} TravelBSTree(root->m_pLeft); printf("%d ", root->m_Value); TravelBSTree(root->m_pRight); } template<typename T>
void TravelBSTreeAsList(BSTTreeNode<T> *head, bool bReverseTravel = false)
{
BSTTreeNode<T>* pNode = head;
while (pNode)
{
printf("%d ", pNode->m_Value);
if (!bReverseTravel)
{
pNode = pNode->m_pRight;
}
else
{
pNode = pNode->m_pLeft;
}
}
} //思路:
//查找树,中序遍历得到的节点排序即为排序的链表,而要求排序的双向链表,
//1. 假设lt为左子树的中序遍历的尾结点,rh为右子树中序遍历的头结点
//2. 化繁为简,如果只有root, lt, rh三个节点,此时只须然这几个节点连接起来即可。
//3. 分别遍历左右子树,重复上述过程。
template<typename T>
void BSTreeHelper(BSTTreeNode<T>* &head, BSTTreeNode<T>* &tail, BSTTreeNode<T>* root)
{
//step 1.
BSTTreeNode<T>* lt = NULL;//左子树尾结点
BSTTreeNode<T>* rh = NULL;//右子树头结点 if (NULL == root)
{
head = nullptr;
tail = nullptr;
return;
} //step 3.
BSTreeHelper(head, lt, root->m_pLeft);
BSTreeHelper(rh, tail, root->m_pRight); //step 2.
if (NULL != lt)
{
lt->m_pRight = root;
root->m_pLeft = lt;
}
else
{
head = root;
} if (NULL != rh)
{
root->m_pRight = rh;
rh->m_pLeft = root;
}
else
{
tail = root;
}
} template<typename T>
BSTTreeNode<T>* TreeToLinkedList(BSTTreeNode<T>* root)
{
BSTTreeNode<T>* head = NULL;
BSTTreeNode<T>* tail = NULL; BSTreeHelper(head, tail, root); return head;
} int _tmain(int argc, _TCHAR* argv[])
{
int arr[] = {, , , , , , }; BSTTreeNode<int> node4();
BSTTreeNode<int> node8();
BSTTreeNode<int> node6(, &node4, &node8);
BSTTreeNode<int> node12();
BSTTreeNode<int> node16();
BSTTreeNode<int> node14(, &node12, &node16);
BSTTreeNode<int> node10(, &node6, &node14);
BSTTreeNode<int>* pRoot = &node10; printf("Travel BSTree: \n");
TravelBSTree<int>(pRoot);
printf("\n"); TreeToLinkedList<int>(pRoot); printf("Travel BSTree: \n");
TravelBSTreeAsList<int>(&node4, false);
printf("\n"); TravelBSTreeAsList<int>(&node16, true);
printf("\n"); return ;
}
MS - 1 - 把二元查找树转变成排序的双向链表的更多相关文章
- 二元查找树转变成排序的双向链表之C#算法实现
此题为July在CSDN发布的微软编程面试100题中的第一题,觉得蛮有趣的,今天也拿过来玩玩,July的代码用的是C++实现,可能因为有指针的原因吧,感觉看起来相对比较容易理解整个的实现过程,而我,试 ...
- 【Data structure & Algorithm】把二元查找树转变成排序的双向链表
把二元查找树转变成排序的双向链表 题目:输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表,要求不能创建任何新节点,只调整指针指向. 比如将二元查找树 10 / \ 6 ...
- 1.把二元查找树转变成排序的双向链表[BST2DoubleLinkedList]
[题目]:输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表.要求不能创建任何新的结点,只调整指针的指向. 比如将二元查找树 . 10 / \ 6 14 / \ / \ 4 8 12 16 转 ...
- IT公司100题-15-求二元查找树的镜像
问题描述: 输入一颗二元查找树,将该树转换为它的镜像树,即对每一个节点,互换左右子树. 例如输入: 6/ \4 12/ \ / \2 5 8 16 输出: 6/ ...
- IT公司100题-9-判断整数序列是不是二元查找树的后序遍历结果
问题描述: 输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果. 如果是返回true,否则返回false. 例如输入4, 8, 6, 12, 16, 14, 10,由于这一整数序列是如下树 ...
- 6.二元查找树的后序遍历结果[PostOrderOfBST]
[题目] 输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果.如果是返回true,否则返回false. 例如输入5.7.6.9.11.10.8,由于这一整数序列是如下树的后序遍历结果: 8 ...
- 11.求二元查找树的镜像[MirrorOfBST]
[题目] 输入一颗二元查找树,将该树转换为它的镜像,即在转换后的二元查找树中,左子树的结点都大于右子树的结点.用递归和循环两种方法完成树的镜像转换. 例如输入: 8 / \ 6 1 ...
- HTTP协议漫谈 C#实现图(Graph) C#实现二叉查找树 浅谈进程同步和互斥的概念 C#实现平衡多路查找树(B树)
HTTP协议漫谈 简介 园子里已经有不少介绍HTTP的的好文章.对HTTP的一些细节介绍的比较好,所以本篇文章不会对HTTP的细节进行深究,而是从够高和更结构化的角度将HTTP协议的元素进行分类讲 ...
- 数据结构:JAVA_二叉数查找树基本实现(上)
数据结构:二叉数查找树基本实现(JAVA语言版) 1.写在前面 二叉查找树是一种能将链表插入的灵活性与有序数组查找的高效性结合在一起的一种数据结构. ..... 2.代码分解 2.1 对节点的结构定义 ...
随机推荐
- ayase系列
[冒个泡]技能视觉效果の自定义 by ayase [11-09 V2.4]任务数量转换属性点的lua脚本 [335]瞎倒腾的item.dbc生成工具..
- Python3 之 import 和 当前目录
环境: Python-3.4.3 Web.py-0.37 安装 web.py 的时候,提示 ImportError: No module named 'utils' 看看源码,setup.py,有这么 ...
- 利用C语言获得网页编码
#include <stdio.h> #include <winsock.h> #include <string.h> #pragma comment(lib, & ...
- SQLServer 自增主键创建, 指定自增主键列值插入数据,插入主键
http://blog.csdn.net/zh2qiang/article/details/5323981 SQLServer 中含自增主键的表,通常不能直接指定ID值插入,可以采用以下方法插入. 1 ...
- 自定义滚动条 - mCustomScrollbar
项目中需要使用自定义滚动条,但是试用了很多都功能不够全,今天发现一个比较全而且用法很简单的 -- mCustomScrollbar http://manos.malihu.gr/jquery-cust ...
- UML基础与Rose建模实训教程
目 录 第1章 初识UML. 1 1.1 初识UML用例图... 1 1.2 初识UML类图... 3 第2章 Rational Rose工具... 6 2.1 安装与配置Rational Ro ...
- python统计元素重复次数
python统计元素重复次数 # !/usr/bin/python3.4 # -*- coding: utf-8 -*- from collections import Counter arr = [ ...
- Java高级开发工程师面试考纲 转
转 http://www.sanesee.com/article/java-engineer-interview-of-content-tree 1 Java基础 1.1 Collection和Map ...
- BRISK: Binary Robust Invariant Scalable Keypoints
注意:本文含有一些数学公式,如果chrome不能看见公式的话请用IE打开网站 1.特征点提取 特征点提取有以下几个步骤: a.尺度空间金字塔结构的构造 和SIFT类似,尺度空间金字塔是由不同的尺度 ...
- 【SharePoint学习笔记】第4章 SharePoint UI 定制
第4章 SharePoint UI 定制 SharePoint 与 ASP.NET 好的Asp.Net人员很快就能成为好的SharePoint开发人员 Web应用程序 Mi ...