二叉查找树转换成有序的双向链表
2010-10-14 20:28
363 查看
首先对于二叉查找树的定义和性质,以及如何得到二叉查找树某个节点的子树下的最大值和最小值和插入一个值的内容可以参考这两篇文章:
(1)http://www.cnblogs.com/chenping-987123/archive/2010/09/25/1834341.html
(2)http://www.cnblogs.com/chenping-987123/archive/2010/09/26/1836133.html
现在有一个二叉查找树如下所示:
再insert 9 这个节点的时候
然后可以要输出的双向链表为:1 2 3 4 5 6 9 10 12
关于双向链表的知识,在数据结构的书中都有了很好的解释。
具体的程序如下:
BSTreeToList(root);
Console.WriteLine("********************BSTreeToList***************");
while (root.left != null)
{
root = root.left;
}
while (root != null)
{
Console.Write(root._value.ToString() + " ");
root = root.right;
}
private static void BSTreeToList(MyNode root)
{
if (root.left != null)
{
BSTreeToList(root.left);
}
if (root.right != null)
{
BSTreeToList(root.right);
}
MyNode max = TreeMax(root.left);
MyNode min = TreeMin(root.right);
if (max != null)
{
max.right = root;
}
root.left = max;
if (min != null)
{
min.left = root;
}
root.right = min;
}
(1)http://www.cnblogs.com/chenping-987123/archive/2010/09/25/1834341.html
(2)http://www.cnblogs.com/chenping-987123/archive/2010/09/26/1836133.html
现在有一个二叉查找树如下所示:
再insert 9 这个节点的时候
然后可以要输出的双向链表为:1 2 3 4 5 6 9 10 12
关于双向链表的知识,在数据结构的书中都有了很好的解释。
具体的程序如下:
BSTreeToList(root);
Console.WriteLine("********************BSTreeToList***************");
while (root.left != null)
{
root = root.left;
}
while (root != null)
{
Console.Write(root._value.ToString() + " ");
root = root.right;
}
private static void BSTreeToList(MyNode root)
{
if (root.left != null)
{
BSTreeToList(root.left);
}
if (root.right != null)
{
BSTreeToList(root.right);
}
MyNode max = TreeMax(root.left);
MyNode min = TreeMin(root.right);
if (max != null)
{
max.right = root;
}
root.left = max;
if (min != null)
{
min.left = root;
}
root.right = min;
}
相关文章推荐
- 二叉查找树转换为有序双向链表
- 静态变量的应用--将二叉排序树转换为有序的双向链表输出
- 面试题27:二叉搜索树转换为有序双向链表
- 【经典数据结构算法】(1)二叉查找树与双向链表之间的转换
- 二叉查找树转变为有序双向链表
- 树(1)把二叉查找树转换成有序的双向链表
- 将二叉查找树转换为有序的双向链表
- 将二叉查找树转换成有序双向链表
- 算法题1:二元查找树转换成有序双向链表
- 二叉查找树转变为有序双向链表
- 二叉搜索树转换为有序双向链表
- 【经典数据结构算法】(1)二叉查找树与双向链表之间的转换
- 二叉查找树转换为顺序的双向链表
- 二叉查找树转变成有序的双向链表
- 微软100题 第一题(查找树转换为有序双向链表)
- 360面试题——搜索二叉树转换成有序的双向链表
- 将搜索二叉树转换成有序的双向链表
- 将二叉查找树变为有序的双向链表
- 如何将搜索二叉树转换成有序双向链表
- 将二叉查找树转换为有序双链表