二叉查找树转换成有序的双向链表
首先對于二叉查找樹的定義和性質,以及如何得到二叉查找樹某個節點的子樹下的最大值和最小值和插入一個值的內容可以參考這兩篇文章:
(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;
??????? }
轉載于:https://www.cnblogs.com/chenping-987123/archive/2010/10/14/1851077.html
總結
以上是生活随笔為你收集整理的二叉查找树转换成有序的双向链表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【转】构建Android平台Google
- 下一篇: 分组行号